File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.39: download - view: text, annotated - select for diffs
Wed Apr 16 13:33:14 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.235.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.39 2014/04/16 13:33:14 raeburn Exp $
    5: ##
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for Authoring Spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, and textbook).  In each case the radio buttons 
  107: allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use LONCAPA qw(:DEFAULT :match);
  170: use LONCAPA::Enrollment;
  171: use LONCAPA::lonauthcgi();
  172: use File::Copy;
  173: use Locale::Language;
  174: use DateTime::TimeZone;
  175: use DateTime::Locale;
  176: 
  177: my $registered_cleanup;
  178: my $modified_urls;
  179: 
  180: sub handler {
  181:     my $r=shift;
  182:     if ($r->header_only) {
  183:         &Apache::loncommon::content_type($r,'text/html');
  184:         $r->send_http_header;
  185:         return OK;
  186:     }
  187: 
  188:     my $context = 'domain';
  189:     my $dom = $env{'request.role.domain'};
  190:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  191:     if (&Apache::lonnet::allowed('mau',$dom)) {
  192:         &Apache::loncommon::content_type($r,'text/html');
  193:         $r->send_http_header;
  194:     } else {
  195:         $env{'user.error.msg'}=
  196:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  197:         return HTTP_NOT_ACCEPTABLE;
  198:     }
  199: 
  200:     $registered_cleanup=0;
  201:     @{$modified_urls}=();
  202: 
  203:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  204:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  205:                                             ['phase','actions']);
  206:     my $phase = 'pickactions';
  207:     if ( exists($env{'form.phase'}) ) {
  208:         $phase = $env{'form.phase'};
  209:     }
  210:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  211:     my %domconfig =
  212:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  213:                 'quotas','autoenroll','autoupdate','autocreate',
  214:                 'directorysrch','usercreation','usermodification',
  215:                 'contacts','defaults','scantron','coursecategories',
  216:                 'serverstatuses','requestcourses','coursedefaults',
  217:                 'usersessions','loadbalancing','requestauthor',
  218:                 'selfenrollment'],$dom);
  219:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  220:                        'autoupdate','autocreate','directorysrch','contacts',
  221:                        'usercreation','selfcreation','usermodification','scantron',
  222:                        'requestcourses','requestauthor','coursecategories',
  223:                        'serverstatuses','coursedefaults','selfenrollment',
  224:                        'usersessions');
  225:     my %existing;
  226:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  227:         %existing = %{$domconfig{'loadbalancing'}};
  228:     }
  229:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  230:         push(@prefs_order,'loadbalancing');
  231:     }
  232:     my %prefs = (
  233:         'rolecolors' =>
  234:                    { text => 'Default color schemes',
  235:                      help => 'Domain_Configuration_Color_Schemes',
  236:                      header => [{col1 => 'Student Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Coordinator Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Author Settings',
  241:                                  col2 => '',},
  242:                                 {col1 => 'Administrator Settings',
  243:                                  col2 => '',}],
  244:                       print => \&print_rolecolors,
  245:                       modify => \&modify_rolecolors,
  246:                     },
  247:         'login' =>
  248:                     { text => 'Log-in page options',
  249:                       help => 'Domain_Configuration_Login_Page',
  250:                       header => [{col1 => 'Log-in Page Items',
  251:                                   col2 => '',},
  252:                                  {col1 => 'Log-in Help',
  253:                                   col2 => 'Value'}],
  254:                       print => \&print_login,
  255:                       modify => \&modify_login,
  256:                     },
  257:         'defaults' => 
  258:                     { text => 'Default authentication/language/timezone/portal',
  259:                       help => 'Domain_Configuration_LangTZAuth',
  260:                       header => [{col1 => 'Setting',
  261:                                   col2 => 'Value'}],
  262:                       print => \&print_defaults,
  263:                       modify => \&modify_defaults,
  264:                     },
  265:         'quotas' => 
  266:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  267:                       help => 'Domain_Configuration_Quotas',
  268:                       header => [{col1 => 'User affiliation',
  269:                                   col2 => 'Available tools',
  270:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  271:                       print => \&print_quotas,
  272:                       modify => \&modify_quotas,
  273:                     },
  274:         'autoenroll' =>
  275:                    { text => 'Auto-enrollment settings',
  276:                      help => 'Domain_Configuration_Auto_Enrollment',
  277:                      header => [{col1 => 'Configuration setting',
  278:                                  col2 => 'Value(s)'}],
  279:                      print => \&print_autoenroll,
  280:                      modify => \&modify_autoenroll,
  281:                    },
  282:         'autoupdate' => 
  283:                    { text => 'Auto-update settings',
  284:                      help => 'Domain_Configuration_Auto_Updates',
  285:                      header => [{col1 => 'Setting',
  286:                                  col2 => 'Value',},
  287:                                 {col1 => 'Setting',
  288:                                  col2 => 'Affiliation'},
  289:                                 {col1 => 'User population',
  290:                                  col2 => 'Updatable user data'}],
  291:                      print => \&print_autoupdate,
  292:                      modify => \&modify_autoupdate,
  293:                   },
  294:         'autocreate' => 
  295:                   { text => 'Auto-course creation settings',
  296:                      help => 'Domain_Configuration_Auto_Creation',
  297:                      header => [{col1 => 'Configuration Setting',
  298:                                  col2 => 'Value',}],
  299:                      print => \&print_autocreate,
  300:                      modify => \&modify_autocreate,
  301:                   },
  302:         'directorysrch' => 
  303:                   { text => 'Institutional directory searches',
  304:                     help => 'Domain_Configuration_InstDirectory_Search',
  305:                     header => [{col1 => 'Setting',
  306:                                 col2 => 'Value',}],
  307:                     print => \&print_directorysrch,
  308:                     modify => \&modify_directorysrch,
  309:                   },
  310:         'contacts' =>
  311:                   { text => 'Contact Information',
  312:                     help => 'Domain_Configuration_Contact_Info',
  313:                     header => [{col1 => 'Setting',
  314:                                 col2 => 'Value',}],
  315:                     print => \&print_contacts,
  316:                     modify => \&modify_contacts,
  317:                   },
  318:         'usercreation' => 
  319:                   { text => 'User creation',
  320:                     help => 'Domain_Configuration_User_Creation',
  321:                     header => [{col1 => 'Format rule type',
  322:                                 col2 => 'Format rules in force'},
  323:                                {col1 => 'User account creation',
  324:                                 col2 => 'Usernames which may be created',},
  325:                                {col1 => 'Context',
  326:                                 col2 => 'Assignable authentication types'}],
  327:                     print => \&print_usercreation,
  328:                     modify => \&modify_usercreation,
  329:                   },
  330:         'selfcreation' => 
  331:                   { text => 'Users self-creating accounts',
  332:                     help => 'Domain_Configuration_Self_Creation', 
  333:                     header => [{col1 => 'Self-creation with institutional username',
  334:                                 col2 => 'Enabled?'},
  335:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  336:                                 col2 => 'Information user can enter'},
  337:                                {col1 => 'Self-creation with e-mail as username',
  338:                                 col2 => 'Settings'}],
  339:                     print => \&print_selfcreation,
  340:                     modify => \&modify_selfcreation,
  341:                   },
  342:         'usermodification' =>
  343:                   { text => 'User modification',
  344:                     help => 'Domain_Configuration_User_Modification',
  345:                     header => [{col1 => 'Target user has role',
  346:                                 col2 => 'User information updatable in author context'},
  347:                                {col1 => 'Target user has role',
  348:                                 col2 => 'User information updatable in course context'}],
  349:                     print => \&print_usermodification,
  350:                     modify => \&modify_usermodification,
  351:                   },
  352:         'scantron' =>
  353:                   { text => 'Bubblesheet format file',
  354:                     help => 'Domain_Configuration_Scantron_Format',
  355:                     header => [ {col1 => 'Item',
  356:                                  col2 => '',
  357:                               }],
  358:                     print => \&print_scantron,
  359:                     modify => \&modify_scantron,
  360:                   },
  361:         'requestcourses' => 
  362:                  {text => 'Request creation of courses',
  363:                   help => 'Domain_Configuration_Request_Courses',
  364:                   header => [{col1 => 'User affiliation',
  365:                               col2 => 'Availability/Processing of requests',},
  366:                              {col1 => 'Setting',
  367:                               col2 => 'Value'},
  368:                              {col1 => 'Available textbooks',
  369:                               col2 => ''},
  370:                              {col1 => 'Validation (not official courses)',
  371:                               col2 => 'Value'},],
  372:                   print => \&print_quotas,
  373:                   modify => \&modify_quotas,
  374:                  },
  375:         'requestauthor' =>
  376:                  {text => 'Request Authoring Space',
  377:                   help => 'Domain_Configuration_Request_Author',
  378:                   header => [{col1 => 'User affiliation',
  379:                               col2 => 'Availability/Processing of requests',},
  380:                              {col1 => 'Setting',
  381:                               col2 => 'Value'}],
  382:                   print => \&print_quotas,
  383:                   modify => \&modify_quotas,
  384:                  },
  385:         'coursecategories' =>
  386:                   { text => 'Cataloging of courses/communities',
  387:                     help => 'Domain_Configuration_Cataloging_Courses',
  388:                     header => [{col1 => 'Category settings',
  389:                                 col2 => '',},
  390:                                {col1 => 'Categories',
  391:                                 col2 => '',
  392:                                }],
  393:                     print => \&print_coursecategories,
  394:                     modify => \&modify_coursecategories,
  395:                   },
  396:         'serverstatuses' =>
  397:                  {text   => 'Access to server status pages',
  398:                   help   => 'Domain_Configuration_Server_Status',
  399:                   header => [{col1 => 'Status Page',
  400:                               col2 => 'Other named users',
  401:                               col3 => 'Specific IPs',
  402:                             }],
  403:                   print => \&print_serverstatuses,
  404:                   modify => \&modify_serverstatuses,
  405:                  },
  406:         'coursedefaults' => 
  407:                  {text => 'Course/Community defaults',
  408:                   help => 'Domain_Configuration_Course_Defaults',
  409:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  410:                               col2 => 'Value',},
  411:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  412:                               col2 => 'Value',},],
  413:                   print => \&print_coursedefaults,
  414:                   modify => \&modify_coursedefaults,
  415:                  },
  416:         'selfenrollment' => 
  417:                  {text   => 'Self-enrollment in Course/Community',
  418:                   help   => 'Domain_Configuration_Selfenrollment',
  419:                   header => [{col1 => 'Configuration Rights',
  420:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  421:                              {col1 => 'Defaults',
  422:                               col2 => 'Value'},
  423:                              {col1 => 'Self-enrollment validation (optional)',
  424:                               col2 => 'Value'},],
  425:                   print => \&print_selfenrollment,
  426:                   modify => \&modify_selfenrollment,
  427:                  },
  428:         'usersessions' =>
  429:                  {text  => 'User session hosting/offloading',
  430:                   help  => 'Domain_Configuration_User_Sessions',
  431:                   header => [{col1 => 'Domain server',
  432:                               col2 => 'Servers to offload sessions to when busy'},
  433:                              {col1 => 'Hosting of users from other domains',
  434:                               col2 => 'Rules'},
  435:                              {col1 => "Hosting domain's own users elsewhere",
  436:                               col2 => 'Rules'}],
  437:                   print => \&print_usersessions,
  438:                   modify => \&modify_usersessions,
  439:                  },
  440:          'loadbalancing' =>
  441:                  {text  => 'Dedicated Load Balancer(s)',
  442:                   help  => 'Domain_Configuration_Load_Balancing',
  443:                   header => [{col1 => 'Balancers',
  444:                               col2 => 'Default destinations',
  445:                               col3 => 'User affiliation',
  446:                               col4 => 'Overrides'},
  447:                             ],
  448:                   print => \&print_loadbalancing,
  449:                   modify => \&modify_loadbalancing,
  450:                  },
  451:     );
  452:     if (keys(%servers) > 1) {
  453:         $prefs{'login'}  = { text   => 'Log-in page options',
  454:                              help   => 'Domain_Configuration_Login_Page',
  455:                             header => [{col1 => 'Log-in Service',
  456:                                         col2 => 'Server Setting',},
  457:                                        {col1 => 'Log-in Page Items',
  458:                                         col2 => ''},
  459:                                        {col1 => 'Log-in Help',
  460:                                         col2 => 'Value'}],
  461:                             print => \&print_login,
  462:                             modify => \&modify_login,
  463:                            };
  464:     }
  465: 
  466:     my @roles = ('student','coordinator','author','admin');
  467:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  468:     &Apache::lonhtmlcommon::add_breadcrumb
  469:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  470:       text=>"Settings to display/modify"});
  471:     my $confname = $dom.'-domainconfig';
  472: 
  473:     if ($phase eq 'process') {
  474:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  475:                                                               \%prefs,\%domconfig,$confname,\@roles);
  476:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  477:             $r->rflush();
  478:             &devalidate_remote_domconfs($dom,$result);
  479:         }
  480:     } elsif ($phase eq 'display') {
  481:         my $js = &recaptcha_js().
  482:                  &credits_js();
  483:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  484:             my ($othertitle,$usertypes,$types) =
  485:                 &Apache::loncommon::sorted_inst_types($dom);
  486:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  487:                                           $domconfig{'loadbalancing'}).
  488:                    &new_spares_js().
  489:                    &common_domprefs_js().
  490:                    &Apache::loncommon::javascript_array_indexof();
  491:         }
  492:         if (grep(/^requestcourses$/,@actions)) {
  493:             my $javascript_validations;
  494:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  495:             $js .= <<END;
  496: <script type="text/javascript">
  497: $javascript_validations
  498: </script>
  499: $coursebrowserjs
  500: END
  501:         }
  502:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  503:     } else {
  504: # check if domconfig user exists for the domain.
  505:         my $servadm = $r->dir_config('lonAdmEMail');
  506:         my ($configuserok,$author_ok,$switchserver) =
  507:             &config_check($dom,$confname,$servadm);
  508:         unless ($configuserok eq 'ok') {
  509:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  510:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  511:                           $confname).
  512:                       '<br />'
  513:             );
  514:             if ($switchserver) {
  515:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  516:                           '<br />'.
  517:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  518:                           '<br />'.
  519:                           &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).
  520:                           '<br />'.
  521:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  522:                 );
  523:             } else {
  524:                 $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.').
  525:                           '<br />'.
  526:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  527:                 );
  528:             }
  529:             $r->print(&Apache::loncommon::end_page());
  530:             return OK;
  531:         }
  532:         if (keys(%domconfig) == 0) {
  533:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  534:             my @ids=&Apache::lonnet::current_machine_ids();
  535:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  536:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  537:                 my @loginimages = ('img','logo','domlogo','login');
  538:                 my $custom_img_count = 0;
  539:                 foreach my $img (@loginimages) {
  540:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  541:                         $custom_img_count ++;
  542:                     }
  543:                 }
  544:                 foreach my $role (@roles) {
  545:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  546:                         $custom_img_count ++;
  547:                     }
  548:                 }
  549:                 if ($custom_img_count > 0) {
  550:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  551:                     my $switch_server = &check_switchserver($dom,$confname);
  552:                     $r->print(
  553:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  554:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  555:     &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 />'.
  556:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  557:                     if ($switch_server) {
  558:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  559:                     }
  560:                     $r->print(&Apache::loncommon::end_page());
  561:                     return OK;
  562:                 }
  563:             }
  564:         }
  565:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  566:     }
  567:     return OK;
  568: }
  569: 
  570: sub process_changes {
  571:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  572:     my %domconfig;
  573:     if (ref($values) eq 'HASH') {
  574:         %domconfig = %{$values};
  575:     }
  576:     my $output;
  577:     if ($action eq 'login') {
  578:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  579:     } elsif ($action eq 'rolecolors') {
  580:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  581:                                      $lastactref,%domconfig);
  582:     } elsif ($action eq 'quotas') {
  583:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  584:     } elsif ($action eq 'autoenroll') {
  585:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  586:     } elsif ($action eq 'autoupdate') {
  587:         $output = &modify_autoupdate($dom,%domconfig);
  588:     } elsif ($action eq 'autocreate') {
  589:         $output = &modify_autocreate($dom,%domconfig);
  590:     } elsif ($action eq 'directorysrch') {
  591:         $output = &modify_directorysrch($dom,%domconfig);
  592:     } elsif ($action eq 'usercreation') {
  593:         $output = &modify_usercreation($dom,%domconfig);
  594:     } elsif ($action eq 'selfcreation') {
  595:         $output = &modify_selfcreation($dom,%domconfig);
  596:     } elsif ($action eq 'usermodification') {
  597:         $output = &modify_usermodification($dom,%domconfig);
  598:     } elsif ($action eq 'contacts') {
  599:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  600:     } elsif ($action eq 'defaults') {
  601:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  602:     } elsif ($action eq 'scantron') {
  603:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  604:     } elsif ($action eq 'coursecategories') {
  605:         $output = &modify_coursecategories($dom,%domconfig);
  606:     } elsif ($action eq 'serverstatuses') {
  607:         $output = &modify_serverstatuses($dom,%domconfig);
  608:     } elsif ($action eq 'requestcourses') {
  609:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  610:     } elsif ($action eq 'requestauthor') {
  611:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  612:     } elsif ($action eq 'coursedefaults') {
  613:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  614:     } elsif ($action eq 'selfenrollment') {
  615:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  616:     } elsif ($action eq 'usersessions') {
  617:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  618:     } elsif ($action eq 'loadbalancing') {
  619:         $output = &modify_loadbalancing($dom,%domconfig);
  620:     }
  621:     return $output;
  622: }
  623: 
  624: sub print_config_box {
  625:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  626:     my $rowtotal = 0;
  627:     my $output;
  628:     if ($action eq 'coursecategories') {
  629:         $output = &coursecategories_javascript($settings);
  630:     }
  631:     $output .= 
  632:          '<table class="LC_nested_outer">
  633:           <tr>
  634:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  635:            &mt($item->{text}).'&nbsp;'.
  636:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  637:           '</tr>';
  638:     $rowtotal ++;
  639:     my $numheaders = 1;
  640:     if (ref($item->{'header'}) eq 'ARRAY') {
  641:         $numheaders = scalar(@{$item->{'header'}});
  642:     }
  643:     if ($numheaders > 1) {
  644:         my $colspan = '';
  645:         my $rightcolspan = '';
  646:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  647:             (($action eq 'login') && ($numheaders < 3))) {
  648:             $colspan = ' colspan="2"';
  649:         }
  650:         if ($action eq 'usersessions') {
  651:             $rightcolspan = ' colspan="3"'; 
  652:         }
  653:         $output .= '
  654:           <tr>
  655:            <td>
  656:             <table class="LC_nested">
  657:              <tr class="LC_info_row">
  658:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  659:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  660:              </tr>';
  661:         $rowtotal ++;
  662:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  663:             ($action eq 'usermodification') || ($action eq 'selfenrollment') ||
  664:             ($action eq 'usersessions')) {
  665:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  666:         } elsif ($action eq 'coursecategories') {
  667:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  668:         } elsif ($action eq 'login') {
  669:             if ($numheaders == 3) {
  670:                 $colspan = ' colspan="2"';
  671:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  672:             } else {
  673:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  674:             }
  675:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  676:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  677:         } elsif ($action eq 'rolecolors') {
  678:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  679:         }
  680:         $output .= '
  681:            </table>
  682:           </td>
  683:          </tr>
  684:          <tr>
  685:            <td>
  686:             <table class="LC_nested">
  687:              <tr class="LC_info_row">
  688:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  689:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  690:              </tr>';
  691:             $rowtotal ++;
  692:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  693:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  694:             ($action eq 'usersessions')) {
  695:             $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal).'
  696:            </table>
  697:           </td>
  698:          </tr>
  699:          <tr>
  700:            <td>
  701:             <table class="LC_nested">
  702:              <tr class="LC_info_row">
  703:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  704:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  705:              </tr>'."\n".
  706:             $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  707:             $rowtotal ++;
  708:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults')) {
  709:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  710:         } elsif ($action eq 'coursecategories') {
  711:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  712:         } elsif ($action eq 'login') {
  713:             if ($numheaders == 3) {
  714:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  715:            </table>
  716:           </td>
  717:          </tr>
  718:          <tr>
  719:            <td>
  720:             <table class="LC_nested">
  721:              <tr class="LC_info_row">
  722:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  723:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  724:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  725:                 $rowtotal ++;
  726:             } else {
  727:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  728:             }
  729:         } elsif ($action eq 'requestcourses') {
  730:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  731:                        &print_studentcode($settings,\$rowtotal).'
  732:            </table>
  733:           </td>
  734:          </tr>
  735:          <tr>
  736:            <td>
  737:             <table class="LC_nested">
  738:              <tr class="LC_info_row">
  739:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  740:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  741:                        &print_textbookcourses($dom,$settings,\$rowtotal).'
  742:             </table>
  743:            </td>
  744:           </tr>
  745:           <tr>
  746:            <td>
  747:             <table class="LC_nested">
  748:              <tr class="LC_info_row">
  749:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  750:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  751:              </tr>'.
  752:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  753:         } elsif ($action eq 'requestauthor') {
  754:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  755:         } elsif ($action eq 'rolecolors') {
  756:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  757:            </table>
  758:           </td>
  759:          </tr>
  760:          <tr>
  761:            <td>
  762:             <table class="LC_nested">
  763:              <tr class="LC_info_row">
  764:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  765:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  766:               <td class="LC_right_item" valign="top">'.
  767:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  768:              </tr>'.
  769:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  770:            </table>
  771:           </td>
  772:          </tr>
  773:          <tr>
  774:            <td>
  775:             <table class="LC_nested">
  776:              <tr class="LC_info_row">
  777:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  778:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  779:              </tr>'.
  780:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  781:             $rowtotal += 2;
  782:         }
  783:     } else {
  784:         $output .= '
  785:           <tr>
  786:            <td>
  787:             <table class="LC_nested">
  788:              <tr class="LC_info_row">';
  789:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  790:             $output .= '  
  791:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  792:         } elsif ($action eq 'serverstatuses') {
  793:             $output .= '
  794:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  795:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  796: 
  797:         } else {
  798:             $output .= '
  799:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  800:         }
  801:         if (defined($item->{'header'}->[0]->{'col3'})) {
  802:             $output .= '<td class="LC_left_item" valign="top">'.
  803:                        &mt($item->{'header'}->[0]->{'col2'});
  804:             if ($action eq 'serverstatuses') {
  805:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  806:             } 
  807:         } else {
  808:             $output .= '<td class="LC_right_item" valign="top">'.
  809:                        &mt($item->{'header'}->[0]->{'col2'});
  810:         }
  811:         $output .= '</td>';
  812:         if ($item->{'header'}->[0]->{'col3'}) {
  813:             if (defined($item->{'header'}->[0]->{'col4'})) {
  814:                 $output .= '<td class="LC_left_item" valign="top">'.
  815:                             &mt($item->{'header'}->[0]->{'col3'});
  816:             } else {
  817:                 $output .= '<td class="LC_right_item" valign="top">'.
  818:                            &mt($item->{'header'}->[0]->{'col3'});
  819:             }
  820:             if ($action eq 'serverstatuses') {
  821:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  822:             }
  823:             $output .= '</td>';
  824:         }
  825:         if ($item->{'header'}->[0]->{'col4'}) {
  826:             $output .= '<td class="LC_right_item" valign="top">'.
  827:                        &mt($item->{'header'}->[0]->{'col4'});
  828:         }
  829:         $output .= '</tr>';
  830:         $rowtotal ++;
  831:         if ($action eq 'quotas') {
  832:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  833:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  834:                  ($action eq 'contacts') || ($action eq 'defaults') || ($action eq 'serverstatuses') ||
  835:                  ($action eq 'loadbalancing')) {
  836:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  837:         } elsif ($action eq 'scantron') {
  838:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  839:         } elsif ($action eq 'helpsettings') {
  840:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  841:         }
  842:     }
  843:     $output .= '
  844:    </table>
  845:   </td>
  846:  </tr>
  847: </table><br />';
  848:     return ($output,$rowtotal);
  849: }
  850: 
  851: sub print_login {
  852:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  853:     my ($css_class,$datatable);
  854:     my %choices = &login_choices();
  855: 
  856:     if ($caller eq 'service') {
  857:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  858:         my $choice = $choices{'disallowlogin'};
  859:         $css_class = ' class="LC_odd_row"';
  860:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  861:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  862:                       '<th>'.$choices{'server'}.'</th>'.
  863:                       '<th>'.$choices{'serverpath'}.'</th>'.
  864:                       '<th>'.$choices{'custompath'}.'</th>'.
  865:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  866:         my %disallowed;
  867:         if (ref($settings) eq 'HASH') {
  868:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  869:                %disallowed = %{$settings->{'loginvia'}};
  870:             }
  871:         }
  872:         foreach my $lonhost (sort(keys(%servers))) {
  873:             my $direct = 'selected="selected"';
  874:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  875:                 if ($disallowed{$lonhost}{'server'} ne '') {
  876:                     $direct = '';
  877:                 }
  878:             }
  879:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  880:                           '<td><select name="'.$lonhost.'_server">'.
  881:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  882:                           '</option>';
  883:             foreach my $hostid (sort(keys(%servers))) {
  884:                 next if ($servers{$hostid} eq $servers{$lonhost});
  885:                 my $selected = '';
  886:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  887:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  888:                         $selected = 'selected="selected"';
  889:                     }
  890:                 }
  891:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  892:                               $servers{$hostid}.'</option>';
  893:             }
  894:             $datatable .= '</select></td>'.
  895:                           '<td><select name="'.$lonhost.'_serverpath">';
  896:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  897:                 my $pathname = $path;
  898:                 if ($path eq 'custom') {
  899:                     $pathname = &mt('Custom Path').' ->';
  900:                 }
  901:                 my $selected = '';
  902:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  903:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  904:                         $selected = 'selected="selected"';
  905:                     }
  906:                 } elsif ($path eq '') {
  907:                     $selected = 'selected="selected"';
  908:                 }
  909:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  910:             }
  911:             $datatable .= '</select></td>';
  912:             my ($custom,$exempt);
  913:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  914:                 $custom = $disallowed{$lonhost}{'custompath'};
  915:                 $exempt = $disallowed{$lonhost}{'exempt'};
  916:             }
  917:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  918:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  919:                           '</tr>';
  920:         }
  921:         $datatable .= '</table></td></tr>';
  922:         return $datatable;
  923:     } elsif ($caller eq 'page') {
  924:         my %defaultchecked = ( 
  925:                                'coursecatalog' => 'on',
  926:                                'helpdesk'      => 'on',
  927:                                'adminmail'     => 'off',
  928:                                'newuser'       => 'off',
  929:                              );
  930:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  931:         my (%checkedon,%checkedoff);
  932:         foreach my $item (@toggles) {
  933:             if ($defaultchecked{$item} eq 'on') { 
  934:                 $checkedon{$item} = ' checked="checked" ';
  935:                 $checkedoff{$item} = ' ';
  936:             } elsif ($defaultchecked{$item} eq 'off') {
  937:                 $checkedoff{$item} = ' checked="checked" ';
  938:                 $checkedon{$item} = ' ';
  939:             }
  940:         }
  941:         my @images = ('img','logo','domlogo','login');
  942:         my @logintext = ('textcol','bgcol');
  943:         my @bgs = ('pgbg','mainbg','sidebg');
  944:         my @links = ('link','alink','vlink');
  945:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  946:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  947:         my (%is_custom,%designs);
  948:         my %defaults = (
  949:                        font => $defaultdesign{'login.font'},
  950:                        );
  951:         foreach my $item (@images) {
  952:             $defaults{$item} = $defaultdesign{'login.'.$item};
  953:             $defaults{'showlogo'}{$item} = 1;
  954:         }
  955:         foreach my $item (@bgs) {
  956:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  957:         }
  958:         foreach my $item (@logintext) {
  959:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  960:         }
  961:         foreach my $item (@links) {
  962:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  963:         }
  964:         if (ref($settings) eq 'HASH') {
  965:             foreach my $item (@toggles) {
  966:                 if ($settings->{$item} eq '1') {
  967:                     $checkedon{$item} =  ' checked="checked" ';
  968:                     $checkedoff{$item} = ' ';
  969:                 } elsif ($settings->{$item} eq '0') {
  970:                     $checkedoff{$item} =  ' checked="checked" ';
  971:                     $checkedon{$item} = ' ';
  972:                 }
  973:             }
  974:             foreach my $item (@images) {
  975:                 if (defined($settings->{$item})) {
  976:                     $designs{$item} = $settings->{$item};
  977:                     $is_custom{$item} = 1;
  978:                 }
  979:                 if (defined($settings->{'showlogo'}{$item})) {
  980:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  981:                 }
  982:             }
  983:             foreach my $item (@logintext) {
  984:                 if ($settings->{$item} ne '') {
  985:                     $designs{'logintext'}{$item} = $settings->{$item};
  986:                     $is_custom{$item} = 1;
  987:                 }
  988:             }
  989:             if ($settings->{'font'} ne '') {
  990:                 $designs{'font'} = $settings->{'font'};
  991:                 $is_custom{'font'} = 1;
  992:             }
  993:             foreach my $item (@bgs) {
  994:                 if ($settings->{$item} ne '') {
  995:                     $designs{'bgs'}{$item} = $settings->{$item};
  996:                     $is_custom{$item} = 1;
  997:                 }
  998:             }
  999:             foreach my $item (@links) {
 1000:                 if ($settings->{$item} ne '') {
 1001:                     $designs{'links'}{$item} = $settings->{$item};
 1002:                     $is_custom{$item} = 1;
 1003:                 }
 1004:             }
 1005:         } else {
 1006:             if ($designhash{$dom.'.login.font'} ne '') {
 1007:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1008:                 $is_custom{'font'} = 1;
 1009:             }
 1010:             foreach my $item (@images) {
 1011:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1012:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1013:                     $is_custom{$item} = 1;
 1014:                 }
 1015:             }
 1016:             foreach my $item (@bgs) {
 1017:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1018:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1019:                     $is_custom{$item} = 1;
 1020:                 }
 1021:             }
 1022:             foreach my $item (@links) {
 1023:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1024:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1025:                     $is_custom{$item} = 1;
 1026:                 }
 1027:             }
 1028:         }
 1029:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1030:                                                       logo => 'Institution Logo',
 1031:                                                       domlogo => 'Domain Logo',
 1032:                                                       login => 'Login box');
 1033:         my $itemcount = 1;
 1034:         foreach my $item (@toggles) {
 1035:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1036:             $datatable .=  
 1037:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1038:                 '</td><td>'.
 1039:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1040:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1041:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1042:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1043:                 '</tr>';
 1044:             $itemcount ++;
 1045:         }
 1046:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1047:         $datatable .= '</tr></table></td></tr>';
 1048:     } elsif ($caller eq 'help') {
 1049:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1050:         my $switchserver = &check_switchserver($dom,$confname);
 1051:         my $itemcount = 1;
 1052:         $defaulturl = '/adm/loginproblems.html';
 1053:         $defaulttype = 'default';
 1054:         %lt = &Apache::lonlocal::texthash (
 1055:                      del     => 'Delete?',
 1056:                      rep     => 'Replace:',
 1057:                      upl     => 'Upload:',
 1058:                      default => 'Default',
 1059:                      custom  => 'Custom',
 1060:                                              );
 1061:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1062:         my @currlangs;
 1063:         if (ref($settings) eq 'HASH') {
 1064:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1065:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1066:                     next if ($settings->{'helpurl'}{$key} eq '');
 1067:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1068:                     $type{$key} = 'custom';
 1069:                     unless ($key eq 'nolang') {
 1070:                         push(@currlangs,$key);
 1071:                     }
 1072:                 }
 1073:             } elsif ($settings->{'helpurl'} ne '') {
 1074:                 $type{'nolang'} = 'custom';
 1075:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1076:             }
 1077:         }
 1078:         foreach my $lang ('nolang',sort(@currlangs)) {
 1079:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1080:             $datatable .= '<tr'.$css_class.'>';
 1081:             if ($url{$lang} eq '') {
 1082:                 $url{$lang} = $defaulturl;
 1083:             }
 1084:             if ($type{$lang} eq '') {
 1085:                 $type{$lang} = $defaulttype;
 1086:             }
 1087:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1088:             if ($lang eq 'nolang') {
 1089:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1090:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1091:             } else {
 1092:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1093:                                   $langchoices{$lang},
 1094:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1095:             }
 1096:             $datatable .= '</span></td>'."\n".
 1097:                           '<td class="LC_left_item">';
 1098:             if ($type{$lang} eq 'custom') {
 1099:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1100:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1101:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1102:             } else {
 1103:                 $datatable .= $lt{'upl'};
 1104:             }
 1105:             $datatable .='<br />';
 1106:             if ($switchserver) {
 1107:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1108:             } else {
 1109:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1110:             }
 1111:             $datatable .= '</td></tr>';
 1112:             $itemcount ++;
 1113:         }
 1114:         my @addlangs;
 1115:         foreach my $lang (sort(keys(%langchoices))) {
 1116:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1117:             push(@addlangs,$lang);
 1118:         }
 1119:         if (@addlangs > 0) {
 1120:             my %toadd;
 1121:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1122:             $toadd{''} = &mt('Select');
 1123:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1124:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1125:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1126:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1127:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1128:             if ($switchserver) {
 1129:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1130:             } else {
 1131:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1132:             }
 1133:             $datatable .= '</td></tr>';
 1134:             $itemcount ++;
 1135:         }
 1136:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1137:     }
 1138:     return $datatable;
 1139: }
 1140: 
 1141: sub login_choices {
 1142:     my %choices =
 1143:         &Apache::lonlocal::texthash (
 1144:             coursecatalog => 'Display Course/Community Catalog link?',
 1145:             adminmail     => "Display Administrator's E-mail Address?",
 1146:             helpdesk      => 'Display "Contact Helpdesk" link',
 1147:             disallowlogin => "Login page requests redirected",
 1148:             hostid        => "Server",
 1149:             server        => "Redirect to:",
 1150:             serverpath    => "Path",
 1151:             custompath    => "Custom", 
 1152:             exempt        => "Exempt IP(s)",
 1153:             directlogin   => "No redirect",
 1154:             newuser       => "Link to create a user account",
 1155:             img           => "Header",
 1156:             logo          => "Main Logo",
 1157:             domlogo       => "Domain Logo",
 1158:             login         => "Log-in Header", 
 1159:             textcol       => "Text color",
 1160:             bgcol         => "Box color",
 1161:             bgs           => "Background colors",
 1162:             links         => "Link colors",
 1163:             font          => "Font color",
 1164:             pgbg          => "Header",
 1165:             mainbg        => "Page",
 1166:             sidebg        => "Login box",
 1167:             link          => "Link",
 1168:             alink         => "Active link",
 1169:             vlink         => "Visited link",
 1170:         );
 1171:     return %choices;
 1172: }
 1173: 
 1174: sub print_rolecolors {
 1175:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1176:     my %choices = &color_font_choices();
 1177:     my @bgs = ('pgbg','tabbg','sidebg');
 1178:     my @links = ('link','alink','vlink');
 1179:     my @images = ('img');
 1180:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1181:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1182:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1183:     my (%is_custom,%designs);
 1184:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1185:     if (ref($settings) eq 'HASH') {
 1186:         if (ref($settings->{$role}) eq 'HASH') {
 1187:             if ($settings->{$role}->{'img'} ne '') {
 1188:                 $designs{'img'} = $settings->{$role}->{'img'};
 1189:                 $is_custom{'img'} = 1;
 1190:             }
 1191:             if ($settings->{$role}->{'font'} ne '') {
 1192:                 $designs{'font'} = $settings->{$role}->{'font'};
 1193:                 $is_custom{'font'} = 1;
 1194:             }
 1195:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1196:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1197:                 $is_custom{'fontmenu'} = 1;
 1198:             }
 1199:             foreach my $item (@bgs) {
 1200:                 if ($settings->{$role}->{$item} ne '') {
 1201:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1202:                     $is_custom{$item} = 1;
 1203:                 }
 1204:             }
 1205:             foreach my $item (@links) {
 1206:                 if ($settings->{$role}->{$item} ne '') {
 1207:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1208:                     $is_custom{$item} = 1;
 1209:                 }
 1210:             }
 1211:         }
 1212:     } else {
 1213:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1214:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1215:             $is_custom{'img'} = 1;
 1216:         }
 1217:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1218:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1219:             $is_custom{'fontmenu'} = 1; 
 1220:         }
 1221:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1222:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1223:             $is_custom{'font'} = 1;
 1224:         }
 1225:         foreach my $item (@bgs) {
 1226:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1227:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1228:                 $is_custom{$item} = 1;
 1229:             
 1230:             }
 1231:         }
 1232:         foreach my $item (@links) {
 1233:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1234:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1235:                 $is_custom{$item} = 1;
 1236:             }
 1237:         }
 1238:     }
 1239:     my $itemcount = 1;
 1240:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1241:     $datatable .= '</tr></table></td></tr>';
 1242:     return $datatable;
 1243: }
 1244: 
 1245: sub role_defaults {
 1246:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1247:     my %defaults;
 1248:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1249:         return %defaults;
 1250:     }
 1251:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1252:     if ($role eq 'login') {
 1253:         %defaults = (
 1254:                        font => $defaultdesign{$role.'.font'},
 1255:                     );
 1256:         if (ref($logintext) eq 'ARRAY') {
 1257:             foreach my $item (@{$logintext}) {
 1258:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1259:             }
 1260:         }
 1261:         foreach my $item (@{$images}) {
 1262:             $defaults{'showlogo'}{$item} = 1;
 1263:         }
 1264:     } else {
 1265:         %defaults = (
 1266:                        img => $defaultdesign{$role.'.img'},
 1267:                        font => $defaultdesign{$role.'.font'},
 1268:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1269:                     );
 1270:     }
 1271:     foreach my $item (@{$bgs}) {
 1272:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1273:     }
 1274:     foreach my $item (@{$links}) {
 1275:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1276:     }
 1277:     foreach my $item (@{$images}) {
 1278:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1279:     }
 1280:     return %defaults;
 1281: }
 1282: 
 1283: sub display_color_options {
 1284:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1285:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1286:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1287:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1288:     my $datatable = '<tr'.$css_class.'>'.
 1289:         '<td>'.$choices->{'font'}.'</td>';
 1290:     if (!$is_custom->{'font'}) {
 1291:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1292:     } else {
 1293:         $datatable .= '<td>&nbsp;</td>';
 1294:     }
 1295:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1296: 
 1297:     $datatable .= '<td><span class="LC_nobreak">'.
 1298:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1299:                   ' value="'.$current_color.'" />&nbsp;'.
 1300:                   '&nbsp;</td></tr>';
 1301:     unless ($role eq 'login') { 
 1302:         $datatable .= '<tr'.$css_class.'>'.
 1303:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1304:         if (!$is_custom->{'fontmenu'}) {
 1305:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1306:         } else {
 1307:             $datatable .= '<td>&nbsp;</td>';
 1308:         }
 1309: 	$current_color = $designs->{'fontmenu'} ?
 1310: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1311:         $datatable .= '<td><span class="LC_nobreak">'.
 1312:                       '<input class="colorchooser" type="text" size="10" name="'
 1313: 		      .$role.'_fontmenu"'.
 1314:                       ' value="'.$current_color.'" />&nbsp;'.
 1315:                       '&nbsp;</td></tr>';
 1316:     }
 1317:     my $switchserver = &check_switchserver($dom,$confname);
 1318:     foreach my $img (@{$images}) {
 1319: 	$itemcount ++;
 1320:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1321:         $datatable .= '<tr'.$css_class.'>'.
 1322:                       '<td>'.$choices->{$img};
 1323:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1324:         if ($role eq 'login') {
 1325:             if ($img eq 'login') {
 1326:                 $login_hdr_pick =
 1327:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1328:                 $logincolors =
 1329:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1330:                                        $designs,$defaults);
 1331:             } elsif ($img ne 'domlogo') {
 1332:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1333:             }
 1334:         }
 1335:         $datatable .= '</td>';
 1336:         if ($designs->{$img} ne '') {
 1337:             $imgfile = $designs->{$img};
 1338: 	    $img_import = ($imgfile =~ m{^/adm/});
 1339:         } else {
 1340:             $imgfile = $defaults->{$img};
 1341:         }
 1342:         if ($imgfile) {
 1343:             my ($showfile,$fullsize);
 1344:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1345:                 my $urldir = $1;
 1346:                 my $filename = $2;
 1347:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1348:                 if (@info) {
 1349:                     my $thumbfile = 'tn-'.$filename;
 1350:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1351:                     if (@thumb) {
 1352:                         $showfile = $urldir.'/'.$thumbfile;
 1353:                     } else {
 1354:                         $showfile = $imgfile;
 1355:                     }
 1356:                 } else {
 1357:                     $showfile = '';
 1358:                 }
 1359:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1360:                 $showfile = $imgfile;
 1361:                 my $imgdir = $1;
 1362:                 my $filename = $2;
 1363:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1364:                     $showfile = "/$imgdir/tn-".$filename;
 1365:                 } else {
 1366:                     my $input = $londocroot.$imgfile;
 1367:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1368:                     if (!-e $output) {
 1369:                         my ($width,$height) = &thumb_dimensions();
 1370:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1371:                         if ($fullwidth ne '' && $fullheight ne '') {
 1372:                             if ($fullwidth > $width && $fullheight > $height) { 
 1373:                                 my $size = $width.'x'.$height;
 1374:                                 system("convert -sample $size $input $output");
 1375:                                 $showfile = "/$imgdir/tn-".$filename;
 1376:                             }
 1377:                         }
 1378:                     }
 1379:                 }
 1380:             }
 1381:             if ($showfile) {
 1382:                 if ($showfile =~ m{^/(adm|res)/}) {
 1383:                     if ($showfile =~ m{^/res/}) {
 1384:                         my $local_showfile =
 1385:                             &Apache::lonnet::filelocation('',$showfile);
 1386:                         &Apache::lonnet::repcopy($local_showfile);
 1387:                     }
 1388:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1389:                 }
 1390:                 if ($imgfile) {
 1391:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1392:                         if ($imgfile =~ m{^/res/}) {
 1393:                             my $local_imgfile =
 1394:                                 &Apache::lonnet::filelocation('',$imgfile);
 1395:                             &Apache::lonnet::repcopy($local_imgfile);
 1396:                         }
 1397:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1398:                     } else {
 1399:                         $fullsize = $imgfile;
 1400:                     }
 1401:                 }
 1402:                 $datatable .= '<td>';
 1403:                 if ($img eq 'login') {
 1404:                     $datatable .= $login_hdr_pick;
 1405:                 } 
 1406:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1407:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1408:             } else {
 1409:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1410:                               &mt('Upload:').'<br />';
 1411:             }
 1412:         } else {
 1413:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1414:                           &mt('Upload:').'<br />';
 1415:         }
 1416:         if ($switchserver) {
 1417:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1418:         } else {
 1419:             if ($img ne 'login') { # suppress file selection for Log-in header
 1420:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1421:             }
 1422:         }
 1423:         $datatable .= '</td></tr>';
 1424:     }
 1425:     $itemcount ++;
 1426:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1427:     $datatable .= '<tr'.$css_class.'>'.
 1428:                   '<td>'.$choices->{'bgs'}.'</td>';
 1429:     my $bgs_def;
 1430:     foreach my $item (@{$bgs}) {
 1431:         if (!$is_custom->{$item}) {
 1432:             $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>';
 1433:         }
 1434:     }
 1435:     if ($bgs_def) {
 1436:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1437:     } else {
 1438:         $datatable .= '<td>&nbsp;</td>';
 1439:     }
 1440:     $datatable .= '<td class="LC_right_item">'.
 1441:                   '<table border="0"><tr>';
 1442: 
 1443:     foreach my $item (@{$bgs}) {
 1444:         $datatable .= '<td align="center">'.$choices->{$item};
 1445: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1446:         if ($designs->{'bgs'}{$item}) {
 1447:             $datatable .= '&nbsp;';
 1448:         }
 1449:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1450:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1451:     }
 1452:     $datatable .= '</tr></table></td></tr>';
 1453:     $itemcount ++;
 1454:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1455:     $datatable .= '<tr'.$css_class.'>'.
 1456:                   '<td>'.$choices->{'links'}.'</td>';
 1457:     my $links_def;
 1458:     foreach my $item (@{$links}) {
 1459:         if (!$is_custom->{$item}) {
 1460:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1461:         }
 1462:     }
 1463:     if ($links_def) {
 1464:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1465:     } else {
 1466:         $datatable .= '<td>&nbsp;</td>';
 1467:     }
 1468:     $datatable .= '<td class="LC_right_item">'.
 1469:                   '<table border="0"><tr>';
 1470:     foreach my $item (@{$links}) {
 1471: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1472:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1473:         if ($designs->{'links'}{$item}) {
 1474:             $datatable.='&nbsp;';
 1475:         }
 1476:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1477:                       '" /></td>';
 1478:     }
 1479:     $$rowtotal += $itemcount;
 1480:     return $datatable;
 1481: }
 1482: 
 1483: sub logo_display_options {
 1484:     my ($img,$defaults,$designs) = @_;
 1485:     my $checkedon;
 1486:     if (ref($defaults) eq 'HASH') {
 1487:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1488:             if ($defaults->{'showlogo'}{$img}) {
 1489:                 $checkedon = 'checked="checked" ';     
 1490:             }
 1491:         } 
 1492:     }
 1493:     if (ref($designs) eq 'HASH') {
 1494:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1495:             if (defined($designs->{'showlogo'}{$img})) {
 1496:                 if ($designs->{'showlogo'}{$img} == 0) {
 1497:                     $checkedon = '';
 1498:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1499:                     $checkedon = 'checked="checked" ';
 1500:                 }
 1501:             }
 1502:         }
 1503:     }
 1504:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1505:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1506:            &mt('show').'</label>'."\n";
 1507: }
 1508: 
 1509: sub login_header_options  {
 1510:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1511:     my $output = '';
 1512:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1513:         $output .= &mt('Text default(s):').'<br />';
 1514:         if (!$is_custom->{'textcol'}) {
 1515:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1516:                        '&nbsp;&nbsp;&nbsp;';
 1517:         }
 1518:         if (!$is_custom->{'bgcol'}) {
 1519:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1520:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1521:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1522:         }
 1523:         $output .= '<br />';
 1524:     }
 1525:     $output .='<br />';
 1526:     return $output;
 1527: }
 1528: 
 1529: sub login_text_colors {
 1530:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1531:     my $color_menu = '<table border="0"><tr>';
 1532:     foreach my $item (@{$logintext}) {
 1533:         $color_menu .= '<td align="center">'.$choices->{$item};
 1534:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1535:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1536:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1537:     }
 1538:     $color_menu .= '</tr></table><br />';
 1539:     return $color_menu;
 1540: }
 1541: 
 1542: sub image_changes {
 1543:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1544:     my $output;
 1545:     if ($img eq 'login') {
 1546:             # suppress image for Log-in header
 1547:     } elsif (!$is_custom) {
 1548:         if ($img ne 'domlogo') {
 1549:             $output .= &mt('Default image:').'<br />';
 1550:         } else {
 1551:             $output .= &mt('Default in use:').'<br />';
 1552:         }
 1553:     }
 1554:     if ($img eq 'login') { # suppress image for Log-in header
 1555:         $output .= '<td>'.$logincolors;
 1556:     } else {
 1557:         if ($img_import) {
 1558:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1559:         }
 1560:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1561:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1562:         if ($is_custom) {
 1563:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1564:                        '<input type="checkbox" name="'.
 1565:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1566:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1567:         } else {
 1568:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1569:         }
 1570:     }
 1571:     return $output;
 1572: }
 1573: 
 1574: sub print_quotas {
 1575:     my ($dom,$settings,$rowtotal,$action) = @_;
 1576:     my $context;
 1577:     if ($action eq 'quotas') {
 1578:         $context = 'tools';
 1579:     } else {
 1580:         $context = $action;
 1581:     }
 1582:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1583:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1584:     my $typecount = 0;
 1585:     my ($css_class,%titles);
 1586:     if ($context eq 'requestcourses') {
 1587:         @usertools = ('official','unofficial','community','textbook');
 1588:         @options =('norequest','approval','validate','autolimit');
 1589:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1590:         %titles = &courserequest_titles();
 1591:     } elsif ($context eq 'requestauthor') {
 1592:         @usertools = ('author');
 1593:         @options = ('norequest','approval','automatic');
 1594:         %titles = &authorrequest_titles();
 1595:     } else {
 1596:         @usertools = ('aboutme','blog','webdav','portfolio');
 1597:         %titles = &tool_titles();
 1598:     }
 1599:     if (ref($types) eq 'ARRAY') {
 1600:         foreach my $type (@{$types}) {
 1601:             my ($currdefquota,$currauthorquota);
 1602:             unless (($context eq 'requestcourses') ||
 1603:                     ($context eq 'requestauthor')) {
 1604:                 if (ref($settings) eq 'HASH') {
 1605:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1606:                         $currdefquota = $settings->{defaultquota}->{$type};
 1607:                     } else {
 1608:                         $currdefquota = $settings->{$type};
 1609:                     }
 1610:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1611:                         $currauthorquota = $settings->{authorquota}->{$type};
 1612:                     }
 1613:                 }
 1614:             }
 1615:             if (defined($usertypes->{$type})) {
 1616:                 $typecount ++;
 1617:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1618:                 $datatable .= '<tr'.$css_class.'>'.
 1619:                               '<td>'.$usertypes->{$type}.'</td>'.
 1620:                               '<td class="LC_left_item">';
 1621:                 if ($context eq 'requestcourses') {
 1622:                     $datatable .= '<table><tr>';
 1623:                 }
 1624:                 my %cell;  
 1625:                 foreach my $item (@usertools) {
 1626:                     if ($context eq 'requestcourses') {
 1627:                         my ($curroption,$currlimit);
 1628:                         if (ref($settings) eq 'HASH') {
 1629:                             if (ref($settings->{$item}) eq 'HASH') {
 1630:                                 $curroption = $settings->{$item}->{$type};
 1631:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1632:                                     $currlimit = $1; 
 1633:                                 }
 1634:                             }
 1635:                         }
 1636:                         if (!$curroption) {
 1637:                             $curroption = 'norequest';
 1638:                         }
 1639:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1640:                         foreach my $option (@options) {
 1641:                             my $val = $option;
 1642:                             if ($option eq 'norequest') {
 1643:                                 $val = 0;  
 1644:                             }
 1645:                             if ($option eq 'validate') {
 1646:                                 my $canvalidate = 0;
 1647:                                 if (ref($validations{$item}) eq 'HASH') { 
 1648:                                     if ($validations{$item}{$type}) {
 1649:                                         $canvalidate = 1;
 1650:                                     }
 1651:                                 }
 1652:                                 next if (!$canvalidate);
 1653:                             }
 1654:                             my $checked = '';
 1655:                             if ($option eq $curroption) {
 1656:                                 $checked = ' checked="checked"';
 1657:                             } elsif ($option eq 'autolimit') {
 1658:                                 if ($curroption =~ /^autolimit/) {
 1659:                                     $checked = ' checked="checked"';
 1660:                                 }                       
 1661:                             } 
 1662:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1663:                                   '<input type="radio" name="crsreq_'.$item.
 1664:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1665:                                   $titles{$option}.'</label>';
 1666:                             if ($option eq 'autolimit') {
 1667:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1668:                                                 $item.'_limit_'.$type.'" size="1" '.
 1669:                                                 'value="'.$currlimit.'" />';
 1670:                             }
 1671:                             $cell{$item} .= '</span> ';
 1672:                             if ($option eq 'autolimit') {
 1673:                                 $cell{$item} .= $titles{'unlimited'};
 1674:                             }
 1675:                         }
 1676:                     } elsif ($context eq 'requestauthor') {
 1677:                         my $curroption;
 1678:                         if (ref($settings) eq 'HASH') {
 1679:                             $curroption = $settings->{$type};
 1680:                         }
 1681:                         if (!$curroption) {
 1682:                             $curroption = 'norequest';
 1683:                         }
 1684:                         foreach my $option (@options) {
 1685:                             my $val = $option;
 1686:                             if ($option eq 'norequest') {
 1687:                                 $val = 0;
 1688:                             }
 1689:                             my $checked = '';
 1690:                             if ($option eq $curroption) {
 1691:                                 $checked = ' checked="checked"';
 1692:                             }
 1693:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1694:                                   '<input type="radio" name="authorreq_'.$type.
 1695:                                   '" value="'.$val.'"'.$checked.' />'.
 1696:                                   $titles{$option}.'</label></span>&nbsp; ';
 1697:                         }
 1698:                     } else {
 1699:                         my $checked = 'checked="checked" ';
 1700:                         if (ref($settings) eq 'HASH') {
 1701:                             if (ref($settings->{$item}) eq 'HASH') {
 1702:                                 if ($settings->{$item}->{$type} == 0) {
 1703:                                     $checked = '';
 1704:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1705:                                     $checked =  'checked="checked" ';
 1706:                                 }
 1707:                             }
 1708:                         }
 1709:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1710:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1711:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1712:                                       '</label></span>&nbsp; ';
 1713:                     }
 1714:                 }
 1715:                 if ($context eq 'requestcourses') {
 1716:                     $datatable .= '</tr><tr>';
 1717:                     foreach my $item (@usertools) {
 1718:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1719:                     }
 1720:                     $datatable .= '</tr></table>';
 1721:                 }
 1722:                 $datatable .= '</td>';
 1723:                 unless (($context eq 'requestcourses') ||
 1724:                         ($context eq 'requestauthor')) {
 1725:                     $datatable .= 
 1726:                               '<td class="LC_right_item">'.
 1727:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1728:                               '<input type="text" name="quota_'.$type.
 1729:                               '" value="'.$currdefquota.
 1730:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1731:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1732:                               '<input type="text" name="authorquota_'.$type.
 1733:                               '" value="'.$currauthorquota.
 1734:                               '" size="5" /></span></td>';
 1735:                 }
 1736:                 $datatable .= '</tr>';
 1737:             }
 1738:         }
 1739:     }
 1740:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1741:         $defaultquota = '20';
 1742:         $authorquota = '500';
 1743:         if (ref($settings) eq 'HASH') {
 1744:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1745:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1746:             } elsif (defined($settings->{'default'})) {
 1747:                 $defaultquota = $settings->{'default'};
 1748:             }
 1749:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1750:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1751:             }
 1752:         }
 1753:     }
 1754:     $typecount ++;
 1755:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1756:     $datatable .= '<tr'.$css_class.'>'.
 1757:                   '<td>'.$othertitle.'</td>'.
 1758:                   '<td class="LC_left_item">';
 1759:     if ($context eq 'requestcourses') {
 1760:         $datatable .= '<table><tr>';
 1761:     }
 1762:     my %defcell;
 1763:     foreach my $item (@usertools) {
 1764:         if ($context eq 'requestcourses') {
 1765:             my ($curroption,$currlimit);
 1766:             if (ref($settings) eq 'HASH') {
 1767:                 if (ref($settings->{$item}) eq 'HASH') {
 1768:                     $curroption = $settings->{$item}->{'default'};
 1769:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1770:                         $currlimit = $1;
 1771:                     }
 1772:                 }
 1773:             }
 1774:             if (!$curroption) {
 1775:                 $curroption = 'norequest';
 1776:             }
 1777:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1778:             foreach my $option (@options) {
 1779:                 my $val = $option;
 1780:                 if ($option eq 'norequest') {
 1781:                     $val = 0;
 1782:                 }
 1783:                 if ($option eq 'validate') {
 1784:                     my $canvalidate = 0;
 1785:                     if (ref($validations{$item}) eq 'HASH') {
 1786:                         if ($validations{$item}{'default'}) {
 1787:                             $canvalidate = 1;
 1788:                         }
 1789:                     }
 1790:                     next if (!$canvalidate);
 1791:                 }
 1792:                 my $checked = '';
 1793:                 if ($option eq $curroption) {
 1794:                     $checked = ' checked="checked"';
 1795:                 } elsif ($option eq 'autolimit') {
 1796:                     if ($curroption =~ /^autolimit/) {
 1797:                         $checked = ' checked="checked"';
 1798:                     }
 1799:                 }
 1800:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1801:                                   '<input type="radio" name="crsreq_'.$item.
 1802:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1803:                                   $titles{$option}.'</label>';
 1804:                 if ($option eq 'autolimit') {
 1805:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1806:                                        $item.'_limit_default" size="1" '.
 1807:                                        'value="'.$currlimit.'" />';
 1808:                 }
 1809:                 $defcell{$item} .= '</span> ';
 1810:                 if ($option eq 'autolimit') {
 1811:                     $defcell{$item} .= $titles{'unlimited'};
 1812:                 }
 1813:             }
 1814:         } elsif ($context eq 'requestauthor') {
 1815:             my $curroption;
 1816:             if (ref($settings) eq 'HASH') {
 1817:                 $curroption = $settings->{'default'};
 1818:             }
 1819:             if (!$curroption) {
 1820:                 $curroption = 'norequest';
 1821:             }
 1822:             foreach my $option (@options) {
 1823:                 my $val = $option;
 1824:                 if ($option eq 'norequest') {
 1825:                     $val = 0;
 1826:                 }
 1827:                 my $checked = '';
 1828:                 if ($option eq $curroption) {
 1829:                     $checked = ' checked="checked"';
 1830:                 }
 1831:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1832:                               '<input type="radio" name="authorreq_default"'.
 1833:                               ' value="'.$val.'"'.$checked.' />'.
 1834:                               $titles{$option}.'</label></span>&nbsp; ';
 1835:             }
 1836:         } else {
 1837:             my $checked = 'checked="checked" ';
 1838:             if (ref($settings) eq 'HASH') {
 1839:                 if (ref($settings->{$item}) eq 'HASH') {
 1840:                     if ($settings->{$item}->{'default'} == 0) {
 1841:                         $checked = '';
 1842:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1843:                         $checked = 'checked="checked" ';
 1844:                     }
 1845:                 }
 1846:             }
 1847:             $datatable .= '<span class="LC_nobreak"><label>'.
 1848:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1849:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1850:                           '</label></span>&nbsp; ';
 1851:         }
 1852:     }
 1853:     if ($context eq 'requestcourses') {
 1854:         $datatable .= '</tr><tr>';
 1855:         foreach my $item (@usertools) {
 1856:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1857:         }
 1858:         $datatable .= '</tr></table>';
 1859:     }
 1860:     $datatable .= '</td>';
 1861:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1862:         $datatable .= '<td class="LC_right_item">'.
 1863:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1864:                       '<input type="text" name="defaultquota" value="'.
 1865:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1866:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1867:                       '<input type="text" name="authorquota" value="'.
 1868:                       $authorquota.'" size="5" /></span></td>';
 1869:     }
 1870:     $datatable .= '</tr>';
 1871:     $typecount ++;
 1872:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1873:     $datatable .= '<tr'.$css_class.'>'.
 1874:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1875:     if ($context eq 'requestcourses') {
 1876:         $datatable .= &mt('(overrides affiliation, if set)').
 1877:                       '</td>'.
 1878:                       '<td class="LC_left_item">'.
 1879:                       '<table><tr>';
 1880:     } else {
 1881:         $datatable .= &mt('(overrides affiliation, if checked)').
 1882:                       '</td>'.
 1883:                       '<td class="LC_left_item" colspan="2">'.
 1884:                       '<br />';
 1885:     }
 1886:     my %advcell;
 1887:     foreach my $item (@usertools) {
 1888:         if ($context eq 'requestcourses') {
 1889:             my ($curroption,$currlimit);
 1890:             if (ref($settings) eq 'HASH') {
 1891:                 if (ref($settings->{$item}) eq 'HASH') {
 1892:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1893:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1894:                         $currlimit = $1;
 1895:                     }
 1896:                 }
 1897:             }
 1898:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1899:             my $checked = '';
 1900:             if ($curroption eq '') {
 1901:                 $checked = ' checked="checked"';
 1902:             }
 1903:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1904:                                '<input type="radio" name="crsreq_'.$item.
 1905:                                '__LC_adv" value=""'.$checked.' />'.
 1906:                                &mt('No override set').'</label></span>&nbsp; ';
 1907:             foreach my $option (@options) {
 1908:                 my $val = $option;
 1909:                 if ($option eq 'norequest') {
 1910:                     $val = 0;
 1911:                 }
 1912:                 if ($option eq 'validate') {
 1913:                     my $canvalidate = 0;
 1914:                     if (ref($validations{$item}) eq 'HASH') {
 1915:                         if ($validations{$item}{'_LC_adv'}) {
 1916:                             $canvalidate = 1;
 1917:                         }
 1918:                     }
 1919:                     next if (!$canvalidate);
 1920:                 }
 1921:                 my $checked = '';
 1922:                 if ($val eq $curroption) {
 1923:                     $checked = ' checked="checked"';
 1924:                 } elsif ($option eq 'autolimit') {
 1925:                     if ($curroption =~ /^autolimit/) {
 1926:                         $checked = ' checked="checked"';
 1927:                     }
 1928:                 }
 1929:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1930:                                   '<input type="radio" name="crsreq_'.$item.
 1931:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1932:                                   $titles{$option}.'</label>';
 1933:                 if ($option eq 'autolimit') {
 1934:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1935:                                        $item.'_limit__LC_adv" size="1" '.
 1936:                                        'value="'.$currlimit.'" />';
 1937:                 }
 1938:                 $advcell{$item} .= '</span> ';
 1939:                 if ($option eq 'autolimit') {
 1940:                     $advcell{$item} .= $titles{'unlimited'};
 1941:                 }
 1942:             }
 1943:         } elsif ($context eq 'requestauthor') {
 1944:             my $curroption;
 1945:             if (ref($settings) eq 'HASH') {
 1946:                 $curroption = $settings->{'_LC_adv'};
 1947:             }
 1948:             my $checked = '';
 1949:             if ($curroption eq '') {
 1950:                 $checked = ' checked="checked"';
 1951:             }
 1952:             $datatable .= '<span class="LC_nobreak"><label>'.
 1953:                           '<input type="radio" name="authorreq__LC_adv"'.
 1954:                           ' value=""'.$checked.' />'.
 1955:                           &mt('No override set').'</label></span>&nbsp; ';
 1956:             foreach my $option (@options) {
 1957:                 my $val = $option;
 1958:                 if ($option eq 'norequest') {
 1959:                     $val = 0;
 1960:                 }
 1961:                 my $checked = '';
 1962:                 if ($val eq $curroption) {
 1963:                     $checked = ' checked="checked"';
 1964:                 }
 1965:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1966:                               '<input type="radio" name="authorreq__LC_adv"'.
 1967:                               ' value="'.$val.'"'.$checked.' />'.
 1968:                               $titles{$option}.'</label></span>&nbsp; ';
 1969:             }
 1970:         } else {
 1971:             my $checked = 'checked="checked" ';
 1972:             if (ref($settings) eq 'HASH') {
 1973:                 if (ref($settings->{$item}) eq 'HASH') {
 1974:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1975:                         $checked = '';
 1976:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1977:                         $checked = 'checked="checked" ';
 1978:                     }
 1979:                 }
 1980:             }
 1981:             $datatable .= '<span class="LC_nobreak"><label>'.
 1982:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1983:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1984:                           '</label></span>&nbsp; ';
 1985:         }
 1986:     }
 1987:     if ($context eq 'requestcourses') {
 1988:         $datatable .= '</tr><tr>';
 1989:         foreach my $item (@usertools) {
 1990:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1991:         }
 1992:         $datatable .= '</tr></table>';
 1993:     }
 1994:     $datatable .= '</td></tr>';
 1995:     $$rowtotal += $typecount;
 1996:     return $datatable;
 1997: }
 1998: 
 1999: sub print_requestmail {
 2000:     my ($dom,$action,$settings,$rowtotal) = @_;
 2001:     my ($now,$datatable,%currapp);
 2002:     $now = time;
 2003:     if (ref($settings) eq 'HASH') {
 2004:         if (ref($settings->{'notify'}) eq 'HASH') {
 2005:             if ($settings->{'notify'}{'approval'} ne '') {
 2006:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2007:             }
 2008:         }
 2009:     }
 2010:     my $numinrow = 2;
 2011:     my $css_class;
 2012:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2013:     my $text;
 2014:     if ($action eq 'requestcourses') {
 2015:         $text = &mt('Receive notification of course requests requiring approval');
 2016:     } elsif ($action eq 'requestauthor') {
 2017:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2018:     } else {
 2019:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2020:     }
 2021:     $datatable = '<tr'.$css_class.'>'.
 2022:                  ' <td>'.$text.'</td>'.
 2023:                  ' <td class="LC_left_item">';
 2024:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2025:                                                  $action.'notifyapproval',%currapp);
 2026:     if ($numdc > 0) {
 2027:         $datatable .= $table;
 2028:     } else {
 2029:         $datatable .= &mt('There are no active Domain Coordinators');
 2030:     }
 2031:     $datatable .='</td></tr>';
 2032:     $$rowtotal += $rows;
 2033:     return $datatable;
 2034: }
 2035: 
 2036: sub print_studentcode {
 2037:     my ($settings,$rowtotal) = @_;
 2038:     my $rownum = 0; 
 2039:     my ($output,%current);
 2040:     my @crstypes = ('official','unofficial','community','textbook');
 2041:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2042:         foreach my $type (@crstypes) {
 2043:             $current{$type} = $settings->{'uniquecode'}{$type};
 2044:         }
 2045:     }
 2046:     $output .= '<tr>'.
 2047:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2048:                '<td class="LC_left_item">';
 2049:     foreach my $type (@crstypes) {
 2050:         my $check = ' ';
 2051:         if ($current{$type}) {
 2052:             $check = ' checked="checked" ';
 2053:         }
 2054:         $output .= '<span class="LC_nobreak"><label>'.
 2055:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2056:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2057:     }
 2058:     $output .= '</td></tr>';
 2059:     $$rowtotal ++;
 2060:     return $output;
 2061: }
 2062: 
 2063: sub print_textbookcourses {
 2064:     my ($dom,$settings,$rowtotal) = @_;
 2065:     my $rownum = 0;
 2066:     my $css_class;
 2067:     my $itemcount = 1;
 2068:     my $maxnum = 0;
 2069:     my $bookshash;
 2070:     if (ref($settings) eq 'HASH') {
 2071:         $bookshash = $settings->{'textbooks'};
 2072:     }
 2073:     my %ordered;
 2074:     if (ref($bookshash) eq 'HASH') {
 2075:         foreach my $item (keys(%{$bookshash})) {
 2076:             if (ref($bookshash->{$item}) eq 'HASH') {
 2077:                 my $num = $bookshash->{$item}{'order'};
 2078:                 $ordered{$num} = $item;
 2079:             }
 2080:         }
 2081:     }
 2082:     my $confname = $dom.'-domainconfig';
 2083:     my $switchserver = &check_switchserver($dom,$confname);
 2084:     my $maxnum = scalar(keys(%ordered));
 2085:     my $datatable = &textbookcourses_javascript(\%ordered);
 2086:     if (keys(%ordered)) {
 2087:         my @items = sort { $a <=> $b } keys(%ordered);
 2088:         for (my $i=0; $i<@items; $i++) {
 2089:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2090:             my $key = $ordered{$items[$i]};
 2091:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2092:             my $coursetitle = $coursehash{'description'};
 2093:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2094:             if (ref($bookshash->{$key}) eq 'HASH') {
 2095:                 $subject = $bookshash->{$key}->{'subject'};
 2096:                 $title = $bookshash->{$key}->{'title'};
 2097:                 $author = $bookshash->{$key}->{'author'};
 2098:                 $image = $bookshash->{$key}->{'image'};
 2099:                 if ($image ne '') {
 2100:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2101:                     my $imagethumb = "$path/tn-".$imagefile;
 2102:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2103:                 }
 2104:             }
 2105:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2106:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2107:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2108:             for (my $k=0; $k<=$maxnum; $k++) {
 2109:                 my $vpos = $k+1;
 2110:                 my $selstr;
 2111:                 if ($k == $i) {
 2112:                     $selstr = ' selected="selected" ';
 2113:                 }
 2114:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2115:             }
 2116:             $datatable .= '</select>'.('&nbsp;'x2).
 2117:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2118:                 &mt('Delete?').'</label></span></td>'.
 2119:                 '<td colspan="2">'.
 2120:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2121:                 ('&nbsp;'x2).
 2122:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2123:                 ('&nbsp;'x2).
 2124:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2125:                 ('&nbsp;'x2).
 2126:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2127:             if ($image) {
 2128:                 $datatable .= '<span class="LC_nobreak">'.
 2129:                               $imgsrc.
 2130:                               '<label><input type="checkbox" name="book_image_del"'.
 2131:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2132:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2133:             }
 2134:             if ($switchserver) {
 2135:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2136:             } else {
 2137:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2138:             }
 2139:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2140:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2141:                           $coursetitle.'</span></td></tr>'."\n";
 2142:             $itemcount ++;
 2143:         }
 2144:     }
 2145:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2146:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2147:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2148:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2149:                   '<select name="addbook_pos"'.$chgstr.'>';
 2150:     for (my $k=0; $k<$maxnum+1; $k++) {
 2151:         my $vpos = $k+1;
 2152:         my $selstr;
 2153:         if ($k == $maxnum) {
 2154:             $selstr = ' selected="selected" ';
 2155:         }
 2156:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2157:     }
 2158:     $datatable .= '</select>&nbsp;'."\n".
 2159:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2160:                   '<td colspan="2">'.
 2161:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2162:                   ('&nbsp;'x2).
 2163:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2164:                   ('&nbsp;'x2).
 2165:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
 2166:                   ('&nbsp;'x2).
 2167:                   '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2168:     if ($switchserver) {
 2169:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2170:     } else {
 2171:         $datatable .= '<input type="file" name="addbook_image" value="" />';
 2172:     }
 2173:     $datatable .= '</span>'."\n".
 2174:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2175:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2176:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2177:                   &Apache::loncommon::selectcourse_link
 2178:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2179:                   '</span></td>'."\n".
 2180:                   '</tr>'."\n";
 2181:     $itemcount ++;
 2182:     return $datatable;
 2183: }
 2184: 
 2185: sub textbookcourses_javascript {
 2186:     my ($textbooks) = @_;
 2187:     return unless(ref($textbooks) eq 'HASH');
 2188:     my $num = scalar(keys(%{$textbooks}));
 2189:     my @jsarray;
 2190:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2191:         push(@jsarray,$textbooks->{$item});
 2192:     }
 2193:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2194:     return <<"ENDSCRIPT";
 2195: <script type="text/javascript">
 2196: // <![CDATA[
 2197: function reorderBooks(form,item) {
 2198:     var changedVal;
 2199: $jstext 
 2200:     var newpos = 'addbook_pos';
 2201:     var current = new Array;
 2202:     var maxh = 1 + $num;
 2203:     var current = new Array;
 2204:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2205:     if (item == newpos) {
 2206:         changedVal = newitemVal;
 2207:     } else {
 2208:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2209:         current[newitemVal] = newpos;
 2210:     }
 2211:     for (var i=0; i<textbooks.length; i++) {
 2212:         var elementName = textbooks[i];
 2213:         if (elementName != item) {
 2214:             if (form.elements[elementName]) {
 2215:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2216:                 current[currVal] = elementName;
 2217:             }
 2218:         }
 2219:     }
 2220:     var oldVal;
 2221:     for (var j=0; j<maxh; j++) {
 2222:         if (current[j] == undefined) {
 2223:             oldVal = j;
 2224:         }
 2225:     }
 2226:     if (oldVal < changedVal) {
 2227:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2228:            var elementName = current[k];
 2229:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2230:         }
 2231:     } else {
 2232:         for (var k=changedVal; k<oldVal; k++) {
 2233:             var elementName = current[k];
 2234:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2235:         }
 2236:     }
 2237:     return;
 2238: }
 2239: 
 2240: // ]]>
 2241: </script>
 2242: 
 2243: ENDSCRIPT
 2244: }
 2245: 
 2246: sub print_autoenroll {
 2247:     my ($dom,$settings,$rowtotal) = @_;
 2248:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2249:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2250:     if (ref($settings) eq 'HASH') {
 2251:         if (exists($settings->{'run'})) {
 2252:             if ($settings->{'run'} eq '0') {
 2253:                 $runoff = ' checked="checked" ';
 2254:                 $runon = ' ';
 2255:             } else {
 2256:                 $runon = ' checked="checked" ';
 2257:                 $runoff = ' ';
 2258:             }
 2259:         } else {
 2260:             if ($autorun) {
 2261:                 $runon = ' checked="checked" ';
 2262:                 $runoff = ' ';
 2263:             } else {
 2264:                 $runoff = ' checked="checked" ';
 2265:                 $runon = ' ';
 2266:             }
 2267:         }
 2268:         if (exists($settings->{'co-owners'})) {
 2269:             if ($settings->{'co-owners'} eq '0') {
 2270:                 $coownersoff = ' checked="checked" ';
 2271:                 $coownerson = ' ';
 2272:             } else {
 2273:                 $coownerson = ' checked="checked" ';
 2274:                 $coownersoff = ' ';
 2275:             }
 2276:         } else {
 2277:             $coownersoff = ' checked="checked" ';
 2278:             $coownerson = ' ';
 2279:         }
 2280:         if (exists($settings->{'sender_domain'})) {
 2281:             $defdom = $settings->{'sender_domain'};
 2282:         }
 2283:     } else {
 2284:         if ($autorun) {
 2285:             $runon = ' checked="checked" ';
 2286:             $runoff = ' ';
 2287:         } else {
 2288:             $runoff = ' checked="checked" ';
 2289:             $runon = ' ';
 2290:         }
 2291:     }
 2292:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2293:     my $notif_sender;
 2294:     if (ref($settings) eq 'HASH') {
 2295:         $notif_sender = $settings->{'sender_uname'};
 2296:     }
 2297:     my $datatable='<tr class="LC_odd_row">'.
 2298:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2299:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2300:                   '<input type="radio" name="autoenroll_run"'.
 2301:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2302:                   '<label><input type="radio" name="autoenroll_run"'.
 2303:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2304:                   '</tr><tr>'.
 2305:                   '<td>'.&mt('Notification messages - sender').
 2306:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2307:                   &mt('username').':&nbsp;'.
 2308:                   '<input type="text" name="sender_uname" value="'.
 2309:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2310:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2311:                   '<tr class="LC_odd_row">'.
 2312:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2313:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2314:                   '<input type="radio" name="autoassign_coowners"'.
 2315:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2316:                   '<label><input type="radio" name="autoassign_coowners"'.
 2317:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2318:                   '</tr>';
 2319:     $$rowtotal += 3;
 2320:     return $datatable;
 2321: }
 2322: 
 2323: sub print_autoupdate {
 2324:     my ($position,$dom,$settings,$rowtotal) = @_;
 2325:     my $datatable;
 2326:     if ($position eq 'top') {
 2327:         my $updateon = ' ';
 2328:         my $updateoff = ' checked="checked" ';
 2329:         my $classlistson = ' ';
 2330:         my $classlistsoff = ' checked="checked" ';
 2331:         if (ref($settings) eq 'HASH') {
 2332:             if ($settings->{'run'} eq '1') {
 2333:                 $updateon = $updateoff;
 2334:                 $updateoff = ' ';
 2335:             }
 2336:             if ($settings->{'classlists'} eq '1') {
 2337:                 $classlistson = $classlistsoff;
 2338:                 $classlistsoff = ' ';
 2339:             }
 2340:         }
 2341:         my %title = (
 2342:                    run => 'Auto-update active?',
 2343:                    classlists => 'Update information in classlists?',
 2344:                     );
 2345:         $datatable = '<tr class="LC_odd_row">'. 
 2346:                   '<td>'.&mt($title{'run'}).'</td>'.
 2347:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2348:                   '<input type="radio" name="autoupdate_run"'.
 2349:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2350:                   '<label><input type="radio" name="autoupdate_run"'.
 2351:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2352:                   '</tr><tr>'.
 2353:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2354:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2355:                   '<label><input type="radio" name="classlists"'.
 2356:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2357:                   '<label><input type="radio" name="classlists"'.
 2358:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2359:                   '</tr>';
 2360:         $$rowtotal += 2;
 2361:     } elsif ($position eq 'middle') {
 2362:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2363:         my $numinrow = 3;
 2364:         my $locknamesettings;
 2365:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2366:                                      $dom,$numinrow,$othertitle,
 2367:                                     'lockablenames');
 2368:         $$rowtotal ++;
 2369:     } else {
 2370:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2371:         my @fields = ('lastname','firstname','middlename','generation',
 2372:                       'permanentemail','id');
 2373:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2374:         my $numrows = 0;
 2375:         if (ref($types) eq 'ARRAY') {
 2376:             if (@{$types} > 0) {
 2377:                 $datatable = 
 2378:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2379:                                          \@fields,$types,\$numrows);
 2380:                     $$rowtotal += @{$types}; 
 2381:             }
 2382:         }
 2383:         $datatable .= 
 2384:             &usertype_update_row($settings,{'default' => $othertitle},
 2385:                                  \%fieldtitles,\@fields,['default'],
 2386:                                  \$numrows);
 2387:         $$rowtotal ++;     
 2388:     }
 2389:     return $datatable;
 2390: }
 2391: 
 2392: sub print_autocreate {
 2393:     my ($dom,$settings,$rowtotal) = @_;
 2394:     my (%createon,%createoff,%currhash);
 2395:     my @types = ('xml','req');
 2396:     if (ref($settings) eq 'HASH') {
 2397:         foreach my $item (@types) {
 2398:             $createoff{$item} = ' checked="checked" ';
 2399:             $createon{$item} = ' ';
 2400:             if (exists($settings->{$item})) {
 2401:                 if ($settings->{$item}) {
 2402:                     $createon{$item} = ' checked="checked" ';
 2403:                     $createoff{$item} = ' ';
 2404:                 }
 2405:             }
 2406:         }
 2407:         if ($settings->{'xmldc'} ne '') {
 2408:             $currhash{$settings->{'xmldc'}} = 1;
 2409:         }
 2410:     } else {
 2411:         foreach my $item (@types) {
 2412:             $createoff{$item} = ' checked="checked" ';
 2413:             $createon{$item} = ' ';
 2414:         }
 2415:     }
 2416:     $$rowtotal += 2;
 2417:     my $numinrow = 2;
 2418:     my $datatable='<tr class="LC_odd_row">'.
 2419:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2420:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2421:                   '<input type="radio" name="autocreate_xml"'.
 2422:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2423:                   '<label><input type="radio" name="autocreate_xml"'.
 2424:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2425:                   '</td></tr><tr>'.
 2426:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2427:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2428:                   '<input type="radio" name="autocreate_req"'.
 2429:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2430:                   '<label><input type="radio" name="autocreate_req"'.
 2431:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2432:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2433:                                                    'autocreate_xmldc',%currhash);
 2434:     if ($numdc > 1) {
 2435:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2436:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2437:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2438:     } else {
 2439:         $datatable .= $dctable.'</td></tr>';
 2440:     }
 2441:     $$rowtotal += $rows;
 2442:     return $datatable;
 2443: }
 2444: 
 2445: sub print_directorysrch {
 2446:     my ($dom,$settings,$rowtotal) = @_;
 2447:     my $srchon = ' ';
 2448:     my $srchoff = ' checked="checked" ';
 2449:     my ($exacton,$containson,$beginson);
 2450:     my $localon = ' ';
 2451:     my $localoff = ' checked="checked" ';
 2452:     if (ref($settings) eq 'HASH') {
 2453:         if ($settings->{'available'} eq '1') {
 2454:             $srchon = $srchoff;
 2455:             $srchoff = ' ';
 2456:         }
 2457:         if ($settings->{'localonly'} eq '1') {
 2458:             $localon = $localoff;
 2459:             $localoff = ' ';
 2460:         }
 2461:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2462:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2463:                 if ($type eq 'exact') {
 2464:                     $exacton = ' checked="checked" ';
 2465:                 } elsif ($type eq 'contains') {
 2466:                     $containson = ' checked="checked" ';
 2467:                 } elsif ($type eq 'begins') {
 2468:                     $beginson = ' checked="checked" ';
 2469:                 }
 2470:             }
 2471:         } else {
 2472:             if ($settings->{'searchtypes'} eq 'exact') {
 2473:                 $exacton = ' checked="checked" ';
 2474:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2475:                 $containson = ' checked="checked" ';
 2476:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2477:                 $exacton = ' checked="checked" ';
 2478:                 $containson = ' checked="checked" ';
 2479:             }
 2480:         }
 2481:     }
 2482:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2483:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2484: 
 2485:     my $numinrow = 4;
 2486:     my $cansrchrow = 0;
 2487:     my $datatable='<tr class="LC_odd_row">'.
 2488:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2489:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2490:                   '<input type="radio" name="dirsrch_available"'.
 2491:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2492:                   '<label><input type="radio" name="dirsrch_available"'.
 2493:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2494:                   '</tr><tr>'.
 2495:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2496:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2497:                   '<input type="radio" name="dirsrch_localonly"'.
 2498:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2499:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2500:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2501:                   '</tr>';
 2502:     $$rowtotal += 2;
 2503:     if (ref($usertypes) eq 'HASH') {
 2504:         if (keys(%{$usertypes}) > 0) {
 2505:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2506:                                          $numinrow,$othertitle,'cansearch');
 2507:             $cansrchrow = 1;
 2508:         }
 2509:     }
 2510:     if ($cansrchrow) {
 2511:         $$rowtotal ++;
 2512:         $datatable .= '<tr>';
 2513:     } else {
 2514:         $datatable .= '<tr class="LC_odd_row">';
 2515:     }
 2516:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2517:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2518:     foreach my $title (@{$titleorder}) {
 2519:         if (defined($searchtitles->{$title})) {
 2520:             my $check = ' ';
 2521:             if (ref($settings) eq 'HASH') {
 2522:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2523:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2524:                         $check = ' checked="checked" ';
 2525:                     }
 2526:                 }
 2527:             }
 2528:             $datatable .= '<td class="LC_left_item">'.
 2529:                           '<span class="LC_nobreak"><label>'.
 2530:                           '<input type="checkbox" name="searchby" '.
 2531:                           'value="'.$title.'"'.$check.'/>'.
 2532:                           $searchtitles->{$title}.'</label></span></td>';
 2533:         }
 2534:     }
 2535:     $datatable .= '</tr></table></td></tr>';
 2536:     $$rowtotal ++;
 2537:     if ($cansrchrow) {
 2538:         $datatable .= '<tr class="LC_odd_row">';
 2539:     } else {
 2540:         $datatable .= '<tr>';
 2541:     }
 2542:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2543:                   '<td class="LC_left_item" colspan="2">'.
 2544:                   '<span class="LC_nobreak"><label>'.
 2545:                   '<input type="checkbox" name="searchtypes" '.
 2546:                   $exacton.' value="exact" />'.&mt('Exact match').
 2547:                   '</label>&nbsp;'.
 2548:                   '<label><input type="checkbox" name="searchtypes" '.
 2549:                   $beginson.' value="begins" />'.&mt('Begins with').
 2550:                   '</label>&nbsp;'.
 2551:                   '<label><input type="checkbox" name="searchtypes" '.
 2552:                   $containson.' value="contains" />'.&mt('Contains').
 2553:                   '</label></span></td></tr>';
 2554:     $$rowtotal ++;
 2555:     return $datatable;
 2556: }
 2557: 
 2558: sub print_contacts {
 2559:     my ($dom,$settings,$rowtotal) = @_;
 2560:     my $datatable;
 2561:     my @contacts = ('adminemail','supportemail');
 2562:     my (%checked,%to,%otheremails,%bccemails);
 2563:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2564:                     'requestsmail','updatesmail','idconflictsmail');
 2565:     foreach my $type (@mailings) {
 2566:         $otheremails{$type} = '';
 2567:     }
 2568:     $bccemails{'helpdeskmail'} = '';
 2569:     if (ref($settings) eq 'HASH') {
 2570:         foreach my $item (@contacts) {
 2571:             if (exists($settings->{$item})) {
 2572:                 $to{$item} = $settings->{$item};
 2573:             }
 2574:         }
 2575:         foreach my $type (@mailings) {
 2576:             if (exists($settings->{$type})) {
 2577:                 if (ref($settings->{$type}) eq 'HASH') {
 2578:                     foreach my $item (@contacts) {
 2579:                         if ($settings->{$type}{$item}) {
 2580:                             $checked{$type}{$item} = ' checked="checked" ';
 2581:                         }
 2582:                     }
 2583:                     $otheremails{$type} = $settings->{$type}{'others'};
 2584:                     if ($type eq 'helpdeskmail') {
 2585:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2586:                     }
 2587:                 }
 2588:             } elsif ($type eq 'lonstatusmail') {
 2589:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2590:             }
 2591:         }
 2592:     } else {
 2593:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2594:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2595:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2596:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2597:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2598:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2599:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2600:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2601:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2602:     }
 2603:     my ($titles,$short_titles) = &contact_titles();
 2604:     my $rownum = 0;
 2605:     my $css_class;
 2606:     foreach my $item (@contacts) {
 2607:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2608:         $datatable .= '<tr'.$css_class.'>'. 
 2609:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2610:                   '</span></td><td class="LC_right_item">'.
 2611:                   '<input type="text" name="'.$item.'" value="'.
 2612:                   $to{$item}.'" /></td></tr>';
 2613:         $rownum ++;
 2614:     }
 2615:     foreach my $type (@mailings) {
 2616:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2617:         $datatable .= '<tr'.$css_class.'>'.
 2618:                       '<td><span class="LC_nobreak">'.
 2619:                       $titles->{$type}.': </span></td>'.
 2620:                       '<td class="LC_left_item">'.
 2621:                       '<span class="LC_nobreak">';
 2622:         foreach my $item (@contacts) {
 2623:             $datatable .= '<label>'.
 2624:                           '<input type="checkbox" name="'.$type.'"'.
 2625:                           $checked{$type}{$item}.
 2626:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2627:                           '</label>&nbsp;';
 2628:         }
 2629:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2630:                       '<input type="text" name="'.$type.'_others" '.
 2631:                       'value="'.$otheremails{$type}.'"  />';
 2632:         if ($type eq 'helpdeskmail') {
 2633:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2634:                           '<input type="text" name="'.$type.'_bcc" '.
 2635:                           'value="'.$bccemails{$type}.'"  />';
 2636:         }
 2637:         $datatable .= '</td></tr>'."\n";
 2638:         $rownum ++;
 2639:     }
 2640:     my %choices;
 2641:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2642:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2643:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2644:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2645:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2646:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2647:     my @toggles = ('reporterrors','reportupdates');
 2648:     my %defaultchecked = ('reporterrors'  => 'on',
 2649:                           'reportupdates' => 'on');
 2650:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2651:                                                \%choices,$rownum);
 2652:     $datatable .= $reports;
 2653:     $$rowtotal += $rownum;
 2654:     return $datatable;
 2655: }
 2656: 
 2657: sub print_helpsettings {
 2658:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2659:     my ($datatable,$itemcount);
 2660:     $itemcount = 1;
 2661:     my (%choices,%defaultchecked,@toggles);
 2662:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2663:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2664:                                  &mt('LON-CAPA bug tracker'),600,500));
 2665:     %defaultchecked = ('submitbugs' => 'on');
 2666:     @toggles = ('submitbugs',);
 2667: 
 2668:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2669:                                                  \%choices,$itemcount);
 2670:     return $datatable;
 2671: }
 2672: 
 2673: sub radiobutton_prefs {
 2674:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2675:         $additional) = @_;
 2676:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2677:                    (ref($choices) eq 'HASH'));
 2678: 
 2679:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2680: 
 2681:     foreach my $item (@{$toggles}) {
 2682:         if ($defaultchecked->{$item} eq 'on') {
 2683:             $checkedon{$item} = ' checked="checked" ';
 2684:             $checkedoff{$item} = ' ';
 2685:         } elsif ($defaultchecked->{$item} eq 'off') {
 2686:             $checkedoff{$item} = ' checked="checked" ';
 2687:             $checkedon{$item} = ' ';
 2688:         }
 2689:     }
 2690:     if (ref($settings) eq 'HASH') {
 2691:         foreach my $item (@{$toggles}) {
 2692:             if ($settings->{$item} eq '1') {
 2693:                 $checkedon{$item} =  ' checked="checked" ';
 2694:                 $checkedoff{$item} = ' ';
 2695:             } elsif ($settings->{$item} eq '0') {
 2696:                 $checkedoff{$item} =  ' checked="checked" ';
 2697:                 $checkedon{$item} = ' ';
 2698:             }
 2699:         }
 2700:     }
 2701:     if ($onclick) {
 2702:         $onclick = ' onclick="'.$onclick.'"';
 2703:     }
 2704:     foreach my $item (@{$toggles}) {
 2705:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2706:         $datatable .=
 2707:             '<tr'.$css_class.'><td valign="top">'.
 2708:             '<span class="LC_nobreak">'.$choices->{$item}.
 2709:             '</span></td>'.
 2710:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2711:             '<label><input type="radio" name="'.
 2712:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2713:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2714:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2715:             '</span>'.$additional.
 2716:             '</td>'.
 2717:             '</tr>';
 2718:         $itemcount ++;
 2719:     }
 2720:     return ($datatable,$itemcount);
 2721: }
 2722: 
 2723: sub print_coursedefaults {
 2724:     my ($position,$dom,$settings,$rowtotal) = @_;
 2725:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2726:     my $itemcount = 1;
 2727:     my %choices =  &Apache::lonlocal::texthash (
 2728:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2729:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2730:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2731:         coursecredits        => 'Credits can be specified for courses',
 2732:     );
 2733:     my %staticdefaults = (
 2734:                            anonsurvey_threshold => 10,
 2735:                            uploadquota          => 500,
 2736:                          );
 2737:     if ($position eq 'top') {
 2738:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2739:         @toggles = ('canuse_pdfforms');
 2740:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2741:                                                  \%choices,$itemcount);
 2742:     } else {
 2743:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2744:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2745:             %curruploadquota);
 2746:         my $currusecredits = 0;
 2747:         my @types = ('official','unofficial','community','textbook');
 2748:         if (ref($settings) eq 'HASH') {
 2749:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2750:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2751:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2752:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2753:                 }
 2754:             }
 2755:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2756:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2757:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2758:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2759:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2760:                     ($def_textbook_credits ne '')) {
 2761:                     $currusecredits = 1;
 2762:                 }
 2763:             }
 2764:         }
 2765:         if (!$currdefresponder) {
 2766:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2767:         } elsif ($currdefresponder < 1) {
 2768:             $currdefresponder = 1;
 2769:         }
 2770:         foreach my $type (@types) {
 2771:             if ($curruploadquota{$type} eq '') {
 2772:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2773:             }
 2774:         }
 2775:         $datatable .=
 2776:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2777:                 $choices{'anonsurvey_threshold'}.
 2778:                 '</span></td>'.
 2779:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2780:                 '<input type="text" name="anonsurvey_threshold"'.
 2781:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2782:                 '</td></tr>'."\n";
 2783:         $itemcount ++;
 2784:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2785:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2786:                       $choices{'uploadquota'}.
 2787:                       '</span></td>'.
 2788:                       '<td align="right" class="LC_right_item">'.
 2789:                       '<table><tr>';
 2790:         foreach my $type (@types) {
 2791:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2792:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2793:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2794:         }
 2795:         $datatable .= '</tr></table></td></tr>'."\n";
 2796:         $itemcount ++;
 2797:         my $onclick = 'toggleCredits(this.form);';
 2798:         my $display = 'none';
 2799:         if ($currusecredits) {
 2800:             $display = 'block';
 2801:         }
 2802:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2803:                          '<span class="LC_nobreak">'.
 2804:                          &mt('Default credits for official courses [_1]',
 2805:                          '<input type="text" name="official_credits" value="'.
 2806:                          $def_official_credits.'" size="3" />').
 2807:                          '</span><br />'.
 2808:                          '<span class="LC_nobreak">'.
 2809:                          &mt('Default credits for unofficial courses [_1]',
 2810:                          '<input type="text" name="unofficial_credits" value="'.
 2811:                          $def_unofficial_credits.'" size="3" />').
 2812:                          '</span><br />'.
 2813:                          '<span class="LC_nobreak">'.
 2814:                          &mt('Default credits for textbook courses [_1]',
 2815:                          '<input type="text" name="textbook_credits" value="'.
 2816:                          $def_textbook_credits.'" size="3" />').
 2817:                          '</span></div>'."\n";
 2818:         %defaultchecked = ('coursecredits' => 'off');
 2819:         @toggles = ('coursecredits');
 2820:         my $current = {
 2821:                         'coursecredits' => $currusecredits,
 2822:                       };
 2823:         (my $table,$itemcount) =
 2824:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2825:                                \%choices,$itemcount,$onclick,$additional);
 2826:         $datatable .= $table;
 2827:         $itemcount ++;
 2828:     }
 2829:     $$rowtotal += $itemcount;
 2830:     return $datatable;
 2831: }
 2832: 
 2833: sub print_selfenrollment {
 2834:     my ($position,$dom,$settings,$rowtotal) = @_;
 2835:     my ($css_class,$datatable);
 2836:     my $itemcount = 1;
 2837:     my @types = ('official','unofficial','community','textbook');
 2838:     if (($position eq 'top') || ($position eq 'middle')) {
 2839:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 2840:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 2841:         my @rows;
 2842:         my $key;
 2843:         if ($position eq 'top') {
 2844:             $key = 'admin'; 
 2845:             if (ref($rowsref) eq 'ARRAY') {
 2846:                 @rows = @{$rowsref};
 2847:             }
 2848:         } elsif ($position eq 'middle') {
 2849:             $key = 'default';
 2850:             @rows = ('types','registered','approval','limit');
 2851:         }
 2852:         foreach my $row (@rows) {
 2853:             if (defined($titlesref->{$row})) {
 2854:                 $itemcount ++;
 2855:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2856:                 $datatable .= '<tr'.$css_class.'>'.
 2857:                               '<td>'.$titlesref->{$row}.'</td>'.
 2858:                               '<td class="LC_left_item">'.
 2859:                               '<table><tr>';
 2860:                 my (%current,%currentcap);
 2861:                 if (ref($settings) eq 'HASH') {
 2862:                     if (ref($settings->{$key}) eq 'HASH') {
 2863:                         foreach my $type (@types) {
 2864:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2865:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2866:                             }
 2867:                             if (($row eq 'limit') && ($key eq 'default')) {
 2868:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2869:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2870:                                 }
 2871:                             }
 2872:                         }
 2873:                     }
 2874:                 }
 2875:                 my %roles = (
 2876:                              '0' => &Apache::lonnet::plaintext('dc'),
 2877:                             ); 
 2878:             
 2879:                 foreach my $type (@types) {
 2880:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2881:                         $datatable .= '<th>'.&mt($type).'</th>';
 2882:                     }
 2883:                 }
 2884:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2885:                     $datatable .= '</tr><tr>';
 2886:                 }
 2887:                 foreach my $type (@types) {
 2888:                     if ($type eq 'community') {
 2889:                         $roles{'1'} = &mt('Community personnel');
 2890:                     } else {
 2891:                         $roles{'1'} = &mt('Course personnel');
 2892:                     }
 2893:                     $datatable .= '<td style="vertical-align: top">';
 2894:                     if ($position eq 'top') {
 2895:                         my %checked;
 2896:                         if ($current{$type} eq '0') {
 2897:                             $checked{'0'} = ' checked="checked"';
 2898:                         } else {
 2899:                             $checked{'1'} = ' checked="checked"';
 2900:                         }
 2901:                         foreach my $role ('1','0') {
 2902:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2903:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2904:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2905:                                           $roles{$role}.'</label></span> ';
 2906:                         }
 2907:                     } else {
 2908:                         if ($row eq 'types') {
 2909:                             my %checked;
 2910:                             if ($current{$type} =~ /^(all|dom)$/) {
 2911:                                 $checked{$1} = ' checked="checked"';
 2912:                             } else {
 2913:                                 $checked{''} = ' checked="checked"';
 2914:                             }
 2915:                             foreach my $val ('','dom','all') {
 2916:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2917:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2918:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2919:                             }
 2920:                         } elsif ($row eq 'registered') {
 2921:                             my %checked;
 2922:                             if ($current{$type} eq '1') {
 2923:                                 $checked{'1'} = ' checked="checked"';
 2924:                             } else {
 2925:                                 $checked{'0'} = ' checked="checked"';
 2926:                             }
 2927:                             foreach my $val ('0','1') {
 2928:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2929:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2930:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2931:                             }
 2932:                         } elsif ($row eq 'approval') {
 2933:                             my %checked;
 2934:                             if ($current{$type} =~ /^([12])$/) {
 2935:                                 $checked{$1} = ' checked="checked"';
 2936:                             } else {
 2937:                                 $checked{'0'} = ' checked="checked"';
 2938:                             }
 2939:                             for my $val (0..2) {
 2940:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2941:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2942:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2943:                             }
 2944:                         } elsif ($row eq 'limit') {
 2945:                             my %checked;
 2946:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 2947:                                 $checked{$1} = ' checked="checked"';
 2948:                             } else {
 2949:                                 $checked{'none'} = ' checked="checked"';
 2950:                             }
 2951:                             my $cap;
 2952:                             if ($currentcap{$type} =~ /^\d+$/) {
 2953:                                 $cap = $currentcap{$type};
 2954:                             }
 2955:                             foreach my $val ('none','allstudents','selfenrolled') {
 2956:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2957:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2958:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2959:                             }
 2960:                             $datatable .= '<br />'.
 2961:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 2962:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 2963:                                           '</span>'; 
 2964:                         }
 2965:                     }
 2966:                     $datatable .= '</td>';
 2967:                 }
 2968:                 $datatable .= '</tr>';
 2969:             }
 2970:             $datatable .= '</table></td></tr>';
 2971:         }
 2972:     } elsif ($position eq 'bottom') {
 2973:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 2974:     }
 2975:     $$rowtotal += $itemcount;
 2976:     return $datatable;
 2977: }
 2978: 
 2979: sub print_validation_rows {
 2980:     my ($caller,$dom,$settings,$rowtotal) = @_;
 2981:     my ($itemsref,$namesref,$fieldsref);
 2982:     if ($caller eq 'selfenroll') { 
 2983:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 2984:     } elsif ($caller eq 'requestcourses') {
 2985:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 2986:     }
 2987:     my %currvalidation;
 2988:     if (ref($settings) eq 'HASH') {
 2989:         if (ref($settings->{'validation'}) eq 'HASH') {
 2990:             %currvalidation = %{$settings->{'validation'}};
 2991:         }
 2992:     }
 2993:     my $datatable;
 2994:     my $itemcount = 0;
 2995:     foreach my $item (@{$itemsref}) {
 2996:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2997:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2998:                       $namesref->{$item}.
 2999:                       '</span></td>'.
 3000:                       '<td class="LC_left_item">';
 3001:         if (($item eq 'url') || ($item eq 'button')) {
 3002:             $datatable .= '<span class="LC_nobreak">'.
 3003:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3004:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3005:         } elsif ($item eq 'fields') {
 3006:             my @currfields;
 3007:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3008:                 @currfields = @{$currvalidation{$item}};
 3009:             }
 3010:             foreach my $field (@{$fieldsref}) {
 3011:                 my $check = '';
 3012:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3013:                     $check = ' checked="checked"';
 3014:                 }
 3015:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3016:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3017:                               ' value="'.$field.'"'.$check.' />'.$field.
 3018:                               '</label></span> ';
 3019:             }
 3020:         } elsif ($item eq 'markup') {
 3021:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3022:                            $currvalidation{$item}.
 3023:                               '</textarea>';
 3024:         }
 3025:         $datatable .= '</td></tr>'."\n";
 3026:         if (ref($rowtotal)) {
 3027:             $itemcount ++;
 3028:         }
 3029:     }
 3030:     if ($caller eq 'requestcourses') {
 3031:         my %currhash;
 3032:         if (ref($settings->{'validation'}) eq 'HASH') {
 3033:             if ($settings->{'validation'}{'dc'} ne '') {
 3034:                 $currhash{$settings->{'validation'}{'dc'}} = 1;
 3035:             }
 3036:         }
 3037:         my $numinrow = 2;
 3038:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3039:                                                        'validationdc',%currhash);
 3040:         if ($numdc > 1) {
 3041:             $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 3042:                           &mt('Course creation processed as: (choose Dom. Coord.)').
 3043:                           '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3044:         } else {
 3045:             $datatable .= $dctable.'</td></tr>';
 3046:         }
 3047:         $itemcount ++;
 3048:     }
 3049:     if (ref($rowtotal)) {
 3050:         $$rowtotal += $itemcount;
 3051:     }
 3052:     return $datatable;
 3053: }
 3054: 
 3055: sub print_usersessions {
 3056:     my ($position,$dom,$settings,$rowtotal) = @_;
 3057:     my ($css_class,$datatable,%checked,%choices);
 3058:     my (%by_ip,%by_location,@intdoms);
 3059:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3060: 
 3061:     my @alldoms = &Apache::lonnet::all_domains();
 3062:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3063:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3064:     my %altids = &id_for_thisdom(%servers);
 3065:     my $itemcount = 1;
 3066:     if ($position eq 'top') {
 3067:         if (keys(%serverhomes) > 1) {
 3068:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3069:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3070:         } else {
 3071:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3072:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3073:         }
 3074:     } else {
 3075:         if (keys(%by_location) == 0) {
 3076:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3077:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3078:         } else {
 3079:             my %lt = &usersession_titles();
 3080:             my $numinrow = 5;
 3081:             my $prefix;
 3082:             my @types;
 3083:             if ($position eq 'bottom') {
 3084:                 $prefix = 'remote';
 3085:                 @types = ('version','excludedomain','includedomain');
 3086:             } else {
 3087:                 $prefix = 'hosted';
 3088:                 @types = ('excludedomain','includedomain');
 3089:             }
 3090:             my (%current,%checkedon,%checkedoff);
 3091:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3092:             my @locations = sort(keys(%by_location));
 3093:             foreach my $type (@types) {
 3094:                 $checkedon{$type} = '';
 3095:                 $checkedoff{$type} = ' checked="checked"';
 3096:             }
 3097:             if (ref($settings) eq 'HASH') {
 3098:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3099:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3100:                         $current{$key} = $settings->{$prefix}{$key};
 3101:                         if ($key eq 'version') {
 3102:                             if ($current{$key} ne '') {
 3103:                                 $checkedon{$key} = ' checked="checked"';
 3104:                                 $checkedoff{$key} = '';
 3105:                             }
 3106:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3107:                             $checkedon{$key} = ' checked="checked"';
 3108:                             $checkedoff{$key} = '';
 3109:                         }
 3110:                     }
 3111:                 }
 3112:             }
 3113:             foreach my $type (@types) {
 3114:                 next if ($type ne 'version' && !@locations);
 3115:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3116:                 $datatable .= '<tr'.$css_class.'>
 3117:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3118:                                <span class="LC_nobreak">&nbsp;
 3119:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3120:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3121:                 if ($type eq 'version') {
 3122:                     my $selector = '<select name="'.$prefix.'_version">';
 3123:                     foreach my $version (@lcversions) {
 3124:                         my $selected = '';
 3125:                         if ($current{'version'} eq $version) {
 3126:                             $selected = ' selected="selected"';
 3127:                         }
 3128:                         $selector .= ' <option value="'.$version.'"'.
 3129:                                      $selected.'>'.$version.'</option>';
 3130:                     }
 3131:                     $selector .= '</select> ';
 3132:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3133:                 } else {
 3134:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3135:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3136:                                  ' />'.('&nbsp;'x2).
 3137:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3138:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3139:                                  "\n".
 3140:                                  '</div><div><table>';
 3141:                     my $rem;
 3142:                     for (my $i=0; $i<@locations; $i++) {
 3143:                         my ($showloc,$value,$checkedtype);
 3144:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3145:                             my $ip = $by_location{$locations[$i]}->[0];
 3146:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3147:                                  $value = join(':',@{$by_ip{$ip}});
 3148:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3149:                                 if (ref($current{$type}) eq 'ARRAY') {
 3150:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3151:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3152:                                             $checkedtype = ' checked="checked"';
 3153:                                             last;
 3154:                                         }
 3155:                                     }
 3156:                                 }
 3157:                             }
 3158:                         }
 3159:                         $rem = $i%($numinrow);
 3160:                         if ($rem == 0) {
 3161:                             if ($i > 0) {
 3162:                                 $datatable .= '</tr>';
 3163:                             }
 3164:                             $datatable .= '<tr>';
 3165:                         }
 3166:                         $datatable .= '<td class="LC_left_item">'.
 3167:                                       '<span class="LC_nobreak"><label>'.
 3168:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3169:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3170:                                       '</label></span></td>';
 3171:                     }
 3172:                     $rem = @locations%($numinrow);
 3173:                     my $colsleft = $numinrow - $rem;
 3174:                     if ($colsleft > 1 ) {
 3175:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3176:                                       '&nbsp;</td>';
 3177:                     } elsif ($colsleft == 1) {
 3178:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3179:                     }
 3180:                     $datatable .= '</tr></table>';
 3181:                 }
 3182:                 $datatable .= '</td></tr>';
 3183:                 $itemcount ++;
 3184:             }
 3185:         }
 3186:     }
 3187:     $$rowtotal += $itemcount;
 3188:     return $datatable;
 3189: }
 3190: 
 3191: sub build_location_hashes {
 3192:     my ($intdoms,$by_ip,$by_location) = @_;
 3193:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3194:                   (ref($by_location) eq 'HASH')); 
 3195:     my %iphost = &Apache::lonnet::get_iphost();
 3196:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3197:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3198:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3199:         foreach my $id (@{$iphost{$primary_ip}}) {
 3200:             my $intdom = &Apache::lonnet::internet_dom($id);
 3201:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3202:                 push(@{$intdoms},$intdom);
 3203:             }
 3204:         }
 3205:     }
 3206:     foreach my $ip (keys(%iphost)) {
 3207:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3208:             foreach my $id (@{$iphost{$ip}}) {
 3209:                 my $location = &Apache::lonnet::internet_dom($id);
 3210:                 if ($location) {
 3211:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3212:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3213:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3214:                             push(@{$by_ip->{$ip}},$location);
 3215:                         }
 3216:                     } else {
 3217:                         $by_ip->{$ip} = [$location];
 3218:                     }
 3219:                 }
 3220:             }
 3221:         }
 3222:     }
 3223:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3224:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3225:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3226:             my $first = $by_ip->{$ip}->[0];
 3227:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3228:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3229:                     push(@{$by_location->{$first}},$ip);
 3230:                 }
 3231:             } else {
 3232:                 $by_location->{$first} = [$ip];
 3233:             }
 3234:         }
 3235:     }
 3236:     return;
 3237: }
 3238: 
 3239: sub current_offloads_to {
 3240:     my ($dom,$settings,$servers) = @_;
 3241:     my (%spareid,%otherdomconfigs);
 3242:     if (ref($servers) eq 'HASH') {
 3243:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3244:             my $gotspares;
 3245:             if (ref($settings) eq 'HASH') {
 3246:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3247:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3248:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3249:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3250:                         $gotspares = 1;
 3251:                     }
 3252:                 }
 3253:             }
 3254:             unless ($gotspares) {
 3255:                 my $gotspares;
 3256:                 my $serverhomeID =
 3257:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3258:                 my $serverhomedom =
 3259:                     &Apache::lonnet::host_domain($serverhomeID);
 3260:                 if ($serverhomedom ne $dom) {
 3261:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3262:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3263:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3264:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3265:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3266:                                 $gotspares = 1;
 3267:                             }
 3268:                         }
 3269:                     } else {
 3270:                         $otherdomconfigs{$serverhomedom} =
 3271:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3272:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3273:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3274:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3275:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3276:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3277:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3278:                                         $gotspares = 1;
 3279:                                     }
 3280:                                 }
 3281:                             }
 3282:                         }
 3283:                     }
 3284:                 }
 3285:             }
 3286:             unless ($gotspares) {
 3287:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3288:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3289:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3290:                } else {
 3291:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3292:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3293:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3294:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3295:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3296:                     } else {
 3297:                         my %what = (
 3298:                              spareid => 1,
 3299:                         );
 3300:                         my ($result,$returnhash) = 
 3301:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3302:                         if ($result eq 'ok') { 
 3303:                             if (ref($returnhash) eq 'HASH') {
 3304:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3305:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3306:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3307:                                 }
 3308:                             }
 3309:                         }
 3310:                     }
 3311:                 }
 3312:             }
 3313:         }
 3314:     }
 3315:     return %spareid;
 3316: }
 3317: 
 3318: sub spares_row {
 3319:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3320:     my $css_class;
 3321:     my $numinrow = 4;
 3322:     my $itemcount = 1;
 3323:     my $datatable;
 3324:     my %typetitles = &sparestype_titles();
 3325:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3326:         foreach my $server (sort(keys(%{$servers}))) {
 3327:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3328:             my ($othercontrol,$serverdom);
 3329:             if ($serverhome ne $server) {
 3330:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3331:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3332:             } else {
 3333:                 $serverdom = &Apache::lonnet::host_domain($server);
 3334:                 if ($serverdom ne $dom) {
 3335:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3336:                 }
 3337:             }
 3338:             next unless (ref($spareid->{$server}) eq 'HASH');
 3339:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3340:             $datatable .= '<tr'.$css_class.'>
 3341:                            <td rowspan="2">
 3342:                             <span class="LC_nobreak">'.
 3343:                           &mt('[_1] when busy, offloads to:'
 3344:                               ,'<b>'.$server.'</b>').
 3345:                           "\n";
 3346:             my (%current,%canselect);
 3347:             my @choices = 
 3348:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3349:             foreach my $type ('primary','default') {
 3350:                 if (ref($spareid->{$server}) eq 'HASH') {
 3351:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3352:                         my @spares = @{$spareid->{$server}{$type}};
 3353:                         if (@spares > 0) {
 3354:                             if ($othercontrol) {
 3355:                                 $current{$type} = join(', ',@spares);
 3356:                             } else {
 3357:                                 $current{$type} .= '<table>';
 3358:                                 my $numspares = scalar(@spares);
 3359:                                 for (my $i=0;  $i<@spares; $i++) {
 3360:                                     my $rem = $i%($numinrow);
 3361:                                     if ($rem == 0) {
 3362:                                         if ($i > 0) {
 3363:                                             $current{$type} .= '</tr>';
 3364:                                         }
 3365:                                         $current{$type} .= '<tr>';
 3366:                                     }
 3367:                                     $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;'.
 3368:                                                        $spareid->{$server}{$type}[$i].
 3369:                                                        '</label></td>'."\n";
 3370:                                 }
 3371:                                 my $rem = @spares%($numinrow);
 3372:                                 my $colsleft = $numinrow - $rem;
 3373:                                 if ($colsleft > 1 ) {
 3374:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3375:                                                        '" class="LC_left_item">'.
 3376:                                                        '&nbsp;</td>';
 3377:                                 } elsif ($colsleft == 1) {
 3378:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3379:                                 }
 3380:                                 $current{$type} .= '</tr></table>';
 3381:                             }
 3382:                         }
 3383:                     }
 3384:                     if ($current{$type} eq '') {
 3385:                         $current{$type} = &mt('None specified');
 3386:                     }
 3387:                     if ($othercontrol) {
 3388:                         if ($type eq 'primary') {
 3389:                             $canselect{$type} = $othercontrol;
 3390:                         }
 3391:                     } else {
 3392:                         $canselect{$type} = 
 3393:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3394:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3395:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3396:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3397:                         if (@choices > 0) {
 3398:                             foreach my $lonhost (@choices) {
 3399:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3400:                             }
 3401:                         }
 3402:                         $canselect{$type} .= '</select>'."\n";
 3403:                     }
 3404:                 } else {
 3405:                     $current{$type} = &mt('Could not be determined');
 3406:                     if ($type eq 'primary') {
 3407:                         $canselect{$type} =  $othercontrol;
 3408:                     }
 3409:                 }
 3410:                 if ($type eq 'default') {
 3411:                     $datatable .= '<tr'.$css_class.'>';
 3412:                 }
 3413:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3414:                               '<td>'.$current{$type}.'</td>'."\n".
 3415:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3416:             }
 3417:             $itemcount ++;
 3418:         }
 3419:     }
 3420:     $$rowtotal += $itemcount;
 3421:     return $datatable;
 3422: }
 3423: 
 3424: sub possible_newspares {
 3425:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3426:     my $serverhostname = &Apache::lonnet::hostname($server);
 3427:     my %excluded;
 3428:     if ($serverhostname ne '') {
 3429:         %excluded = (
 3430:                        $serverhostname => 1,
 3431:                     );
 3432:     }
 3433:     if (ref($currspares) eq 'HASH') {
 3434:         foreach my $type (keys(%{$currspares})) {
 3435:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3436:                 if (@{$currspares->{$type}} > 0) {
 3437:                     foreach my $curr (@{$currspares->{$type}}) {
 3438:                         my $hostname = &Apache::lonnet::hostname($curr);
 3439:                         $excluded{$hostname} = 1;
 3440:                     }
 3441:                 }
 3442:             }
 3443:         }
 3444:     }
 3445:     my @choices;
 3446:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3447:         if (keys(%{$serverhomes}) > 1) {
 3448:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3449:                 unless ($excluded{$name}) {
 3450:                     if (exists($altids->{$serverhomes->{$name}})) {
 3451:                         push(@choices,$altids->{$serverhomes->{$name}});
 3452:                     } else {
 3453:                         push(@choices,$serverhomes->{$name});
 3454:                     }
 3455:                 }
 3456:             }
 3457:         }
 3458:     }
 3459:     return sort(@choices);
 3460: }
 3461: 
 3462: sub print_loadbalancing {
 3463:     my ($dom,$settings,$rowtotal) = @_;
 3464:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3465:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3466:     my $numinrow = 1;
 3467:     my $datatable;
 3468:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3469:     my (%currbalancer,%currtargets,%currrules,%existing);
 3470:     if (ref($settings) eq 'HASH') {
 3471:         %existing = %{$settings};
 3472:     }
 3473:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3474:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3475:                                   \%currtargets,\%currrules);
 3476:     } else {
 3477:         return;
 3478:     }
 3479:     my ($othertitle,$usertypes,$types) =
 3480:         &Apache::loncommon::sorted_inst_types($dom);
 3481:     my $rownum = 8;
 3482:     if (ref($types) eq 'ARRAY') {
 3483:         $rownum += scalar(@{$types});
 3484:     }
 3485:     my @css_class = ('LC_odd_row','LC_even_row');
 3486:     my $balnum = 0;
 3487:     my $islast;
 3488:     my (@toshow,$disabledtext);
 3489:     if (keys(%currbalancer) > 0) {
 3490:         @toshow = sort(keys(%currbalancer));
 3491:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3492:             push(@toshow,'');
 3493:         }
 3494:     } else {
 3495:         @toshow = ('');
 3496:         $disabledtext = &mt('No existing load balancer');
 3497:     }
 3498:     foreach my $lonhost (@toshow) {
 3499:         if ($balnum == scalar(@toshow)-1) {
 3500:             $islast = 1;
 3501:         } else {
 3502:             $islast = 0;
 3503:         }
 3504:         my $cssidx = $balnum%2;
 3505:         my $targets_div_style = 'display: none';
 3506:         my $disabled_div_style = 'display: block';
 3507:         my $homedom_div_style = 'display: none';
 3508:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3509:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3510:                       '<p>';
 3511:         if ($lonhost eq '') {
 3512:             $datatable .= '<span class="LC_nobreak">';
 3513:             if (keys(%currbalancer) > 0) {
 3514:                 $datatable .= &mt('Add balancer:');
 3515:             } else {
 3516:                 $datatable .= &mt('Enable balancer:');
 3517:             }
 3518:             $datatable .= '&nbsp;'.
 3519:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3520:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3521:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3522:                           '<option value="" selected="selected">'.&mt('None').
 3523:                           '</option>'."\n";
 3524:             foreach my $server (sort(keys(%servers))) {
 3525:                 next if ($currbalancer{$server});
 3526:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3527:             }
 3528:             $datatable .=
 3529:                 '</select>'."\n".
 3530:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3531:         } else {
 3532:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3533:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3534:                            &mt('Stop balancing').'</label>'.
 3535:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3536:             $targets_div_style = 'display: block';
 3537:             $disabled_div_style = 'display: none';
 3538:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3539:                 $homedom_div_style = 'display: block';
 3540:             }
 3541:         }
 3542:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3543:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3544:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3545:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3546:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3547:         my @sparestypes = ('primary','default');
 3548:         my %typetitles = &sparestype_titles();
 3549:         foreach my $sparetype (@sparestypes) {
 3550:             my $targettable;
 3551:             for (my $i=0; $i<$numspares; $i++) {
 3552:                 my $checked;
 3553:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3554:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3555:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3556:                             $checked = ' checked="checked"';
 3557:                         }
 3558:                     }
 3559:                 }
 3560:                 my ($chkboxval,$disabled);
 3561:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3562:                     $chkboxval = $spares[$i];
 3563:                 }
 3564:                 if (exists($currbalancer{$spares[$i]})) {
 3565:                     $disabled = ' disabled="disabled"';
 3566:                 }
 3567:                 $targettable .=
 3568:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3569:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3570:                     '</span></label></td>';
 3571:                 my $rem = $i%($numinrow);
 3572:                 if ($rem == 0) {
 3573:                     if (($i > 0) && ($i < $numspares-1)) {
 3574:                         $targettable .= '</tr>';
 3575:                     }
 3576:                     if ($i < $numspares-1) {
 3577:                         $targettable .= '<tr>';
 3578:                     }
 3579:                 }
 3580:             }
 3581:             if ($targettable ne '') {
 3582:                 my $rem = $numspares%($numinrow);
 3583:                 my $colsleft = $numinrow - $rem;
 3584:                 if ($colsleft > 1 ) {
 3585:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3586:                                     '&nbsp;</td>';
 3587:                 } elsif ($colsleft == 1) {
 3588:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3589:                 }
 3590:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3591:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3592:             }
 3593:         }
 3594:         $datatable .= '</div></td></tr>'.
 3595:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3596:                                            $othertitle,$usertypes,$types,\%servers,
 3597:                                            \%currbalancer,$lonhost,
 3598:                                            $targets_div_style,$homedom_div_style,
 3599:                                            $css_class[$cssidx],$balnum,$islast);
 3600:         $$rowtotal += $rownum;
 3601:         $balnum ++;
 3602:     }
 3603:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3604:     return $datatable;
 3605: }
 3606: 
 3607: sub get_loadbalancers_config {
 3608:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3609:     return unless ((ref($servers) eq 'HASH') &&
 3610:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3611:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3612:     if (keys(%{$existing}) > 0) {
 3613:         my $oldlonhost;
 3614:         foreach my $key (sort(keys(%{$existing}))) {
 3615:             if ($key eq 'lonhost') {
 3616:                 $oldlonhost = $existing->{'lonhost'};
 3617:                 $currbalancer->{$oldlonhost} = 1;
 3618:             } elsif ($key eq 'targets') {
 3619:                 if ($oldlonhost) {
 3620:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3621:                 }
 3622:             } elsif ($key eq 'rules') {
 3623:                 if ($oldlonhost) {
 3624:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3625:                 }
 3626:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3627:                 $currbalancer->{$key} = 1;
 3628:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3629:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3630:             }
 3631:         }
 3632:     } else {
 3633:         my ($balancerref,$targetsref) =
 3634:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3635:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3636:             foreach my $server (sort(keys(%{$balancerref}))) {
 3637:                 $currbalancer->{$server} = 1;
 3638:                 $currtargets->{$server} = $targetsref->{$server};
 3639:             }
 3640:         }
 3641:     }
 3642:     return;
 3643: }
 3644: 
 3645: sub loadbalancing_rules {
 3646:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3647:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3648:         $css_class,$balnum,$islast) = @_;
 3649:     my $output;
 3650:     my $num = 0;
 3651:     my ($alltypes,$othertypes,$titles) =
 3652:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3653:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3654:         foreach my $type (@{$alltypes}) {
 3655:             $num ++;
 3656:             my $current;
 3657:             if (ref($currrules) eq 'HASH') {
 3658:                 $current = $currrules->{$type};
 3659:             }
 3660:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3661:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3662:                     $current = '';
 3663:                 }
 3664:             }
 3665:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3666:                                              $servers,$currbalancer,$lonhost,$dom,
 3667:                                              $targets_div_style,$homedom_div_style,
 3668:                                              $css_class,$balnum,$num,$islast);
 3669:         }
 3670:     }
 3671:     return $output;
 3672: }
 3673: 
 3674: sub loadbalancing_titles {
 3675:     my ($dom,$intdom,$usertypes,$types) = @_;
 3676:     my %othertypes = (
 3677:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3678:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3679:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3680:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3681:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3682:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3683:                      );
 3684:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3685:     if (ref($types) eq 'ARRAY') {
 3686:         unshift(@alltypes,@{$types},'default');
 3687:     }
 3688:     my %titles;
 3689:     foreach my $type (@alltypes) {
 3690:         if ($type =~ /^_LC_/) {
 3691:             $titles{$type} = $othertypes{$type};
 3692:         } elsif ($type eq 'default') {
 3693:             $titles{$type} = &mt('All users from [_1]',$dom);
 3694:             if (ref($types) eq 'ARRAY') {
 3695:                 if (@{$types} > 0) {
 3696:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3697:                 }
 3698:             }
 3699:         } elsif (ref($usertypes) eq 'HASH') {
 3700:             $titles{$type} = $usertypes->{$type};
 3701:         }
 3702:     }
 3703:     return (\@alltypes,\%othertypes,\%titles);
 3704: }
 3705: 
 3706: sub loadbalance_rule_row {
 3707:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3708:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3709:     my @rulenames;
 3710:     my %ruletitles = &offloadtype_text();
 3711:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3712:         @rulenames = ('balancer','offloadedto');
 3713:     } else {
 3714:         @rulenames = ('default','homeserver');
 3715:         if ($type eq '_LC_external') {
 3716:             push(@rulenames,'externalbalancer');
 3717:         } else {
 3718:             push(@rulenames,'specific');
 3719:         }
 3720:         push(@rulenames,'none');
 3721:     }
 3722:     my $style = $targets_div_style;
 3723:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3724:         $style = $homedom_div_style;
 3725:     }
 3726:     my $space;
 3727:     if ($islast && $num == 1) {
 3728:         $space = '<div display="inline-block">&nbsp;</div>';
 3729:     }
 3730:     my $output =
 3731:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3732:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3733:         '<td valaign="top">'.$space.
 3734:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3735:     for (my $i=0; $i<@rulenames; $i++) {
 3736:         my $rule = $rulenames[$i];
 3737:         my ($checked,$extra);
 3738:         if ($rulenames[$i] eq 'default') {
 3739:             $rule = '';
 3740:         }
 3741:         if ($rulenames[$i] eq 'specific') {
 3742:             if (ref($servers) eq 'HASH') {
 3743:                 my $default;
 3744:                 if (($current ne '') && (exists($servers->{$current}))) {
 3745:                     $checked = ' checked="checked"';
 3746:                 }
 3747:                 unless ($checked) {
 3748:                     $default = ' selected="selected"';
 3749:                 }
 3750:                 $extra =
 3751:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3752:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3753:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3754:                     '<option value=""'.$default.'></option>'."\n";
 3755:                 foreach my $server (sort(keys(%{$servers}))) {
 3756:                     if (ref($currbalancer) eq 'HASH') {
 3757:                         next if (exists($currbalancer->{$server}));
 3758:                     }
 3759:                     my $selected;
 3760:                     if ($server eq $current) {
 3761:                         $selected = ' selected="selected"';
 3762:                     }
 3763:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3764:                 }
 3765:                 $extra .= '</select>';
 3766:             }
 3767:         } elsif ($rule eq $current) {
 3768:             $checked = ' checked="checked"';
 3769:         }
 3770:         $output .= '<span class="LC_nobreak"><label>'.
 3771:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3772:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3773:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3774:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3775:                    '</label>'.$extra.'</span><br />'."\n";
 3776:     }
 3777:     $output .= '</div></td></tr>'."\n";
 3778:     return $output;
 3779: }
 3780: 
 3781: sub offloadtype_text {
 3782:     my %ruletitles = &Apache::lonlocal::texthash (
 3783:            'default'          => 'Offloads to default destinations',
 3784:            'homeserver'       => "Offloads to user's home server",
 3785:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3786:            'specific'         => 'Offloads to specific server',
 3787:            'none'             => 'No offload',
 3788:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3789:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3790:     );
 3791:     return %ruletitles;
 3792: }
 3793: 
 3794: sub sparestype_titles {
 3795:     my %typestitles = &Apache::lonlocal::texthash (
 3796:                           'primary' => 'primary',
 3797:                           'default' => 'default',
 3798:                       );
 3799:     return %typestitles;
 3800: }
 3801: 
 3802: sub contact_titles {
 3803:     my %titles = &Apache::lonlocal::texthash (
 3804:                    'supportemail' => 'Support E-mail address',
 3805:                    'adminemail'   => 'Default Server Admin E-mail address',
 3806:                    'errormail'    => 'Error reports to be e-mailed to',
 3807:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3808:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3809:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3810:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3811:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3812:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3813:                  );
 3814:     my %short_titles = &Apache::lonlocal::texthash (
 3815:                            adminemail   => 'Admin E-mail address',
 3816:                            supportemail => 'Support E-mail',
 3817:                        );   
 3818:     return (\%titles,\%short_titles);
 3819: }
 3820: 
 3821: sub tool_titles {
 3822:     my %titles = &Apache::lonlocal::texthash (
 3823:                      aboutme    => 'Personal web page',
 3824:                      blog       => 'Blog',
 3825:                      webdav     => 'WebDAV',
 3826:                      portfolio  => 'Portfolio',
 3827:                      official   => 'Official courses (with institutional codes)',
 3828:                      unofficial => 'Unofficial courses',
 3829:                      community  => 'Communities',
 3830:                      textbook   => 'Textbook courses',
 3831:                  );
 3832:     return %titles;
 3833: }
 3834: 
 3835: sub courserequest_titles {
 3836:     my %titles = &Apache::lonlocal::texthash (
 3837:                                    official   => 'Official',
 3838:                                    unofficial => 'Unofficial',
 3839:                                    community  => 'Communities',
 3840:                                    textbook   => 'Textbook',
 3841:                                    norequest  => 'Not allowed',
 3842:                                    approval   => 'Approval by Dom. Coord.',
 3843:                                    validate   => 'With validation',
 3844:                                    autolimit  => 'Numerical limit',
 3845:                                    unlimited  => '(blank for unlimited)',
 3846:                  );
 3847:     return %titles;
 3848: }
 3849: 
 3850: sub authorrequest_titles {
 3851:     my %titles = &Apache::lonlocal::texthash (
 3852:                                    norequest  => 'Not allowed',
 3853:                                    approval   => 'Approval by Dom. Coord.',
 3854:                                    automatic  => 'Automatic approval',
 3855:                  );
 3856:     return %titles;
 3857: }
 3858: 
 3859: sub courserequest_conditions {
 3860:     my %conditions = &Apache::lonlocal::texthash (
 3861:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3862:        validate   => '(Processing of request subject to institutional validation).',
 3863:                  );
 3864:     return %conditions;
 3865: }
 3866: 
 3867: 
 3868: sub print_usercreation {
 3869:     my ($position,$dom,$settings,$rowtotal) = @_;
 3870:     my $numinrow = 4;
 3871:     my $datatable;
 3872:     if ($position eq 'top') {
 3873:         $$rowtotal ++;
 3874:         my $rowcount = 0;
 3875:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3876:         if (ref($rules) eq 'HASH') {
 3877:             if (keys(%{$rules}) > 0) {
 3878:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3879:                                                 $ruleorder,$numinrow,$rowcount);
 3880:                 $$rowtotal ++;
 3881:                 $rowcount ++;
 3882:             }
 3883:         }
 3884:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3885:         if (ref($idrules) eq 'HASH') {
 3886:             if (keys(%{$idrules}) > 0) {
 3887:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3888:                                                 $idruleorder,$numinrow,$rowcount);
 3889:                 $$rowtotal ++;
 3890:                 $rowcount ++;
 3891:             }
 3892:         }
 3893:         if ($rowcount == 0) {
 3894:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3895:             $$rowtotal ++;
 3896:             $rowcount ++;
 3897:         }
 3898:     } elsif ($position eq 'middle') {
 3899:         my @creators = ('author','course','requestcrs');
 3900:         my ($rules,$ruleorder) =
 3901:             &Apache::lonnet::inst_userrules($dom,'username');
 3902:         my %lt = &usercreation_types();
 3903:         my %checked;
 3904:         if (ref($settings) eq 'HASH') {
 3905:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3906:                 foreach my $item (@creators) {
 3907:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3908:                 }
 3909:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3910:                 foreach my $item (@creators) {
 3911:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3912:                         $checked{$item} = 'none';
 3913:                     }
 3914:                 }
 3915:             }
 3916:         }
 3917:         my $rownum = 0;
 3918:         foreach my $item (@creators) {
 3919:             $rownum ++;
 3920:             if ($checked{$item} eq '') {
 3921:                 $checked{$item} = 'any';
 3922:             }
 3923:             my $css_class;
 3924:             if ($rownum%2) {
 3925:                 $css_class = '';
 3926:             } else {
 3927:                 $css_class = ' class="LC_odd_row" ';
 3928:             }
 3929:             $datatable .= '<tr'.$css_class.'>'.
 3930:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3931:                          '</span></td><td align="right">';
 3932:             my @options = ('any');
 3933:             if (ref($rules) eq 'HASH') {
 3934:                 if (keys(%{$rules}) > 0) {
 3935:                     push(@options,('official','unofficial'));
 3936:                 }
 3937:             }
 3938:             push(@options,'none');
 3939:             foreach my $option (@options) {
 3940:                 my $type = 'radio';
 3941:                 my $check = ' ';
 3942:                 if ($checked{$item} eq $option) {
 3943:                     $check = ' checked="checked" ';
 3944:                 } 
 3945:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3946:                               '<input type="'.$type.'" name="can_createuser_'.
 3947:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3948:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3949:             }
 3950:             $datatable .= '</td></tr>';
 3951:         }
 3952:     } else {
 3953:         my @contexts = ('author','course','domain');
 3954:         my @authtypes = ('int','krb4','krb5','loc');
 3955:         my %checked;
 3956:         if (ref($settings) eq 'HASH') {
 3957:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3958:                 foreach my $item (@contexts) {
 3959:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3960:                         foreach my $auth (@authtypes) {
 3961:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3962:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3963:                             }
 3964:                         }
 3965:                     }
 3966:                 }
 3967:             }
 3968:         } else {
 3969:             foreach my $item (@contexts) {
 3970:                 foreach my $auth (@authtypes) {
 3971:                     $checked{$item}{$auth} = ' checked="checked" ';
 3972:                 }
 3973:             }
 3974:         }
 3975:         my %title = &context_names();
 3976:         my %authname = &authtype_names();
 3977:         my $rownum = 0;
 3978:         my $css_class; 
 3979:         foreach my $item (@contexts) {
 3980:             if ($rownum%2) {
 3981:                 $css_class = '';
 3982:             } else {
 3983:                 $css_class = ' class="LC_odd_row" ';
 3984:             }
 3985:             $datatable .=   '<tr'.$css_class.'>'.
 3986:                             '<td>'.$title{$item}.
 3987:                             '</td><td class="LC_left_item">'.
 3988:                             '<span class="LC_nobreak">';
 3989:             foreach my $auth (@authtypes) {
 3990:                 $datatable .= '<label>'. 
 3991:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3992:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3993:                               $authname{$auth}.'</label>&nbsp;';
 3994:             }
 3995:             $datatable .= '</span></td></tr>';
 3996:             $rownum ++;
 3997:         }
 3998:         $$rowtotal += $rownum;
 3999:     }
 4000:     return $datatable;
 4001: }
 4002: 
 4003: sub print_selfcreation {
 4004:     my ($position,$dom,$settings,$rowtotal) = @_;
 4005:     my (@selfcreate,$createsettings,$datatable);
 4006:     if (ref($settings) eq 'HASH') {
 4007:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4008:             $createsettings = $settings->{'cancreate'};
 4009:             if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 4010:                 @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 4011:             } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 4012:                 if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4013:                     @selfcreate = ('email','login','sso');
 4014:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 4015:                     @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 4016:                 }
 4017:             }
 4018:         }
 4019:     }
 4020:     my %radiohash;
 4021:     my $numinrow = 4;
 4022:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4023:     if ($position eq 'top') {
 4024:         my %choices = &Apache::lonlocal::texthash (
 4025:                                                       cancreate_login      => 'Institutional Login',
 4026:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4027:                                                   );
 4028:         my @toggles = sort(keys(%choices));
 4029:         my %defaultchecked = (
 4030:                                'cancreate_login' => 'off',
 4031:                                'cancreate_sso'   => 'off',
 4032:                              );
 4033:         my ($onclick,$itemcount);
 4034:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4035:                                                      \%choices,$itemcount,$onclick);
 4036:         $$rowtotal += $itemcount;
 4037:         
 4038:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4039: 
 4040:         if (ref($usertypes) eq 'HASH') {
 4041:             if (keys(%{$usertypes}) > 0) {
 4042:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4043:                                              $dom,$numinrow,$othertitle,
 4044:                                              'statustocreate',$$rowtotal);
 4045:                 $$rowtotal ++;
 4046:             }
 4047:         }
 4048:     } elsif ($position eq 'middle') {
 4049:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4050:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4051:         $usertypes->{'default'} = $othertitle;
 4052:         if (ref($types) eq 'ARRAY') {
 4053:             push(@{$types},'default');
 4054:             $usertypes->{'default'} = $othertitle;
 4055:             foreach my $status (@{$types}) {
 4056:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4057:                                                        $numinrow,$$rowtotal,$usertypes);
 4058:                 $$rowtotal ++;
 4059:             }
 4060:         }
 4061:     } else {
 4062:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4063:         my %choices =
 4064:             &Apache::lonlocal::texthash(
 4065:                                           email         => 'Approved automatically',
 4066:                                           emailapproval => 'Queued for approval by DC',
 4067:                                           off           => 'Not enabled',
 4068:                                        );
 4069:         $datatable .= '<tr'.$css_class.'>'.
 4070:                       '<td>'.&mt('E-mail address as username').
 4071:                       '</td><td class="LC_left_item">'.
 4072:                       '<span class="LC_nobreak">';
 4073:         foreach my $option ('email','emailapproval','off') {
 4074:             my $checked;
 4075:             if ($option eq 'email') {
 4076:                 if ($radiohash{'cancreate_email'}) {
 4077:                     $checked = 'checked="checked"';
 4078:                 }
 4079:             } elsif ($option eq 'emailapproval') {
 4080:                 if ($radiohash{'cancreate_emailapproval'}) {
 4081:                     $checked = 'checked="checked"';
 4082:                 }
 4083:             } else {
 4084:                 if ((!$radiohash{'cancreate_email'}) && (!$radiohash{'cancreate_emailapproval'})) {
 4085:                     $checked = 'checked="checked"';  
 4086:                 }
 4087:             }
 4088:             $datatable .= '<label>'.
 4089:                           '<input type="radio" name="cancreate_email" '.
 4090:                           $checked.' value="'.$option.'" />'.
 4091:                           $choices{$option}.'</label>&nbsp;';
 4092:         }
 4093:         $$rowtotal ++;
 4094:         $datatable .= '</span></td></tr>'.
 4095:                       &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4096:         $$rowtotal ++;
 4097:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4098:         $numinrow = 1;
 4099:         my ($othertitle,$usertypes,$types) =  &Apache::loncommon::sorted_inst_types($dom);
 4100:         $usertypes->{'default'} = $othertitle;
 4101:         if (ref($types) eq 'ARRAY') {
 4102:             push(@{$types},'default');
 4103:             $usertypes->{'default'} = $othertitle;
 4104:             foreach my $status (@{$types}) {
 4105:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4106:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4107:                 $$rowtotal ++;
 4108:             }
 4109:         }
 4110:         my ($emailrules,$emailruleorder) =
 4111:             &Apache::lonnet::inst_userrules($dom,'email');
 4112:         if (ref($emailrules) eq 'HASH') {
 4113:             if (keys(%{$emailrules}) > 0) {
 4114:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4115:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4116:                 $$rowtotal ++;
 4117:             }
 4118:         }
 4119:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4120:     }
 4121:     return $datatable;
 4122: }
 4123: 
 4124: sub captcha_choice {
 4125:     my ($context,$settings,$itemcount) = @_;
 4126:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4127:     my %lt = &captcha_phrases();
 4128:     $keyentry = 'hidden';
 4129:     if ($context eq 'cancreate') {
 4130:         $rowname = &mt('CAPTCHA validation');
 4131:     } elsif ($context eq 'login') {
 4132:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4133:     }
 4134:     if (ref($settings) eq 'HASH') {
 4135:         if ($settings->{'captcha'}) {
 4136:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4137:         } else {
 4138:             $checked{'original'} = ' checked="checked"';
 4139:         }
 4140:         if ($settings->{'captcha'} eq 'recaptcha') {
 4141:             $pubtext = $lt{'pub'};
 4142:             $privtext = $lt{'priv'};
 4143:             $keyentry = 'text';
 4144:         }
 4145:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4146:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4147:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4148:         }
 4149:     } else {
 4150:         $checked{'original'} = ' checked="checked"';
 4151:     }
 4152:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4153:     my $output = '<tr'.$css_class.'>'.
 4154:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4155:                  '<table><tr><td>'."\n";
 4156:     foreach my $option ('original','recaptcha','notused') {
 4157:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4158:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4159:                    $lt{$option}.'</label></span>';
 4160:         unless ($option eq 'notused') {
 4161:             $output .= ('&nbsp;'x2)."\n";
 4162:         }
 4163:     }
 4164: #
 4165: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4166: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4167: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4168: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4169: #
 4170:     $output .= '</td></tr>'."\n".
 4171:                '<tr><td>'."\n".
 4172:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4173:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4174:                $currpub.'" size="40" /></span><br />'."\n".
 4175:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4176:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4177:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4178:                '</td></tr>';
 4179:     return $output;
 4180: }
 4181: 
 4182: sub user_formats_row {
 4183:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4184:     my $output;
 4185:     my %text = (
 4186:                    'username' => 'new usernames',
 4187:                    'id'       => 'IDs',
 4188:                    'email'    => 'self-created accounts (e-mail)',
 4189:                );
 4190:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4191:     $output = '<tr '.$css_class.'>'.
 4192:               '<td><span class="LC_nobreak">';
 4193:     if ($type eq 'email') {
 4194:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4195:     } else {
 4196:         $output .= &mt("Format rules to check for $text{$type}: ");
 4197:     }
 4198:     $output .= '</span></td>'.
 4199:                '<td class="LC_left_item" colspan="2"><table>';
 4200:     my $rem;
 4201:     if (ref($ruleorder) eq 'ARRAY') {
 4202:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4203:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4204:                 my $rem = $i%($numinrow);
 4205:                 if ($rem == 0) {
 4206:                     if ($i > 0) {
 4207:                         $output .= '</tr>';
 4208:                     }
 4209:                     $output .= '<tr>';
 4210:                 }
 4211:                 my $check = ' ';
 4212:                 if (ref($settings) eq 'HASH') {
 4213:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4214:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4215:                             $check = ' checked="checked" ';
 4216:                         }
 4217:                     }
 4218:                 }
 4219:                 $output .= '<td class="LC_left_item">'.
 4220:                            '<span class="LC_nobreak"><label>'.
 4221:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4222:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4223:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4224:             }
 4225:         }
 4226:         $rem = @{$ruleorder}%($numinrow);
 4227:     }
 4228:     my $colsleft = $numinrow - $rem;
 4229:     if ($colsleft > 1 ) {
 4230:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4231:                    '&nbsp;</td>';
 4232:     } elsif ($colsleft == 1) {
 4233:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4234:     }
 4235:     $output .= '</tr></table></td></tr>';
 4236:     return $output;
 4237: }
 4238: 
 4239: sub usercreation_types {
 4240:     my %lt = &Apache::lonlocal::texthash (
 4241:                     author     => 'When adding a co-author',
 4242:                     course     => 'When adding a user to a course',
 4243:                     requestcrs => 'When requesting a course',
 4244:                     any        => 'Any',
 4245:                     official   => 'Institutional only ',
 4246:                     unofficial => 'Non-institutional only',
 4247:                     none       => 'None',
 4248:     );
 4249:     return %lt;
 4250: }
 4251: 
 4252: sub selfcreation_types {
 4253:     my %lt = &Apache::lonlocal::texthash (
 4254:                     selfcreate => 'User creates own account',
 4255:                     any        => 'Any',
 4256:                     official   => 'Institutional only ',
 4257:                     unofficial => 'Non-institutional only',
 4258:                     email      => 'E-mail address',
 4259:                     login      => 'Institutional Login',
 4260:                     sso        => 'SSO',
 4261:              );
 4262: }
 4263: 
 4264: sub authtype_names {
 4265:     my %lt = &Apache::lonlocal::texthash(
 4266:                       int    => 'Internal',
 4267:                       krb4   => 'Kerberos 4',
 4268:                       krb5   => 'Kerberos 5',
 4269:                       loc    => 'Local',
 4270:                   );
 4271:     return %lt;
 4272: }
 4273: 
 4274: sub context_names {
 4275:     my %context_title = &Apache::lonlocal::texthash(
 4276:        author => 'Creating users when an Author',
 4277:        course => 'Creating users when in a course',
 4278:        domain => 'Creating users when a Domain Coordinator',
 4279:     );
 4280:     return %context_title;
 4281: }
 4282: 
 4283: sub print_usermodification {
 4284:     my ($position,$dom,$settings,$rowtotal) = @_;
 4285:     my $numinrow = 4;
 4286:     my ($context,$datatable,$rowcount);
 4287:     if ($position eq 'top') {
 4288:         $rowcount = 0;
 4289:         $context = 'author'; 
 4290:         foreach my $role ('ca','aa') {
 4291:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4292:                                                    $numinrow,$rowcount);
 4293:             $$rowtotal ++;
 4294:             $rowcount ++;
 4295:         }
 4296:     } elsif ($position eq 'bottom') {
 4297:         $context = 'course';
 4298:         $rowcount = 0;
 4299:         foreach my $role ('st','ep','ta','in','cr') {
 4300:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4301:                                                    $numinrow,$rowcount);
 4302:             $$rowtotal ++;
 4303:             $rowcount ++;
 4304:         }
 4305:     }
 4306:     return $datatable;
 4307: }
 4308: 
 4309: sub print_defaults {
 4310:     my ($dom,$settings,$rowtotal) = @_;
 4311:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4312:                  'datelocale_def','portal_def');
 4313:     my %defaults;
 4314:     if (ref($settings) eq 'HASH') {
 4315:         %defaults = %{$settings};
 4316:     } else {
 4317:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4318:         foreach my $item (@items) {
 4319:             $defaults{$item} = $domdefaults{$item};
 4320:         }
 4321:     }
 4322:     my $titles = &defaults_titles($dom);
 4323:     my $rownum = 0;
 4324:     my ($datatable,$css_class);
 4325:     foreach my $item (@items) {
 4326:         if ($rownum%2) {
 4327:             $css_class = '';
 4328:         } else {
 4329:             $css_class = ' class="LC_odd_row" ';
 4330:         }
 4331:         $datatable .= '<tr'.$css_class.'>'.
 4332:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 4333:                   '</span></td><td class="LC_right_item">';
 4334:         if ($item eq 'auth_def') {
 4335:             my @authtypes = ('internal','krb4','krb5','localauth');
 4336:             my %shortauth = (
 4337:                              internal => 'int',
 4338:                              krb4 => 'krb4',
 4339:                              krb5 => 'krb5',
 4340:                              localauth  => 'loc'
 4341:                            );
 4342:             my %authnames = &authtype_names();
 4343:             foreach my $auth (@authtypes) {
 4344:                 my $checked = ' ';
 4345:                 if ($defaults{$item} eq $auth) {
 4346:                     $checked = ' checked="checked" ';
 4347:                 }
 4348:                 $datatable .= '<label><input type="radio" name="'.$item.
 4349:                               '" value="'.$auth.'"'.$checked.'/>'.
 4350:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4351:             }
 4352:         } elsif ($item eq 'timezone_def') {
 4353:             my $includeempty = 1;
 4354:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4355:         } elsif ($item eq 'datelocale_def') {
 4356:             my $includeempty = 1;
 4357:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4358:         } elsif ($item eq 'lang_def') {
 4359:             my %langchoices = &get_languages_hash();
 4360:             $langchoices{''} = 'No language preference';
 4361:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4362:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4363:                                                           \%langchoices);
 4364:         } else {
 4365:             my $size;
 4366:             if ($item eq 'portal_def') {
 4367:                 $size = ' size="25"';
 4368:             }
 4369:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 4370:                           $defaults{$item}.'"'.$size.' />';
 4371:         }
 4372:         $datatable .= '</td></tr>';
 4373:         $rownum ++;
 4374:     }
 4375:     $$rowtotal += $rownum;
 4376:     return $datatable;
 4377: }
 4378: 
 4379: sub get_languages_hash {
 4380:     my %langchoices;
 4381:     foreach my $id (&Apache::loncommon::languageids()) {
 4382:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4383:         if ($code ne '') {
 4384:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4385:         }
 4386:     }
 4387:     return %langchoices;
 4388: }
 4389: 
 4390: sub defaults_titles {
 4391:     my ($dom) = @_;
 4392:     my %titles = &Apache::lonlocal::texthash (
 4393:                    'auth_def'      => 'Default authentication type',
 4394:                    'auth_arg_def'  => 'Default authentication argument',
 4395:                    'lang_def'      => 'Default language',
 4396:                    'timezone_def'  => 'Default timezone',
 4397:                    'datelocale_def' => 'Default locale for dates',
 4398:                    'portal_def'     => 'Portal/Default URL',
 4399:                  );
 4400:     if ($dom) {
 4401:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4402:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4403:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4404:         $protocol = 'http' if ($protocol ne 'https');
 4405:         if ($uint_dom) {
 4406:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4407:                                          $uint_dom);
 4408:         }
 4409:     }
 4410:     return (\%titles);
 4411: }
 4412: 
 4413: sub print_scantronformat {
 4414:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4415:     my $itemcount = 1;
 4416:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4417:         %confhash);
 4418:     my $switchserver = &check_switchserver($dom,$confname);
 4419:     my %lt = &Apache::lonlocal::texthash (
 4420:                 default => 'Default bubblesheet format file error',
 4421:                 custom  => 'Custom bubblesheet format file error',
 4422:              );
 4423:     my %scantronfiles = (
 4424:         default => 'default.tab',
 4425:         custom => 'custom.tab',
 4426:     );
 4427:     foreach my $key (keys(%scantronfiles)) {
 4428:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4429:                               .$scantronfiles{$key};
 4430:     }
 4431:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4432:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4433:         if (!$switchserver) {
 4434:             my $servadm = $r->dir_config('lonAdmEMail');
 4435:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4436:             if ($configuserok eq 'ok') {
 4437:                 if ($author_ok eq 'ok') {
 4438:                     my %legacyfile = (
 4439:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4440:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4441:                     );
 4442:                     my %md5chk;
 4443:                     foreach my $type (keys(%legacyfile)) {
 4444:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4445:                         chomp($md5chk{$type});
 4446:                     }
 4447:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4448:                         foreach my $type (keys(%legacyfile)) {
 4449:                             ($scantronurls{$type},my $error) = 
 4450:                                 &legacy_scantronformat($r,$dom,$confname,
 4451:                                                  $type,$legacyfile{$type},
 4452:                                                  $scantronurls{$type},
 4453:                                                  $scantronfiles{$type});
 4454:                             if ($error ne '') {
 4455:                                 $error{$type} = $error;
 4456:                             }
 4457:                         }
 4458:                         if (keys(%error) == 0) {
 4459:                             $is_custom = 1;
 4460:                             $confhash{'scantron'}{'scantronformat'} = 
 4461:                                 $scantronurls{'custom'};
 4462:                             my $putresult = 
 4463:                                 &Apache::lonnet::put_dom('configuration',
 4464:                                                          \%confhash,$dom);
 4465:                             if ($putresult ne 'ok') {
 4466:                                 $error{'custom'} = 
 4467:                                     '<span class="LC_error">'.
 4468:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4469:                             }
 4470:                         }
 4471:                     } else {
 4472:                         ($scantronurls{'default'},my $error) =
 4473:                             &legacy_scantronformat($r,$dom,$confname,
 4474:                                           'default',$legacyfile{'default'},
 4475:                                           $scantronurls{'default'},
 4476:                                           $scantronfiles{'default'});
 4477:                         if ($error eq '') {
 4478:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4479:                             my $putresult =
 4480:                                 &Apache::lonnet::put_dom('configuration',
 4481:                                                          \%confhash,$dom);
 4482:                             if ($putresult ne 'ok') {
 4483:                                 $error{'default'} =
 4484:                                     '<span class="LC_error">'.
 4485:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4486:                             }
 4487:                         } else {
 4488:                             $error{'default'} = $error;
 4489:                         }
 4490:                     }
 4491:                 }
 4492:             }
 4493:         } else {
 4494:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4495:         }
 4496:     }
 4497:     if (ref($settings) eq 'HASH') {
 4498:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4499:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4500:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4501:                 $scantronurl = '';
 4502:             } else {
 4503:                 $scantronurl = $settings->{'scantronformat'};
 4504:             }
 4505:             $is_custom = 1;
 4506:         } else {
 4507:             $scantronurl = $scantronurls{'default'};
 4508:         }
 4509:     } else {
 4510:         if ($is_custom) {
 4511:             $scantronurl = $scantronurls{'custom'};
 4512:         } else {
 4513:             $scantronurl = $scantronurls{'default'};
 4514:         }
 4515:     }
 4516:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4517:     $datatable .= '<tr'.$css_class.'>';
 4518:     if (!$is_custom) {
 4519:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4520:                       '<span class="LC_nobreak">';
 4521:         if ($scantronurl) {
 4522:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4523:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4524:         } else {
 4525:             $datatable = &mt('File unavailable for display');
 4526:         }
 4527:         $datatable .= '</span></td>';
 4528:         if (keys(%error) == 0) { 
 4529:             $datatable .= '<td valign="bottom">';
 4530:             if (!$switchserver) {
 4531:                 $datatable .= &mt('Upload:').'<br />';
 4532:             }
 4533:         } else {
 4534:             my $errorstr;
 4535:             foreach my $key (sort(keys(%error))) {
 4536:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4537:             }
 4538:             $datatable .= '<td>'.$errorstr;
 4539:         }
 4540:     } else {
 4541:         if (keys(%error) > 0) {
 4542:             my $errorstr;
 4543:             foreach my $key (sort(keys(%error))) {
 4544:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4545:             } 
 4546:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4547:         } elsif ($scantronurl) {
 4548:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4549:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4550:             $datatable .= '<td><span class="LC_nobreak">'.
 4551:                           $link.
 4552:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4553:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4554:                           '<td><span class="LC_nobreak">&nbsp;'.
 4555:                           &mt('Replace:').'</span><br />';
 4556:         }
 4557:     }
 4558:     if (keys(%error) == 0) {
 4559:         if ($switchserver) {
 4560:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4561:         } else {
 4562:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4563:                          '<input type="file" name="scantronformat" /></span>';
 4564:         }
 4565:     }
 4566:     $datatable .= '</td></tr>';
 4567:     $$rowtotal ++;
 4568:     return $datatable;
 4569: }
 4570: 
 4571: sub legacy_scantronformat {
 4572:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4573:     my ($url,$error);
 4574:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4575:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4576:         (my $result,$url) =
 4577:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4578:                          '','',$newfile);
 4579:         if ($result ne 'ok') {
 4580:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4581:         }
 4582:     }
 4583:     return ($url,$error);
 4584: }
 4585: 
 4586: sub print_coursecategories {
 4587:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4588:     my $datatable;
 4589:     if ($position eq 'top') {
 4590:         my $toggle_cats_crs = ' ';
 4591:         my $toggle_cats_dom = ' checked="checked" ';
 4592:         my $can_cat_crs = ' ';
 4593:         my $can_cat_dom = ' checked="checked" ';
 4594:         my $toggle_catscomm_comm = ' ';
 4595:         my $toggle_catscomm_dom = ' checked="checked" ';
 4596:         my $can_catcomm_comm = ' ';
 4597:         my $can_catcomm_dom = ' checked="checked" ';
 4598: 
 4599:         if (ref($settings) eq 'HASH') {
 4600:             if ($settings->{'togglecats'} eq 'crs') {
 4601:                 $toggle_cats_crs = $toggle_cats_dom;
 4602:                 $toggle_cats_dom = ' ';
 4603:             }
 4604:             if ($settings->{'categorize'} eq 'crs') {
 4605:                 $can_cat_crs = $can_cat_dom;
 4606:                 $can_cat_dom = ' ';
 4607:             }
 4608:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4609:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4610:                 $toggle_catscomm_dom = ' ';
 4611:             }
 4612:             if ($settings->{'categorizecomm'} eq 'comm') {
 4613:                 $can_catcomm_comm = $can_catcomm_dom;
 4614:                 $can_catcomm_dom = ' ';
 4615:             }
 4616:         }
 4617:         my %title = &Apache::lonlocal::texthash (
 4618:                      togglecats     => 'Show/Hide a course in catalog',
 4619:                      togglecatscomm => 'Show/Hide a community in catalog',
 4620:                      categorize     => 'Assign a category to a course',
 4621:                      categorizecomm => 'Assign a category to a community',
 4622:                     );
 4623:         my %level = &Apache::lonlocal::texthash (
 4624:                      dom  => 'Set in Domain',
 4625:                      crs  => 'Set in Course',
 4626:                      comm => 'Set in Community',
 4627:                     );
 4628:         $datatable = '<tr class="LC_odd_row">'.
 4629:                   '<td>'.$title{'togglecats'}.'</td>'.
 4630:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4631:                   '<input type="radio" name="togglecats"'.
 4632:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4633:                   '<label><input type="radio" name="togglecats"'.
 4634:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4635:                   '</tr><tr>'.
 4636:                   '<td>'.$title{'categorize'}.'</td>'.
 4637:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4638:                   '<label><input type="radio" name="categorize"'.
 4639:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4640:                   '<label><input type="radio" name="categorize"'.
 4641:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4642:                   '</tr><tr class="LC_odd_row">'.
 4643:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4644:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4645:                   '<input type="radio" name="togglecatscomm"'.
 4646:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4647:                   '<label><input type="radio" name="togglecatscomm"'.
 4648:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4649:                   '</tr><tr>'.
 4650:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4651:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4652:                   '<label><input type="radio" name="categorizecomm"'.
 4653:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4654:                   '<label><input type="radio" name="categorizecomm"'.
 4655:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4656:                   '</tr>';
 4657:         $$rowtotal += 4;
 4658:     } else {
 4659:         my $css_class;
 4660:         my $itemcount = 1;
 4661:         my $cathash; 
 4662:         if (ref($settings) eq 'HASH') {
 4663:             $cathash = $settings->{'cats'};
 4664:         }
 4665:         if (ref($cathash) eq 'HASH') {
 4666:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4667:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4668:                                                    \%allitems,\%idx,\@jsarray);
 4669:             my $maxdepth = scalar(@cats);
 4670:             my $colattrib = '';
 4671:             if ($maxdepth > 2) {
 4672:                 $colattrib = ' colspan="2" ';
 4673:             }
 4674:             my @path;
 4675:             if (@cats > 0) {
 4676:                 if (ref($cats[0]) eq 'ARRAY') {
 4677:                     my $numtop = @{$cats[0]};
 4678:                     my $maxnum = $numtop;
 4679:                     my %default_names = (
 4680:                           instcode    => &mt('Official courses'),
 4681:                           communities => &mt('Communities'),
 4682:                     );
 4683: 
 4684:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4685:                         ($cathash->{'instcode::0'} eq '') ||
 4686:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4687:                         ($cathash->{'communities::0'} eq '')) {
 4688:                         $maxnum ++;
 4689:                     }
 4690:                     my $lastidx;
 4691:                     for (my $i=0; $i<$numtop; $i++) {
 4692:                         my $parent = $cats[0][$i];
 4693:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4694:                         my $item = &escape($parent).'::0';
 4695:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4696:                         $lastidx = $idx{$item};
 4697:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4698:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4699:                         for (my $k=0; $k<=$maxnum; $k++) {
 4700:                             my $vpos = $k+1;
 4701:                             my $selstr;
 4702:                             if ($k == $i) {
 4703:                                 $selstr = ' selected="selected" ';
 4704:                             }
 4705:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4706:                         }
 4707:                         $datatable .= '</select></span></td><td>';
 4708:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4709:                             $datatable .=  '<span class="LC_nobreak">'
 4710:                                            .$default_names{$parent}.'</span>';
 4711:                             if ($parent eq 'instcode') {
 4712:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4713:                                               .&mt('with institutional codes')
 4714:                                               .')</span></td><td'.$colattrib.'>';
 4715:                             } else {
 4716:                                 $datatable .= '<table><tr><td>';
 4717:                             }
 4718:                             $datatable .= '<span class="LC_nobreak">'
 4719:                                           .'<label><input type="radio" name="'
 4720:                                           .$parent.'" value="1" checked="checked" />'
 4721:                                           .&mt('Display').'</label>';
 4722:                             if ($parent eq 'instcode') {
 4723:                                 $datatable .= '&nbsp;';
 4724:                             } else {
 4725:                                 $datatable .= '</span></td></tr><tr><td>'
 4726:                                               .'<span class="LC_nobreak">';
 4727:                             }
 4728:                             $datatable .= '<label><input type="radio" name="'
 4729:                                           .$parent.'" value="0" />'
 4730:                                           .&mt('Do not display').'</label></span>';
 4731:                             if ($parent eq 'communities') {
 4732:                                 $datatable .= '</td></tr></table>';
 4733:                             }
 4734:                             $datatable .= '</td>';
 4735:                         } else {
 4736:                             $datatable .= $parent
 4737:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4738:                                           .'<input type="checkbox" name="deletecategory" '
 4739:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4740:                         }
 4741:                         my $depth = 1;
 4742:                         push(@path,$parent);
 4743:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4744:                         pop(@path);
 4745:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4746:                         $itemcount ++;
 4747:                     }
 4748:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4749:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4750:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4751:                     for (my $k=0; $k<=$maxnum; $k++) {
 4752:                         my $vpos = $k+1;
 4753:                         my $selstr;
 4754:                         if ($k == $numtop) {
 4755:                             $selstr = ' selected="selected" ';
 4756:                         }
 4757:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4758:                     }
 4759:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4760:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4761:                                   .'</tr>'."\n";
 4762:                     $itemcount ++;
 4763:                     foreach my $default ('instcode','communities') {
 4764:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4765:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4766:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4767:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4768:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4769:                             for (my $k=0; $k<=$maxnum; $k++) {
 4770:                                 my $vpos = $k+1;
 4771:                                 my $selstr;
 4772:                                 if ($k == $maxnum) {
 4773:                                     $selstr = ' selected="selected" ';
 4774:                                 }
 4775:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4776:                             }
 4777:                             $datatable .= '</select></span></td>'.
 4778:                                           '<td><span class="LC_nobreak">'.
 4779:                                           $default_names{$default}.'</span>';
 4780:                             if ($default eq 'instcode') {
 4781:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4782:                                               .&mt('with institutional codes').')</span>';
 4783:                             }
 4784:                             $datatable .= '</td>'
 4785:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4786:                                           .&mt('Display').'</label>&nbsp;'
 4787:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4788:                                           .&mt('Do not display').'</label></span></td></tr>';
 4789:                         }
 4790:                     }
 4791:                 }
 4792:             } else {
 4793:                 $datatable .= &initialize_categories($itemcount);
 4794:             }
 4795:         } else {
 4796:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4797:                           .&initialize_categories($itemcount);
 4798:         }
 4799:         $$rowtotal += $itemcount;
 4800:     }
 4801:     return $datatable;
 4802: }
 4803: 
 4804: sub print_serverstatuses {
 4805:     my ($dom,$settings,$rowtotal) = @_;
 4806:     my $datatable;
 4807:     my @pages = &serverstatus_pages();
 4808:     my (%namedaccess,%machineaccess);
 4809:     foreach my $type (@pages) {
 4810:         $namedaccess{$type} = '';
 4811:         $machineaccess{$type}= '';
 4812:     }
 4813:     if (ref($settings) eq 'HASH') {
 4814:         foreach my $type (@pages) {
 4815:             if (exists($settings->{$type})) {
 4816:                 if (ref($settings->{$type}) eq 'HASH') {
 4817:                     foreach my $key (keys(%{$settings->{$type}})) {
 4818:                         if ($key eq 'namedusers') {
 4819:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4820:                         } elsif ($key eq 'machines') {
 4821:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4822:                         }
 4823:                     }
 4824:                 }
 4825:             }
 4826:         }
 4827:     }
 4828:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4829:     my $rownum = 0;
 4830:     my $css_class;
 4831:     foreach my $type (@pages) {
 4832:         $rownum ++;
 4833:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4834:         $datatable .= '<tr'.$css_class.'>'.
 4835:                       '<td><span class="LC_nobreak">'.
 4836:                       $titles->{$type}.'</span></td>'.
 4837:                       '<td class="LC_left_item">'.
 4838:                       '<input type="text" name="'.$type.'_namedusers" '.
 4839:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4840:                       '<td class="LC_right_item">'.
 4841:                       '<span class="LC_nobreak">'.
 4842:                       '<input type="text" name="'.$type.'_machines" '.
 4843:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4844:                       '</td></tr>'."\n";
 4845:     }
 4846:     $$rowtotal += $rownum;
 4847:     return $datatable;
 4848: }
 4849: 
 4850: sub serverstatus_pages {
 4851:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4852:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4853:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 4854:             'uniquecodes','diskusage');
 4855: }
 4856: 
 4857: sub coursecategories_javascript {
 4858:     my ($settings) = @_;
 4859:     my ($output,$jstext,$cathash);
 4860:     if (ref($settings) eq 'HASH') {
 4861:         $cathash = $settings->{'cats'};
 4862:     }
 4863:     if (ref($cathash) eq 'HASH') {
 4864:         my (@cats,@jsarray,%idx);
 4865:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4866:         if (@jsarray > 0) {
 4867:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4868:             for (my $i=0; $i<@jsarray; $i++) {
 4869:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4870:                     my $catstr = join('","',@{$jsarray[$i]});
 4871:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4872:                 }
 4873:             }
 4874:         }
 4875:     } else {
 4876:         $jstext  = '    var categories = Array(1);'."\n".
 4877:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4878:     }
 4879:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4880:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4881:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4882:     $output = <<"ENDSCRIPT";
 4883: <script type="text/javascript">
 4884: // <![CDATA[
 4885: function reorderCats(form,parent,item,idx) {
 4886:     var changedVal;
 4887: $jstext
 4888:     var newpos = 'addcategory_pos';
 4889:     var current = new Array;
 4890:     if (parent == '') {
 4891:         var has_instcode = 0;
 4892:         var maxtop = categories[idx].length;
 4893:         for (var j=0; j<maxtop; j++) {
 4894:             if (categories[idx][j] == 'instcode::0') {
 4895:                 has_instcode == 1;
 4896:             }
 4897:         }
 4898:         if (has_instcode == 0) {
 4899:             categories[idx][maxtop] = 'instcode_pos';
 4900:         }
 4901:     } else {
 4902:         newpos += '_'+parent;
 4903:     }
 4904:     var maxh = 1 + categories[idx].length;
 4905:     var current = new Array;
 4906:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4907:     if (item == newpos) {
 4908:         changedVal = newitemVal;
 4909:     } else {
 4910:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4911:         current[newitemVal] = newpos;
 4912:     }
 4913:     for (var i=0; i<categories[idx].length; i++) {
 4914:         var elementName = categories[idx][i];
 4915:         if (elementName != item) {
 4916:             if (form.elements[elementName]) {
 4917:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4918:                 current[currVal] = elementName;
 4919:             }
 4920:         }
 4921:     }
 4922:     var oldVal;
 4923:     for (var j=0; j<maxh; j++) {
 4924:         if (current[j] == undefined) {
 4925:             oldVal = j;
 4926:         }
 4927:     }
 4928:     if (oldVal < changedVal) {
 4929:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4930:            var elementName = current[k];
 4931:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4932:         }
 4933:     } else {
 4934:         for (var k=changedVal; k<oldVal; k++) {
 4935:             var elementName = current[k];
 4936:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4937:         }
 4938:     }
 4939:     return;
 4940: }
 4941: 
 4942: function categoryCheck(form) {
 4943:     if (form.elements['addcategory_name'].value == 'instcode') {
 4944:         alert('$instcode_reserved\\n$choose_again');
 4945:         return false;
 4946:     }
 4947:     if (form.elements['addcategory_name'].value == 'communities') {
 4948:         alert('$communities_reserved\\n$choose_again');
 4949:         return false;
 4950:     }
 4951:     return true;
 4952: }
 4953: 
 4954: // ]]>
 4955: </script>
 4956: 
 4957: ENDSCRIPT
 4958:     return $output;
 4959: }
 4960: 
 4961: sub initialize_categories {
 4962:     my ($itemcount) = @_;
 4963:     my ($datatable,$css_class,$chgstr);
 4964:     my %default_names = (
 4965:                       instcode    => 'Official courses (with institutional codes)',
 4966:                       communities => 'Communities',
 4967:                         );
 4968:     my $select0 = ' selected="selected"';
 4969:     my $select1 = '';
 4970:     foreach my $default ('instcode','communities') {
 4971:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4972:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4973:         if ($default eq 'communities') {
 4974:             $select1 = $select0;
 4975:             $select0 = '';
 4976:         }
 4977:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4978:                      .'<select name="'.$default.'_pos">'
 4979:                      .'<option value="0"'.$select0.'>1</option>'
 4980:                      .'<option value="1"'.$select1.'>2</option>'
 4981:                      .'<option value="2">3</option></select>&nbsp;'
 4982:                      .$default_names{$default}
 4983:                      .'</span></td><td><span class="LC_nobreak">'
 4984:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4985:                      .&mt('Display').'</label>&nbsp;<label>'
 4986:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4987:                  .'</label></span></td></tr>';
 4988:         $itemcount ++;
 4989:     }
 4990:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4991:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4992:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4993:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4994:                   .'<option value="0">1</option>'
 4995:                   .'<option value="1">2</option>'
 4996:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4997:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4998:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4999:     return $datatable;
 5000: }
 5001: 
 5002: sub build_category_rows {
 5003:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5004:     my ($text,$name,$item,$chgstr);
 5005:     if (ref($cats) eq 'ARRAY') {
 5006:         my $maxdepth = scalar(@{$cats});
 5007:         if (ref($cats->[$depth]) eq 'HASH') {
 5008:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5009:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5010:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5011:                 $text .= '<td><table class="LC_data_table">';
 5012:                 my ($idxnum,$parent_name,$parent_item);
 5013:                 my $higher = $depth - 1;
 5014:                 if ($higher == 0) {
 5015:                     $parent_name = &escape($parent).'::'.$higher;
 5016:                 } else {
 5017:                     if (ref($path) eq 'ARRAY') {
 5018:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5019:                     }
 5020:                 }
 5021:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5022:                 for (my $j=0; $j<=$numchildren; $j++) {
 5023:                     if ($j < $numchildren) {
 5024:                         $name = $cats->[$depth]{$parent}[$j];
 5025:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5026:                         $idxnum = $idx->{$item};
 5027:                     } else {
 5028:                         $name = $parent_name;
 5029:                         $item = $parent_item;
 5030:                     }
 5031:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5032:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5033:                     for (my $i=0; $i<=$numchildren; $i++) {
 5034:                         my $vpos = $i+1;
 5035:                         my $selstr;
 5036:                         if ($j == $i) {
 5037:                             $selstr = ' selected="selected" ';
 5038:                         }
 5039:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5040:                     }
 5041:                     $text .= '</select>&nbsp;';
 5042:                     if ($j < $numchildren) {
 5043:                         my $deeper = $depth+1;
 5044:                         $text .= $name.'&nbsp;'
 5045:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5046:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5047:                         if(ref($path) eq 'ARRAY') {
 5048:                             push(@{$path},$name);
 5049:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5050:                             pop(@{$path});
 5051:                         }
 5052:                     } else {
 5053:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5054:                         if ($j == $numchildren) {
 5055:                             $text .= $name;
 5056:                         } else {
 5057:                             $text .= $item;
 5058:                         }
 5059:                         $text .= '" value="" />';
 5060:                     }
 5061:                     $text .= '</td></tr>';
 5062:                 }
 5063:                 $text .= '</table></td>';
 5064:             } else {
 5065:                 my $higher = $depth-1;
 5066:                 if ($higher == 0) {
 5067:                     $name = &escape($parent).'::'.$higher;
 5068:                 } else {
 5069:                     if (ref($path) eq 'ARRAY') {
 5070:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5071:                     }
 5072:                 }
 5073:                 my $colspan;
 5074:                 if ($parent ne 'instcode') {
 5075:                     $colspan = $maxdepth - $depth - 1;
 5076:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5077:                 }
 5078:             }
 5079:         }
 5080:     }
 5081:     return $text;
 5082: }
 5083: 
 5084: sub modifiable_userdata_row {
 5085:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5086:     my ($role,$rolename,$statustype);
 5087:     $role = $item;
 5088:     if ($context eq 'cancreate') {
 5089:         if ($item =~ /^emailusername_(.+)$/) {
 5090:             $statustype = $1;
 5091:             $role = 'emailusername';
 5092:             if (ref($usertypes) eq 'HASH') {
 5093:                 if ($usertypes->{$statustype}) {
 5094:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5095:                 } else {
 5096:                     $rolename = &mt('Data provided by user');
 5097:                 }
 5098:             }
 5099:         }
 5100:     } elsif ($context eq 'selfcreate') {
 5101:         if (ref($usertypes) eq 'HASH') {
 5102:             $rolename = $usertypes->{$role};
 5103:         } else {
 5104:             $rolename = $role;
 5105:         }
 5106:     } else {
 5107:         if ($role eq 'cr') {
 5108:             $rolename = &mt('Custom role');
 5109:         } else {
 5110:             $rolename = &Apache::lonnet::plaintext($role);
 5111:         }
 5112:     }
 5113:     my (@fields,%fieldtitles);
 5114:     if (ref($fieldsref) eq 'ARRAY') {
 5115:         @fields = @{$fieldsref};
 5116:     } else {
 5117:         @fields = ('lastname','firstname','middlename','generation',
 5118:                    'permanentemail','id');
 5119:     }
 5120:     if ((ref($titlesref) eq 'HASH')) {
 5121:         %fieldtitles = %{$titlesref};
 5122:     } else {
 5123:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5124:     }
 5125:     my $output;
 5126:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5127:     $output = '<tr '.$css_class.'>'.
 5128:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5129:               '<td class="LC_left_item" colspan="2"><table>';
 5130:     my $rem;
 5131:     my %checks;
 5132:     if (ref($settings) eq 'HASH') {
 5133:         if (ref($settings->{$context}) eq 'HASH') {
 5134:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5135:                 my $hashref = $settings->{$context}->{$role};
 5136:                 if ($role eq 'emailusername') {
 5137:                     if ($statustype) {
 5138:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5139:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5140:                             if (ref($hashref) eq 'HASH') { 
 5141:                                 foreach my $field (@fields) {
 5142:                                     if ($hashref->{$field}) {
 5143:                                         $checks{$field} = $hashref->{$field};
 5144:                                     }
 5145:                                 }
 5146:                             }
 5147:                         }
 5148:                     }
 5149:                 } else {
 5150:                     if (ref($hashref) eq 'HASH') {
 5151:                         foreach my $field (@fields) {
 5152:                             if ($hashref->{$field}) {
 5153:                                 $checks{$field} = ' checked="checked" ';
 5154:                             }
 5155:                         }
 5156:                     }
 5157:                 }
 5158:             }
 5159:         }
 5160:     }
 5161:      
 5162:     for (my $i=0; $i<@fields; $i++) {
 5163:         my $rem = $i%($numinrow);
 5164:         if ($rem == 0) {
 5165:             if ($i > 0) {
 5166:                 $output .= '</tr>';
 5167:             }
 5168:             $output .= '<tr>';
 5169:         }
 5170:         my $check = ' ';
 5171:         unless ($role eq 'emailusername') {
 5172:             if (exists($checks{$fields[$i]})) {
 5173:                 $check = $checks{$fields[$i]}
 5174:             } else {
 5175:                 if ($role eq 'st') {
 5176:                     if (ref($settings) ne 'HASH') {
 5177:                         $check = ' checked="checked" '; 
 5178:                     }
 5179:                 }
 5180:             }
 5181:         }
 5182:         $output .= '<td class="LC_left_item">'.
 5183:                    '<span class="LC_nobreak">';
 5184:         if ($role eq 'emailusername') {
 5185:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5186:                 $checks{$fields[$i]} = 'omit';
 5187:             }
 5188:             foreach my $option ('required','optional','omit') {
 5189:                 my $checked='';
 5190:                 if ($checks{$fields[$i]} eq $option) {
 5191:                     $checked='checked="checked" ';
 5192:                 }
 5193:                 $output .= '<label>'.
 5194:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5195:                            &mt($option).'</label>'.('&nbsp;' x2);
 5196:             }
 5197:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5198:         } else {
 5199:             $output .= '<label>'.
 5200:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5201:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5202:                        '</label>';
 5203:         }
 5204:         $output .= '</span></td>';
 5205:         $rem = @fields%($numinrow);
 5206:     }
 5207:     my $colsleft = $numinrow - $rem;
 5208:     if ($colsleft > 1 ) {
 5209:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5210:                    '&nbsp;</td>';
 5211:     } elsif ($colsleft == 1) {
 5212:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5213:     }
 5214:     $output .= '</tr></table></td></tr>';
 5215:     return $output;
 5216: }
 5217: 
 5218: sub insttypes_row {
 5219:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5220:     my %lt = &Apache::lonlocal::texthash (
 5221:                       cansearch => 'Users allowed to search',
 5222:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5223:                       lockablenames => 'User preference to lock name',
 5224:              );
 5225:     my $showdom;
 5226:     if ($context eq 'cansearch') {
 5227:         $showdom = ' ('.$dom.')';
 5228:     }
 5229:     my $class = 'LC_left_item';
 5230:     if ($context eq 'statustocreate') {
 5231:         $class = 'LC_right_item';
 5232:     }
 5233:     my $css_class = ' class="LC_odd_row"';
 5234:     if ($rownum ne '') { 
 5235:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5236:     }
 5237:     my $output = '<tr'.$css_class.'>'.
 5238:                  '<td>'.$lt{$context}.$showdom.
 5239:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5240:     my $rem;
 5241:     if (ref($types) eq 'ARRAY') {
 5242:         for (my $i=0; $i<@{$types}; $i++) {
 5243:             if (defined($usertypes->{$types->[$i]})) {
 5244:                 my $rem = $i%($numinrow);
 5245:                 if ($rem == 0) {
 5246:                     if ($i > 0) {
 5247:                         $output .= '</tr>';
 5248:                     }
 5249:                     $output .= '<tr>';
 5250:                 }
 5251:                 my $check = ' ';
 5252:                 if (ref($settings) eq 'HASH') {
 5253:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5254:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5255:                             $check = ' checked="checked" ';
 5256:                         }
 5257:                     } elsif ($context eq 'statustocreate') {
 5258:                         $check = ' checked="checked" ';
 5259:                     }
 5260:                 }
 5261:                 $output .= '<td class="LC_left_item">'.
 5262:                            '<span class="LC_nobreak"><label>'.
 5263:                            '<input type="checkbox" name="'.$context.'" '.
 5264:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5265:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5266:             }
 5267:         }
 5268:         $rem = @{$types}%($numinrow);
 5269:     }
 5270:     my $colsleft = $numinrow - $rem;
 5271:     if (($rem == 0) && (@{$types} > 0)) {
 5272:         $output .= '<tr>';
 5273:     }
 5274:     if ($colsleft > 1) {
 5275:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5276:     } else {
 5277:         $output .= '<td class="LC_left_item">';
 5278:     }
 5279:     my $defcheck = ' ';
 5280:     if (ref($settings) eq 'HASH') {  
 5281:         if (ref($settings->{$context}) eq 'ARRAY') {
 5282:             if (grep(/^default$/,@{$settings->{$context}})) {
 5283:                 $defcheck = ' checked="checked" ';
 5284:             }
 5285:         } elsif ($context eq 'statustocreate') {
 5286:             $defcheck = ' checked="checked" ';
 5287:         }
 5288:     }
 5289:     $output .= '<span class="LC_nobreak"><label>'.
 5290:                '<input type="checkbox" name="'.$context.'" '.
 5291:                'value="default"'.$defcheck.'/>'.
 5292:                $othertitle.'</label></span></td>'.
 5293:                '</tr></table></td></tr>';
 5294:     return $output;
 5295: }
 5296: 
 5297: sub sorted_searchtitles {
 5298:     my %searchtitles = &Apache::lonlocal::texthash(
 5299:                          'uname' => 'username',
 5300:                          'lastname' => 'last name',
 5301:                          'lastfirst' => 'last name, first name',
 5302:                      );
 5303:     my @titleorder = ('uname','lastname','lastfirst');
 5304:     return (\%searchtitles,\@titleorder);
 5305: }
 5306: 
 5307: sub sorted_searchtypes {
 5308:     my %srchtypes_desc = (
 5309:                            exact    => 'is exact match',
 5310:                            contains => 'contains ..',
 5311:                            begins   => 'begins with ..',
 5312:                          );
 5313:     my @srchtypeorder = ('exact','begins','contains');
 5314:     return (\%srchtypes_desc,\@srchtypeorder);
 5315: }
 5316: 
 5317: sub usertype_update_row {
 5318:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5319:     my $datatable;
 5320:     my $numinrow = 4;
 5321:     foreach my $type (@{$types}) {
 5322:         if (defined($usertypes->{$type})) {
 5323:             $$rownums ++;
 5324:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5325:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5326:                           '</td><td class="LC_left_item"><table>';
 5327:             for (my $i=0; $i<@{$fields}; $i++) {
 5328:                 my $rem = $i%($numinrow);
 5329:                 if ($rem == 0) {
 5330:                     if ($i > 0) {
 5331:                         $datatable .= '</tr>';
 5332:                     }
 5333:                     $datatable .= '<tr>';
 5334:                 }
 5335:                 my $check = ' ';
 5336:                 if (ref($settings) eq 'HASH') {
 5337:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5338:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5339:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5340:                                 $check = ' checked="checked" ';
 5341:                             }
 5342:                         }
 5343:                     }
 5344:                 }
 5345: 
 5346:                 if ($i == @{$fields}-1) {
 5347:                     my $colsleft = $numinrow - $rem;
 5348:                     if ($colsleft > 1) {
 5349:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5350:                     } else {
 5351:                         $datatable .= '<td>';
 5352:                     }
 5353:                 } else {
 5354:                     $datatable .= '<td>';
 5355:                 }
 5356:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5357:                               '<input type="checkbox" name="updateable_'.$type.
 5358:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5359:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5360:             }
 5361:             $datatable .= '</tr></table></td></tr>';
 5362:         }
 5363:     }
 5364:     return $datatable;
 5365: }
 5366: 
 5367: sub modify_login {
 5368:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5369:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5370:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5371:     %title = ( coursecatalog => 'Display course catalog',
 5372:                adminmail => 'Display administrator E-mail address',
 5373:                helpdesk  => 'Display "Contact Helpdesk" link',
 5374:                newuser => 'Link for visitors to create a user account',
 5375:                loginheader => 'Log-in box header');
 5376:     @offon = ('off','on');
 5377:     if (ref($domconfig{login}) eq 'HASH') {
 5378:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5379:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5380:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5381:             }
 5382:         }
 5383:     }
 5384:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5385:                                            \%domconfig,\%loginhash);
 5386:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5387:     foreach my $item (@toggles) {
 5388:         $loginhash{login}{$item} = $env{'form.'.$item};
 5389:     }
 5390:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5391:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5392:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5393:                                          \%loginhash);
 5394:     }
 5395: 
 5396:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5397:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5398:     if (keys(%servers) > 1) {
 5399:         foreach my $lonhost (keys(%servers)) {
 5400:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5401:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5402:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5403:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5404:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5405:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5406:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5407:                         $changes{'loginvia'}{$lonhost} = 1;
 5408:                     } else {
 5409:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5410:                         $changes{'loginvia'}{$lonhost} = 1;
 5411:                     }
 5412:                 } else {
 5413:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5414:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5415:                         $changes{'loginvia'}{$lonhost} = 1;
 5416:                     }
 5417:                 }
 5418:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5419:                     foreach my $item (@loginvia_attribs) {
 5420:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5421:                     }
 5422:                 } else {
 5423:                     foreach my $item (@loginvia_attribs) {
 5424:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5425:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5426:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5427:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5428:                                 $new = '/';
 5429:                             }
 5430:                         }
 5431:                         if (($item eq 'custompath') && 
 5432:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5433:                             $new = '';
 5434:                         }
 5435:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5436:                             $changes{'loginvia'}{$lonhost} = 1;
 5437:                         }
 5438:                         if ($item eq 'exempt') {
 5439:                             $new =~ s/^\s+//;
 5440:                             $new =~ s/\s+$//;
 5441:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5442:                             my @okips;
 5443:                             foreach my $ip (@poss_ips) {
 5444:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5445:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5446:                                         push(@okips,$ip); 
 5447:                                     }
 5448:                                 }
 5449:                             }
 5450:                             if (@okips > 0) {
 5451:                                 $new = join(',',@okips); 
 5452:                             } else {
 5453:                                 $new = ''; 
 5454:                             }
 5455:                         }
 5456:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5457:                     }
 5458:                 }
 5459:             } else {
 5460:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5461:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5462:                     $changes{'loginvia'}{$lonhost} = 1;
 5463:                     foreach my $item (@loginvia_attribs) {
 5464:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5465:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5466:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5467:                                 $new = '/';
 5468:                             }
 5469:                         }
 5470:                         if (($item eq 'custompath') && 
 5471:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5472:                             $new = '';
 5473:                         }
 5474:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5475:                     }
 5476:                 }
 5477:             }
 5478:         }
 5479:     }
 5480: 
 5481:     my $servadm = $r->dir_config('lonAdmEMail');
 5482:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5483:     if (ref($domconfig{'login'}) eq 'HASH') {
 5484:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5485:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5486:                 if ($lang eq 'nolang') {
 5487:                     push(@currlangs,$lang);
 5488:                 } elsif (defined($langchoices{$lang})) {
 5489:                     push(@currlangs,$lang);
 5490:                 } else {
 5491:                     next;
 5492:                 }
 5493:             }
 5494:         }
 5495:     }
 5496:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5497:     if (@currlangs > 0) {
 5498:         foreach my $lang (@currlangs) {
 5499:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5500:                 $changes{'helpurl'}{$lang} = 1;
 5501:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5502:                 $changes{'helpurl'}{$lang} = 1;
 5503:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5504:                 push(@newlangs,$lang);
 5505:             } else {
 5506:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5507:             }
 5508:         }
 5509:     }
 5510:     unless (grep(/^nolang$/,@currlangs)) {
 5511:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5512:             $changes{'helpurl'}{'nolang'} = 1;
 5513:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5514:             push(@newlangs,'nolang');
 5515:         }
 5516:     }
 5517:     if ($env{'form.loginhelpurl_add_lang'}) {
 5518:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5519:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5520:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5521:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5522:         }
 5523:     }
 5524:     if ((@newlangs > 0) || ($addedfile)) {
 5525:         my $error;
 5526:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5527:         if ($configuserok eq 'ok') {
 5528:             if ($switchserver) {
 5529:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5530:             } elsif ($author_ok eq 'ok') {
 5531:                 my @allnew = @newlangs;
 5532:                 if ($addedfile ne '') {
 5533:                     push(@allnew,$addedfile);
 5534:                 }
 5535:                 foreach my $lang (@allnew) {
 5536:                     my $formelem = 'loginhelpurl_'.$lang;
 5537:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5538:                         $formelem = 'loginhelpurl_add_file';
 5539:                     }
 5540:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5541:                                                                "help/$lang",'','',$newfile{$lang});
 5542:                     if ($result eq 'ok') {
 5543:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5544:                         $changes{'helpurl'}{$lang} = 1;
 5545:                     } else {
 5546:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5547:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5548:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5549:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5550: 
 5551:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5552:                         }
 5553:                     }
 5554:                 }
 5555:             } else {
 5556:                 $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);
 5557:             }
 5558:         } else {
 5559:             $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);
 5560:         }
 5561:         if ($error) {
 5562:             &Apache::lonnet::logthis($error);
 5563:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5564:         }
 5565:     }
 5566:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5567: 
 5568:     my $defaulthelpfile = '/adm/loginproblems.html';
 5569:     my $defaulttext = &mt('Default in use');
 5570: 
 5571:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5572:                                              $dom);
 5573:     if ($putresult eq 'ok') {
 5574:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5575:         my %defaultchecked = (
 5576:                     'coursecatalog' => 'on',
 5577:                     'helpdesk'      => 'on',
 5578:                     'adminmail'     => 'off',
 5579:                     'newuser'       => 'off',
 5580:         );
 5581:         if (ref($domconfig{'login'}) eq 'HASH') {
 5582:             foreach my $item (@toggles) {
 5583:                 if ($defaultchecked{$item} eq 'on') { 
 5584:                     if (($domconfig{'login'}{$item} eq '0') &&
 5585:                         ($env{'form.'.$item} eq '1')) {
 5586:                         $changes{$item} = 1;
 5587:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5588:                               $domconfig{'login'}{$item} eq '1') &&
 5589:                              ($env{'form.'.$item} eq '0')) {
 5590:                         $changes{$item} = 1;
 5591:                     }
 5592:                 } elsif ($defaultchecked{$item} eq 'off') {
 5593:                     if (($domconfig{'login'}{$item} eq '1') &&
 5594:                         ($env{'form.'.$item} eq '0')) {
 5595:                         $changes{$item} = 1;
 5596:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5597:                               $domconfig{'login'}{$item} eq '0') &&
 5598:                              ($env{'form.'.$item} eq '1')) {
 5599:                         $changes{$item} = 1;
 5600:                     }
 5601:                 }
 5602:             }
 5603:         }
 5604:         if (keys(%changes) > 0 || $colchgtext) {
 5605:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5606:             if (ref($lastactref) eq 'HASH') {
 5607:                 $lastactref->{'domainconfig'} = 1;
 5608:             }
 5609:             $resulttext = &mt('Changes made:').'<ul>';
 5610:             foreach my $item (sort(keys(%changes))) {
 5611:                 if ($item eq 'loginvia') {
 5612:                     if (ref($changes{$item}) eq 'HASH') {
 5613:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5614:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5615:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5616:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5617:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5618:                                     $protocol = 'http' if ($protocol ne 'https');
 5619:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5620: 
 5621:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5622:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5623:                                     } else {
 5624:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5625:                                     }
 5626:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5627:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5628:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5629:                                     }
 5630:                                     $resulttext .= '</li>';
 5631:                                 } else {
 5632:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5633:                                 }
 5634:                             } else {
 5635:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5636:                             }
 5637:                         }
 5638:                         $resulttext .= '</ul></li>';
 5639:                     }
 5640:                 } elsif ($item eq 'helpurl') {
 5641:                     if (ref($changes{$item}) eq 'HASH') {
 5642:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5643:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5644:                                 my ($chg,$link);
 5645:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5646:                                 if ($lang eq 'nolang') {
 5647:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5648:                                 } else {
 5649:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5650:                                 }
 5651:                                 $resulttext .= '<li>'.$chg.'</li>';
 5652:                             } else {
 5653:                                 my $chg;
 5654:                                 if ($lang eq 'nolang') {
 5655:                                     $chg = &mt('custom log-in help file for no preferred language');
 5656:                                 } else {
 5657:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5658:                                 }
 5659:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5660:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5661:                                                       '?inhibitmenu=yes',$chg,600,500).
 5662:                                                '</li>';
 5663:                             }
 5664:                         }
 5665:                     }
 5666:                 } elsif ($item eq 'captcha') {
 5667:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5668:                         my $chgtxt;
 5669:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5670:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5671:                         } else {
 5672:                             my %captchas = &captcha_phrases();
 5673:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5674:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5675:                             } else {
 5676:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5677:                             }
 5678:                         }
 5679:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5680:                     }
 5681:                 } elsif ($item eq 'recaptchakeys') {
 5682:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5683:                         my ($privkey,$pubkey);
 5684:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5685:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5686:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5687:                         }
 5688:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5689:                         if (!$pubkey) {
 5690:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5691:                         } else {
 5692:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5693:                         }
 5694:                         if (!$privkey) {
 5695:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5696:                         } else {
 5697:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5698:                         }
 5699:                         $chgtxt .= '</ul>';
 5700:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5701:                     }
 5702:                 } else {
 5703:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5704:                 }
 5705:             }
 5706:             $resulttext .= $colchgtext.'</ul>';
 5707:         } else {
 5708:             $resulttext = &mt('No changes made to log-in page settings');
 5709:         }
 5710:     } else {
 5711:         $resulttext = '<span class="LC_error">'.
 5712: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5713:     }
 5714:     if ($errors) {
 5715:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5716:                        $errors.'</ul>';
 5717:     }
 5718:     return $resulttext;
 5719: }
 5720: 
 5721: sub color_font_choices {
 5722:     my %choices =
 5723:         &Apache::lonlocal::texthash (
 5724:             img => "Header",
 5725:             bgs => "Background colors",
 5726:             links => "Link colors",
 5727:             images => "Images",
 5728:             font => "Font color",
 5729:             fontmenu => "Font menu",
 5730:             pgbg => "Page",
 5731:             tabbg => "Header",
 5732:             sidebg => "Border",
 5733:             link => "Link",
 5734:             alink => "Active link",
 5735:             vlink => "Visited link",
 5736:         );
 5737:     return %choices;
 5738: }
 5739: 
 5740: sub modify_rolecolors {
 5741:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5742:     my ($resulttext,%rolehash);
 5743:     $rolehash{'rolecolors'} = {};
 5744:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5745:         if ($domconfig{'rolecolors'} eq '') {
 5746:             $domconfig{'rolecolors'} = {};
 5747:         }
 5748:     }
 5749:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5750:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5751:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5752:                                              $dom);
 5753:     if ($putresult eq 'ok') {
 5754:         if (keys(%changes) > 0) {
 5755:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5756:             if (ref($lastactref) eq 'HASH') {
 5757:                 $lastactref->{'domainconfig'} = 1;
 5758:             }
 5759:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5760:                                              $rolehash{'rolecolors'});
 5761:         } else {
 5762:             $resulttext = &mt('No changes made to default color schemes');
 5763:         }
 5764:     } else {
 5765:         $resulttext = '<span class="LC_error">'.
 5766: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5767:     }
 5768:     if ($errors) {
 5769:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5770:                        $errors.'</ul>';
 5771:     }
 5772:     return $resulttext;
 5773: }
 5774: 
 5775: sub modify_colors {
 5776:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5777:     my (%changes,%choices);
 5778:     my @bgs;
 5779:     my @links = ('link','alink','vlink');
 5780:     my @logintext;
 5781:     my @images;
 5782:     my $servadm = $r->dir_config('lonAdmEMail');
 5783:     my $errors;
 5784:     my %defaults;
 5785:     foreach my $role (@{$roles}) {
 5786:         if ($role eq 'login') {
 5787:             %choices = &login_choices();
 5788:             @logintext = ('textcol','bgcol');
 5789:         } else {
 5790:             %choices = &color_font_choices();
 5791:         }
 5792:         if ($role eq 'login') {
 5793:             @images = ('img','logo','domlogo','login');
 5794:             @bgs = ('pgbg','mainbg','sidebg');
 5795:         } else {
 5796:             @images = ('img');
 5797:             @bgs = ('pgbg','tabbg','sidebg');
 5798:         }
 5799:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5800:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5801:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5802:         }
 5803:         if ($role eq 'login') {
 5804:             foreach my $item (@logintext) {
 5805:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 5806:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 5807:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 5808:                 }
 5809:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 5810:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5811:                 }
 5812:             }
 5813:         } else {
 5814:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 5815:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 5816:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 5817:             }
 5818:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 5819:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5820:             }
 5821:         }
 5822:         foreach my $item (@bgs) {
 5823:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 5824:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 5825:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 5826:             }
 5827:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 5828:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5829:             }
 5830:         }
 5831:         foreach my $item (@links) {
 5832:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 5833:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 5834:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 5835:             }
 5836:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 5837:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5838:             }
 5839:         }
 5840:         my ($configuserok,$author_ok,$switchserver) = 
 5841:             &config_check($dom,$confname,$servadm);
 5842:         my ($width,$height) = &thumb_dimensions();
 5843:         if (ref($domconfig->{$role}) ne 'HASH') {
 5844:             $domconfig->{$role} = {};
 5845:         }
 5846:         foreach my $img (@images) {
 5847:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5848:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5849:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5850:                 } else { 
 5851:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5852:                 }
 5853:             } 
 5854: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5855: 		 && !defined($domconfig->{$role}{$img})
 5856: 		 && !$env{'form.'.$role.'_del_'.$img}
 5857: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5858: 		# import the old configured image from the .tab setting
 5859: 		# if they haven't provided a new one 
 5860: 		$domconfig->{$role}{$img} = 
 5861: 		    $env{'form.'.$role.'_import_'.$img};
 5862: 	    }
 5863:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5864:                 my $error;
 5865:                 if ($configuserok eq 'ok') {
 5866:                     if ($switchserver) {
 5867:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5868:                     } else {
 5869:                         if ($author_ok eq 'ok') {
 5870:                             my ($result,$logourl) = 
 5871:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5872:                                            $dom,$confname,$img,$width,$height);
 5873:                             if ($result eq 'ok') {
 5874:                                 $confhash->{$role}{$img} = $logourl;
 5875:                                 $changes{$role}{'images'}{$img} = 1;
 5876:                             } else {
 5877:                                 $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);
 5878:                             }
 5879:                         } else {
 5880:                             $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);
 5881:                         }
 5882:                     }
 5883:                 } else {
 5884:                     $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);
 5885:                 }
 5886:                 if ($error) {
 5887:                     &Apache::lonnet::logthis($error);
 5888:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5889:                 }
 5890:             } elsif ($domconfig->{$role}{$img} ne '') {
 5891:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5892:                     my $error;
 5893:                     if ($configuserok eq 'ok') {
 5894: # is confname an author?
 5895:                         if ($switchserver eq '') {
 5896:                             if ($author_ok eq 'ok') {
 5897:                                 my ($result,$logourl) = 
 5898:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5899:                                             $dom,$confname,$img,$width,$height);
 5900:                                 if ($result eq 'ok') {
 5901:                                     $confhash->{$role}{$img} = $logourl;
 5902: 				    $changes{$role}{'images'}{$img} = 1;
 5903:                                 }
 5904:                             }
 5905:                         }
 5906:                     }
 5907:                 }
 5908:             }
 5909:         }
 5910:         if (ref($domconfig) eq 'HASH') {
 5911:             if (ref($domconfig->{$role}) eq 'HASH') {
 5912:                 foreach my $img (@images) {
 5913:                     if ($domconfig->{$role}{$img} ne '') {
 5914:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5915:                             $confhash->{$role}{$img} = '';
 5916:                             $changes{$role}{'images'}{$img} = 1;
 5917:                         } else {
 5918:                             if ($confhash->{$role}{$img} eq '') {
 5919:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5920:                             }
 5921:                         }
 5922:                     } else {
 5923:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5924:                             $confhash->{$role}{$img} = '';
 5925:                             $changes{$role}{'images'}{$img} = 1;
 5926:                         } 
 5927:                     }
 5928:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5929:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5930:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5931:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5932:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5933:                             }
 5934:                         } else {
 5935:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5936:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5937:                             }
 5938:                         }
 5939:                     }
 5940:                 }
 5941:                 if ($domconfig->{$role}{'font'} ne '') {
 5942:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5943:                         $changes{$role}{'font'} = 1;
 5944:                     }
 5945:                 } else {
 5946:                     if ($confhash->{$role}{'font'}) {
 5947:                         $changes{$role}{'font'} = 1;
 5948:                     }
 5949:                 }
 5950:                 if ($role ne 'login') {
 5951:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5952:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5953:                             $changes{$role}{'fontmenu'} = 1;
 5954:                         }
 5955:                     } else {
 5956:                         if ($confhash->{$role}{'fontmenu'}) {
 5957:                             $changes{$role}{'fontmenu'} = 1;
 5958:                         }
 5959:                     }
 5960:                 }
 5961:                 foreach my $item (@bgs) {
 5962:                     if ($domconfig->{$role}{$item} ne '') {
 5963:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5964:                             $changes{$role}{'bgs'}{$item} = 1;
 5965:                         } 
 5966:                     } else {
 5967:                         if ($confhash->{$role}{$item}) {
 5968:                             $changes{$role}{'bgs'}{$item} = 1;
 5969:                         }
 5970:                     }
 5971:                 }
 5972:                 foreach my $item (@links) {
 5973:                     if ($domconfig->{$role}{$item} ne '') {
 5974:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5975:                             $changes{$role}{'links'}{$item} = 1;
 5976:                         }
 5977:                     } else {
 5978:                         if ($confhash->{$role}{$item}) {
 5979:                             $changes{$role}{'links'}{$item} = 1;
 5980:                         }
 5981:                     }
 5982:                 }
 5983:                 foreach my $item (@logintext) {
 5984:                     if ($domconfig->{$role}{$item} ne '') {
 5985:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5986:                             $changes{$role}{'logintext'}{$item} = 1;
 5987:                         }
 5988:                     } else {
 5989:                         if ($confhash->{$role}{$item}) {
 5990:                             $changes{$role}{'logintext'}{$item} = 1;
 5991:                         }
 5992:                     }
 5993:                 }
 5994:             } else {
 5995:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5996:                                         \@logintext,$confhash,\%changes); 
 5997:             }
 5998:         } else {
 5999:             &default_change_checker($role,\@images,\@links,\@bgs,
 6000:                                     \@logintext,$confhash,\%changes); 
 6001:         }
 6002:     }
 6003:     return ($errors,%changes);
 6004: }
 6005: 
 6006: sub config_check {
 6007:     my ($dom,$confname,$servadm) = @_;
 6008:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6009:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6010:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6011:                                                    $confname,$servadm);
 6012:     if ($configuserok eq 'ok') {
 6013:         $switchserver = &check_switchserver($dom,$confname);
 6014:         if ($switchserver eq '') {
 6015:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6016:         }
 6017:     }
 6018:     return ($configuserok,$author_ok,$switchserver);
 6019: }
 6020: 
 6021: sub default_change_checker {
 6022:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6023:     foreach my $item (@{$links}) {
 6024:         if ($confhash->{$role}{$item}) {
 6025:             $changes->{$role}{'links'}{$item} = 1;
 6026:         }
 6027:     }
 6028:     foreach my $item (@{$bgs}) {
 6029:         if ($confhash->{$role}{$item}) {
 6030:             $changes->{$role}{'bgs'}{$item} = 1;
 6031:         }
 6032:     }
 6033:     foreach my $item (@{$logintext}) {
 6034:         if ($confhash->{$role}{$item}) {
 6035:             $changes->{$role}{'logintext'}{$item} = 1;
 6036:         }
 6037:     }
 6038:     foreach my $img (@{$images}) {
 6039:         if ($env{'form.'.$role.'_del_'.$img}) {
 6040:             $confhash->{$role}{$img} = '';
 6041:             $changes->{$role}{'images'}{$img} = 1;
 6042:         }
 6043:         if ($role eq 'login') {
 6044:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6045:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6046:             }
 6047:         }
 6048:     }
 6049:     if ($confhash->{$role}{'font'}) {
 6050:         $changes->{$role}{'font'} = 1;
 6051:     }
 6052: }
 6053: 
 6054: sub display_colorchgs {
 6055:     my ($dom,$changes,$roles,$confhash) = @_;
 6056:     my (%choices,$resulttext);
 6057:     if (!grep(/^login$/,@{$roles})) {
 6058:         $resulttext = &mt('Changes made:').'<br />';
 6059:     }
 6060:     foreach my $role (@{$roles}) {
 6061:         if ($role eq 'login') {
 6062:             %choices = &login_choices();
 6063:         } else {
 6064:             %choices = &color_font_choices();
 6065:         }
 6066:         if (ref($changes->{$role}) eq 'HASH') {
 6067:             if ($role ne 'login') {
 6068:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6069:             }
 6070:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6071:                 if ($role ne 'login') {
 6072:                     $resulttext .= '<ul>';
 6073:                 }
 6074:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6075:                     if ($role ne 'login') {
 6076:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6077:                     }
 6078:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6079:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6080:                             if ($confhash->{$role}{$key}{$item}) {
 6081:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6082:                             } else {
 6083:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6084:                             }
 6085:                         } elsif ($confhash->{$role}{$item} eq '') {
 6086:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6087:                         } else {
 6088:                             my $newitem = $confhash->{$role}{$item};
 6089:                             if ($key eq 'images') {
 6090:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6091:                             }
 6092:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6093:                         }
 6094:                     }
 6095:                     if ($role ne 'login') {
 6096:                         $resulttext .= '</ul></li>';
 6097:                     }
 6098:                 } else {
 6099:                     if ($confhash->{$role}{$key} eq '') {
 6100:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6101:                     } else {
 6102:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6103:                     }
 6104:                 }
 6105:                 if ($role ne 'login') {
 6106:                     $resulttext .= '</ul>';
 6107:                 }
 6108:             }
 6109:         }
 6110:     }
 6111:     return $resulttext;
 6112: }
 6113: 
 6114: sub thumb_dimensions {
 6115:     return ('200','50');
 6116: }
 6117: 
 6118: sub check_dimensions {
 6119:     my ($inputfile) = @_;
 6120:     my ($fullwidth,$fullheight);
 6121:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6122:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6123:             my $imageinfo = <PIPE>;
 6124:             if (!close(PIPE)) {
 6125:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6126:             }
 6127:             chomp($imageinfo);
 6128:             my ($fullsize) = 
 6129:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6130:             if ($fullsize) {
 6131:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6132:             }
 6133:         }
 6134:     }
 6135:     return ($fullwidth,$fullheight);
 6136: }
 6137: 
 6138: sub check_configuser {
 6139:     my ($uhome,$dom,$confname,$servadm) = @_;
 6140:     my ($configuserok,%currroles);
 6141:     if ($uhome eq 'no_host') {
 6142:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6143:         my $configpass = &LONCAPA::Enrollment::create_password();
 6144:         $configuserok = 
 6145:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6146:                              $configpass,'','','','','',undef,$servadm);
 6147:     } else {
 6148:         $configuserok = 'ok';
 6149:         %currroles = 
 6150:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6151:     }
 6152:     return ($configuserok,%currroles);
 6153: }
 6154: 
 6155: sub check_authorstatus {
 6156:     my ($dom,$confname,%currroles) = @_;
 6157:     my $author_ok;
 6158:     if (!$currroles{':'.$dom.':au'}) {
 6159:         my $start = time;
 6160:         my $end = 0;
 6161:         $author_ok = 
 6162:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6163:                                         'au',$end,$start,'','','domconfig');
 6164:     } else {
 6165:         $author_ok = 'ok';
 6166:     }
 6167:     return $author_ok;
 6168: }
 6169: 
 6170: sub publishlogo {
 6171:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6172:     my ($output,$fname,$logourl);
 6173:     if ($action eq 'upload') {
 6174:         $fname=$env{'form.'.$formname.'.filename'};
 6175:         chop($env{'form.'.$formname});
 6176:     } else {
 6177:         ($fname) = ($formname =~ /([^\/]+)$/);
 6178:     }
 6179:     if ($savefileas ne '') {
 6180:         $fname = $savefileas;
 6181:     }
 6182:     $fname=&Apache::lonnet::clean_filename($fname);
 6183: # See if there is anything left
 6184:     unless ($fname) { return ('error: no uploaded file'); }
 6185:     $fname="$subdir/$fname";
 6186:     my $docroot=$r->dir_config('lonDocRoot');
 6187:     my $filepath="$docroot/priv";
 6188:     my $relpath = "$dom/$confname";
 6189:     my ($fnamepath,$file,$fetchthumb);
 6190:     $file=$fname;
 6191:     if ($fname=~m|/|) {
 6192:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6193:     }
 6194:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6195:     my $count;
 6196:     for ($count=5;$count<=$#parts;$count++) {
 6197:         $filepath.="/$parts[$count]";
 6198:         if ((-e $filepath)!=1) {
 6199:             mkdir($filepath,02770);
 6200:         }
 6201:     }
 6202:     # Check for bad extension and disallow upload
 6203:     if ($file=~/\.(\w+)$/ &&
 6204:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6205:         $output = 
 6206:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6207:     } elsif ($file=~/\.(\w+)$/ &&
 6208:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6209:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6210:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6211:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6212:     } elsif (-d "$filepath/$file") {
 6213:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6214:     } else {
 6215:         my $source = $filepath.'/'.$file;
 6216:         my $logfile;
 6217:         if (!open($logfile,">>$source".'.log')) {
 6218:             return (&mt('No write permission to Authoring Space'));
 6219:         }
 6220:         print $logfile
 6221: "\n================= Publish ".localtime()." ================\n".
 6222: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6223: # Save the file
 6224:         if (!open(FH,'>'.$source)) {
 6225:             &Apache::lonnet::logthis('Failed to create '.$source);
 6226:             return (&mt('Failed to create file'));
 6227:         }
 6228:         if ($action eq 'upload') {
 6229:             if (!print FH ($env{'form.'.$formname})) {
 6230:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6231:                 return (&mt('Failed to write file'));
 6232:             }
 6233:         } else {
 6234:             my $original = &Apache::lonnet::filelocation('',$formname);
 6235:             if(!copy($original,$source)) {
 6236:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6237:                 return (&mt('Failed to write file'));
 6238:             }
 6239:         }
 6240:         close(FH);
 6241:         chmod(0660, $source); # Permissions to rw-rw---.
 6242: 
 6243:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6244:         my $copyfile=$targetdir.'/'.$file;
 6245: 
 6246:         my @parts=split(/\//,$targetdir);
 6247:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6248:         for (my $count=5;$count<=$#parts;$count++) {
 6249:             $path.="/$parts[$count]";
 6250:             if (!-e $path) {
 6251:                 print $logfile "\nCreating directory ".$path;
 6252:                 mkdir($path,02770);
 6253:             }
 6254:         }
 6255:         my $versionresult;
 6256:         if (-e $copyfile) {
 6257:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6258:         } else {
 6259:             $versionresult = 'ok';
 6260:         }
 6261:         if ($versionresult eq 'ok') {
 6262:             if (copy($source,$copyfile)) {
 6263:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6264:                 $output = 'ok';
 6265:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6266:                 push(@{$modified_urls},[$copyfile,$source]);
 6267:                 my $metaoutput = 
 6268:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6269:                 unless ($registered_cleanup) {
 6270:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6271:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6272:                     $registered_cleanup=1;
 6273:                 }
 6274:             } else {
 6275:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6276:                 $output = &mt('Failed to copy file to RES space').", $!";
 6277:             }
 6278:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6279:                 my $inputfile = $filepath.'/'.$file;
 6280:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6281:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6282:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6283:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6284:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6285:                         system("convert -sample $thumbsize $inputfile $outfile");
 6286:                         chmod(0660, $filepath.'/tn-'.$file);
 6287:                         if (-e $outfile) {
 6288:                             my $copyfile=$targetdir.'/tn-'.$file;
 6289:                             if (copy($outfile,$copyfile)) {
 6290:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6291:                                 my $thumb_metaoutput = 
 6292:                                     &write_metadata($dom,$confname,$formname,
 6293:                                                     $targetdir,'tn-'.$file,$logfile);
 6294:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6295:                                 unless ($registered_cleanup) {
 6296:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6297:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6298:                                     $registered_cleanup=1;
 6299:                                 }
 6300:                             } else {
 6301:                                 print $logfile "\nUnable to write ".$copyfile.
 6302:                                                ':'.$!."\n";
 6303:                             }
 6304:                         }
 6305:                     }
 6306:                 }
 6307:             }
 6308:         } else {
 6309:             $output = $versionresult;
 6310:         }
 6311:     }
 6312:     return ($output,$logourl);
 6313: }
 6314: 
 6315: sub logo_versioning {
 6316:     my ($targetdir,$file,$logfile) = @_;
 6317:     my $target = $targetdir.'/'.$file;
 6318:     my ($maxversion,$fn,$extn,$output);
 6319:     $maxversion = 0;
 6320:     if ($file =~ /^(.+)\.(\w+)$/) {
 6321:         $fn=$1;
 6322:         $extn=$2;
 6323:     }
 6324:     opendir(DIR,$targetdir);
 6325:     while (my $filename=readdir(DIR)) {
 6326:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6327:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6328:         }
 6329:     }
 6330:     $maxversion++;
 6331:     print $logfile "\nCreating old version ".$maxversion."\n";
 6332:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6333:     if (copy($target,$copyfile)) {
 6334:         print $logfile "Copied old target to ".$copyfile."\n";
 6335:         $copyfile=$copyfile.'.meta';
 6336:         if (copy($target.'.meta',$copyfile)) {
 6337:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6338:             $output = 'ok';
 6339:         } else {
 6340:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6341:             $output = &mt('Failed to copy old meta').", $!, ";
 6342:         }
 6343:     } else {
 6344:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6345:         $output = &mt('Failed to copy old target').", $!, ";
 6346:     }
 6347:     return $output;
 6348: }
 6349: 
 6350: sub write_metadata {
 6351:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6352:     my (%metadatafields,%metadatakeys,$output);
 6353:     $metadatafields{'title'}=$formname;
 6354:     $metadatafields{'creationdate'}=time;
 6355:     $metadatafields{'lastrevisiondate'}=time;
 6356:     $metadatafields{'copyright'}='public';
 6357:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6358:                                          $env{'user.domain'};
 6359:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6360:     $metadatafields{'domain'}=$dom;
 6361:     {
 6362:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6363:         my $mfh;
 6364:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6365:             foreach (sort(keys(%metadatafields))) {
 6366:                 unless ($_=~/\./) {
 6367:                     my $unikey=$_;
 6368:                     $unikey=~/^([A-Za-z]+)/;
 6369:                     my $tag=$1;
 6370:                     $tag=~tr/A-Z/a-z/;
 6371:                     print $mfh "\n\<$tag";
 6372:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6373:                         my $value=$metadatafields{$unikey.'.'.$_};
 6374:                         $value=~s/\"/\'\'/g;
 6375:                         print $mfh ' '.$_.'="'.$value.'"';
 6376:                     }
 6377:                     print $mfh '>'.
 6378:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6379:                             .'</'.$tag.'>';
 6380:                 }
 6381:             }
 6382:             $output = 'ok';
 6383:             print $logfile "\nWrote metadata";
 6384:             close($mfh);
 6385:         } else {
 6386:             print $logfile "\nFailed to open metadata file";
 6387:             $output = &mt('Could not write metadata');
 6388:         }
 6389:     }
 6390:     return $output;
 6391: }
 6392: 
 6393: sub notifysubscribed {
 6394:     foreach my $targetsource (@{$modified_urls}){
 6395:         next unless (ref($targetsource) eq 'ARRAY');
 6396:         my ($target,$source)=@{$targetsource};
 6397:         if ($source ne '') {
 6398:             if (open(my $logfh,'>>'.$source.'.log')) {
 6399:                 print $logfh "\nCleanup phase: Notifications\n";
 6400:                 my @subscribed=&subscribed_hosts($target);
 6401:                 foreach my $subhost (@subscribed) {
 6402:                     print $logfh "\nNotifying host ".$subhost.':';
 6403:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6404:                     print $logfh $reply;
 6405:                 }
 6406:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6407:                 foreach my $subhost (@subscribedmeta) {
 6408:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6409:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6410:                                                         $subhost);
 6411:                     print $logfh $reply;
 6412:                 }
 6413:                 print $logfh "\n============ Done ============\n";
 6414:                 close($logfh);
 6415:             }
 6416:         }
 6417:     }
 6418:     return OK;
 6419: }
 6420: 
 6421: sub subscribed_hosts {
 6422:     my ($target) = @_;
 6423:     my @subscribed;
 6424:     if (open(my $fh,"<$target.subscription")) {
 6425:         while (my $subline=<$fh>) {
 6426:             if ($subline =~ /^($match_lonid):/) {
 6427:                 my $host = $1;
 6428:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6429:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6430:                         push(@subscribed,$host);
 6431:                     }
 6432:                 }
 6433:             }
 6434:         }
 6435:     }
 6436:     return @subscribed;
 6437: }
 6438: 
 6439: sub check_switchserver {
 6440:     my ($dom,$confname) = @_;
 6441:     my ($allowed,$switchserver);
 6442:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6443:     if ($home eq 'no_host') {
 6444:         $home = &Apache::lonnet::domain($dom,'primary');
 6445:     }
 6446:     my @ids=&Apache::lonnet::current_machine_ids();
 6447:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6448:     if (!$allowed) {
 6449: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6450:     }
 6451:     return $switchserver;
 6452: }
 6453: 
 6454: sub modify_quotas {
 6455:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6456:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6457:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6458:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 6459:         $validationfieldsref);
 6460:     if ($action eq 'quotas') {
 6461:         $context = 'tools'; 
 6462:     } else {
 6463:         $context = $action;
 6464:     }
 6465:     if ($context eq 'requestcourses') {
 6466:         @usertools = ('official','unofficial','community','textbook');
 6467:         @options =('norequest','approval','validate','autolimit');
 6468:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6469:         %titles = &courserequest_titles();
 6470:         $toolregexp = join('|',@usertools);
 6471:         %conditions = &courserequest_conditions();
 6472:         $confname = $dom.'-domainconfig';
 6473:         my $servadm = $r->dir_config('lonAdmEMail');
 6474:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6475:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 6476:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6477:     } elsif ($context eq 'requestauthor') {
 6478:         @usertools = ('author');
 6479:         %titles = &authorrequest_titles();
 6480:     } else {
 6481:         @usertools = ('aboutme','blog','webdav','portfolio');
 6482:         %titles = &tool_titles();
 6483:     }
 6484:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6485:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6486:     foreach my $key (keys(%env)) {
 6487:         if ($context eq 'requestcourses') {
 6488:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6489:                 my $item = $1;
 6490:                 my $type = $2;
 6491:                 if ($type =~ /^limit_(.+)/) {
 6492:                     $limithash{$item}{$1} = $env{$key};
 6493:                 } else {
 6494:                     $confhash{$item}{$type} = $env{$key};
 6495:                 }
 6496:             }
 6497:         } elsif ($context eq 'requestauthor') {
 6498:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6499:                 $confhash{$1} = $env{$key};
 6500:             }
 6501:         } else {
 6502:             if ($key =~ /^form\.quota_(.+)$/) {
 6503:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6504:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6505:                 $confhash{'authorquota'}{$1} = $env{$key};
 6506:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6507:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6508:             }
 6509:         }
 6510:     }
 6511:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6512:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6513:         @approvalnotify = sort(@approvalnotify);
 6514:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6515:         my @crstypes = ('official','unofficial','community','textbook');
 6516:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6517:         foreach my $type (@hasuniquecode) {
 6518:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6519:                 $confhash{'uniquecode'}{$type} = 1;
 6520:             }
 6521:         }
 6522:         my ($newbook,@allpos);
 6523:         if ($context eq 'requestcourses') {
 6524:             if ($env{'form.addbook'}) {
 6525:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6526:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6527:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6528:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6529:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6530:                                    '</span></li>';
 6531:                     } else {
 6532:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6533:                         my $position = $env{'form.addbook_pos'};
 6534:                         $position =~ s/\D+//g;
 6535:                         if ($position ne '') {
 6536:                             $allpos[$position] = $newbook;
 6537:                         }
 6538:                     }
 6539:                 } else {
 6540:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6541:                                '</span></li>';
 6542:                 }
 6543:             }
 6544: 
 6545:         }
 6546:         if (ref($domconfig{$action}) eq 'HASH') {
 6547:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6548:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6549:                     $changes{'notify'}{'approval'} = 1;
 6550:                 }
 6551:             } else {
 6552:                 if ($confhash{'notify'}{'approval'}) {
 6553:                     $changes{'notify'}{'approval'} = 1;
 6554:                 }
 6555:             }
 6556:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6557:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6558:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6559:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6560:                             $changes{'uniquecode'} = 1;
 6561:                         }
 6562:                     }
 6563:                     unless ($changes{'uniquecode'}) {
 6564:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6565:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6566:                                 $changes{'uniquecode'} = 1;
 6567:                             }
 6568:                         }
 6569:                     }
 6570:                } else {
 6571:                    $changes{'uniquecode'} = 1;
 6572:                }
 6573:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6574:                 $changes{'uniquecode'} = 1;
 6575:             }
 6576:             if ($context eq 'requestcourses') {
 6577:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6578:                     my %deletions;
 6579:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6580:                     if (@todelete) {
 6581:                         map { $deletions{$_} = 1; } @todelete;
 6582:                     }
 6583:                     my %imgdeletions;
 6584:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6585:                     if (@todeleteimages) {
 6586:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6587:                     }
 6588:                     my $maxnum = $env{'form.book_maxnum'};
 6589:                     for (my $i=0; $i<=$maxnum; $i++) {
 6590:                         my $key = $env{'form.book_id_'.$i};
 6591:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6592:                             if ($deletions{$key}) {
 6593:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6594:                                     #FIXME need to obsolete item in RES space
 6595:                                 }
 6596:                                 next;
 6597:                             } else {
 6598:                                 my $newpos = $env{'form.'.$key};
 6599:                                 $newpos =~ s/\D+//g;
 6600:                                 foreach my $item ('subject','title','author') {
 6601:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6602:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6603:                                         $changes{'textbooks'}{$key} = 1;
 6604:                                     }
 6605:                                 }
 6606:                                 $allpos[$newpos] = $key;
 6607:                             }
 6608:                             if ($imgdeletions{$key}) {
 6609:                                 $changes{'textbooks'}{$key} = 1;
 6610:                                 #FIXME need to obsolete item in RES space
 6611:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6612:                                 my ($cdom,$cnum) = split(/_/,$key);
 6613:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6614:                                                                               $cdom,$cnum,$configuserok,
 6615:                                                                               $switchserver,$author_ok);
 6616:                                 if ($imgurl) {
 6617:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6618:                                     $changes{'textbooks'}{$key} = 1; 
 6619:                                 }
 6620:                                 if ($error) {
 6621:                                     &Apache::lonnet::logthis($error);
 6622:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6623:                                 } 
 6624:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6625:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6626:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6627:                             }
 6628:                         }
 6629:                     }
 6630:                 }
 6631:             }
 6632:         } else {
 6633:             if ($confhash{'notify'}{'approval'}) {
 6634:                 $changes{'notify'}{'approval'} = 1;
 6635:             }
 6636:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6637:                 $changes{'uniquecode'} = 1;
 6638:             }
 6639:         }
 6640:         if ($context eq 'requestcourses') {
 6641:             if ($newbook) {
 6642:                 $changes{'textbooks'}{$newbook} = 1;
 6643:                 foreach my $item ('subject','title','author') {
 6644:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6645:                     if ($env{'form.addbook_'.$item}) {
 6646:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6647:                     }
 6648:                 }
 6649:                 if ($env{'form.addbook_image.filename'} ne '') {
 6650:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6651:                     my ($imageurl,$error) =
 6652:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6653:                                                 $switchserver,$author_ok);
 6654:                     if ($imageurl) {
 6655:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6656:                     }
 6657:                     if ($error) {
 6658:                         &Apache::lonnet::logthis($error);
 6659:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6660:                     }
 6661:                 }
 6662:             }
 6663:             if (@allpos > 0) {
 6664:                 my $idx = 0;
 6665:                 foreach my $item (@allpos) {
 6666:                     if ($item ne '') {
 6667:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6668:                         if (ref($domconfig{$action}) eq 'HASH') {
 6669:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6670:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6671:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6672:                                         $changes{'textbooks'}{$item} = 1;
 6673:                                     }
 6674:                                 }
 6675:                             }
 6676:                         }
 6677:                         $idx ++;
 6678:                     }
 6679:                 }
 6680:             }
 6681:             if (ref($validationitemsref) eq 'ARRAY') {
 6682:                 foreach my $item (@{$validationitemsref}) {
 6683:                     if ($item eq 'fields') {
 6684:                         my @changed;
 6685:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 6686:                         if (@{$confhash{'validation'}{$item}} > 0) {
 6687:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 6688:                         }
 6689:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 6690:                             if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 6691:                                 @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 6692:                                                                               $domconfig{'requestcourses'}{'validation'}{$item});
 6693:                             } else {
 6694:                                 @changed = @{$confhash{'validation'}{$item}};
 6695:                             }
 6696:                         } else {
 6697:                             @changed = @{$confhash{'validation'}{$item}};
 6698:                         }
 6699:                         if (@changed) {
 6700:                             if ($confhash{'validation'}{$item}) {
 6701:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 6702:                             } else {
 6703:                                 $changes{'validation'}{$item} = &mt('None');
 6704:                             }
 6705:                         }
 6706:                     } else {
 6707:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 6708:                         if ($item eq 'markup') {
 6709:                             if ($env{'form.requestcourses_validation_'.$item}) {
 6710:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 6711:                             }
 6712:                         }
 6713:                         if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 6714:                             if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 6715:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 6716:                             }
 6717:                         } else {
 6718:                             if ($confhash{'validation'}{$item} ne '') {
 6719:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 6720:                             }
 6721:                         }
 6722:                     }
 6723:                 }
 6724:             }
 6725:             if ($env{'form.validationdc'}) {
 6726:                 my $newval = $env{'form.validationdc'};
 6727:                 my %domcoords = &get_active_dcs($dom);
 6728:                 if (exists($domcoords{$newval})) {
 6729:                     $confhash{'validation'}{'dc'} = $newval;
 6730:                 }
 6731:             }
 6732:             if (ref($confhash{'validation'}) eq 'HASH') {
 6733:                 if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 6734:                     if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 6735:                         unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 6736:                             if ($confhash{'validation'}{'dc'} eq '') {
 6737:                                 $changes{'validation'}{'dc'} = &mt('None');
 6738:                             } else {
 6739:                                 $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 6740:                             }
 6741:                         }
 6742:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 6743:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 6744:                     }
 6745:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 6746:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 6747:                 }
 6748:             } elsif (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 6749:                 if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 6750:                     $changes{'validation'}{'dc'} = &mt('None');
 6751:                 }
 6752:             }
 6753:         }
 6754:     } else {
 6755:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 6756:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 6757:     }
 6758:     foreach my $item (@usertools) {
 6759:         foreach my $type (@{$types},'default','_LC_adv') {
 6760:             my $unset; 
 6761:             if ($context eq 'requestcourses') {
 6762:                 $unset = '0';
 6763:                 if ($type eq '_LC_adv') {
 6764:                     $unset = '';
 6765:                 }
 6766:                 if ($confhash{$item}{$type} eq 'autolimit') {
 6767:                     $confhash{$item}{$type} .= '=';
 6768:                     unless ($limithash{$item}{$type} =~ /\D/) {
 6769:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 6770:                     }
 6771:                 }
 6772:             } elsif ($context eq 'requestauthor') {
 6773:                 $unset = '0';
 6774:                 if ($type eq '_LC_adv') {
 6775:                     $unset = '';
 6776:                 }
 6777:             } else {
 6778:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 6779:                     $confhash{$item}{$type} = 1;
 6780:                 } else {
 6781:                     $confhash{$item}{$type} = 0;
 6782:                 }
 6783:             }
 6784:             if (ref($domconfig{$action}) eq 'HASH') {
 6785:                 if ($action eq 'requestauthor') {
 6786:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 6787:                         $changes{$type} = 1;
 6788:                     }
 6789:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 6790:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 6791:                         $changes{$item}{$type} = 1;
 6792:                     }
 6793:                 } else {
 6794:                     if ($context eq 'requestcourses') {
 6795:                         if ($confhash{$item}{$type} ne $unset) {
 6796:                             $changes{$item}{$type} = 1;
 6797:                         }
 6798:                     } else {
 6799:                         if (!$confhash{$item}{$type}) {
 6800:                             $changes{$item}{$type} = 1;
 6801:                         }
 6802:                     }
 6803:                 }
 6804:             } else {
 6805:                 if ($context eq 'requestcourses') {
 6806:                     if ($confhash{$item}{$type} ne $unset) {
 6807:                         $changes{$item}{$type} = 1;
 6808:                     }
 6809:                 } elsif ($context eq 'requestauthor') {
 6810:                     if ($confhash{$type} ne $unset) {
 6811:                         $changes{$type} = 1;
 6812:                     }
 6813:                 } else {
 6814:                     if (!$confhash{$item}{$type}) {
 6815:                         $changes{$item}{$type} = 1;
 6816:                     }
 6817:                 }
 6818:             }
 6819:         }
 6820:     }
 6821:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6822:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 6823:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6824:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 6825:                     if (exists($confhash{'defaultquota'}{$key})) {
 6826:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 6827:                             $changes{'defaultquota'}{$key} = 1;
 6828:                         }
 6829:                     } else {
 6830:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 6831:                     }
 6832:                 }
 6833:             } else {
 6834:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 6835:                     if (exists($confhash{'defaultquota'}{$key})) {
 6836:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 6837:                             $changes{'defaultquota'}{$key} = 1;
 6838:                         }
 6839:                     } else {
 6840:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 6841:                     }
 6842:                 }
 6843:             }
 6844:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6845:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 6846:                     if (exists($confhash{'authorquota'}{$key})) {
 6847:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 6848:                             $changes{'authorquota'}{$key} = 1;
 6849:                         }
 6850:                     } else {
 6851:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 6852:                     }
 6853:                 }
 6854:             }
 6855:         }
 6856:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 6857:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 6858:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6859:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6860:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 6861:                             $changes{'defaultquota'}{$key} = 1;
 6862:                         }
 6863:                     } else {
 6864:                         if (!exists($domconfig{'quotas'}{$key})) {
 6865:                             $changes{'defaultquota'}{$key} = 1;
 6866:                         }
 6867:                     }
 6868:                 } else {
 6869:                     $changes{'defaultquota'}{$key} = 1;
 6870:                 }
 6871:             }
 6872:         }
 6873:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6874:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6875:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6876:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6877:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6878:                             $changes{'authorquota'}{$key} = 1;
 6879:                         }
 6880:                     } else {
 6881:                         $changes{'authorquota'}{$key} = 1;
 6882:                     }
 6883:                 } else {
 6884:                     $changes{'authorquota'}{$key} = 1;
 6885:                 }
 6886:             }
 6887:         }
 6888:     }
 6889: 
 6890:     if ($context eq 'requestauthor') {
 6891:         $domdefaults{'requestauthor'} = \%confhash;
 6892:     } else {
 6893:         foreach my $key (keys(%confhash)) {
 6894:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 6895:                 $domdefaults{$key} = $confhash{$key};
 6896:             }
 6897:         }
 6898:     }
 6899: 
 6900:     my %quotahash = (
 6901:                       $action => { %confhash }
 6902:                     );
 6903:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6904:                                              $dom);
 6905:     if ($putresult eq 'ok') {
 6906:         if (keys(%changes) > 0) {
 6907:             my $cachetime = 24*60*60;
 6908:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6909:             if (ref($lastactref) eq 'HASH') {
 6910:                 $lastactref->{'domdefaults'} = 1;
 6911:             }
 6912:             $resulttext = &mt('Changes made:').'<ul>';
 6913:             unless (($context eq 'requestcourses') ||
 6914:                     ($context eq 'requestauthor')) {
 6915:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6916:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6917:                     foreach my $type (@{$types},'default') {
 6918:                         if (defined($changes{'defaultquota'}{$type})) {
 6919:                             my $typetitle = $usertypes->{$type};
 6920:                             if ($type eq 'default') {
 6921:                                 $typetitle = $othertitle;
 6922:                             }
 6923:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6924:                         }
 6925:                     }
 6926:                     $resulttext .= '</ul></li>';
 6927:                 }
 6928:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6929:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 6930:                     foreach my $type (@{$types},'default') {
 6931:                         if (defined($changes{'authorquota'}{$type})) {
 6932:                             my $typetitle = $usertypes->{$type};
 6933:                             if ($type eq 'default') {
 6934:                                 $typetitle = $othertitle;
 6935:                             }
 6936:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6937:                         }
 6938:                     }
 6939:                     $resulttext .= '</ul></li>';
 6940:                 }
 6941:             }
 6942:             my %newenv;
 6943:             foreach my $item (@usertools) {
 6944:                 my (%haschgs,%inconf);
 6945:                 if ($context eq 'requestauthor') {
 6946:                     %haschgs = %changes;
 6947:                     %inconf = %confhash;
 6948:                 } else {
 6949:                     if (ref($changes{$item}) eq 'HASH') {
 6950:                         %haschgs = %{$changes{$item}};
 6951:                     }
 6952:                     if (ref($confhash{$item}) eq 'HASH') {
 6953:                         %inconf = %{$confhash{$item}};
 6954:                     }
 6955:                 }
 6956:                 if (keys(%haschgs) > 0) {
 6957:                     my $newacc = 
 6958:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6959:                                                           $env{'user.domain'},
 6960:                                                           $item,'reload',$context);
 6961:                     if (($context eq 'requestcourses') ||
 6962:                         ($context eq 'requestauthor')) {
 6963:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6964:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6965:                         }
 6966:                     } else {
 6967:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6968:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6969:                         }
 6970:                     }
 6971:                     unless ($context eq 'requestauthor') {
 6972:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6973:                     }
 6974:                     foreach my $type (@{$types},'default','_LC_adv') {
 6975:                         if ($haschgs{$type}) {
 6976:                             my $typetitle = $usertypes->{$type};
 6977:                             if ($type eq 'default') {
 6978:                                 $typetitle = $othertitle;
 6979:                             } elsif ($type eq '_LC_adv') {
 6980:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6981:                             }
 6982:                             if ($inconf{$type}) {
 6983:                                 if ($context eq 'requestcourses') {
 6984:                                     my $cond;
 6985:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6986:                                         if ($1 eq '') {
 6987:                                             $cond = &mt('(Automatic processing of any request).');
 6988:                                         } else {
 6989:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6990:                                         }
 6991:                                     } else { 
 6992:                                         $cond = $conditions{$inconf{$type}};
 6993:                                     }
 6994:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6995:                                 } elsif ($context eq 'requestauthor') {
 6996:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6997:                                                              $titles{$inconf{$type}},$typetitle);
 6998: 
 6999:                                 } else {
 7000:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7001:                                 }
 7002:                             } else {
 7003:                                 if ($type eq '_LC_adv') {
 7004:                                     if ($inconf{$type} eq '0') {
 7005:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7006:                                     } else { 
 7007:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7008:                                     }
 7009:                                 } else {
 7010:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7011:                                 }
 7012:                             }
 7013:                         }
 7014:                     }
 7015:                     unless ($context eq 'requestauthor') {
 7016:                         $resulttext .= '</ul></li>';
 7017:                     }
 7018:                 }
 7019:             }
 7020:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7021:                 if (ref($changes{'notify'}) eq 'HASH') {
 7022:                     if ($changes{'notify'}{'approval'}) {
 7023:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7024:                             if ($confhash{'notify'}{'approval'}) {
 7025:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7026:                             } else {
 7027:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7028:                             }
 7029:                         }
 7030:                     }
 7031:                 }
 7032:             }
 7033:             if ($action eq 'requestcourses') {
 7034:                 my @offon = ('off','on');
 7035:                 if ($changes{'uniquecode'}) {
 7036:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7037:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7038:                         $resulttext .= '<li>'.
 7039:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7040:                                        '</li>';
 7041:                     } else {
 7042:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7043:                                        '</li>';
 7044:                     }
 7045:                 }
 7046:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 7047:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 7048:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 7049:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 7050:                         my $coursetitle = $coursehash{'description'};
 7051:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 7052:                         $resulttext .= '<li>';
 7053:                         foreach my $item ('subject','title','author') {
 7054:                             my $name = $item.':';
 7055:                             $name =~ s/^(\w)/\U$1/;
 7056:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 7057:                         }
 7058:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7059:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 7060:                             $resulttext .= ' '.&mt('Image: [_1]',
 7061:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 7062:                                                ' alt="Textbook cover" />').'<br />';
 7063:                         }
 7064:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7065:                     }
 7066:                     $resulttext .= '</ul></li>';
 7067:                 }
 7068:                 if (ref($changes{'validation'}) eq 'HASH') {
 7069:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7070:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7071:                         foreach my $item (@{$validationitemsref}) {
 7072:                             if (exists($changes{'validation'}{$item})) {
 7073:                                 if ($item eq 'markup') {
 7074:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7075:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7076:                                 } else {
 7077:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7078:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7079:                                 }
 7080:                             }
 7081:                         }
 7082:                         if (exists($changes{'validation'}{'dc'})) {
 7083:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7084:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7085:                         }
 7086:                     }
 7087:                 }
 7088:             }
 7089:             $resulttext .= '</ul>';
 7090:             if (keys(%newenv)) {
 7091:                 &Apache::lonnet::appenv(\%newenv);
 7092:             }
 7093:         } else {
 7094:             if ($context eq 'requestcourses') {
 7095:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7096:             } elsif ($context eq 'requestauthor') {
 7097:                 $resulttext = &mt('No changes made to rights to request author space.');
 7098:             } else {
 7099:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7100:             }
 7101:         }
 7102:     } else {
 7103:         $resulttext = '<span class="LC_error">'.
 7104: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7105:     }
 7106:     if ($errors) {
 7107:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7108:                        '<ul>'.$errors.'</ul></p>';
 7109:     }
 7110:     return $resulttext;
 7111: }
 7112: 
 7113: sub process_textbook_image {
 7114:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 7115:     my $filename = $env{'form.'.$caller.'.filename'};
 7116:     my ($error,$url);
 7117:     my ($width,$height) = (50,50);
 7118:     if ($configuserok eq 'ok') {
 7119:         if ($switchserver) {
 7120:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7121:                          $switchserver);
 7122:         } elsif ($author_ok eq 'ok') {
 7123:             my ($result,$imageurl) =
 7124:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7125:                              "textbooks/$dom/$cnum/cover",$width,$height);
 7126:             if ($result eq 'ok') {
 7127:                 $url = $imageurl;
 7128:             } else {
 7129:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7130:             }
 7131:         } else {
 7132:             $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);
 7133:         }
 7134:     } else {
 7135:         $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);
 7136:     }
 7137:     return ($url,$error);
 7138: }
 7139: 
 7140: sub modify_autoenroll {
 7141:     my ($dom,$lastactref,%domconfig) = @_;
 7142:     my ($resulttext,%changes);
 7143:     my %currautoenroll;
 7144:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7145:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7146:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7147:         }
 7148:     }
 7149:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7150:     my %title = ( run => 'Auto-enrollment active',
 7151:                   sender => 'Sender for notification messages',
 7152:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7153:     my @offon = ('off','on');
 7154:     my $sender_uname = $env{'form.sender_uname'};
 7155:     my $sender_domain = $env{'form.sender_domain'};
 7156:     if ($sender_domain eq '') {
 7157:         $sender_uname = '';
 7158:     } elsif ($sender_uname eq '') {
 7159:         $sender_domain = '';
 7160:     }
 7161:     my $coowners = $env{'form.autoassign_coowners'};
 7162:     my %autoenrollhash =  (
 7163:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7164:                                        'sender_uname' => $sender_uname,
 7165:                                        'sender_domain' => $sender_domain,
 7166:                                        'co-owners' => $coowners,
 7167:                                 }
 7168:                      );
 7169:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7170:                                              $dom);
 7171:     if ($putresult eq 'ok') {
 7172:         if (exists($currautoenroll{'run'})) {
 7173:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7174:                  $changes{'run'} = 1;
 7175:              }
 7176:         } elsif ($autorun) {
 7177:             if ($env{'form.autoenroll_run'} ne '1') {
 7178:                  $changes{'run'} = 1;
 7179:             }
 7180:         }
 7181:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7182:             $changes{'sender'} = 1;
 7183:         }
 7184:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7185:             $changes{'sender'} = 1;
 7186:         }
 7187:         if ($currautoenroll{'co-owners'} ne '') {
 7188:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7189:                 $changes{'coowners'} = 1;
 7190:             }
 7191:         } elsif ($coowners) {
 7192:             $changes{'coowners'} = 1;
 7193:         }      
 7194:         if (keys(%changes) > 0) {
 7195:             $resulttext = &mt('Changes made:').'<ul>';
 7196:             if ($changes{'run'}) {
 7197:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7198:             }
 7199:             if ($changes{'sender'}) {
 7200:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7201:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7202:                 } else {
 7203:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7204:                 }
 7205:             }
 7206:             if ($changes{'coowners'}) {
 7207:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7208:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7209:                 if (ref($lastactref) eq 'HASH') {
 7210:                     $lastactref->{'domainconfig'} = 1;
 7211:                 }
 7212:             }
 7213:             $resulttext .= '</ul>';
 7214:         } else {
 7215:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7216:         }
 7217:     } else {
 7218:         $resulttext = '<span class="LC_error">'.
 7219: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7220:     }
 7221:     return $resulttext;
 7222: }
 7223: 
 7224: sub modify_autoupdate {
 7225:     my ($dom,%domconfig) = @_;
 7226:     my ($resulttext,%currautoupdate,%fields,%changes);
 7227:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7228:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7229:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7230:         }
 7231:     }
 7232:     my @offon = ('off','on');
 7233:     my %title = &Apache::lonlocal::texthash (
 7234:                    run => 'Auto-update:',
 7235:                    classlists => 'Updates to user information in classlists?'
 7236:                 );
 7237:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7238:     my %fieldtitles = &Apache::lonlocal::texthash (
 7239:                         id => 'Student/Employee ID',
 7240:                         permanentemail => 'E-mail address',
 7241:                         lastname => 'Last Name',
 7242:                         firstname => 'First Name',
 7243:                         middlename => 'Middle Name',
 7244:                         generation => 'Generation',
 7245:                       );
 7246:     $othertitle = &mt('All users');
 7247:     if (keys(%{$usertypes}) >  0) {
 7248:         $othertitle = &mt('Other users');
 7249:     }
 7250:     foreach my $key (keys(%env)) {
 7251:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7252:             my ($usertype,$item) = ($1,$2);
 7253:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7254:                 if ($usertype eq 'default') {   
 7255:                     push(@{$fields{$1}},$2);
 7256:                 } elsif (ref($types) eq 'ARRAY') {
 7257:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7258:                         push(@{$fields{$1}},$2);
 7259:                     }
 7260:                 }
 7261:             }
 7262:         }
 7263:     }
 7264:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7265:     @lockablenames = sort(@lockablenames);
 7266:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7267:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7268:         if (@changed) {
 7269:             $changes{'lockablenames'} = 1;
 7270:         }
 7271:     } else {
 7272:         if (@lockablenames) {
 7273:             $changes{'lockablenames'} = 1;
 7274:         }
 7275:     }
 7276:     my %updatehash = (
 7277:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7278:                                       classlists => $env{'form.classlists'},
 7279:                                       fields => {%fields},
 7280:                                       lockablenames => \@lockablenames,
 7281:                                     }
 7282:                      );
 7283:     foreach my $key (keys(%currautoupdate)) {
 7284:         if (($key eq 'run') || ($key eq 'classlists')) {
 7285:             if (exists($updatehash{autoupdate}{$key})) {
 7286:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7287:                     $changes{$key} = 1;
 7288:                 }
 7289:             }
 7290:         } elsif ($key eq 'fields') {
 7291:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7292:                 foreach my $item (@{$types},'default') {
 7293:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7294:                         my $change = 0;
 7295:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7296:                             if (!exists($fields{$item})) {
 7297:                                 $change = 1;
 7298:                                 last;
 7299:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7300:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7301:                                     $change = 1;
 7302:                                     last;
 7303:                                 }
 7304:                             }
 7305:                         }
 7306:                         if ($change) {
 7307:                             push(@{$changes{$key}},$item);
 7308:                         }
 7309:                     } 
 7310:                 }
 7311:             }
 7312:         } elsif ($key eq 'lockablenames') {
 7313:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7314:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7315:                 if (@changed) {
 7316:                     $changes{'lockablenames'} = 1;
 7317:                 }
 7318:             } else {
 7319:                 if (@lockablenames) {
 7320:                     $changes{'lockablenames'} = 1;
 7321:                 }
 7322:             }
 7323:         }
 7324:     }
 7325:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7326:         if (@lockablenames) {
 7327:             $changes{'lockablenames'} = 1;
 7328:         }
 7329:     }
 7330:     foreach my $item (@{$types},'default') {
 7331:         if (defined($fields{$item})) {
 7332:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7333:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7334:                     my $change = 0;
 7335:                     if (ref($fields{$item}) eq 'ARRAY') {
 7336:                         foreach my $type (@{$fields{$item}}) {
 7337:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7338:                                 $change = 1;
 7339:                                 last;
 7340:                             }
 7341:                         }
 7342:                     }
 7343:                     if ($change) {
 7344:                         push(@{$changes{'fields'}},$item);
 7345:                     }
 7346:                 } else {
 7347:                     push(@{$changes{'fields'}},$item);
 7348:                 }
 7349:             } else {
 7350:                 push(@{$changes{'fields'}},$item);
 7351:             }
 7352:         }
 7353:     }
 7354:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7355:                                              $dom);
 7356:     if ($putresult eq 'ok') {
 7357:         if (keys(%changes) > 0) {
 7358:             $resulttext = &mt('Changes made:').'<ul>';
 7359:             foreach my $key (sort(keys(%changes))) {
 7360:                 if ($key eq 'lockablenames') {
 7361:                     $resulttext .= '<li>';
 7362:                     if (@lockablenames) {
 7363:                         $usertypes->{'default'} = $othertitle;
 7364:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7365:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7366:                     } else {
 7367:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7368:                     }
 7369:                     $resulttext .= '</li>';
 7370:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7371:                     foreach my $item (@{$changes{$key}}) {
 7372:                         my @newvalues;
 7373:                         foreach my $type (@{$fields{$item}}) {
 7374:                             push(@newvalues,$fieldtitles{$type});
 7375:                         }
 7376:                         my $newvaluestr;
 7377:                         if (@newvalues > 0) {
 7378:                             $newvaluestr = join(', ',@newvalues);
 7379:                         } else {
 7380:                             $newvaluestr = &mt('none');
 7381:                         }
 7382:                         if ($item eq 'default') {
 7383:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7384:                         } else {
 7385:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7386:                         }
 7387:                     }
 7388:                 } else {
 7389:                     my $newvalue;
 7390:                     if ($key eq 'run') {
 7391:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7392:                     } else {
 7393:                         $newvalue = $offon[$env{'form.'.$key}];
 7394:                     }
 7395:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7396:                 }
 7397:             }
 7398:             $resulttext .= '</ul>';
 7399:         } else {
 7400:             $resulttext = &mt('No changes made to autoupdates');
 7401:         }
 7402:     } else {
 7403:         $resulttext = '<span class="LC_error">'.
 7404: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7405:     }
 7406:     return $resulttext;
 7407: }
 7408: 
 7409: sub modify_autocreate {
 7410:     my ($dom,%domconfig) = @_;
 7411:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7412:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7413:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7414:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7415:         }
 7416:     }
 7417:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7418:                  req => 'Auto-creation of validated requests for official courses',
 7419:                  xmldc => 'Identity of course creator of courses from XML files',
 7420:                );
 7421:     my @types = ('xml','req');
 7422:     foreach my $item (@types) {
 7423:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7424:         $newvals{$item} =~ s/\D//g;
 7425:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7426:     }
 7427:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7428:     my %domcoords = &get_active_dcs($dom);
 7429:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7430:         $newvals{'xmldc'} = '';
 7431:     } 
 7432:     %autocreatehash =  (
 7433:                         autocreate => { xml => $newvals{'xml'},
 7434:                                         req => $newvals{'req'},
 7435:                                       }
 7436:                        );
 7437:     if ($newvals{'xmldc'} ne '') {
 7438:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7439:     }
 7440:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7441:                                              $dom);
 7442:     if ($putresult eq 'ok') {
 7443:         my @items = @types;
 7444:         if ($newvals{'xml'}) {
 7445:             push(@items,'xmldc');
 7446:         }
 7447:         foreach my $item (@items) {
 7448:             if (exists($currautocreate{$item})) {
 7449:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7450:                     $changes{$item} = 1;
 7451:                 }
 7452:             } elsif ($newvals{$item}) {
 7453:                 $changes{$item} = 1;
 7454:             }
 7455:         }
 7456:         if (keys(%changes) > 0) {
 7457:             my @offon = ('off','on'); 
 7458:             $resulttext = &mt('Changes made:').'<ul>';
 7459:             foreach my $item (@types) {
 7460:                 if ($changes{$item}) {
 7461:                     my $newtxt = $offon[$newvals{$item}];
 7462:                     $resulttext .= '<li>'.
 7463:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7464:                                        '<b>','</b>').
 7465:                                    '</li>';
 7466:                 }
 7467:             }
 7468:             if ($changes{'xmldc'}) {
 7469:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7470:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7471:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7472:             }
 7473:             $resulttext .= '</ul>';
 7474:         } else {
 7475:             $resulttext = &mt('No changes made to auto-creation settings');
 7476:         }
 7477:     } else {
 7478:         $resulttext = '<span class="LC_error">'.
 7479:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7480:     }
 7481:     return $resulttext;
 7482: }
 7483: 
 7484: sub modify_directorysrch {
 7485:     my ($dom,%domconfig) = @_;
 7486:     my ($resulttext,%changes);
 7487:     my %currdirsrch;
 7488:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7489:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7490:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7491:         }
 7492:     }
 7493:     my %title = ( available => 'Directory search available',
 7494:                   localonly => 'Other domains can search',
 7495:                   searchby => 'Search types',
 7496:                   searchtypes => 'Search latitude');
 7497:     my @offon = ('off','on');
 7498:     my @otherdoms = ('Yes','No');
 7499: 
 7500:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7501:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7502:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7503: 
 7504:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7505:     if (keys(%{$usertypes}) == 0) {
 7506:         @cansearch = ('default');
 7507:     } else {
 7508:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7509:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7510:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7511:                     push(@{$changes{'cansearch'}},$type);
 7512:                 }
 7513:             }
 7514:             foreach my $type (@cansearch) {
 7515:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7516:                     push(@{$changes{'cansearch'}},$type);
 7517:                 }
 7518:             }
 7519:         } else {
 7520:             push(@{$changes{'cansearch'}},@cansearch);
 7521:         }
 7522:     }
 7523: 
 7524:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7525:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7526:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7527:                 push(@{$changes{'searchby'}},$by);
 7528:             }
 7529:         }
 7530:         foreach my $by (@searchby) {
 7531:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7532:                 push(@{$changes{'searchby'}},$by);
 7533:             }
 7534:         }
 7535:     } else {
 7536:         push(@{$changes{'searchby'}},@searchby);
 7537:     }
 7538: 
 7539:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7540:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7541:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7542:                 push(@{$changes{'searchtypes'}},$type);
 7543:             }
 7544:         }
 7545:         foreach my $type (@searchtypes) {
 7546:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7547:                 push(@{$changes{'searchtypes'}},$type);
 7548:             }
 7549:         }
 7550:     } else {
 7551:         if (exists($currdirsrch{'searchtypes'})) {
 7552:             foreach my $type (@searchtypes) {  
 7553:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7554:                     push(@{$changes{'searchtypes'}},$type);
 7555:                 }
 7556:             }
 7557:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7558:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7559:             }   
 7560:         } else {
 7561:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7562:         }
 7563:     }
 7564: 
 7565:     my %dirsrch_hash =  (
 7566:             directorysrch => { available => $env{'form.dirsrch_available'},
 7567:                                cansearch => \@cansearch,
 7568:                                localonly => $env{'form.dirsrch_localonly'},
 7569:                                searchby => \@searchby,
 7570:                                searchtypes => \@searchtypes,
 7571:                              }
 7572:             );
 7573:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7574:                                              $dom);
 7575:     if ($putresult eq 'ok') {
 7576:         if (exists($currdirsrch{'available'})) {
 7577:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7578:                  $changes{'available'} = 1;
 7579:              }
 7580:         } else {
 7581:             if ($env{'form.dirsrch_available'} eq '1') {
 7582:                 $changes{'available'} = 1;
 7583:             }
 7584:         }
 7585:         if (exists($currdirsrch{'localonly'})) {
 7586:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7587:                  $changes{'localonly'} = 1;
 7588:              }
 7589:         } else {
 7590:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7591:                 $changes{'localonly'} = 1;
 7592:             }
 7593:         }
 7594:         if (keys(%changes) > 0) {
 7595:             $resulttext = &mt('Changes made:').'<ul>';
 7596:             if ($changes{'available'}) {
 7597:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7598:             }
 7599:             if ($changes{'localonly'}) {
 7600:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7601:             }
 7602: 
 7603:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7604:                 my $chgtext;
 7605:                 if (ref($usertypes) eq 'HASH') {
 7606:                     if (keys(%{$usertypes}) > 0) {
 7607:                         foreach my $type (@{$types}) {
 7608:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7609:                                 $chgtext .= $usertypes->{$type}.'; ';
 7610:                             }
 7611:                         }
 7612:                         if (grep(/^default$/,@cansearch)) {
 7613:                             $chgtext .= $othertitle;
 7614:                         } else {
 7615:                             $chgtext =~ s/\; $//;
 7616:                         }
 7617:                         $resulttext .=
 7618:                             '<li>'.
 7619:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7620:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7621:                             '</li>';
 7622:                     }
 7623:                 }
 7624:             }
 7625:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7626:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7627:                 my $chgtext;
 7628:                 foreach my $type (@{$titleorder}) {
 7629:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7630:                         if (defined($searchtitles->{$type})) {
 7631:                             $chgtext .= $searchtitles->{$type}.'; ';
 7632:                         }
 7633:                     }
 7634:                 }
 7635:                 $chgtext =~ s/\; $//;
 7636:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7637:             }
 7638:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7639:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7640:                 my $chgtext;
 7641:                 foreach my $type (@{$srchtypeorder}) {
 7642:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7643:                         if (defined($srchtypes_desc->{$type})) {
 7644:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7645:                         }
 7646:                     }
 7647:                 }
 7648:                 $chgtext =~ s/\; $//;
 7649:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7650:             }
 7651:             $resulttext .= '</ul>';
 7652:         } else {
 7653:             $resulttext = &mt('No changes made to institution directory search settings');
 7654:         }
 7655:     } else {
 7656:         $resulttext = '<span class="LC_error">'.
 7657:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7658:     }
 7659:     return $resulttext;
 7660: }
 7661: 
 7662: sub modify_contacts {
 7663:     my ($dom,$lastactref,%domconfig) = @_;
 7664:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7665:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7666:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7667:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7668:         }
 7669:     }
 7670:     my (%others,%to,%bcc);
 7671:     my @contacts = ('supportemail','adminemail');
 7672:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7673:                     'requestsmail','updatesmail','idconflictsmail');
 7674:     my @toggles = ('reporterrors','reportupdates');
 7675:     foreach my $type (@mailings) {
 7676:         @{$newsetting{$type}} = 
 7677:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7678:         foreach my $item (@contacts) {
 7679:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7680:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7681:             } else {
 7682:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7683:             }
 7684:         }  
 7685:         $others{$type} = $env{'form.'.$type.'_others'};
 7686:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7687:         if ($type eq 'helpdeskmail') {
 7688:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 7689:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 7690:         }
 7691:     }
 7692:     foreach my $item (@contacts) {
 7693:         $to{$item} = $env{'form.'.$item};
 7694:         $contacts_hash{'contacts'}{$item} = $to{$item};
 7695:     }
 7696:     foreach my $item (@toggles) {
 7697:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 7698:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 7699:         }
 7700:     }
 7701:     if (keys(%currsetting) > 0) {
 7702:         foreach my $item (@contacts) {
 7703:             if ($to{$item} ne $currsetting{$item}) {
 7704:                 $changes{$item} = 1;
 7705:             }
 7706:         }
 7707:         foreach my $type (@mailings) {
 7708:             foreach my $item (@contacts) {
 7709:                 if (ref($currsetting{$type}) eq 'HASH') {
 7710:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 7711:                         push(@{$changes{$type}},$item);
 7712:                     }
 7713:                 } else {
 7714:                     push(@{$changes{$type}},@{$newsetting{$type}});
 7715:                 }
 7716:             }
 7717:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 7718:                 push(@{$changes{$type}},'others');
 7719:             }
 7720:             if ($type eq 'helpdeskmail') {   
 7721:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 7722:                     push(@{$changes{$type}},'bcc'); 
 7723:                 }
 7724:             }
 7725:         }
 7726:     } else {
 7727:         my %default;
 7728:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 7729:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 7730:         $default{'errormail'} = 'adminemail';
 7731:         $default{'packagesmail'} = 'adminemail';
 7732:         $default{'helpdeskmail'} = 'supportemail';
 7733:         $default{'lonstatusmail'} = 'adminemail';
 7734:         $default{'requestsmail'} = 'adminemail';
 7735:         $default{'updatesmail'} = 'adminemail';
 7736:         foreach my $item (@contacts) {
 7737:            if ($to{$item} ne $default{$item}) {
 7738:               $changes{$item} = 1;
 7739:            }
 7740:         }
 7741:         foreach my $type (@mailings) {
 7742:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 7743:                
 7744:                 push(@{$changes{$type}},@{$newsetting{$type}});
 7745:             }
 7746:             if ($others{$type} ne '') {
 7747:                 push(@{$changes{$type}},'others');
 7748:             }
 7749:             if ($type eq 'helpdeskmail') {
 7750:                 if ($bcc{$type} ne '') {
 7751:                     push(@{$changes{$type}},'bcc');
 7752:                 }
 7753:             }
 7754:         }
 7755:     }
 7756:     foreach my $item (@toggles) {
 7757:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 7758:             $changes{$item} = 1;
 7759:         } elsif ((!$env{'form.'.$item}) &&
 7760:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 7761:             $changes{$item} = 1;
 7762:         }
 7763:     }
 7764:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 7765:                                              $dom);
 7766:     if ($putresult eq 'ok') {
 7767:         if (keys(%changes) > 0) {
 7768:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7769:             if (ref($lastactref) eq 'HASH') {
 7770:                 $lastactref->{'domainconfig'} = 1;
 7771:             }
 7772:             my ($titles,$short_titles)  = &contact_titles();
 7773:             $resulttext = &mt('Changes made:').'<ul>';
 7774:             foreach my $item (@contacts) {
 7775:                 if ($changes{$item}) {
 7776:                     $resulttext .= '<li>'.$titles->{$item}.
 7777:                                     &mt(' set to: ').
 7778:                                     '<span class="LC_cusr_emph">'.
 7779:                                     $to{$item}.'</span></li>';
 7780:                 }
 7781:             }
 7782:             foreach my $type (@mailings) {
 7783:                 if (ref($changes{$type}) eq 'ARRAY') {
 7784:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 7785:                     my @text;
 7786:                     foreach my $item (@{$newsetting{$type}}) {
 7787:                         push(@text,$short_titles->{$item});
 7788:                     }
 7789:                     if ($others{$type} ne '') {
 7790:                         push(@text,$others{$type});
 7791:                     }
 7792:                     $resulttext .= '<span class="LC_cusr_emph">'.
 7793:                                    join(', ',@text).'</span>';
 7794:                     if ($type eq 'helpdeskmail') {
 7795:                         if ($bcc{$type} ne '') {
 7796:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 7797:                         }
 7798:                     }
 7799:                     $resulttext .= '</li>';
 7800:                 }
 7801:             }
 7802:             my @offon = ('off','on');
 7803:             if ($changes{'reporterrors'}) {
 7804:                 $resulttext .= '<li>'.
 7805:                                &mt('E-mail error reports to [_1] set to "'.
 7806:                                    $offon[$env{'form.reporterrors'}].'".',
 7807:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7808:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 7809:                                '</li>';
 7810:             }
 7811:             if ($changes{'reportupdates'}) {
 7812:                 $resulttext .= '<li>'.
 7813:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 7814:                                     $offon[$env{'form.reportupdates'}].'".',
 7815:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7816:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 7817:                                 '</li>';
 7818:             }
 7819:             $resulttext .= '</ul>';
 7820:         } else {
 7821:             $resulttext = &mt('No changes made to contact information');
 7822:         }
 7823:     } else {
 7824:         $resulttext = '<span class="LC_error">'.
 7825:             &mt('An error occurred: [_1].',$putresult).'</span>';
 7826:     }
 7827:     return $resulttext;
 7828: }
 7829: 
 7830: sub modify_usercreation {
 7831:     my ($dom,%domconfig) = @_;
 7832:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 7833:     my $warningmsg;
 7834:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7835:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7836:             if ($key eq 'cancreate') {
 7837:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7838:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7839:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7840:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 7841:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7842:                         } else {
 7843:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7844:                         }
 7845:                     }
 7846:                 }
 7847:             } elsif ($key eq 'email_rule') {
 7848:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7849:             } else {
 7850:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7851:             }
 7852:         }
 7853:     }
 7854:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 7855:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 7856:     my @contexts = ('author','course','requestcrs');
 7857:     foreach my $item(@contexts) {
 7858:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 7859:     }
 7860:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7861:         foreach my $item (@contexts) {
 7862:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 7863:                 push(@{$changes{'cancreate'}},$item);
 7864:             }
 7865:         }
 7866:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 7867:         foreach my $item (@contexts) {
 7868:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 7869:                 if ($cancreate{$item} ne 'any') {
 7870:                     push(@{$changes{'cancreate'}},$item);
 7871:                 }
 7872:             } else {
 7873:                 if ($cancreate{$item} ne 'none') {
 7874:                     push(@{$changes{'cancreate'}},$item);
 7875:                 }
 7876:             }
 7877:         }
 7878:     } else {
 7879:         foreach my $item (@contexts)  {
 7880:             push(@{$changes{'cancreate'}},$item);
 7881:         }
 7882:     }
 7883: 
 7884:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 7885:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 7886:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 7887:                 push(@{$changes{'username_rule'}},$type);
 7888:             }
 7889:         }
 7890:         foreach my $type (@username_rule) {
 7891:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 7892:                 push(@{$changes{'username_rule'}},$type);
 7893:             }
 7894:         }
 7895:     } else {
 7896:         push(@{$changes{'username_rule'}},@username_rule);
 7897:     }
 7898: 
 7899:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 7900:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7901:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7902:                 push(@{$changes{'id_rule'}},$type);
 7903:             }
 7904:         }
 7905:         foreach my $type (@id_rule) {
 7906:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7907:                 push(@{$changes{'id_rule'}},$type);
 7908:             }
 7909:         }
 7910:     } else {
 7911:         push(@{$changes{'id_rule'}},@id_rule);
 7912:     }
 7913: 
 7914:     my @authen_contexts = ('author','course','domain');
 7915:     my @authtypes = ('int','krb4','krb5','loc');
 7916:     my %authhash;
 7917:     foreach my $item (@authen_contexts) {
 7918:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7919:         foreach my $auth (@authtypes) {
 7920:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7921:                 $authhash{$item}{$auth} = 1;
 7922:             } else {
 7923:                 $authhash{$item}{$auth} = 0;
 7924:             }
 7925:         }
 7926:     }
 7927:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7928:         foreach my $item (@authen_contexts) {
 7929:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7930:                 foreach my $auth (@authtypes) {
 7931:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7932:                         push(@{$changes{'authtypes'}},$item);
 7933:                         last;
 7934:                     }
 7935:                 }
 7936:             }
 7937:         }
 7938:     } else {
 7939:         foreach my $item (@authen_contexts) {
 7940:             push(@{$changes{'authtypes'}},$item);
 7941:         }
 7942:     }
 7943: 
 7944:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 7945:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 7946:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 7947:     $save_usercreate{'id_rule'} = \@id_rule;
 7948:     $save_usercreate{'username_rule'} = \@username_rule,
 7949:     $save_usercreate{'authtypes'} = \%authhash;
 7950: 
 7951:     my %usercreation_hash =  (
 7952:         usercreation     => \%save_usercreate,
 7953:     );
 7954: 
 7955:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7956:                                              $dom);
 7957: 
 7958:     if ($putresult eq 'ok') {
 7959:         if (keys(%changes) > 0) {
 7960:             $resulttext = &mt('Changes made:').'<ul>';
 7961:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7962:                 my %lt = &usercreation_types();
 7963:                 foreach my $type (@{$changes{'cancreate'}}) {
 7964:                     my $chgtext = $lt{$type}.', ';
 7965:                     if ($cancreate{$type} eq 'none') {
 7966:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7967:                     } elsif ($cancreate{$type} eq 'any') {
 7968:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7969:                     } elsif ($cancreate{$type} eq 'official') {
 7970:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7971:                     } elsif ($cancreate{$type} eq 'unofficial') {
 7972:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7973:                     }
 7974:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7975:                 }
 7976:             }
 7977:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7978:                 my ($rules,$ruleorder) = 
 7979:                     &Apache::lonnet::inst_userrules($dom,'username');
 7980:                 my $chgtext = '<ul>';
 7981:                 foreach my $type (@username_rule) {
 7982:                     if (ref($rules->{$type}) eq 'HASH') {
 7983:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7984:                     }
 7985:                 }
 7986:                 $chgtext .= '</ul>';
 7987:                 if (@username_rule > 0) {
 7988:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7989:                 } else {
 7990:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7991:                 }
 7992:             }
 7993:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7994:                 my ($idrules,$idruleorder) = 
 7995:                     &Apache::lonnet::inst_userrules($dom,'id');
 7996:                 my $chgtext = '<ul>';
 7997:                 foreach my $type (@id_rule) {
 7998:                     if (ref($idrules->{$type}) eq 'HASH') {
 7999:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8000:                     }
 8001:                 }
 8002:                 $chgtext .= '</ul>';
 8003:                 if (@id_rule > 0) {
 8004:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8005:                 } else {
 8006:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8007:                 }
 8008:             }
 8009:             my %authname = &authtype_names();
 8010:             my %context_title = &context_names();
 8011:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8012:                 my $chgtext = '<ul>';
 8013:                 foreach my $type (@{$changes{'authtypes'}}) {
 8014:                     my @allowed;
 8015:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8016:                     foreach my $auth (@authtypes) {
 8017:                         if ($authhash{$type}{$auth}) {
 8018:                             push(@allowed,$authname{$auth});
 8019:                         }
 8020:                     }
 8021:                     if (@allowed > 0) {
 8022:                         $chgtext .= join(', ',@allowed).'</li>';
 8023:                     } else {
 8024:                         $chgtext .= &mt('none').'</li>';
 8025:                     }
 8026:                 }
 8027:                 $chgtext .= '</ul>';
 8028:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8029:                 $resulttext .= '</li>';
 8030:             }
 8031:             $resulttext .= '</ul>';
 8032:         } else {
 8033:             $resulttext = &mt('No changes made to user creation settings');
 8034:         }
 8035:     } else {
 8036:         $resulttext = '<span class="LC_error">'.
 8037:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8038:     }
 8039:     if ($warningmsg ne '') {
 8040:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8041:     }
 8042:     return $resulttext;
 8043: }
 8044: 
 8045: sub modify_selfcreation {
 8046:     my ($dom,%domconfig) = @_;
 8047:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8048:     my (%save_usercreate,%save_usermodify);
 8049:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8050:     if (ref($types) eq 'ARRAY') {
 8051:         $usertypes->{'default'} = $othertitle;
 8052:         push(@{$types},'default');
 8053:     }
 8054: #
 8055: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8056: #
 8057:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8058:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8059:             if ($key eq 'cancreate') {
 8060:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8061:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8062:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8063:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8064:                             ($item eq 'emailusername') || ($item eq 'notify')) {
 8065:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8066:                         } else {
 8067:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8068:                         }
 8069:                     }
 8070:                 }
 8071:             } elsif ($key eq 'email_rule') {
 8072:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8073:             } else {
 8074:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8075:             }
 8076:         }
 8077:     }
 8078: #
 8079: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8080: #
 8081:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8082:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8083:             if ($key eq 'selfcreate') {
 8084:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8085:             } else {
 8086:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8087:             }
 8088:         }
 8089:     }
 8090: 
 8091:     my @contexts = ('selfcreate');
 8092:     @{$cancreate{'selfcreate'}} = ();
 8093:     %{$cancreate{'emailusername'}} = ();
 8094:     @{$cancreate{'statustocreate'}} = ();
 8095:     my %selfcreatetypes = (
 8096:                              sso   => 'users authenticated by institutional single sign on',
 8097:                              login => 'users authenticated by institutional log-in',
 8098:                              email => 'users who provide a valid e-mail address for use as username (automatic creation)',
 8099:                              emailapproval => 'users who provide a valid e-mail address for use as username (queued for Domain Coordinator review)',
 8100:                           );
 8101: #
 8102: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8103: # is permitted.
 8104: #
 8105:     foreach my $item ('login','sso','email') {
 8106:         if ($item eq 'email') {
 8107:             if ($env{'form.cancreate_email'} eq 'email') {
 8108:                 push(@{$cancreate{'selfcreate'}},'email');
 8109:             } elsif ($env{'form.cancreate_email'} eq 'emailapproval') {
 8110:                 push(@{$cancreate{'selfcreate'}},'emailapproval');
 8111:             }
 8112:         } else {
 8113:             if ($env{'form.cancreate_'.$item}) {
 8114:                 push(@{$cancreate{'selfcreate'}},$item);
 8115:             }
 8116:         }
 8117:     }
 8118:     my (@email_rule,%userinfo,%savecaptcha);
 8119:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8120: #
 8121: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8122: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8123: #
 8124:     if (($env{'form.cancreate_email'} eq 'email') || ($env{'form.cancreate_email'} eq 'emailapproval')) {
 8125:         push(@contexts,'emailusername');
 8126:         if (ref($types) eq 'ARRAY') {
 8127:             foreach my $type (@{$types}) {
 8128:                 if (ref($infofields) eq 'ARRAY') {
 8129:                     foreach my $field (@{$infofields}) {
 8130:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8131:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8132:                         }
 8133:                     }
 8134:                 }
 8135:             }
 8136:         }
 8137: #
 8138: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8139: # queued requests for self-creation of account using e-mail address as username
 8140: #
 8141: 
 8142:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8143:         @approvalnotify = sort(@approvalnotify);
 8144:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8145:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8146:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8147:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8148:                     push(@{$changes{'cancreate'}},'notify');
 8149:                 }
 8150:             } else {
 8151:                 if ($cancreate{'notify'}{'approval'}) {
 8152:                     push(@{$changes{'cancreate'}},'notify');
 8153:                 }
 8154:             }
 8155:         } elsif ($cancreate{'notify'}{'approval'}) {
 8156:             push(@{$changes{'cancreate'}},'notify');
 8157:         }
 8158: 
 8159: #
 8160: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8161: #
 8162:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8163:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8164:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8165:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8166:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8167:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8168:                         push(@{$changes{'email_rule'}},$type);
 8169:                     }
 8170:                 }
 8171:             }
 8172:             if (@email_rule > 0) {
 8173:                 foreach my $type (@email_rule) {
 8174:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8175:                         push(@{$changes{'email_rule'}},$type);
 8176:                     }
 8177:                 }
 8178:             }
 8179:         } elsif (@email_rule > 0) {
 8180:             push(@{$changes{'email_rule'}},@email_rule);
 8181:         }
 8182:     }
 8183: #  
 8184: # Check if domain default is set appropriately, if selef-creation of accounts is to be available for
 8185: # institutional log-in.
 8186: #
 8187:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8188:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8189:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8190:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8191:             $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.').' '.
 8192:                           &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.');
 8193:         }
 8194:     }
 8195:     my @fields = ('lastname','firstname','middlename','generation',
 8196:                   'permanentemail','id');
 8197:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8198: #
 8199: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8200: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8201: # may self-create accounts 
 8202: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8203: # which the user may supply, if institutional data is unavailable.
 8204: #
 8205:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8206:         if (ref($types) eq 'ARRAY') {
 8207:             if (@{$types} > 1) {
 8208:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8209:                 push(@contexts,'statustocreate');
 8210:             } else {
 8211:                 undef($cancreate{'statustocreate'});
 8212:             } 
 8213:             foreach my $type (@{$types}) {
 8214:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8215:                 foreach my $field (@fields) {
 8216:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8217:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8218:                     } else {
 8219:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8220:                     }
 8221:                 }
 8222:             }
 8223:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8224:                 foreach my $type (@{$types}) {
 8225:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8226:                         foreach my $field (@fields) {
 8227:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8228:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8229:                                 push(@{$changes{'selfcreate'}},$type);
 8230:                                 last;
 8231:                             }
 8232:                         }
 8233:                     }
 8234:                 }
 8235:             } else {
 8236:                 foreach my $type (@{$types}) {
 8237:                     push(@{$changes{'selfcreate'}},$type);
 8238:                 }
 8239:             }
 8240:         }
 8241:     }
 8242:     foreach my $item (@contexts) {
 8243:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8244:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8245:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8246:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8247:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8248:                             push(@{$changes{'cancreate'}},$item);
 8249:                         }
 8250:                     }
 8251:                 }
 8252:             }
 8253:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8254:                 foreach my $type (@{$cancreate{$item}}) {
 8255:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8256:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8257:                             push(@{$changes{'cancreate'}},$item);
 8258:                         }
 8259:                     }
 8260:                 }
 8261:             }
 8262:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8263:             if (ref($cancreate{$item}) eq 'HASH') {
 8264:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8265:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8266:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8267:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8268:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8269:                                     push(@{$changes{'cancreate'}},$item);
 8270:                                 }
 8271:                             }
 8272:                         }
 8273:                     } else {
 8274:                         if (!$cancreate{$item}{$curr}) {
 8275:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8276:                                 push(@{$changes{'cancreate'}},$item);
 8277:                             }
 8278:                         }
 8279:                     }
 8280:                 }
 8281:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8282:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8283:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8284:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8285:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8286:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8287:                                         push(@{$changes{'cancreate'}},$item);
 8288:                                     }
 8289:                                 }
 8290:                             } else {
 8291:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8292:                                     push(@{$changes{'cancreate'}},$item);
 8293:                                 }
 8294:                             }
 8295:                         }
 8296:                     } else {
 8297:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8298:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8299:                                 push(@{$changes{'cancreate'}},$item);
 8300:                             }
 8301:                         }
 8302:                     }
 8303:                 }
 8304:             }
 8305:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8306:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8307:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8308:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8309:                         push(@{$changes{'cancreate'}},$item);
 8310:                     }
 8311:                 }
 8312:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8313:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8314:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8315:                         push(@{$changes{'cancreate'}},$item);
 8316:                     }
 8317:                 }
 8318:             }
 8319:         } elsif ($item eq 'emailusername') {
 8320:             if (ref($cancreate{$item}) eq 'HASH') {
 8321:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8322:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8323:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8324:                             if ($cancreate{$item}{$type}{$field}) {
 8325:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8326:                                     push(@{$changes{'cancreate'}},$item);
 8327:                                 }
 8328:                                 last;
 8329:                             }
 8330:                         }
 8331:                     }
 8332:                 }
 8333:             }
 8334:         }
 8335:     }
 8336: #
 8337: # Populate %save_usercreate hash with updates to self-creation configuration.
 8338: #
 8339:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8340:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8341:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8342:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8343:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8344:     }
 8345:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8346:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8347:     }
 8348:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8349:     $save_usercreate{'emailrule'} = \@email_rule;
 8350: 
 8351:     my %userconfig_hash = (
 8352:             usercreation     => \%save_usercreate,
 8353:             usermodification => \%save_usermodify,
 8354:     );
 8355:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8356:                                              $dom);
 8357: #
 8358: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8359: #
 8360:     if ($putresult eq 'ok') {
 8361:         if (keys(%changes) > 0) {
 8362:             $resulttext = &mt('Changes made:').'<ul>';
 8363:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8364:                 my %lt = &selfcreation_types();
 8365:                 foreach my $type (@{$changes{'cancreate'}}) {
 8366:                     my $chgtext;
 8367:                     if ($type eq 'selfcreate') {
 8368:                         if (@{$cancreate{$type}} == 0) {
 8369:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8370:                         } else {
 8371:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8372:                                         '<ul>';
 8373:                             foreach my $case (@{$cancreate{$type}}) {
 8374:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8375:                             }
 8376:                             $chgtext .= '</ul>';
 8377:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8378:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8379:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8380:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8381:                                             $chgtext .= '<br />'.
 8382:                                                         '<span class="LC_warning">'.
 8383:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8384:                                                         '</span>';
 8385:                                         }
 8386:                                     }
 8387:                                 }
 8388:                             }
 8389:                         }
 8390:                     } elsif ($type eq 'statustocreate') {
 8391:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8392:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8393:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8394:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8395:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8396:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8397:                                         $chgtext .= '<br />'.
 8398:                                                     '<span class="LC_warning">'.
 8399:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8400:                                                     '</span>';
 8401:                                     }
 8402:                                 } elsif (ref($usertypes) eq 'HASH') {
 8403:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8404:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8405:                                     } else {
 8406:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8407:                                     }
 8408:                                     $chgtext .= '<ul>';
 8409:                                     foreach my $case (@{$cancreate{$type}}) {
 8410:                                         if ($case eq 'default') {
 8411:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8412:                                         } else {
 8413:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8414:                                         }
 8415:                                     }
 8416:                                     $chgtext .= '</ul>';
 8417:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8418:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8419:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8420:                                                     '</span>';
 8421:                                     }
 8422:                                 }
 8423:                             } else {
 8424:                                 if (@{$cancreate{$type}} == 0) {
 8425:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8426:                                 } else {
 8427:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8428:                                 }
 8429:                             }
 8430:                         }
 8431:                     } elsif ($type eq 'captcha') {
 8432:                         if ($savecaptcha{$type} eq 'notused') {
 8433:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8434:                         } else {
 8435:                             my %captchas = &captcha_phrases();
 8436:                             if ($captchas{$savecaptcha{$type}}) {
 8437:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8438:                             } else {
 8439:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8440:                             }
 8441:                         }
 8442:                     } elsif ($type eq 'recaptchakeys') {
 8443:                         my ($privkey,$pubkey);
 8444:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8445:                             $pubkey = $savecaptcha{$type}{'public'};
 8446:                             $privkey = $savecaptcha{$type}{'private'};
 8447:                         }
 8448:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8449:                         if (!$pubkey) {
 8450:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8451:                         } else {
 8452:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8453:                         }
 8454:                         if (!$privkey) {
 8455:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8456:                         } else {
 8457:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8458:                         }
 8459:                         $chgtext .= '</ul>';
 8460:                     } elsif ($type eq 'emailusername') {
 8461:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8462:                             if (ref($types) eq 'ARRAY') {
 8463:                                 foreach my $type (@{$types}) {
 8464:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8465:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8466:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',$usertypes->{$type}).
 8467:                                                     '<ul>';
 8468:                                             foreach my $field (@{$infofields}) {
 8469:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8470:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8471:                                                 }
 8472:                                             }
 8473:                                         }
 8474:                                         $chgtext .= '</ul>';
 8475:                                     } else {
 8476:                                         $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 />';
 8477:                                     }
 8478:                                 }
 8479:                             }
 8480:                         }
 8481:                     } elsif ($type eq 'notify') {
 8482:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8483:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8484:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8485:                                 if ($cancreate{'notify'}{'approval'}) {
 8486:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8487:                                 }
 8488:                             }
 8489:                         }
 8490:                     }
 8491:                     if ($chgtext) {
 8492:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8493:                     }
 8494:                 }
 8495:             }
 8496:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8497:                 my ($emailrules,$emailruleorder) =
 8498:                     &Apache::lonnet::inst_userrules($dom,'email');
 8499:                 my $chgtext = '<ul>';
 8500:                 foreach my $type (@email_rule) {
 8501:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8502:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8503:                     }
 8504:                 }
 8505:                 $chgtext .= '</ul>';
 8506:                 if (@email_rule > 0) {
 8507:                     $resulttext .= '<li>'.
 8508:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8509:                                        $chgtext.
 8510:                                    '</li>';
 8511:                 } else {
 8512:                     $resulttext .= '<li>'.
 8513:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8514:                                    '</li>';
 8515:                 }
 8516:             }
 8517:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8518:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8519:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8520:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8521:                     my $typename = $type;
 8522:                     if (ref($usertypes) eq 'HASH') {
 8523:                         if ($usertypes->{$type} ne '') {
 8524:                             $typename = $usertypes->{$type};
 8525:                         }
 8526:                     }
 8527:                     my @modifiable;
 8528:                     $resulttext .= '<li>'.
 8529:                                     &mt('Self-creation of account by users with status: [_1]',
 8530:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 8531:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 8532:                     foreach my $field (@fields) {
 8533:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 8534:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 8535:                         }
 8536:                     }
 8537:                     if (@modifiable > 0) {
 8538:                         $resulttext .= join(', ',@modifiable);
 8539:                     } else {
 8540:                         $resulttext .= &mt('none');
 8541:                     }
 8542:                     $resulttext .= '</li>';
 8543:                 }
 8544:                 $resulttext .= '</ul></li>';
 8545:             }
 8546:             $resulttext .= '</ul>';
 8547:         } else {
 8548:             $resulttext = &mt('No changes made to self-creation settings');
 8549:         }
 8550:     } else {
 8551:         $resulttext = '<span class="LC_error">'.
 8552:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8553:     }
 8554:     if ($warningmsg ne '') {
 8555:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8556:     }
 8557:     return $resulttext;
 8558: }
 8559: 
 8560: sub process_captcha {
 8561:     my ($container,$changes,$newsettings,$current) = @_;
 8562:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 8563:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 8564:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 8565:         $newsettings->{'captcha'} = 'original';
 8566:     }
 8567:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 8568:         if ($container eq 'cancreate') {
 8569:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8570:                 push(@{$changes->{'cancreate'}},'captcha');
 8571:             } elsif (!defined($changes->{'cancreate'})) {
 8572:                 $changes->{'cancreate'} = ['captcha'];
 8573:             }
 8574:         } else {
 8575:             $changes->{'captcha'} = 1;
 8576:         }
 8577:     }
 8578:     my ($newpub,$newpriv,$currpub,$currpriv);
 8579:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 8580:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 8581:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 8582:         $newpub =~ s/\W//g;
 8583:         $newpriv =~ s/\W//g;
 8584:         $newsettings->{'recaptchakeys'} = {
 8585:                                              public  => $newpub,
 8586:                                              private => $newpriv,
 8587:                                           };
 8588:     }
 8589:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 8590:         $currpub = $current->{'recaptchakeys'}{'public'};
 8591:         $currpriv = $current->{'recaptchakeys'}{'private'};
 8592:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 8593:             $newsettings->{'recaptchakeys'} = {
 8594:                                                  public  => '',
 8595:                                                  private => '',
 8596:                                               }
 8597:         }
 8598:     }
 8599:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 8600:         if ($container eq 'cancreate') {
 8601:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8602:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 8603:             } elsif (!defined($changes->{'cancreate'})) {
 8604:                 $changes->{'cancreate'} = ['recaptchakeys'];
 8605:             }
 8606:         } else {
 8607:             $changes->{'recaptchakeys'} = 1;
 8608:         }
 8609:     }
 8610:     return;
 8611: }
 8612: 
 8613: sub modify_usermodification {
 8614:     my ($dom,%domconfig) = @_;
 8615:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 8616:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8617:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8618:             if ($key eq 'selfcreate') {
 8619:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 8620:             } else {  
 8621:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 8622:             }
 8623:         }
 8624:     }
 8625:     my @contexts = ('author','course');
 8626:     my %context_title = (
 8627:                            author => 'In author context',
 8628:                            course => 'In course context',
 8629:                         );
 8630:     my @fields = ('lastname','firstname','middlename','generation',
 8631:                   'permanentemail','id');
 8632:     my %roles = (
 8633:                   author => ['ca','aa'],
 8634:                   course => ['st','ep','ta','in','cr'],
 8635:                 );
 8636:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8637:     foreach my $context (@contexts) {
 8638:         foreach my $role (@{$roles{$context}}) {
 8639:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 8640:             foreach my $item (@fields) {
 8641:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 8642:                     $modifyhash{$context}{$role}{$item} = 1;
 8643:                 } else {
 8644:                     $modifyhash{$context}{$role}{$item} = 0;
 8645:                 }
 8646:             }
 8647:         }
 8648:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 8649:             foreach my $role (@{$roles{$context}}) {
 8650:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 8651:                     foreach my $field (@fields) {
 8652:                         if ($modifyhash{$context}{$role}{$field} ne 
 8653:                                 $curr_usermodification{$context}{$role}{$field}) {
 8654:                             push(@{$changes{$context}},$role);
 8655:                             last;
 8656:                         }
 8657:                     }
 8658:                 }
 8659:             }
 8660:         } else {
 8661:             foreach my $context (@contexts) {
 8662:                 foreach my $role (@{$roles{$context}}) {
 8663:                     push(@{$changes{$context}},$role);
 8664:                 }
 8665:             }
 8666:         }
 8667:     }
 8668:     my %usermodification_hash =  (
 8669:                                    usermodification => \%modifyhash,
 8670:                                  );
 8671:     my $putresult = &Apache::lonnet::put_dom('configuration',
 8672:                                              \%usermodification_hash,$dom);
 8673:     if ($putresult eq 'ok') {
 8674:         if (keys(%changes) > 0) {
 8675:             $resulttext = &mt('Changes made: ').'<ul>';
 8676:             foreach my $context (@contexts) {
 8677:                 if (ref($changes{$context}) eq 'ARRAY') {
 8678:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 8679:                     if (ref($changes{$context}) eq 'ARRAY') {
 8680:                         foreach my $role (@{$changes{$context}}) {
 8681:                             my $rolename;
 8682:                             if ($role eq 'cr') {
 8683:                                 $rolename = &mt('Custom');
 8684:                             } else {
 8685:                                 $rolename = &Apache::lonnet::plaintext($role);
 8686:                             }
 8687:                             my @modifiable;
 8688:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 8689:                             foreach my $field (@fields) {
 8690:                                 if ($modifyhash{$context}{$role}{$field}) {
 8691:                                     push(@modifiable,$fieldtitles{$field});
 8692:                                 }
 8693:                             }
 8694:                             if (@modifiable > 0) {
 8695:                                 $resulttext .= join(', ',@modifiable);
 8696:                             } else {
 8697:                                 $resulttext .= &mt('none'); 
 8698:                             }
 8699:                             $resulttext .= '</li>';
 8700:                         }
 8701:                         $resulttext .= '</ul></li>';
 8702:                     }
 8703:                 }
 8704:             }
 8705:             $resulttext .= '</ul>';
 8706:         } else {
 8707:             $resulttext = &mt('No changes made to user modification settings');
 8708:         }
 8709:     } else {
 8710:         $resulttext = '<span class="LC_error">'.
 8711:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8712:     }
 8713:     return $resulttext;
 8714: }
 8715: 
 8716: sub modify_defaults {
 8717:     my ($dom,$lastactref,%domconfig) = @_;
 8718:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 8719:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8720:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 8721:     my @authtypes = ('internal','krb4','krb5','localauth');
 8722:     foreach my $item (@items) {
 8723:         $newvalues{$item} = $env{'form.'.$item};
 8724:         if ($item eq 'auth_def') {
 8725:             if ($newvalues{$item} ne '') {
 8726:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 8727:                     push(@errors,$item);
 8728:                 }
 8729:             }
 8730:         } elsif ($item eq 'lang_def') {
 8731:             if ($newvalues{$item} ne '') {
 8732:                 if ($newvalues{$item} =~ /^(\w+)/) {
 8733:                     my $langcode = $1;
 8734:                     if ($langcode ne 'x_chef') {
 8735:                         if (code2language($langcode) eq '') {
 8736:                             push(@errors,$item);
 8737:                         }
 8738:                     }
 8739:                 } else {
 8740:                     push(@errors,$item);
 8741:                 }
 8742:             }
 8743:         } elsif ($item eq 'timezone_def') {
 8744:             if ($newvalues{$item} ne '') {
 8745:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 8746:                     push(@errors,$item);   
 8747:                 }
 8748:             }
 8749:         } elsif ($item eq 'datelocale_def') {
 8750:             if ($newvalues{$item} ne '') {
 8751:                 my @datelocale_ids = DateTime::Locale->ids();
 8752:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 8753:                     push(@errors,$item);
 8754:                 }
 8755:             }
 8756:         } elsif ($item eq 'portal_def') {
 8757:             if ($newvalues{$item} ne '') {
 8758:                 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])\/?$/) {
 8759:                     push(@errors,$item);
 8760:                 }
 8761:             }
 8762:         }
 8763:         if (grep(/^\Q$item\E$/,@errors)) {
 8764:             $newvalues{$item} = $domdefaults{$item};
 8765:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 8766:             $changes{$item} = 1;
 8767:         }
 8768:         $domdefaults{$item} = $newvalues{$item};
 8769:     }
 8770:     my %defaults_hash = (
 8771:                          defaults => \%newvalues,
 8772:                         );
 8773:     my $title = &defaults_titles();
 8774:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 8775:                                              $dom);
 8776:     if ($putresult eq 'ok') {
 8777:         if (keys(%changes) > 0) {
 8778:             $resulttext = &mt('Changes made:').'<ul>';
 8779:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 8780:             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";
 8781:             foreach my $item (sort(keys(%changes))) {
 8782:                 my $value = $env{'form.'.$item};
 8783:                 if ($value eq '') {
 8784:                     $value = &mt('none');
 8785:                 } elsif ($item eq 'auth_def') {
 8786:                     my %authnames = &authtype_names();
 8787:                     my %shortauth = (
 8788:                              internal => 'int',
 8789:                              krb4 => 'krb4',
 8790:                              krb5 => 'krb5',
 8791:                              localauth  => 'loc',
 8792:                     );
 8793:                     $value = $authnames{$shortauth{$value}};
 8794:                 }
 8795:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 8796:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 8797:             }
 8798:             $resulttext .= '</ul>';
 8799:             $mailmsgtext .= "\n";
 8800:             my $cachetime = 24*60*60;
 8801:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8802:             if (ref($lastactref) eq 'HASH') {
 8803:                 $lastactref->{'domdefaults'} = 1;
 8804:             }
 8805:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 8806:                 my $notify = 1;
 8807:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 8808:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 8809:                         $notify = 0;
 8810:                     }
 8811:                 }
 8812:                 if ($notify) {
 8813:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 8814:                                                "LON-CAPA Domain Settings Change - $dom",
 8815:                                                $mailmsgtext);
 8816:                 }
 8817:             }
 8818:         } else {
 8819:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 8820:         }
 8821:     } else {
 8822:         $resulttext = '<span class="LC_error">'.
 8823:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8824:     }
 8825:     if (@errors > 0) {
 8826:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 8827:         foreach my $item (@errors) {
 8828:             $resulttext .= ' "'.$title->{$item}.'",';
 8829:         }
 8830:         $resulttext =~ s/,$//;
 8831:     }
 8832:     return $resulttext;
 8833: }
 8834: 
 8835: sub modify_scantron {
 8836:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8837:     my ($resulttext,%confhash,%changes,$errors);
 8838:     my $custom = 'custom.tab';
 8839:     my $default = 'default.tab';
 8840:     my $servadm = $r->dir_config('lonAdmEMail');
 8841:     my ($configuserok,$author_ok,$switchserver) = 
 8842:         &config_check($dom,$confname,$servadm);
 8843:     if ($env{'form.scantronformat.filename'} ne '') {
 8844:         my $error;
 8845:         if ($configuserok eq 'ok') {
 8846:             if ($switchserver) {
 8847:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 8848:             } else {
 8849:                 if ($author_ok eq 'ok') {
 8850:                     my ($result,$scantronurl) =
 8851:                         &publishlogo($r,'upload','scantronformat',$dom,
 8852:                                      $confname,'scantron','','',$custom);
 8853:                     if ($result eq 'ok') {
 8854:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 8855:                         $changes{'scantronformat'} = 1;
 8856:                     } else {
 8857:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 8858:                     }
 8859:                 } else {
 8860:                     $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);
 8861:                 }
 8862:             }
 8863:         } else {
 8864:             $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);
 8865:         }
 8866:         if ($error) {
 8867:             &Apache::lonnet::logthis($error);
 8868:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8869:         }
 8870:     }
 8871:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 8872:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 8873:             if ($env{'form.scantronformat_del'}) {
 8874:                 $confhash{'scantron'}{'scantronformat'} = '';
 8875:                 $changes{'scantronformat'} = 1;
 8876:             }
 8877:         }
 8878:     }
 8879:     if (keys(%confhash) > 0) {
 8880:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 8881:                                                  $dom);
 8882:         if ($putresult eq 'ok') {
 8883:             if (keys(%changes) > 0) {
 8884:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 8885:                     $resulttext = &mt('Changes made:').'<ul>';
 8886:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 8887:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 8888:                     } else {
 8889:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 8890:                     }
 8891:                     $resulttext .= '</ul>';
 8892:                 } else {
 8893:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 8894:                 }
 8895:                 $resulttext .= '</ul>';
 8896:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8897:                 if (ref($lastactref) eq 'HASH') {
 8898:                     $lastactref->{'domainconfig'} = 1;
 8899:                 }
 8900:             } else {
 8901:                 $resulttext = &mt('No changes made to bubblesheet format file');
 8902:             }
 8903:         } else {
 8904:             $resulttext = '<span class="LC_error">'.
 8905:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 8906:         }
 8907:     } else {
 8908:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 8909:     }
 8910:     if ($errors) {
 8911:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8912:                        $errors.'</ul>';
 8913:     }
 8914:     return $resulttext;
 8915: }
 8916: 
 8917: sub modify_coursecategories {
 8918:     my ($dom,%domconfig) = @_;
 8919:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 8920:         $cathash);
 8921:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 8922:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8923:         $cathash = $domconfig{'coursecategories'}{'cats'};
 8924:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 8925:             $changes{'togglecats'} = 1;
 8926:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 8927:         }
 8928:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 8929:             $changes{'categorize'} = 1;
 8930:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 8931:         }
 8932:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 8933:             $changes{'togglecatscomm'} = 1;
 8934:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 8935:         }
 8936:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 8937:             $changes{'categorizecomm'} = 1;
 8938:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 8939:         }
 8940:     } else {
 8941:         $changes{'togglecats'} = 1;
 8942:         $changes{'categorize'} = 1;
 8943:         $changes{'togglecatscomm'} = 1;
 8944:         $changes{'categorizecomm'} = 1;
 8945:         $domconfig{'coursecategories'} = {
 8946:                                              togglecats => $env{'form.togglecats'},
 8947:                                              categorize => $env{'form.categorize'},
 8948:                                              togglecatscomm => $env{'form.togglecatscomm'},
 8949:                                              categorizecomm => $env{'form.categorizecomm'},
 8950:                                          };
 8951:     }
 8952:     if (ref($cathash) eq 'HASH') {
 8953:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 8954:             push (@deletecategory,'instcode::0');
 8955:         }
 8956:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 8957:             push(@deletecategory,'communities::0');
 8958:         }
 8959:     }
 8960:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 8961:     if (ref($cathash) eq 'HASH') {
 8962:         if (@deletecategory > 0) {
 8963:             #FIXME Need to remove category from all courses using a deleted category 
 8964:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 8965:             foreach my $item (@deletecategory) {
 8966:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 8967:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 8968:                     $deletions{$item} = 1;
 8969:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 8970:                 }
 8971:             }
 8972:         }
 8973:         foreach my $item (keys(%{$cathash})) {
 8974:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8975:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 8976:                 $reorderings{$item} = 1;
 8977:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 8978:             }
 8979:             if ($env{'form.addcategory_name_'.$item} ne '') {
 8980:                 my $newcat = $env{'form.addcategory_name_'.$item};
 8981:                 my $newdepth = $depth+1;
 8982:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8983:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 8984:                 $adds{$newitem} = 1; 
 8985:             }
 8986:             if ($env{'form.subcat_'.$item} ne '') {
 8987:                 my $newcat = $env{'form.subcat_'.$item};
 8988:                 my $newdepth = $depth+1;
 8989:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8990:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 8991:                 $adds{$newitem} = 1;
 8992:             }
 8993:         }
 8994:     }
 8995:     if ($env{'form.instcode'} eq '1') {
 8996:         if (ref($cathash) eq 'HASH') {
 8997:             my $newitem = 'instcode::0';
 8998:             if ($cathash->{$newitem} eq '') {  
 8999:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9000:                 $adds{$newitem} = 1;
 9001:             }
 9002:         } else {
 9003:             my $newitem = 'instcode::0';
 9004:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9005:             $adds{$newitem} = 1;
 9006:         }
 9007:     }
 9008:     if ($env{'form.communities'} eq '1') {
 9009:         if (ref($cathash) eq 'HASH') {
 9010:             my $newitem = 'communities::0';
 9011:             if ($cathash->{$newitem} eq '') {
 9012:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9013:                 $adds{$newitem} = 1;
 9014:             }
 9015:         } else {
 9016:             my $newitem = 'communities::0';
 9017:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9018:             $adds{$newitem} = 1;
 9019:         }
 9020:     }
 9021:     if ($env{'form.addcategory_name'} ne '') {
 9022:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9023:             ($env{'form.addcategory_name'} ne 'communities')) {
 9024:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9025:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9026:             $adds{$newitem} = 1;
 9027:         }
 9028:     }
 9029:     my $putresult;
 9030:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9031:         if (keys(%deletions) > 0) {
 9032:             foreach my $key (keys(%deletions)) {
 9033:                 if ($predelallitems{$key} ne '') {
 9034:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 9035:                 }
 9036:             }
 9037:         }
 9038:         my (@chkcats,@chktrails,%chkallitems);
 9039:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 9040:         if (ref($chkcats[0]) eq 'ARRAY') {
 9041:             my $depth = 0;
 9042:             my $chg = 0;
 9043:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 9044:                 my $name = $chkcats[0][$i];
 9045:                 my $item;
 9046:                 if ($name eq '') {
 9047:                     $chg ++;
 9048:                 } else {
 9049:                     $item = &escape($name).'::0';
 9050:                     if ($chg) {
 9051:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 9052:                     }
 9053:                     $depth ++; 
 9054:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 9055:                     $depth --;
 9056:                 }
 9057:             }
 9058:         }
 9059:     }
 9060:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9061:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 9062:         if ($putresult eq 'ok') {
 9063:             my %title = (
 9064:                          togglecats     => 'Show/Hide a course in catalog',
 9065:                          categorize     => 'Assign a category to a course',
 9066:                          togglecatscomm => 'Show/Hide a community in catalog',
 9067:                          categorizecomm => 'Assign a category to a community',
 9068:                         );
 9069:             my %level = (
 9070:                          dom  => 'set in Domain ("Modify Course/Community")',
 9071:                          crs  => 'set in Course ("Course Configuration")',
 9072:                          comm => 'set in Community ("Community Configuration")',
 9073:                         );
 9074:             $resulttext = &mt('Changes made:').'<ul>';
 9075:             if ($changes{'togglecats'}) {
 9076:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 9077:             }
 9078:             if ($changes{'categorize'}) {
 9079:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 9080:             }
 9081:             if ($changes{'togglecatscomm'}) {
 9082:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 9083:             }
 9084:             if ($changes{'categorizecomm'}) {
 9085:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 9086:             }
 9087:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 9088:                 my $cathash;
 9089:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9090:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 9091:                 } else {
 9092:                     $cathash = {};
 9093:                 } 
 9094:                 my (@cats,@trails,%allitems);
 9095:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9096:                 if (keys(%deletions) > 0) {
 9097:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9098:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9099:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9100:                     }
 9101:                     $resulttext .= '</ul></li>';
 9102:                 }
 9103:                 if (keys(%reorderings) > 0) {
 9104:                     my %sort_by_trail;
 9105:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9106:                     foreach my $key (keys(%reorderings)) {
 9107:                         if ($allitems{$key} ne '') {
 9108:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9109:                         }
 9110:                     }
 9111:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9112:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9113:                     }
 9114:                     $resulttext .= '</ul></li>';
 9115:                 }
 9116:                 if (keys(%adds) > 0) {
 9117:                     my %sort_by_trail;
 9118:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9119:                     foreach my $key (keys(%adds)) {
 9120:                         if ($allitems{$key} ne '') {
 9121:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9122:                         }
 9123:                     }
 9124:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9125:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9126:                     }
 9127:                     $resulttext .= '</ul></li>';
 9128:                 }
 9129:             }
 9130:             $resulttext .= '</ul>';
 9131:         } else {
 9132:             $resulttext = '<span class="LC_error">'.
 9133:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9134:         }
 9135:     } else {
 9136:         $resulttext = &mt('No changes made to course and community categories');
 9137:     }
 9138:     return $resulttext;
 9139: }
 9140: 
 9141: sub modify_serverstatuses {
 9142:     my ($dom,%domconfig) = @_;
 9143:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9144:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9145:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9146:     }
 9147:     my @pages = &serverstatus_pages();
 9148:     foreach my $type (@pages) {
 9149:         $newserverstatus{$type}{'namedusers'} = '';
 9150:         $newserverstatus{$type}{'machines'} = '';
 9151:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9152:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9153:             my @okusers;
 9154:             foreach my $user (@users) {
 9155:                 my ($uname,$udom) = split(/:/,$user);
 9156:                 if (($udom =~ /^$match_domain$/) &&   
 9157:                     (&Apache::lonnet::domain($udom)) &&
 9158:                     ($uname =~ /^$match_username$/)) {
 9159:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9160:                         push(@okusers,$user);
 9161:                     }
 9162:                 }
 9163:             }
 9164:             if (@okusers > 0) {
 9165:                  @okusers = sort(@okusers);
 9166:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9167:             }
 9168:         }
 9169:         if (defined($env{'form.'.$type.'_machines'})) {
 9170:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9171:             my @okmachines;
 9172:             foreach my $ip (@machines) {
 9173:                 my @parts = split(/\./,$ip);
 9174:                 next if (@parts < 4);
 9175:                 my $badip = 0;
 9176:                 for (my $i=0; $i<4; $i++) {
 9177:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9178:                         $badip = 1;
 9179:                         last;
 9180:                     }
 9181:                 }
 9182:                 if (!$badip) {
 9183:                     push(@okmachines,$ip);     
 9184:                 }
 9185:             }
 9186:             @okmachines = sort(@okmachines);
 9187:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9188:         }
 9189:     }
 9190:     my %serverstatushash =  (
 9191:                                 serverstatuses => \%newserverstatus,
 9192:                             );
 9193:     foreach my $type (@pages) {
 9194:         foreach my $setting ('namedusers','machines') {
 9195:             my (@current,@new);
 9196:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9197:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9198:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9199:                 }
 9200:             }
 9201:             if ($newserverstatus{$type}{$setting} ne '') {
 9202:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9203:             }
 9204:             if (@current > 0) {
 9205:                 if (@new > 0) {
 9206:                     foreach my $item (@current) {
 9207:                         if (!grep(/^\Q$item\E$/,@new)) {
 9208:                             $changes{$type}{$setting} = 1;
 9209:                             last;
 9210:                         }
 9211:                     }
 9212:                     foreach my $item (@new) {
 9213:                         if (!grep(/^\Q$item\E$/,@current)) {
 9214:                             $changes{$type}{$setting} = 1;
 9215:                             last;
 9216:                         }
 9217:                     }
 9218:                 } else {
 9219:                     $changes{$type}{$setting} = 1;
 9220:                 }
 9221:             } elsif (@new > 0) {
 9222:                 $changes{$type}{$setting} = 1;
 9223:             }
 9224:         }
 9225:     }
 9226:     if (keys(%changes) > 0) {
 9227:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9228:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9229:                                                  \%serverstatushash,$dom);
 9230:         if ($putresult eq 'ok') {
 9231:             $resulttext .= &mt('Changes made:').'<ul>';
 9232:             foreach my $type (@pages) {
 9233:                 if (ref($changes{$type}) eq 'HASH') {
 9234:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9235:                     if ($changes{$type}{'namedusers'}) {
 9236:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9237:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9238:                         } else {
 9239:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9240:                         }
 9241:                     }
 9242:                     if ($changes{$type}{'machines'}) {
 9243:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9244:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9245:                         } else {
 9246:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9247:                         }
 9248: 
 9249:                     }
 9250:                     $resulttext .= '</ul></li>';
 9251:                 }
 9252:             }
 9253:             $resulttext .= '</ul>';
 9254:         } else {
 9255:             $resulttext = '<span class="LC_error">'.
 9256:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9257: 
 9258:         }
 9259:     } else {
 9260:         $resulttext = &mt('No changes made to access to server status pages');
 9261:     }
 9262:     return $resulttext;
 9263: }
 9264: 
 9265: sub modify_helpsettings {
 9266:     my ($r,$dom,$confname,%domconfig) = @_;
 9267:     my ($resulttext,$errors,%changes,%helphash);
 9268:     my %defaultchecked = ('submitbugs' => 'on');
 9269:     my @offon = ('off','on');
 9270:     my @toggles = ('submitbugs');
 9271:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9272:         foreach my $item (@toggles) {
 9273:             if ($defaultchecked{$item} eq 'on') { 
 9274:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9275:                     if ($env{'form.'.$item} eq '0') {
 9276:                         $changes{$item} = 1;
 9277:                     }
 9278:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9279:                     $changes{$item} = 1;
 9280:                 }
 9281:             } elsif ($defaultchecked{$item} eq 'off') {
 9282:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9283:                     if ($env{'form.'.$item} eq '1') {
 9284:                         $changes{$item} = 1;
 9285:                     }
 9286:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9287:                     $changes{$item} = 1;
 9288:                 }
 9289:             }
 9290:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9291:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9292:             }
 9293:         }
 9294:     }
 9295:     my $putresult;
 9296:     if (keys(%changes) > 0) {
 9297:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9298:         if ($putresult eq 'ok') {
 9299:             $resulttext = &mt('Changes made:').'<ul>';
 9300:             foreach my $item (sort(keys(%changes))) {
 9301:                 if ($item eq 'submitbugs') {
 9302:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9303:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9304:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9305:                 }
 9306:             }
 9307:             $resulttext .= '</ul>';
 9308:         } else {
 9309:             $resulttext = &mt('No changes made to help settings');
 9310:             $errors .= '<li><span class="LC_error">'.
 9311:                        &mt('An error occurred storing the settings: [_1]',
 9312:                            $putresult).'</span></li>';
 9313:         }
 9314:     }
 9315:     if ($errors) {
 9316:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9317:                        $errors.'</ul>';
 9318:     }
 9319:     return $resulttext;
 9320: }
 9321: 
 9322: sub modify_coursedefaults {
 9323:     my ($dom,$lastactref,%domconfig) = @_;
 9324:     my ($resulttext,$errors,%changes,%defaultshash);
 9325:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9326:     my @toggles = ('canuse_pdfforms');
 9327:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9328:                    'uploadquota_community','uploadquota_textbook');
 9329:     my @types = ('official','unofficial','community','textbook');
 9330:     my %staticdefaults = (
 9331:                            anonsurvey_threshold => 10,
 9332:                            uploadquota          => 500,
 9333:                          );
 9334: 
 9335:     $defaultshash{'coursedefaults'} = {};
 9336: 
 9337:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9338:         if ($domconfig{'coursedefaults'} eq '') {
 9339:             $domconfig{'coursedefaults'} = {};
 9340:         }
 9341:     }
 9342: 
 9343:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9344:         foreach my $item (@toggles) {
 9345:             if ($defaultchecked{$item} eq 'on') {
 9346:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9347:                     ($env{'form.'.$item} eq '0')) {
 9348:                     $changes{$item} = 1;
 9349:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9350:                     $changes{$item} = 1;
 9351:                 }
 9352:             } elsif ($defaultchecked{$item} eq 'off') {
 9353:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9354:                     ($env{'form.'.$item} eq '1')) {
 9355:                     $changes{$item} = 1;
 9356:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9357:                     $changes{$item} = 1;
 9358:                 }
 9359:             }
 9360:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 9361:         }
 9362:         foreach my $item (@numbers) {
 9363:             my ($currdef,$newdef);
 9364:             $newdef = $env{'form.'.$item};
 9365:             if ($item eq 'anonsurvey_threshold') {
 9366:                 $currdef = $domconfig{'coursedefaults'}{$item};
 9367:                 $newdef =~ s/\D//g;
 9368:                 if ($newdef eq '' || $newdef < 1) {
 9369:                     $newdef = 1;
 9370:                 }
 9371:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 9372:             } else {
 9373:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 9374:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9375:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 9376:                 }
 9377:                 $newdef =~ s/[^\w.\-]//g;
 9378:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 9379:             }
 9380:             if ($currdef ne $newdef) {
 9381:                 my $staticdef;
 9382:                 if ($item eq 'anonsurvey_threshold') {
 9383:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 9384:                         $changes{$item} = 1;
 9385:                     }
 9386:                 } else {
 9387:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 9388:                         $changes{'uploadquota'} = 1;
 9389:                     }
 9390:                 }
 9391:             }
 9392:         }
 9393: 
 9394:         my $officialcreds = $env{'form.official_credits'};
 9395:         $officialcreds =~ s/[^\d.]+//g;
 9396:         my $unofficialcreds = $env{'form.unofficial_credits'};
 9397:         $unofficialcreds =~ s/[^\d.]+//g;
 9398:         my $textbookcreds = $env{'form.textbook_credits'};
 9399:         $textbookcreds =~ s/[^\d.]+//g;
 9400:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 9401:                 ($env{'form.coursecredits'} eq '1')) {
 9402:                 $changes{'coursecredits'} = 1;
 9403:         } else {
 9404:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 9405:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 9406:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 9407:                 $changes{'coursecredits'} = 1;
 9408:             }
 9409:         }
 9410:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 9411:             official   => $officialcreds,
 9412:             unofficial => $unofficialcreds,
 9413:             textbook   => $textbookcreds,
 9414:         }
 9415:     }
 9416:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9417:                                              $dom);
 9418:     if ($putresult eq 'ok') {
 9419:         if (keys(%changes) > 0) {
 9420:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9421:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
 9422:                 ($changes{'uploadquota'})) { 
 9423:                 if ($changes{'canuse_pdfforms'}) {
 9424:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 9425:                 }
 9426:                 if ($changes{'coursecredits'}) {
 9427:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9428:                         $domdefaults{'officialcredits'} =
 9429:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 9430:                         $domdefaults{'unofficialcredits'} =
 9431:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 9432:                         $domdefaults{'textbookcredits'} =
 9433:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 9434:                     }
 9435:                 }
 9436:                 if ($changes{'uploadquota'}) {
 9437:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9438:                         foreach my $type (@types) {
 9439:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 9440:                         }
 9441:                     }
 9442:                 }
 9443:                 my $cachetime = 24*60*60;
 9444:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9445:                 if (ref($lastactref) eq 'HASH') {
 9446:                     $lastactref->{'domdefaults'} = 1;
 9447:                 }
 9448:             }
 9449:             $resulttext = &mt('Changes made:').'<ul>';
 9450:             foreach my $item (sort(keys(%changes))) {
 9451:                 if ($item eq 'canuse_pdfforms') {
 9452:                     if ($env{'form.'.$item} eq '1') {
 9453:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 9454:                     } else {
 9455:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 9456:                     }
 9457:                 } elsif ($item eq 'anonsurvey_threshold') {
 9458:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 9459:                 } elsif ($item eq 'uploadquota') {
 9460:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9461:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 9462:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 9463:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 9464:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
 9465: 
 9466:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 9467:                                        '</ul>'.
 9468:                                        '</li>';
 9469:                     } else {
 9470:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 9471:                     }
 9472:                 } elsif ($item eq 'coursecredits') {
 9473:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9474:                         if (($domdefaults{'officialcredits'} eq '') &&
 9475:                             ($domdefaults{'unofficialcredits'} eq '') &&
 9476:                             ($domdefaults{'textbookcredits'} eq '')) {
 9477:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9478:                         } else {
 9479:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 9480:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 9481:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 9482:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
 9483:                                            '</ul>'.
 9484:                                            '</li>';
 9485:                         }
 9486:                     } else {
 9487:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9488:                     }
 9489:                 }
 9490:             }
 9491:             $resulttext .= '</ul>';
 9492:         } else {
 9493:             $resulttext = &mt('No changes made to course defaults');
 9494:         }
 9495:     } else {
 9496:         $resulttext = '<span class="LC_error">'.
 9497:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9498:     }
 9499:     return $resulttext;
 9500: }
 9501: 
 9502: sub modify_selfenrollment {
 9503:     my ($dom,$lastactref,%domconfig) = @_;
 9504:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
 9505:     my @types = ('official','unofficial','community','textbook');
 9506:     my %titles = &tool_titles();
 9507:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 9508:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 9509:     $ordered{'default'} = ['types','registered','approval','limit'];
 9510: 
 9511:     my (%roles,%shown,%toplevel);
 9512:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
 9513: 
 9514:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
 9515:         if ($domconfig{'selfenrollment'} eq '') {
 9516:             $domconfig{'selfenrollment'} = {};
 9517:         }
 9518:     }
 9519:     %toplevel = (
 9520:                   admin      => 'Configuration Rights',
 9521:                   default    => 'Default settings',
 9522:                   validation => 'Validation of self-enrollment requests',
 9523:                 );
 9524:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 9525: 
 9526:     if (ref($ordered{'admin'}) eq 'ARRAY') {
 9527:         foreach my $item (@{$ordered{'admin'}}) {
 9528:             foreach my $type (@types) {
 9529:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
 9530:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
 9531:                 } else {
 9532:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
 9533:                 }
 9534:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 9535:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 9536:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
 9537:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
 9538:                             push(@{$changes{'admin'}{$type}},$item);
 9539:                         }
 9540:                     } else {
 9541:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
 9542:                             push(@{$changes{'admin'}{$type}},$item);
 9543:                         }
 9544:                     }
 9545:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
 9546:                     push(@{$changes{'admin'}{$type}},$item);
 9547:                 }
 9548:             }
 9549:         }
 9550:     }
 9551: 
 9552:     foreach my $item (@{$ordered{'default'}}) {
 9553:         foreach my $type (@types) {
 9554:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
 9555:             if ($item eq 'types') {
 9556:                 unless (($value eq 'all') || ($value eq 'dom')) {
 9557:                     $value = '';
 9558:                 }
 9559:             } elsif ($item eq 'registered') {
 9560:                 unless ($value eq '1') {
 9561:                     $value = 0;
 9562:                 }
 9563:             } elsif ($item eq 'approval') {
 9564:                 unless ($value =~ /^[012]$/) {
 9565:                     $value = 0;
 9566:                 }
 9567:             } else {
 9568:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
 9569:                     $value = 'none';
 9570:                 }
 9571:             }
 9572:             $selfenrollhash{'default'}{$type}{$item} = $value;
 9573:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 9574:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 9575:                     if ($selfenrollhash{'default'}{$type}{$item} ne
 9576:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
 9577:                          push(@{$changes{'default'}{$type}},$item);
 9578:                     }
 9579:                 } else {
 9580:                     push(@{$changes{'default'}{$type}},$item);
 9581:                 }
 9582:             } else {
 9583:                 push(@{$changes{'default'}{$type}},$item);
 9584:             }
 9585:             if ($item eq 'limit') {
 9586:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
 9587:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
 9588:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
 9589:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
 9590:                     }
 9591:                 } else {
 9592:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
 9593:                 }
 9594:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 9595:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
 9596:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
 9597:                          push(@{$changes{'default'}{$type}},'cap');
 9598:                     }
 9599:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
 9600:                     push(@{$changes{'default'}{$type}},'cap');
 9601:                 }
 9602:             }
 9603:         }
 9604:     }
 9605: 
 9606:     foreach my $item (@{$itemsref}) {
 9607:         if ($item eq 'fields') {
 9608:             my @changed;
 9609:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
 9610:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
 9611:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
 9612:             }
 9613:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
 9614:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
 9615:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
 9616:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
 9617:                 } else {
 9618:                     @changed = @{$selfenrollhash{'validation'}{$item}};
 9619:                 }
 9620:             } else {
 9621:                 @changed = @{$selfenrollhash{'validation'}{$item}};
 9622:             }
 9623:             if (@changed) {
 9624:                 if ($selfenrollhash{'validation'}{$item}) { 
 9625:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
 9626:                 } else {
 9627:                     $changes{'validation'}{$item} = &mt('None');
 9628:                 }
 9629:             }
 9630:         } else {
 9631:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
 9632:             if ($item eq 'markup') {
 9633:                if ($env{'form.selfenroll_validation_'.$item}) {
 9634:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9635:                }
 9636:             }
 9637:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
 9638:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
 9639:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
 9640:                 }
 9641:             }
 9642:         }
 9643:     }
 9644: 
 9645:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
 9646:                                              $dom);
 9647:     if ($putresult eq 'ok') {
 9648:         if (keys(%changes) > 0) {
 9649:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9650:             $resulttext = &mt('Changes made:').'<ul>';
 9651:             foreach my $key ('admin','default','validation') {
 9652:                 if (ref($changes{$key}) eq 'HASH') {
 9653:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
 9654:                     if ($key eq 'validation') {
 9655:                         foreach my $item (@{$itemsref}) {
 9656:                             if (exists($changes{$key}{$item})) {
 9657:                                 if ($item eq 'markup') {
 9658:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
 9659:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
 9660:                                 } else {  
 9661:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
 9662:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
 9663:                                 }
 9664:                             }
 9665:                         }
 9666:                     } else {
 9667:                         foreach my $type (@types) {
 9668:                             if ($type eq 'community') {
 9669:                                 $roles{'1'} = &mt('Community personnel');
 9670:                             } else {
 9671:                                 $roles{'1'} = &mt('Course personnel');
 9672:                             }
 9673:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
 9674:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
 9675:                                     if ($key eq 'admin') {
 9676:                                         my @mgrdc = ();
 9677:                                         if (ref($ordered{$key}) eq 'ARRAY') {
 9678:                                             foreach my $item (@{$ordered{'admin'}}) {
 9679:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
 9680:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
 9681:                                                         push(@mgrdc,$item);
 9682:                                                     }
 9683:                                                 }
 9684:                                             }
 9685:                                             if (@mgrdc) {
 9686:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 9687:                                             } else {
 9688:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
 9689:                                             }
 9690:                                         }
 9691:                                     } else {
 9692:                                         if (ref($ordered{$key}) eq 'ARRAY') {
 9693:                                             foreach my $item (@{$ordered{$key}}) {
 9694:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
 9695:                                                     $domdefaults{$type.'selfenroll'.$item} =
 9696:                                                         $selfenrollhash{$key}{$type}{$item};
 9697:                                                 }
 9698:                                             }
 9699:                                         }
 9700:                                     }
 9701:                                 }
 9702:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
 9703:                                 foreach my $item (@{$ordered{$key}}) {
 9704:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
 9705:                                         $resulttext .= '<li>';
 9706:                                         if ($key eq 'admin') {
 9707:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
 9708:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
 9709:                                         } else {
 9710:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
 9711:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
 9712:                                         }
 9713:                                         $resulttext .= '</li>';
 9714:                                     }
 9715:                                 }
 9716:                                 $resulttext .= '</ul></li>';
 9717:                             }
 9718:                         }
 9719:                         $resulttext .= '</ul></li>'; 
 9720:                     }
 9721:                 }
 9722:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
 9723:                     my $cachetime = 24*60*60;
 9724:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9725:                     if (ref($lastactref) eq 'HASH') {
 9726:                         $lastactref->{'domdefaults'} = 1;
 9727:                     }
 9728:                 }
 9729:             }
 9730:             $resulttext .= '</ul>';
 9731:         } else {
 9732:             $resulttext = &mt('No changes made to self-enrollment settings');
 9733:         }
 9734:     } else {
 9735:         $resulttext = '<span class="LC_error">'.
 9736:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9737:     }
 9738:     return $resulttext;
 9739: }
 9740: 
 9741: sub modify_usersessions {
 9742:     my ($dom,$lastactref,%domconfig) = @_;
 9743:     my @hostingtypes = ('version','excludedomain','includedomain');
 9744:     my @offloadtypes = ('primary','default');
 9745:     my %types = (
 9746:                   remote => \@hostingtypes,
 9747:                   hosted => \@hostingtypes,
 9748:                   spares => \@offloadtypes,
 9749:                 );
 9750:     my @prefixes = ('remote','hosted','spares');
 9751:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 9752:     my (%by_ip,%by_location,@intdoms);
 9753:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 9754:     my @locations = sort(keys(%by_location));
 9755:     my (%defaultshash,%changes);
 9756:     foreach my $prefix (@prefixes) {
 9757:         $defaultshash{'usersessions'}{$prefix} = {};
 9758:     }
 9759:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9760:     my $resulttext;
 9761:     my %iphost = &Apache::lonnet::get_iphost();
 9762:     foreach my $prefix (@prefixes) {
 9763:         next if ($prefix eq 'spares');
 9764:         foreach my $type (@{$types{$prefix}}) {
 9765:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 9766:             if ($type eq 'version') {
 9767:                 my $value = $env{'form.'.$prefix.'_'.$type};
 9768:                 my $okvalue;
 9769:                 if ($value ne '') {
 9770:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 9771:                         $okvalue = $value;
 9772:                     }
 9773:                 }
 9774:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9775:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9776:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 9777:                             if ($inuse == 0) {
 9778:                                 $changes{$prefix}{$type} = 1;
 9779:                             } else {
 9780:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 9781:                                     $changes{$prefix}{$type} = 1;
 9782:                                 }
 9783:                                 if ($okvalue ne '') {
 9784:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9785:                                 } 
 9786:                             }
 9787:                         } else {
 9788:                             if (($inuse == 1) && ($okvalue ne '')) {
 9789:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9790:                                 $changes{$prefix}{$type} = 1;
 9791:                             }
 9792:                         }
 9793:                     } else {
 9794:                         if (($inuse == 1) && ($okvalue ne '')) {
 9795:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9796:                             $changes{$prefix}{$type} = 1;
 9797:                         }
 9798:                     }
 9799:                 } else {
 9800:                     if (($inuse == 1) && ($okvalue ne '')) {
 9801:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9802:                         $changes{$prefix}{$type} = 1;
 9803:                     }
 9804:                 }
 9805:             } else {
 9806:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 9807:                 my @okvals;
 9808:                 foreach my $val (@vals) {
 9809:                     if ($val =~ /:/) {
 9810:                         my @items = split(/:/,$val);
 9811:                         foreach my $item (@items) {
 9812:                             if (ref($by_location{$item}) eq 'ARRAY') {
 9813:                                 push(@okvals,$item);
 9814:                             }
 9815:                         }
 9816:                     } else {
 9817:                         if (ref($by_location{$val}) eq 'ARRAY') {
 9818:                             push(@okvals,$val);
 9819:                         }
 9820:                     }
 9821:                 }
 9822:                 @okvals = sort(@okvals);
 9823:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9824:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9825:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9826:                             if ($inuse == 0) {
 9827:                                 $changes{$prefix}{$type} = 1; 
 9828:                             } else {
 9829:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9830:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 9831:                                 if (@changed > 0) {
 9832:                                     $changes{$prefix}{$type} = 1;
 9833:                                 }
 9834:                             }
 9835:                         } else {
 9836:                             if ($inuse == 1) {
 9837:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9838:                                 $changes{$prefix}{$type} = 1;
 9839:                             }
 9840:                         } 
 9841:                     } else {
 9842:                         if ($inuse == 1) {
 9843:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9844:                             $changes{$prefix}{$type} = 1;
 9845:                         }
 9846:                     }
 9847:                 } else {
 9848:                     if ($inuse == 1) {
 9849:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9850:                         $changes{$prefix}{$type} = 1;
 9851:                     }
 9852:                 }
 9853:             }
 9854:         }
 9855:     }
 9856: 
 9857:     my @alldoms = &Apache::lonnet::all_domains();
 9858:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9859:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 9860:     my $savespares;
 9861: 
 9862:     foreach my $lonhost (sort(keys(%servers))) {
 9863:         my $serverhomeID =
 9864:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 9865:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 9866:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 9867:         my %spareschg;
 9868:         foreach my $type (@{$types{'spares'}}) {
 9869:             my @okspares;
 9870:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 9871:             foreach my $server (@checked) {
 9872:                 if (&Apache::lonnet::hostname($server) ne '') {
 9873:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 9874:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 9875:                             push(@okspares,$server);
 9876:                         }
 9877:                     }
 9878:                 }
 9879:             }
 9880:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 9881:             my $newspare;
 9882:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 9883:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 9884:                     $newspare = $new;
 9885:                 }
 9886:             }
 9887:             my @spares;
 9888:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 9889:                 @spares = sort(@okspares,$newspare);
 9890:             } else {
 9891:                 @spares = sort(@okspares);
 9892:             }
 9893:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 9894:             if (ref($spareid{$lonhost}) eq 'HASH') {
 9895:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 9896:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 9897:                     if (@diffs > 0) {
 9898:                         $spareschg{$type} = 1;
 9899:                     }
 9900:                 }
 9901:             }
 9902:         }
 9903:         if (keys(%spareschg) > 0) {
 9904:             $changes{'spares'}{$lonhost} = \%spareschg;
 9905:         }
 9906:     }
 9907: 
 9908:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9909:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 9910:             if (ref($changes{'spares'}) eq 'HASH') {
 9911:                 if (keys(%{$changes{'spares'}}) > 0) {
 9912:                     $savespares = 1;
 9913:                 }
 9914:             }
 9915:         } else {
 9916:             $savespares = 1;
 9917:         }
 9918:     }
 9919: 
 9920:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 9921:     if ((keys(%changes) > 0) || ($savespares)) {
 9922:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9923:                                                  $dom);
 9924:         if ($putresult eq 'ok') {
 9925:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9926:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 9927:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 9928:                 }
 9929:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 9930:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 9931:                 }
 9932:             }
 9933:             my $cachetime = 24*60*60;
 9934:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9935:             if (ref($lastactref) eq 'HASH') {
 9936:                 $lastactref->{'domdefaults'} = 1;
 9937:             }
 9938:             if (keys(%changes) > 0) {
 9939:                 my %lt = &usersession_titles();
 9940:                 $resulttext = &mt('Changes made:').'<ul>';
 9941:                 foreach my $prefix (@prefixes) {
 9942:                     if (ref($changes{$prefix}) eq 'HASH') {
 9943:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 9944:                         if ($prefix eq 'spares') {
 9945:                             if (ref($changes{$prefix}) eq 'HASH') {
 9946:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 9947:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 9948:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 9949:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 9950:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 9951:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 9952:                                         foreach my $type (@{$types{$prefix}}) {
 9953:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 9954:                                                 my $offloadto = &mt('None');
 9955:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 9956:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 9957:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 9958:                                                     }
 9959:                                                 }
 9960:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 9961:                                             }
 9962:                                         }
 9963:                                     }
 9964:                                     $resulttext .= '</li>';
 9965:                                 }
 9966:                             }
 9967:                         } else {
 9968:                             foreach my $type (@{$types{$prefix}}) {
 9969:                                 if (defined($changes{$prefix}{$type})) {
 9970:                                     my $newvalue;
 9971:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9972:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 9973:                                             if ($type eq 'version') {
 9974:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 9975:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9976:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 9977:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 9978:                                                 }
 9979:                                             }
 9980:                                         }
 9981:                                     }
 9982:                                     if ($newvalue eq '') {
 9983:                                         if ($type eq 'version') {
 9984:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 9985:                                         } else {
 9986:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 9987:                                         }
 9988:                                     } else {
 9989:                                         if ($type eq 'version') {
 9990:                                             $newvalue .= ' '.&mt('(or later)'); 
 9991:                                         }
 9992:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 9993:                                     }
 9994:                                 }
 9995:                             }
 9996:                         }
 9997:                         $resulttext .= '</ul>';
 9998:                     }
 9999:                 }
10000:                 $resulttext .= '</ul>';
10001:             } else {
10002:                 $resulttext = $nochgmsg;
10003:             }
10004:         } else {
10005:             $resulttext = '<span class="LC_error">'.
10006:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10007:         }
10008:     } else {
10009:         $resulttext = $nochgmsg;
10010:     }
10011:     return $resulttext;
10012: }
10013: 
10014: sub modify_loadbalancing {
10015:     my ($dom,%domconfig) = @_;
10016:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
10017:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
10018:     my ($othertitle,$usertypes,$types) =
10019:         &Apache::loncommon::sorted_inst_types($dom);
10020:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10021:     my @sparestypes = ('primary','default');
10022:     my %typetitles = &sparestype_titles();
10023:     my $resulttext;
10024:     my (%currbalancer,%currtargets,%currrules,%existing);
10025:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10026:         %existing = %{$domconfig{'loadbalancing'}};
10027:     }
10028:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
10029:                               \%currtargets,\%currrules);
10030:     my ($saveloadbalancing,%defaultshash,%changes);
10031:     my ($alltypes,$othertypes,$titles) =
10032:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
10033:     my %ruletitles = &offloadtype_text();
10034:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
10035:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
10036:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
10037:         if ($balancer eq '') {
10038:             next;
10039:         }
10040:         if (!exists($servers{$balancer})) {
10041:             if (exists($currbalancer{$balancer})) {
10042:                 push(@{$changes{'delete'}},$balancer);
10043:             }
10044:             next;
10045:         }
10046:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
10047:             push(@{$changes{'delete'}},$balancer);
10048:             next;
10049:         }
10050:         if (!exists($currbalancer{$balancer})) {
10051:             push(@{$changes{'add'}},$balancer);
10052:         }
10053:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
10054:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
10055:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
10056:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
10057:             $saveloadbalancing = 1;
10058:         }
10059:         foreach my $sparetype (@sparestypes) {
10060:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
10061:             my @offloadto;
10062:             foreach my $target (@targets) {
10063:                 if (($servers{$target}) && ($target ne $balancer)) {
10064:                     if ($sparetype eq 'default') {
10065:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
10066:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
10067:                         }
10068:                     }
10069:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
10070:                         push(@offloadto,$target);
10071:                     }
10072:                 }
10073:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
10074:             }
10075:         }
10076:         if (ref($currtargets{$balancer}) eq 'HASH') {
10077:             foreach my $sparetype (@sparestypes) {
10078:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
10079:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
10080:                     if (@targetdiffs > 0) {
10081:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10082:                     }
10083:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10084:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10085:                         $changes{'curr'}{$balancer}{'targets'} = 1;
10086:                     }
10087:                 }
10088:             }
10089:         } else {
10090:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
10091:                 foreach my $sparetype (@sparestypes) {
10092:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10093:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10094:                             $changes{'curr'}{$balancer}{'targets'} = 1;
10095:                         }
10096:                     }
10097:                 }
10098:             }
10099:         }
10100:         my $ishomedom;
10101:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
10102:             $ishomedom = 1;
10103:         }
10104:         if (ref($alltypes) eq 'ARRAY') {
10105:             foreach my $type (@{$alltypes}) {
10106:                 my $rule;
10107:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
10108:                          (!$ishomedom)) {
10109:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
10110:                 }
10111:                 if ($rule eq 'specific') {
10112:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
10113:                 }
10114:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
10115:                 if (ref($currrules{$balancer}) eq 'HASH') {
10116:                     if ($rule ne $currrules{$balancer}{$type}) {
10117:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10118:                     }
10119:                 } elsif ($rule ne '') {
10120:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
10121:                 }
10122:             }
10123:         }
10124:     }
10125:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
10126:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
10127:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
10128:             $defaultshash{'loadbalancing'} = {};
10129:         }
10130:         my $putresult = &Apache::lonnet::put_dom('configuration',
10131:                                                  \%defaultshash,$dom);
10132:         if ($putresult eq 'ok') {
10133:             if (keys(%changes) > 0) {
10134:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10135:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10136:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10137:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10138:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10139:                     }
10140:                 }
10141:                 if (ref($changes{'add'}) eq 'ARRAY') {
10142:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10143:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10144:                     }
10145:                 }
10146:                 if (ref($changes{'curr'}) eq 'HASH') {
10147:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10148:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10149:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10150:                                 my %offloadstr;
10151:                                 foreach my $sparetype (@sparestypes) {
10152:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10153:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10154:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10155:                                         }
10156:                                     }
10157:                                 }
10158:                                 if (keys(%offloadstr) == 0) {
10159:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10160:                                 } else {
10161:                                     my $showoffload;
10162:                                     foreach my $sparetype (@sparestypes) {
10163:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10164:                                         if (defined($offloadstr{$sparetype})) {
10165:                                             $showoffload .= $offloadstr{$sparetype};
10166:                                         } else {
10167:                                             $showoffload .= &mt('None');
10168:                                         }
10169:                                         $showoffload .= ('&nbsp;'x3);
10170:                                     }
10171:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10172:                                 }
10173:                             }
10174:                         }
10175:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10176:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10177:                                 foreach my $type (@{$alltypes}) {
10178:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10179:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10180:                                         my $balancetext;
10181:                                         if ($rule eq '') {
10182:                                             $balancetext =  $ruletitles{'default'};
10183:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10184:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10185:                                             $balancetext =  $ruletitles{$rule};
10186:                                         } else {
10187:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10188:                                         }
10189:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10190:                                     }
10191:                                 }
10192:                             }
10193:                         }
10194:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10195:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10196:                     }
10197:                 }
10198:                 if ($resulttext ne '') {
10199:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10200:                 } else {
10201:                     $resulttext = $nochgmsg;
10202:                 }
10203:             } else {
10204:                 $resulttext = $nochgmsg;
10205:             }
10206:         } else {
10207:             $resulttext = '<span class="LC_error">'.
10208:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10209:         }
10210:     } else {
10211:         $resulttext = $nochgmsg;
10212:     }
10213:     return $resulttext;
10214: }
10215: 
10216: sub recurse_check {
10217:     my ($chkcats,$categories,$depth,$name) = @_;
10218:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10219:         my $chg = 0;
10220:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10221:             my $category = $chkcats->[$depth]{$name}[$j];
10222:             my $item;
10223:             if ($category eq '') {
10224:                 $chg ++;
10225:             } else {
10226:                 my $deeper = $depth + 1;
10227:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10228:                 if ($chg) {
10229:                     $categories->{$item} -= $chg;
10230:                 }
10231:                 &recurse_check($chkcats,$categories,$deeper,$category);
10232:                 $deeper --;
10233:             }
10234:         }
10235:     }
10236:     return;
10237: }
10238: 
10239: sub recurse_cat_deletes {
10240:     my ($item,$coursecategories,$deletions) = @_;
10241:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10242:     my $subdepth = $depth + 1;
10243:     if (ref($coursecategories) eq 'HASH') {
10244:         foreach my $subitem (keys(%{$coursecategories})) {
10245:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10246:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10247:                 delete($coursecategories->{$subitem});
10248:                 $deletions->{$subitem} = 1;
10249:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10250:             }
10251:         }
10252:     }
10253:     return;
10254: }
10255: 
10256: sub get_active_dcs {
10257:     my ($dom) = @_;
10258:     my $now = time;
10259:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10260:     my %domcoords;
10261:     my $numdcs = 0;
10262:     foreach my $server (keys(%dompersonnel)) {
10263:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10264:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10265:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10266:         }
10267:     }
10268:     return %domcoords;
10269: }
10270: 
10271: sub active_dc_picker {
10272:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10273:     my %domcoords = &get_active_dcs($dom);
10274:     my @domcoord = keys(%domcoords);
10275:     if (keys(%currhash)) {
10276:         foreach my $dc (keys(%currhash)) {
10277:             unless (exists($domcoords{$dc})) {
10278:                 push(@domcoord,$dc);
10279:             }
10280:         }
10281:     }
10282:     @domcoord = sort(@domcoord);
10283:     my $numdcs = scalar(@domcoord);
10284:     my $rows = 0;
10285:     my $table;
10286:     if ($numdcs > 1) {
10287:         $table = '<table>';
10288:         for (my $i=0; $i<@domcoord; $i++) {
10289:             my $rem = $i%($numinrow);
10290:             if ($rem == 0) {
10291:                 if ($i > 0) {
10292:                     $table .= '</tr>';
10293:                 }
10294:                 $table .= '<tr>';
10295:                 $rows ++;
10296:             }
10297:             my $check = '';
10298:             if ($inputtype eq 'radio') {
10299:                 if (keys(%currhash) == 0) {
10300:                     if (!$i) {
10301:                         $check = ' checked="checked"';
10302:                     }
10303:                 } elsif (exists($currhash{$domcoord[$i]})) {
10304:                     $check = ' checked="checked"';
10305:                 }
10306:             } else {
10307:                 if (exists($currhash{$domcoord[$i]})) {
10308:                     $check = ' checked="checked"';
10309:                 }
10310:             }
10311:             if ($i == @domcoord - 1) {
10312:                 my $colsleft = $numinrow - $rem;
10313:                 if ($colsleft > 1) {
10314:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10315:                 } else {
10316:                     $table .= '<td class="LC_left_item">';
10317:                 }
10318:             } else {
10319:                 $table .= '<td class="LC_left_item">';
10320:             }
10321:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10322:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10323:             $table .= '<span class="LC_nobreak"><label>'.
10324:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10325:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10326:             if ($user ne $dcname.':'.$dcdom) {
10327:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10328:             }
10329:             $table .= '</label></span></td>';
10330:         }
10331:         $table .= '</tr></table>';
10332:     } elsif ($numdcs == 1) {
10333:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10334:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10335:         if ($inputtype eq 'radio') {
10336:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10337:             if ($user ne $dcname.':'.$dcdom) {
10338:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10339:             }
10340:         } else {
10341:             my $check;
10342:             if (exists($currhash{$domcoord[0]})) {
10343:                 $check = ' checked="checked"';
10344:             }
10345:             $table .= '<span class="LC_nobreak"><label>'.
10346:                       '<input type="checkbox" name="'.$name.'" '.
10347:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
10348:             if ($user ne $dcname.':'.$dcdom) {
10349:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10350:             }
10351:             $table .= '</label></span>';
10352:             $rows ++;
10353:         }
10354:     }
10355:     return ($numdcs,$table,$rows);
10356: }
10357: 
10358: sub usersession_titles {
10359:     return &Apache::lonlocal::texthash(
10360:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
10361:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
10362:                spares => 'Servers offloaded to, when busy',
10363:                version => 'LON-CAPA version requirement',
10364:                excludedomain => 'Allow all, but exclude specific domains',
10365:                includedomain => 'Deny all, but include specific domains',
10366:                primary => 'Primary (checked first)',
10367:                default => 'Default',
10368:            );
10369: }
10370: 
10371: sub id_for_thisdom {
10372:     my (%servers) = @_;
10373:     my %altids;
10374:     foreach my $server (keys(%servers)) {
10375:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
10376:         if ($serverhome ne $server) {
10377:             $altids{$serverhome} = $server;
10378:         }
10379:     }
10380:     return %altids;
10381: }
10382: 
10383: sub count_servers {
10384:     my ($currbalancer,%servers) = @_;
10385:     my (@spares,$numspares);
10386:     foreach my $lonhost (sort(keys(%servers))) {
10387:         next if ($currbalancer eq $lonhost);
10388:         push(@spares,$lonhost);
10389:     }
10390:     if ($currbalancer) {
10391:         $numspares = scalar(@spares);
10392:     } else {
10393:         $numspares = scalar(@spares) - 1;
10394:     }
10395:     return ($numspares,@spares);
10396: }
10397: 
10398: sub lonbalance_targets_js {
10399:     my ($dom,$types,$servers,$settings) = @_;
10400:     my $select = &mt('Select');
10401:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
10402:     if (ref($servers) eq 'HASH') {
10403:         $alltargets = join("','",sort(keys(%{$servers})));
10404:         my @homedoms;
10405:         foreach my $server (sort(keys(%{$servers}))) {
10406:             if (&Apache::lonnet::host_domain($server) eq $dom) {
10407:                 push(@homedoms,'1');
10408:             } else {
10409:                 push(@homedoms,'0');
10410:             }
10411:         }
10412:         $allishome = join("','",@homedoms);
10413:     }
10414:     if (ref($types) eq 'ARRAY') {
10415:         if (@{$types} > 0) {
10416:             @alltypes = @{$types};
10417:         }
10418:     }
10419:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
10420:     $allinsttypes = join("','",@alltypes);
10421:     my (%currbalancer,%currtargets,%currrules,%existing);
10422:     if (ref($settings) eq 'HASH') {
10423:         %existing = %{$settings};
10424:     }
10425:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
10426:                               \%currtargets,\%currrules);
10427:     my $balancers = join("','",sort(keys(%currbalancer)));
10428:     return <<"END";
10429: 
10430: <script type="text/javascript">
10431: // <![CDATA[
10432: 
10433: currBalancers = new Array('$balancers');
10434: 
10435: function toggleTargets(balnum) {
10436:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10437:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
10438:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
10439:     var prevbalancer = prevhostitem.value;
10440:     var baltotal = document.getElementById('loadbalancing_total').value;
10441:     prevhostitem.value = balancer;
10442:     if (prevbalancer != '') {
10443:         var prevIdx = currBalancers.indexOf(prevbalancer);
10444:         if (prevIdx != -1) {
10445:             currBalancers.splice(prevIdx,1);
10446:         }
10447:     }
10448:     if (balancer == '') {
10449:         hideSpares(balnum);
10450:     } else {
10451:         var currIdx = currBalancers.indexOf(balancer);
10452:         if (currIdx == -1) {
10453:             currBalancers.push(balancer);
10454:         }
10455:         var homedoms = new Array('$allishome');
10456:         var ishomedom = homedoms[lonhostitem.selectedIndex];
10457:         showSpares(balancer,ishomedom,balnum);
10458:     }
10459:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
10460:     return;
10461: }
10462: 
10463: function showSpares(balancer,ishomedom,balnum) {
10464:     var alltargets = new Array('$alltargets');
10465:     var insttypes = new Array('$allinsttypes');
10466:     var offloadtypes = new Array('primary','default');
10467: 
10468:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
10469:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
10470:  
10471:     for (var i=0; i<offloadtypes.length; i++) {
10472:         var count = 0;
10473:         for (var j=0; j<alltargets.length; j++) {
10474:             if (alltargets[j] != balancer) {
10475:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
10476:                 item.value = alltargets[j];
10477:                 item.style.textAlign='left';
10478:                 item.style.textFace='normal';
10479:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
10480:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
10481:                     item.disabled = '';
10482:                 } else {
10483:                     item.disabled = 'disabled';
10484:                     item.checked = false;
10485:                 }
10486:                 count ++;
10487:             }
10488:         }
10489:     }
10490:     for (var k=0; k<insttypes.length; k++) {
10491:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
10492:             if (ishomedom == 1) {
10493:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
10494:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
10495:             } else {
10496:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
10497:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
10498:             }
10499:         } else {
10500:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
10501:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
10502:         }
10503:         if ((insttypes[k] != '_LC_external') && 
10504:             ((insttypes[k] != '_LC_internetdom') ||
10505:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
10506:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
10507:             item.options.length = 0;
10508:             item.options[0] = new Option("","",true,true);
10509:             var idx = 0;
10510:             for (var m=0; m<alltargets.length; m++) {
10511:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
10512:                     idx ++;
10513:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10514:                 }
10515:             }
10516:         }
10517:     }
10518:     return;
10519: }
10520: 
10521: function hideSpares(balnum) {
10522:     var alltargets = new Array('$alltargets');
10523:     var insttypes = new Array('$allinsttypes');
10524:     var offloadtypes = new Array('primary','default');
10525: 
10526:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
10527:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
10528: 
10529:     var total = alltargets.length - 1;
10530:     for (var i=0; i<offloadtypes; i++) {
10531:         for (var j=0; j<total; j++) {
10532:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
10533:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
10534:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
10535:         }
10536:     }
10537:     for (var k=0; k<insttypes.length; k++) {
10538:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
10539:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
10540:         if (insttypes[k] != '_LC_external') {
10541:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
10542:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
10543:         }
10544:     }
10545:     return;
10546: }
10547: 
10548: function checkOffloads(item,balnum,type) {
10549:     var alltargets = new Array('$alltargets');
10550:     var offloadtypes = new Array('primary','default');
10551:     if (item.checked) {
10552:         var total = alltargets.length - 1;
10553:         var other;
10554:         if (type == offloadtypes[0]) {
10555:             other = offloadtypes[1];
10556:         } else {
10557:             other = offloadtypes[0];
10558:         }
10559:         for (var i=0; i<total; i++) {
10560:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
10561:             if (server == item.value) {
10562:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
10563:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
10564:                 }
10565:             }
10566:         }
10567:     }
10568:     return;
10569: }
10570: 
10571: function singleServerToggle(balnum,type) {
10572:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
10573:     if (offloadtoSelIdx == 0) {
10574:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
10575:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
10576: 
10577:     } else {
10578:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
10579:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
10580:     }
10581:     return;
10582: }
10583: 
10584: function balanceruleChange(formname,balnum,type) {
10585:     if (type == '_LC_external') {
10586:         return;
10587:     }
10588:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
10589:     for (var i=0; i<typesRules.length; i++) {
10590:         if (formname.elements[typesRules[i]].checked) {
10591:             if (formname.elements[typesRules[i]].value != 'specific') {
10592:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
10593:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
10594:             } else {
10595:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
10596:             }
10597:         }
10598:     }
10599:     return;
10600: }
10601: 
10602: function balancerDeleteChange(balnum) {
10603:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10604:     var baltotal = document.getElementById('loadbalancing_total').value;
10605:     var addtarget;
10606:     var removetarget;
10607:     var action = 'delete';
10608:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
10609:         var lonhost = hostitem.value;
10610:         var currIdx = currBalancers.indexOf(lonhost);
10611:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
10612:             if (currIdx != -1) {
10613:                 currBalancers.splice(currIdx,1);
10614:             }
10615:             addtarget = lonhost;
10616:         } else {
10617:             if (currIdx == -1) {
10618:                 currBalancers.push(lonhost);
10619:             }
10620:             removetarget = lonhost;
10621:             action = 'undelete';
10622:         }
10623:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
10624:     }
10625:     return;
10626: }
10627: 
10628: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
10629:     if (baltotal > 1) {
10630:         var offloadtypes = new Array('primary','default');
10631:         var alltargets = new Array('$alltargets');
10632:         var insttypes = new Array('$allinsttypes');
10633:         for (var i=0; i<baltotal; i++) {
10634:             if (i != balnum) {
10635:                 for (var j=0; j<offloadtypes.length; j++) {
10636:                     var total = alltargets.length - 1;
10637:                     for (var k=0; k<total; k++) {
10638:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
10639:                         var server = serveritem.value;
10640:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
10641:                             if (server == addtarget) {
10642:                                 serveritem.disabled = '';
10643:                             }
10644:                         }
10645:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10646:                             if (server == removetarget) {
10647:                                 serveritem.disabled = 'disabled';
10648:                                 serveritem.checked = false;
10649:                             }
10650:                         }
10651:                     }
10652:                 }
10653:                 for (var j=0; j<insttypes.length; j++) {
10654:                     if (insttypes[j] != '_LC_external') {
10655:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
10656:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
10657:                             var currSel = singleserver.selectedIndex;
10658:                             var currVal = singleserver.options[currSel].value;
10659:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
10660:                                 var numoptions = singleserver.options.length;
10661:                                 var needsnew = 1;
10662:                                 for (var k=0; k<numoptions; k++) {
10663:                                     if (singleserver.options[k] == addtarget) {
10664:                                         needsnew = 0;
10665:                                         break;
10666:                                     }
10667:                                 }
10668:                                 if (needsnew == 1) {
10669:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
10670:                                 }
10671:                             }
10672:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10673:                                 singleserver.options.length = 0;
10674:                                 if ((currVal) && (currVal != removetarget)) {
10675:                                     singleserver.options[0] = new Option("","",false,false);
10676:                                 } else {
10677:                                     singleserver.options[0] = new Option("","",true,true);
10678:                                 }
10679:                                 var idx = 0;
10680:                                 for (var m=0; m<alltargets.length; m++) {
10681:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
10682:                                         idx ++;
10683:                                         if (currVal == alltargets[m]) {
10684:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
10685:                                         } else {
10686:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10687:                                         }
10688:                                     }
10689:                                 }
10690:                             }
10691:                         }
10692:                     }
10693:                 }
10694:             }
10695:         }
10696:     }
10697:     return;
10698: }
10699: 
10700: // ]]>
10701: </script>
10702: 
10703: END
10704: }
10705: 
10706: sub new_spares_js {
10707:     my @sparestypes = ('primary','default');
10708:     my $types = join("','",@sparestypes);
10709:     my $select = &mt('Select');
10710:     return <<"END";
10711: 
10712: <script type="text/javascript">
10713: // <![CDATA[
10714: 
10715: function updateNewSpares(formname,lonhost) {
10716:     var types = new Array('$types');
10717:     var include = new Array();
10718:     var exclude = new Array();
10719:     for (var i=0; i<types.length; i++) {
10720:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
10721:         for (var j=0; j<spareboxes.length; j++) {
10722:             if (formname.elements[spareboxes[j]].checked) {
10723:                 exclude.push(formname.elements[spareboxes[j]].value);
10724:             } else {
10725:                 include.push(formname.elements[spareboxes[j]].value);
10726:             }
10727:         }
10728:     }
10729:     for (var i=0; i<types.length; i++) {
10730:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
10731:         var selIdx = newSpare.selectedIndex;
10732:         var currnew = newSpare.options[selIdx].value;
10733:         var okSpares = new Array();
10734:         for (var j=0; j<newSpare.options.length; j++) {
10735:             var possible = newSpare.options[j].value;
10736:             if (possible != '') {
10737:                 if (exclude.indexOf(possible) == -1) {
10738:                     okSpares.push(possible);
10739:                 } else {
10740:                     if (currnew == possible) {
10741:                         selIdx = 0;
10742:                     }
10743:                 }
10744:             }
10745:         }
10746:         for (var k=0; k<include.length; k++) {
10747:             if (okSpares.indexOf(include[k]) == -1) {
10748:                 okSpares.push(include[k]);
10749:             }
10750:         }
10751:         okSpares.sort();
10752:         newSpare.options.length = 0;
10753:         if (selIdx == 0) {
10754:             newSpare.options[0] = new Option("$select","",true,true);
10755:         } else {
10756:             newSpare.options[0] = new Option("$select","",false,false);
10757:         }
10758:         for (var m=0; m<okSpares.length; m++) {
10759:             var idx = m+1;
10760:             var selThis = 0;
10761:             if (selIdx != 0) {
10762:                 if (okSpares[m] == currnew) {
10763:                     selThis = 1;
10764:                 }
10765:             }
10766:             if (selThis == 1) {
10767:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
10768:             } else {
10769:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
10770:             }
10771:         }
10772:     }
10773:     return;
10774: }
10775: 
10776: function checkNewSpares(lonhost,type) {
10777:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
10778:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
10779:     if (chosen != '') { 
10780:         var othertype;
10781:         var othernewSpare;
10782:         if (type == 'primary') {
10783:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
10784:         }
10785:         if (type == 'default') {
10786:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
10787:         }
10788:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
10789:             othernewSpare.selectedIndex = 0;
10790:         }
10791:     }
10792:     return;
10793: }
10794: 
10795: // ]]>
10796: </script>
10797: 
10798: END
10799: 
10800: }
10801: 
10802: sub common_domprefs_js {
10803:     return <<"END";
10804: 
10805: <script type="text/javascript">
10806: // <![CDATA[
10807: 
10808: function getIndicesByName(formname,item) {
10809:     var group = new Array();
10810:     for (var i=0;i<formname.elements.length;i++) {
10811:         if (formname.elements[i].name == item) {
10812:             group.push(formname.elements[i].id);
10813:         }
10814:     }
10815:     return group;
10816: }
10817: 
10818: // ]]>
10819: </script>
10820: 
10821: END
10822: 
10823: }
10824: 
10825: sub recaptcha_js {
10826:     my %lt = &captcha_phrases();
10827:     return <<"END";
10828: 
10829: <script type="text/javascript">
10830: // <![CDATA[
10831: 
10832: function updateCaptcha(caller,context) {
10833:     var privitem;
10834:     var pubitem;
10835:     var privtext;
10836:     var pubtext;
10837:     if (document.getElementById(context+'_recaptchapub')) {
10838:         pubitem = document.getElementById(context+'_recaptchapub');
10839:     } else {
10840:         return;
10841:     }
10842:     if (document.getElementById(context+'_recaptchapriv')) {
10843:         privitem = document.getElementById(context+'_recaptchapriv');
10844:     } else {
10845:         return;
10846:     }
10847:     if (document.getElementById(context+'_recaptchapubtxt')) {
10848:         pubtext = document.getElementById(context+'_recaptchapubtxt');
10849:     } else {
10850:         return;
10851:     }
10852:     if (document.getElementById(context+'_recaptchaprivtxt')) {
10853:         privtext = document.getElementById(context+'_recaptchaprivtxt');
10854:     } else {
10855:         return;
10856:     }
10857:     if (caller.checked) {
10858:         if (caller.value == 'recaptcha') {
10859:             pubitem.type = 'text';
10860:             privitem.type = 'text';
10861:             pubitem.size = '40';
10862:             privitem.size = '40';
10863:             pubtext.innerHTML = "$lt{'pub'}";
10864:             privtext.innerHTML = "$lt{'priv'}";
10865:         } else {
10866:             pubitem.type = 'hidden';
10867:             privitem.type = 'hidden';
10868:             pubtext.innerHTML = '';
10869:             privtext.innerHTML = '';
10870:         }
10871:     }
10872:     return;
10873: }
10874: 
10875: // ]]>
10876: </script>
10877: 
10878: END
10879: 
10880: }
10881: 
10882: sub credits_js {
10883:     return <<"END";
10884: 
10885: <script type="text/javascript">
10886: // <![CDATA[
10887: 
10888: function toggleCredits(domForm) {
10889:     if (document.getElementById('credits')) {
10890:         creditsitem = document.getElementById('credits');
10891:         var creditsLength = domForm.coursecredits.length;
10892:         if (creditsLength) {
10893:             var currval;
10894:             for (var i=0; i<creditsLength; i++) {
10895:                 if (domForm.coursecredits[i].checked) {
10896:                    currval = domForm.coursecredits[i].value;
10897:                 }
10898:             }
10899:             if (currval == 1) {
10900:                 creditsitem.style.display = 'block';
10901:             } else {
10902:                 creditsitem.style.display = 'none';
10903:             }
10904:         }
10905:     }
10906:     return;
10907: }
10908: 
10909: // ]]>
10910: </script>
10911: 
10912: END
10913: 
10914: }
10915: 
10916: sub captcha_phrases {
10917:     return &Apache::lonlocal::texthash (
10918:                  priv => 'Private key',
10919:                  pub  => 'Public key',
10920:                  original  => 'original (CAPTCHA)',
10921:                  recaptcha => 'successor (ReCAPTCHA)',
10922:                  notused   => 'unused',
10923:     );
10924: }
10925: 
10926: sub devalidate_remote_domconfs {
10927:     my ($dom,$cachekeys) = @_;
10928:     return unless (ref($cachekeys) eq 'HASH');
10929:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10930:     my %thismachine;
10931:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
10932:     my @posscached = ('domainconfig','domdefaults');
10933:     if (keys(%servers) > 1) {
10934:         foreach my $server (keys(%servers)) {
10935:             next if ($thismachine{$server});
10936:             my @cached;
10937:             foreach my $name (@posscached) {
10938:                 if ($cachekeys->{$name}) {
10939:                     push(@cached,&escape($name).':'.&escape($dom));
10940:                 }
10941:             }
10942:             if (@cached) {
10943:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
10944:             }
10945:         }
10946:     }
10947:     return;
10948: }
10949: 
10950: 1;

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