File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.231: download - view: text, annotated - select for diffs
Sat Mar 29 20:35:21 2014 UTC (10 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add domain configuration for self-enrollment in Course/Community.
  - Can set who (i.e., DC or CC) may set self-enrollment configuration
    settings based on course type (official, unofficial, community or
    textbook).
  - Can set defaults for certain self-enrollment settings (may vary by
    course type).
  - Can set option "validation", e.g., for e-commerce.
    - specify url, items to send as POSTed values, text of "submit" button,
      HTML description of validation requirement.
    - will apply if processing of self-enrollment set to:
      "Queued, pending validation".

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.231 2014/03/29 20:35:21 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 LONCAPA qw(:DEFAULT :match);
  168: use LONCAPA::Enrollment;
  169: use LONCAPA::lonauthcgi();
  170: use File::Copy;
  171: use Locale::Language;
  172: use DateTime::TimeZone;
  173: use DateTime::Locale;
  174: 
  175: my $registered_cleanup;
  176: my $modified_urls;
  177: 
  178: sub handler {
  179:     my $r=shift;
  180:     if ($r->header_only) {
  181:         &Apache::loncommon::content_type($r,'text/html');
  182:         $r->send_http_header;
  183:         return OK;
  184:     }
  185: 
  186:     my $context = 'domain';
  187:     my $dom = $env{'request.role.domain'};
  188:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  189:     if (&Apache::lonnet::allowed('mau',$dom)) {
  190:         &Apache::loncommon::content_type($r,'text/html');
  191:         $r->send_http_header;
  192:     } else {
  193:         $env{'user.error.msg'}=
  194:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  195:         return HTTP_NOT_ACCEPTABLE;
  196:     }
  197: 
  198:     $registered_cleanup=0;
  199:     @{$modified_urls}=();
  200: 
  201:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  202:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  203:                                             ['phase','actions']);
  204:     my $phase = 'pickactions';
  205:     if ( exists($env{'form.phase'}) ) {
  206:         $phase = $env{'form.phase'};
  207:     }
  208:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  209:     my %domconfig =
  210:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  211:                 'quotas','autoenroll','autoupdate','autocreate',
  212:                 'directorysrch','usercreation','usermodification',
  213:                 'contacts','defaults','scantron','coursecategories',
  214:                 'serverstatuses','requestcourses','helpsettings',
  215:                 'coursedefaults','usersessions','loadbalancing',
  216:                 'requestauthor','selfenrollment'],$dom);
  217:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  218:                        'autoupdate','autocreate','directorysrch','contacts',
  219:                        'usercreation','selfcreation','usermodification','scantron',
  220:                        'requestcourses','requestauthor','coursecategories',
  221:                        'serverstatuses','helpsettings',
  222:                        'coursedefaults','selfenrollment','usersessions');
  223:     my %existing;
  224:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  225:         %existing = %{$domconfig{'loadbalancing'}};
  226:     }
  227:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  228:         push(@prefs_order,'loadbalancing');
  229:     }
  230:     my %prefs = (
  231:         'rolecolors' =>
  232:                    { text => 'Default color schemes',
  233:                      help => 'Domain_Configuration_Color_Schemes',
  234:                      header => [{col1 => 'Student Settings',
  235:                                  col2 => '',},
  236:                                 {col1 => 'Coordinator Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Author Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Administrator Settings',
  241:                                  col2 => '',}],
  242:                       print => \&print_rolecolors,
  243:                       modify => \&modify_rolecolors,
  244:                     },
  245:         'login' =>
  246:                     { text => 'Log-in page options',
  247:                       help => 'Domain_Configuration_Login_Page',
  248:                       header => [{col1 => 'Log-in Page Items',
  249:                                   col2 => '',},
  250:                                  {col1 => 'Log-in Help',
  251:                                   col2 => 'Value'}],
  252:                       print => \&print_login,
  253:                       modify => \&modify_login,
  254:                     },
  255:         'defaults' => 
  256:                     { text => 'Default authentication/language/timezone/portal',
  257:                       help => 'Domain_Configuration_LangTZAuth',
  258:                       header => [{col1 => 'Setting',
  259:                                   col2 => 'Value'}],
  260:                       print => \&print_defaults,
  261:                       modify => \&modify_defaults,
  262:                     },
  263:         'quotas' => 
  264:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  265:                       help => 'Domain_Configuration_Quotas',
  266:                       header => [{col1 => 'User affiliation',
  267:                                   col2 => 'Available tools',
  268:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  269:                       print => \&print_quotas,
  270:                       modify => \&modify_quotas,
  271:                     },
  272:         'autoenroll' =>
  273:                    { text => 'Auto-enrollment settings',
  274:                      help => 'Domain_Configuration_Auto_Enrollment',
  275:                      header => [{col1 => 'Configuration setting',
  276:                                  col2 => 'Value(s)'}],
  277:                      print => \&print_autoenroll,
  278:                      modify => \&modify_autoenroll,
  279:                    },
  280:         'autoupdate' => 
  281:                    { text => 'Auto-update settings',
  282:                      help => 'Domain_Configuration_Auto_Updates',
  283:                      header => [{col1 => 'Setting',
  284:                                  col2 => 'Value',},
  285:                                 {col1 => 'Setting',
  286:                                  col2 => 'Affiliation'},
  287:                                 {col1 => 'User population',
  288:                                  col2 => 'Updatable user data'}],
  289:                      print => \&print_autoupdate,
  290:                      modify => \&modify_autoupdate,
  291:                   },
  292:         'autocreate' => 
  293:                   { text => 'Auto-course creation settings',
  294:                      help => 'Domain_Configuration_Auto_Creation',
  295:                      header => [{col1 => 'Configuration Setting',
  296:                                  col2 => 'Value',}],
  297:                      print => \&print_autocreate,
  298:                      modify => \&modify_autocreate,
  299:                   },
  300:         'directorysrch' => 
  301:                   { text => 'Institutional directory searches',
  302:                     help => 'Domain_Configuration_InstDirectory_Search',
  303:                     header => [{col1 => 'Setting',
  304:                                 col2 => 'Value',}],
  305:                     print => \&print_directorysrch,
  306:                     modify => \&modify_directorysrch,
  307:                   },
  308:         'contacts' =>
  309:                   { text => 'Contact Information',
  310:                     help => 'Domain_Configuration_Contact_Info',
  311:                     header => [{col1 => 'Setting',
  312:                                 col2 => 'Value',}],
  313:                     print => \&print_contacts,
  314:                     modify => \&modify_contacts,
  315:                   },
  316:         'usercreation' => 
  317:                   { text => 'User creation',
  318:                     help => 'Domain_Configuration_User_Creation',
  319:                     header => [{col1 => 'Format rule type',
  320:                                 col2 => 'Format rules in force'},
  321:                                {col1 => 'User account creation',
  322:                                 col2 => 'Usernames which may be created',},
  323:                                {col1 => 'Context',
  324:                                 col2 => 'Assignable authentication types'}],
  325:                     print => \&print_usercreation,
  326:                     modify => \&modify_usercreation,
  327:                   },
  328:         'selfcreation' => 
  329:                   { text => 'Users self-creating accounts',
  330:                     help => 'Domain_Configuration_Self_Creation', 
  331:                     header => [{col1 => 'Self-creation with institutional username',
  332:                                 col2 => 'Enabled?'},
  333:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  334:                                 col2 => 'Information user can enter'},
  335:                                {col1 => 'Self-creation with e-mail as username',
  336:                                 col2 => 'Settings'}],
  337:                     print => \&print_selfcreation,
  338:                     modify => \&modify_selfcreation,
  339:                   },
  340:         'usermodification' =>
  341:                   { text => 'User modification',
  342:                     help => 'Domain_Configuration_User_Modification',
  343:                     header => [{col1 => 'Target user has role',
  344:                                 col2 => 'User information updatable in author context'},
  345:                                {col1 => 'Target user has role',
  346:                                 col2 => 'User information updatable in course context'}],
  347:                     print => \&print_usermodification,
  348:                     modify => \&modify_usermodification,
  349:                   },
  350:         'scantron' =>
  351:                   { text => 'Bubblesheet format file',
  352:                     help => 'Domain_Configuration_Scantron_Format',
  353:                     header => [ {col1 => 'Item',
  354:                                  col2 => '',
  355:                               }],
  356:                     print => \&print_scantron,
  357:                     modify => \&modify_scantron,
  358:                   },
  359:         'requestcourses' => 
  360:                  {text => 'Request creation of courses',
  361:                   help => 'Domain_Configuration_Request_Courses',
  362:                   header => [{col1 => 'User affiliation',
  363:                               col2 => 'Availability/Processing of requests',},
  364:                              {col1 => 'Setting',
  365:                               col2 => 'Value'},
  366:                              {col1 => 'Available textbooks',
  367:                               col2 => ''}],
  368:                   print => \&print_quotas,
  369:                   modify => \&modify_quotas,
  370:                  },
  371:         'requestauthor' =>
  372:                  {text => 'Request Authoring Space',
  373:                   help => 'Domain_Configuration_Request_Author',
  374:                   header => [{col1 => 'User affiliation',
  375:                               col2 => 'Availability/Processing of requests',},
  376:                              {col1 => 'Setting',
  377:                               col2 => 'Value'}],
  378:                   print => \&print_quotas,
  379:                   modify => \&modify_quotas,
  380:                  },
  381:         'coursecategories' =>
  382:                   { text => 'Cataloging of courses/communities',
  383:                     help => 'Domain_Configuration_Cataloging_Courses',
  384:                     header => [{col1 => 'Category settings',
  385:                                 col2 => '',},
  386:                                {col1 => 'Categories',
  387:                                 col2 => '',
  388:                                }],
  389:                     print => \&print_coursecategories,
  390:                     modify => \&modify_coursecategories,
  391:                   },
  392:         'serverstatuses' =>
  393:                  {text   => 'Access to server status pages',
  394:                   help   => 'Domain_Configuration_Server_Status',
  395:                   header => [{col1 => 'Status Page',
  396:                               col2 => 'Other named users',
  397:                               col3 => 'Specific IPs',
  398:                             }],
  399:                   print => \&print_serverstatuses,
  400:                   modify => \&modify_serverstatuses,
  401:                  },
  402:         'helpsettings' =>
  403:                  {text   => 'Help page settings',
  404:                   help   => 'Domain_Configuration_Help_Settings',
  405:                   header => [{col1 => 'Help Settings (logged-in users)',
  406:                               col2 => 'Value'}],
  407:                   print  => \&print_helpsettings,
  408:                   modify => \&modify_helpsettings,
  409:                  },
  410:         'coursedefaults' => 
  411:                  {text => 'Course/Community defaults',
  412:                   help => 'Domain_Configuration_Course_Defaults',
  413:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  414:                               col2 => 'Value',},
  415:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  416:                               col2 => 'Value',},],
  417:                   print => \&print_coursedefaults,
  418:                   modify => \&modify_coursedefaults,
  419:                  },
  420:         'selfenrollment' => 
  421:                  {text   => 'Self-enrollment in Course/Community',
  422:                   help   => 'Domain_Configuration_Selfenrollment',
  423:                   header => [{col1 => 'Configuration Rights',
  424:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  425:                              {col1 => 'Defaults',
  426:                               col2 => 'Value'},
  427:                              {col1 => 'Self-enrollment validation (optional)',
  428:                               col2 => 'Value'},],
  429:                   print => \&print_selfenrollment,
  430:                   modify => \&modify_selfenrollment,
  431:                  },
  432:         'privacy' => 
  433:                  {text   => 'User Privacy',
  434:                   help   => 'Domain_Configuration_User_Privacy',
  435:                   header => [{col1 => 'Setting',
  436:                               col2 => 'Value',}],
  437:                   print => \&print_privacy,
  438:                   modify => \&modify_privacy,
  439:                  },
  440:         'usersessions' =>
  441:                  {text  => 'User session hosting/offloading',
  442:                   help  => 'Domain_Configuration_User_Sessions',
  443:                   header => [{col1 => 'Domain server',
  444:                               col2 => 'Servers to offload sessions to when busy'},
  445:                              {col1 => 'Hosting of users from other domains',
  446:                               col2 => 'Rules'},
  447:                              {col1 => "Hosting domain's own users elsewhere",
  448:                               col2 => 'Rules'}],
  449:                   print => \&print_usersessions,
  450:                   modify => \&modify_usersessions,
  451:                  },
  452:          'loadbalancing' =>
  453:                  {text  => 'Dedicated Load Balancer(s)',
  454:                   help  => 'Domain_Configuration_Load_Balancing',
  455:                   header => [{col1 => 'Balancers',
  456:                               col2 => 'Default destinations',
  457:                               col3 => 'User affiliation',
  458:                               col4 => 'Overrides'},
  459:                             ],
  460:                   print => \&print_loadbalancing,
  461:                   modify => \&modify_loadbalancing,
  462:                  },
  463:     );
  464:     if (keys(%servers) > 1) {
  465:         $prefs{'login'}  = { text   => 'Log-in page options',
  466:                              help   => 'Domain_Configuration_Login_Page',
  467:                             header => [{col1 => 'Log-in Service',
  468:                                         col2 => 'Server Setting',},
  469:                                        {col1 => 'Log-in Page Items',
  470:                                         col2 => ''},
  471:                                        {col1 => 'Log-in Help',
  472:                                         col2 => 'Value'}],
  473:                             print => \&print_login,
  474:                             modify => \&modify_login,
  475:                            };
  476:     }
  477: 
  478:     my @roles = ('student','coordinator','author','admin');
  479:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  480:     &Apache::lonhtmlcommon::add_breadcrumb
  481:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  482:       text=>"Settings to display/modify"});
  483:     my $confname = $dom.'-domainconfig';
  484: 
  485:     if ($phase eq 'process') {
  486:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  487:                                                               \%prefs,\%domconfig,$confname,\@roles);
  488:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  489:             $r->rflush();
  490:             &devalidate_remote_domconfs($dom,$result);
  491:         }
  492:     } elsif ($phase eq 'display') {
  493:         my $js = &recaptcha_js().
  494:                  &credits_js();
  495:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  496:             my ($othertitle,$usertypes,$types) =
  497:                 &Apache::loncommon::sorted_inst_types($dom);
  498:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  499:                                           $domconfig{'loadbalancing'}).
  500:                    &new_spares_js().
  501:                    &common_domprefs_js().
  502:                    &Apache::loncommon::javascript_array_indexof();
  503:         }
  504:         if (grep(/^requestcourses$/,@actions)) {
  505:             my $javascript_validations;
  506:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  507:             $js .= <<END;
  508: <script type="text/javascript">
  509: $javascript_validations
  510: </script>
  511: $coursebrowserjs
  512: END
  513:         }
  514:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  515:     } else {
  516: # check if domconfig user exists for the domain.
  517:         my $servadm = $r->dir_config('lonAdmEMail');
  518:         my ($configuserok,$author_ok,$switchserver) =
  519:             &config_check($dom,$confname,$servadm);
  520:         unless ($configuserok eq 'ok') {
  521:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  522:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  523:                           $confname).
  524:                       '<br />'
  525:             );
  526:             if ($switchserver) {
  527:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  528:                           '<br />'.
  529:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  530:                           '<br />'.
  531:                           &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).
  532:                           '<br />'.
  533:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  534:                 );
  535:             } else {
  536:                 $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.').
  537:                           '<br />'.
  538:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  539:                 );
  540:             }
  541:             $r->print(&Apache::loncommon::end_page());
  542:             return OK;
  543:         }
  544:         if (keys(%domconfig) == 0) {
  545:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  546:             my @ids=&Apache::lonnet::current_machine_ids();
  547:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  548:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  549:                 my @loginimages = ('img','logo','domlogo','login');
  550:                 my $custom_img_count = 0;
  551:                 foreach my $img (@loginimages) {
  552:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  553:                         $custom_img_count ++;
  554:                     }
  555:                 }
  556:                 foreach my $role (@roles) {
  557:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  558:                         $custom_img_count ++;
  559:                     }
  560:                 }
  561:                 if ($custom_img_count > 0) {
  562:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  563:                     my $switch_server = &check_switchserver($dom,$confname);
  564:                     $r->print(
  565:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  566:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  567:     &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 />'.
  568:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  569:                     if ($switch_server) {
  570:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  571:                     }
  572:                     $r->print(&Apache::loncommon::end_page());
  573:                     return OK;
  574:                 }
  575:             }
  576:         }
  577:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  578:     }
  579:     return OK;
  580: }
  581: 
  582: sub process_changes {
  583:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  584:     my %domconfig;
  585:     if (ref($values) eq 'HASH') {
  586:         %domconfig = %{$values};
  587:     }
  588:     my $output;
  589:     if ($action eq 'login') {
  590:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  591:     } elsif ($action eq 'rolecolors') {
  592:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  593:                                      $lastactref,%domconfig);
  594:     } elsif ($action eq 'quotas') {
  595:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  596:     } elsif ($action eq 'autoenroll') {
  597:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  598:     } elsif ($action eq 'autoupdate') {
  599:         $output = &modify_autoupdate($dom,%domconfig);
  600:     } elsif ($action eq 'autocreate') {
  601:         $output = &modify_autocreate($dom,%domconfig);
  602:     } elsif ($action eq 'directorysrch') {
  603:         $output = &modify_directorysrch($dom,%domconfig);
  604:     } elsif ($action eq 'usercreation') {
  605:         $output = &modify_usercreation($dom,%domconfig);
  606:     } elsif ($action eq 'selfcreation') {
  607:         $output = &modify_selfcreation($dom,%domconfig);
  608:     } elsif ($action eq 'usermodification') {
  609:         $output = &modify_usermodification($dom,%domconfig);
  610:     } elsif ($action eq 'contacts') {
  611:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  612:     } elsif ($action eq 'defaults') {
  613:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  614:     } elsif ($action eq 'scantron') {
  615:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  616:     } elsif ($action eq 'coursecategories') {
  617:         $output = &modify_coursecategories($dom,%domconfig);
  618:     } elsif ($action eq 'serverstatuses') {
  619:         $output = &modify_serverstatuses($dom,%domconfig);
  620:     } elsif ($action eq 'requestcourses') {
  621:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  622:     } elsif ($action eq 'requestauthor') {
  623:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  624:     } elsif ($action eq 'helpsettings') {
  625:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  626:     } elsif ($action eq 'coursedefaults') {
  627:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  628:     } elsif ($action eq 'selfenrollment') {
  629:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  630:     } elsif ($action eq 'usersessions') {
  631:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  632:     } elsif ($action eq 'loadbalancing') {
  633:         $output = &modify_loadbalancing($dom,%domconfig);
  634:     }
  635:     return $output;
  636: }
  637: 
  638: sub print_config_box {
  639:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  640:     my $rowtotal = 0;
  641:     my $output;
  642:     if ($action eq 'coursecategories') {
  643:         $output = &coursecategories_javascript($settings);
  644:     }
  645:     $output .= 
  646:          '<table class="LC_nested_outer">
  647:           <tr>
  648:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  649:            &mt($item->{text}).'&nbsp;'.
  650:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  651:           '</tr>';
  652:     $rowtotal ++;
  653:     my $numheaders = 1;
  654:     if (ref($item->{'header'}) eq 'ARRAY') {
  655:         $numheaders = scalar(@{$item->{'header'}});
  656:     }
  657:     if ($numheaders > 1) {
  658:         my $colspan = '';
  659:         my $rightcolspan = '';
  660:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  661:             (($action eq 'login') && ($numheaders < 3))) {
  662:             $colspan = ' colspan="2"';
  663:         }
  664:         if ($action eq 'usersessions') {
  665:             $rightcolspan = ' colspan="3"'; 
  666:         }
  667:         $output .= '
  668:           <tr>
  669:            <td>
  670:             <table class="LC_nested">
  671:              <tr class="LC_info_row">
  672:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  673:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  674:              </tr>';
  675:         $rowtotal ++;
  676:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  677:             ($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  678:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  679:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  680:         } elsif ($action eq 'coursecategories') {
  681:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  682:         } elsif ($action eq 'login') {
  683:             if ($numheaders == 3) {
  684:                 $colspan = ' colspan="2"';
  685:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  686:             } else {
  687:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  688:             }
  689:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  690:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  691:         } elsif ($action eq 'rolecolors') {
  692:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  693:         }
  694:         $output .= '
  695:            </table>
  696:           </td>
  697:          </tr>
  698:          <tr>
  699:            <td>
  700:             <table class="LC_nested">
  701:              <tr class="LC_info_row">
  702:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  703:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  704:              </tr>';
  705:             $rowtotal ++;
  706:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  707:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  708:             ($action eq 'usersessions')) {
  709:             $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal).'
  710:            </table>
  711:           </td>
  712:          </tr>
  713:          <tr>
  714:            <td>
  715:             <table class="LC_nested">
  716:              <tr class="LC_info_row">
  717:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  718:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  719:              </tr>'."\n".
  720:             $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  721:             $rowtotal ++;
  722:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults')) {
  723:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  724:         } elsif ($action eq 'coursecategories') {
  725:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  726:         } elsif ($action eq 'login') {
  727:             if ($numheaders == 3) {
  728:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  729:            </table>
  730:           </td>
  731:          </tr>
  732:          <tr>
  733:            <td>
  734:             <table class="LC_nested">
  735:              <tr class="LC_info_row">
  736:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  737:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  738:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  739:                 $rowtotal ++;
  740:             } else {
  741:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  742:             }
  743:         } elsif ($action eq 'requestcourses') {
  744:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  745:                        &print_studentcode($settings,\$rowtotal).'
  746:            </table>
  747:           </td>
  748:          </tr>
  749:          <tr>
  750:            <td>
  751:             <table class="LC_nested">
  752:              <tr class="LC_info_row">
  753:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  754:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  755:                        &print_textbookcourses($dom,$settings,\$rowtotal);
  756:         } elsif ($action eq 'requestauthor') {
  757:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  758:         } elsif ($action eq 'rolecolors') {
  759:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  760:            </table>
  761:           </td>
  762:          </tr>
  763:          <tr>
  764:            <td>
  765:             <table class="LC_nested">
  766:              <tr class="LC_info_row">
  767:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  768:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  769:               <td class="LC_right_item" valign="top">'.
  770:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  771:              </tr>'.
  772:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  773:            </table>
  774:           </td>
  775:          </tr>
  776:          <tr>
  777:            <td>
  778:             <table class="LC_nested">
  779:              <tr class="LC_info_row">
  780:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  781:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  782:              </tr>'.
  783:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  784:             $rowtotal += 2;
  785:         }
  786:     } else {
  787:         $output .= '
  788:           <tr>
  789:            <td>
  790:             <table class="LC_nested">
  791:              <tr class="LC_info_row">';
  792:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  793:             $output .= '  
  794:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  795:         } elsif ($action eq 'serverstatuses') {
  796:             $output .= '
  797:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  798:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  799: 
  800:         } else {
  801:             $output .= '
  802:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  803:         }
  804:         if (defined($item->{'header'}->[0]->{'col3'})) {
  805:             $output .= '<td class="LC_left_item" valign="top">'.
  806:                        &mt($item->{'header'}->[0]->{'col2'});
  807:             if ($action eq 'serverstatuses') {
  808:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  809:             } 
  810:         } else {
  811:             $output .= '<td class="LC_right_item" valign="top">'.
  812:                        &mt($item->{'header'}->[0]->{'col2'});
  813:         }
  814:         $output .= '</td>';
  815:         if ($item->{'header'}->[0]->{'col3'}) {
  816:             if (defined($item->{'header'}->[0]->{'col4'})) {
  817:                 $output .= '<td class="LC_left_item" valign="top">'.
  818:                             &mt($item->{'header'}->[0]->{'col3'});
  819:             } else {
  820:                 $output .= '<td class="LC_right_item" valign="top">'.
  821:                            &mt($item->{'header'}->[0]->{'col3'});
  822:             }
  823:             if ($action eq 'serverstatuses') {
  824:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  825:             }
  826:             $output .= '</td>';
  827:         }
  828:         if ($item->{'header'}->[0]->{'col4'}) {
  829:             $output .= '<td class="LC_right_item" valign="top">'.
  830:                        &mt($item->{'header'}->[0]->{'col4'});
  831:         }
  832:         $output .= '</tr>';
  833:         $rowtotal ++;
  834:         if ($action eq 'quotas') {
  835:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  836:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  837:                  ($action eq 'contacts') || ($action eq 'defaults') || ($action eq 'serverstatuses') ||
  838:                  ($action eq 'loadbalancing')) {
  839:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  840:         } elsif ($action eq 'scantron') {
  841:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  842:         } elsif ($action eq 'helpsettings') {
  843:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  844:         }
  845:     }
  846:     $output .= '
  847:    </table>
  848:   </td>
  849:  </tr>
  850: </table><br />';
  851:     return ($output,$rowtotal);
  852: }
  853: 
  854: sub print_login {
  855:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  856:     my ($css_class,$datatable);
  857:     my %choices = &login_choices();
  858: 
  859:     if ($caller eq 'service') {
  860:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  861:         my $choice = $choices{'disallowlogin'};
  862:         $css_class = ' class="LC_odd_row"';
  863:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  864:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  865:                       '<th>'.$choices{'server'}.'</th>'.
  866:                       '<th>'.$choices{'serverpath'}.'</th>'.
  867:                       '<th>'.$choices{'custompath'}.'</th>'.
  868:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  869:         my %disallowed;
  870:         if (ref($settings) eq 'HASH') {
  871:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  872:                %disallowed = %{$settings->{'loginvia'}};
  873:             }
  874:         }
  875:         foreach my $lonhost (sort(keys(%servers))) {
  876:             my $direct = 'selected="selected"';
  877:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  878:                 if ($disallowed{$lonhost}{'server'} ne '') {
  879:                     $direct = '';
  880:                 }
  881:             }
  882:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  883:                           '<td><select name="'.$lonhost.'_server">'.
  884:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  885:                           '</option>';
  886:             foreach my $hostid (sort(keys(%servers))) {
  887:                 next if ($servers{$hostid} eq $servers{$lonhost});
  888:                 my $selected = '';
  889:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  890:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  891:                         $selected = 'selected="selected"';
  892:                     }
  893:                 }
  894:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  895:                               $servers{$hostid}.'</option>';
  896:             }
  897:             $datatable .= '</select></td>'.
  898:                           '<td><select name="'.$lonhost.'_serverpath">';
  899:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  900:                 my $pathname = $path;
  901:                 if ($path eq 'custom') {
  902:                     $pathname = &mt('Custom Path').' ->';
  903:                 }
  904:                 my $selected = '';
  905:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  906:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  907:                         $selected = 'selected="selected"';
  908:                     }
  909:                 } elsif ($path eq '') {
  910:                     $selected = 'selected="selected"';
  911:                 }
  912:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  913:             }
  914:             $datatable .= '</select></td>';
  915:             my ($custom,$exempt);
  916:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  917:                 $custom = $disallowed{$lonhost}{'custompath'};
  918:                 $exempt = $disallowed{$lonhost}{'exempt'};
  919:             }
  920:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  921:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  922:                           '</tr>';
  923:         }
  924:         $datatable .= '</table></td></tr>';
  925:         return $datatable;
  926:     } elsif ($caller eq 'page') {
  927:         my %defaultchecked = ( 
  928:                                'coursecatalog' => 'on',
  929:                                'helpdesk'      => 'on',
  930:                                'adminmail'     => 'off',
  931:                                'newuser'       => 'off',
  932:                              );
  933:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  934:         my (%checkedon,%checkedoff);
  935:         foreach my $item (@toggles) {
  936:             if ($defaultchecked{$item} eq 'on') { 
  937:                 $checkedon{$item} = ' checked="checked" ';
  938:                 $checkedoff{$item} = ' ';
  939:             } elsif ($defaultchecked{$item} eq 'off') {
  940:                 $checkedoff{$item} = ' checked="checked" ';
  941:                 $checkedon{$item} = ' ';
  942:             }
  943:         }
  944:         my @images = ('img','logo','domlogo','login');
  945:         my @logintext = ('textcol','bgcol');
  946:         my @bgs = ('pgbg','mainbg','sidebg');
  947:         my @links = ('link','alink','vlink');
  948:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  949:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  950:         my (%is_custom,%designs);
  951:         my %defaults = (
  952:                        font => $defaultdesign{'login.font'},
  953:                        );
  954:         foreach my $item (@images) {
  955:             $defaults{$item} = $defaultdesign{'login.'.$item};
  956:             $defaults{'showlogo'}{$item} = 1;
  957:         }
  958:         foreach my $item (@bgs) {
  959:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  960:         }
  961:         foreach my $item (@logintext) {
  962:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  963:         }
  964:         foreach my $item (@links) {
  965:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  966:         }
  967:         if (ref($settings) eq 'HASH') {
  968:             foreach my $item (@toggles) {
  969:                 if ($settings->{$item} eq '1') {
  970:                     $checkedon{$item} =  ' checked="checked" ';
  971:                     $checkedoff{$item} = ' ';
  972:                 } elsif ($settings->{$item} eq '0') {
  973:                     $checkedoff{$item} =  ' checked="checked" ';
  974:                     $checkedon{$item} = ' ';
  975:                 }
  976:             }
  977:             foreach my $item (@images) {
  978:                 if (defined($settings->{$item})) {
  979:                     $designs{$item} = $settings->{$item};
  980:                     $is_custom{$item} = 1;
  981:                 }
  982:                 if (defined($settings->{'showlogo'}{$item})) {
  983:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  984:                 }
  985:             }
  986:             foreach my $item (@logintext) {
  987:                 if ($settings->{$item} ne '') {
  988:                     $designs{'logintext'}{$item} = $settings->{$item};
  989:                     $is_custom{$item} = 1;
  990:                 }
  991:             }
  992:             if ($settings->{'font'} ne '') {
  993:                 $designs{'font'} = $settings->{'font'};
  994:                 $is_custom{'font'} = 1;
  995:             }
  996:             foreach my $item (@bgs) {
  997:                 if ($settings->{$item} ne '') {
  998:                     $designs{'bgs'}{$item} = $settings->{$item};
  999:                     $is_custom{$item} = 1;
 1000:                 }
 1001:             }
 1002:             foreach my $item (@links) {
 1003:                 if ($settings->{$item} ne '') {
 1004:                     $designs{'links'}{$item} = $settings->{$item};
 1005:                     $is_custom{$item} = 1;
 1006:                 }
 1007:             }
 1008:         } else {
 1009:             if ($designhash{$dom.'.login.font'} ne '') {
 1010:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1011:                 $is_custom{'font'} = 1;
 1012:             }
 1013:             foreach my $item (@images) {
 1014:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1015:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1016:                     $is_custom{$item} = 1;
 1017:                 }
 1018:             }
 1019:             foreach my $item (@bgs) {
 1020:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1021:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1022:                     $is_custom{$item} = 1;
 1023:                 }
 1024:             }
 1025:             foreach my $item (@links) {
 1026:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1027:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1028:                     $is_custom{$item} = 1;
 1029:                 }
 1030:             }
 1031:         }
 1032:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1033:                                                       logo => 'Institution Logo',
 1034:                                                       domlogo => 'Domain Logo',
 1035:                                                       login => 'Login box');
 1036:         my $itemcount = 1;
 1037:         foreach my $item (@toggles) {
 1038:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1039:             $datatable .=  
 1040:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1041:                 '</td><td>'.
 1042:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1043:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1044:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1045:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1046:                 '</tr>';
 1047:             $itemcount ++;
 1048:         }
 1049:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1050:         $datatable .= '</tr></table></td></tr>';
 1051:     } elsif ($caller eq 'help') {
 1052:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1053:         my $switchserver = &check_switchserver($dom,$confname);
 1054:         my $itemcount = 1;
 1055:         $defaulturl = '/adm/loginproblems.html';
 1056:         $defaulttype = 'default';
 1057:         %lt = &Apache::lonlocal::texthash (
 1058:                      del     => 'Delete?',
 1059:                      rep     => 'Replace:',
 1060:                      upl     => 'Upload:',
 1061:                      default => 'Default',
 1062:                      custom  => 'Custom',
 1063:                                              );
 1064:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1065:         my @currlangs;
 1066:         if (ref($settings) eq 'HASH') {
 1067:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1068:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1069:                     next if ($settings->{'helpurl'}{$key} eq '');
 1070:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1071:                     $type{$key} = 'custom';
 1072:                     unless ($key eq 'nolang') {
 1073:                         push(@currlangs,$key);
 1074:                     }
 1075:                 }
 1076:             } elsif ($settings->{'helpurl'} ne '') {
 1077:                 $type{'nolang'} = 'custom';
 1078:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1079:             }
 1080:         }
 1081:         foreach my $lang ('nolang',sort(@currlangs)) {
 1082:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1083:             $datatable .= '<tr'.$css_class.'>';
 1084:             if ($url{$lang} eq '') {
 1085:                 $url{$lang} = $defaulturl;
 1086:             }
 1087:             if ($type{$lang} eq '') {
 1088:                 $type{$lang} = $defaulttype;
 1089:             }
 1090:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1091:             if ($lang eq 'nolang') {
 1092:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1093:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1094:             } else {
 1095:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1096:                                   $langchoices{$lang},
 1097:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1098:             }
 1099:             $datatable .= '</span></td>'."\n".
 1100:                           '<td class="LC_left_item">';
 1101:             if ($type{$lang} eq 'custom') {
 1102:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1103:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1104:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1105:             } else {
 1106:                 $datatable .= $lt{'upl'};
 1107:             }
 1108:             $datatable .='<br />';
 1109:             if ($switchserver) {
 1110:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1111:             } else {
 1112:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1113:             }
 1114:             $datatable .= '</td></tr>';
 1115:             $itemcount ++;
 1116:         }
 1117:         my @addlangs;
 1118:         foreach my $lang (sort(keys(%langchoices))) {
 1119:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1120:             push(@addlangs,$lang);
 1121:         }
 1122:         if (@addlangs > 0) {
 1123:             my %toadd;
 1124:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1125:             $toadd{''} = &mt('Select');
 1126:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1127:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1128:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1129:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1130:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1131:             if ($switchserver) {
 1132:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1133:             } else {
 1134:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1135:             }
 1136:             $datatable .= '</td></tr>';
 1137:             $itemcount ++;
 1138:         }
 1139:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1140:     }
 1141:     return $datatable;
 1142: }
 1143: 
 1144: sub login_choices {
 1145:     my %choices =
 1146:         &Apache::lonlocal::texthash (
 1147:             coursecatalog => 'Display Course/Community Catalog link?',
 1148:             adminmail     => "Display Administrator's E-mail Address?",
 1149:             helpdesk      => 'Display "Contact Helpdesk" link',
 1150:             disallowlogin => "Login page requests redirected",
 1151:             hostid        => "Server",
 1152:             server        => "Redirect to:",
 1153:             serverpath    => "Path",
 1154:             custompath    => "Custom", 
 1155:             exempt        => "Exempt IP(s)",
 1156:             directlogin   => "No redirect",
 1157:             newuser       => "Link to create a user account",
 1158:             img           => "Header",
 1159:             logo          => "Main Logo",
 1160:             domlogo       => "Domain Logo",
 1161:             login         => "Log-in Header", 
 1162:             textcol       => "Text color",
 1163:             bgcol         => "Box color",
 1164:             bgs           => "Background colors",
 1165:             links         => "Link colors",
 1166:             font          => "Font color",
 1167:             pgbg          => "Header",
 1168:             mainbg        => "Page",
 1169:             sidebg        => "Login box",
 1170:             link          => "Link",
 1171:             alink         => "Active link",
 1172:             vlink         => "Visited link",
 1173:         );
 1174:     return %choices;
 1175: }
 1176: 
 1177: sub print_rolecolors {
 1178:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1179:     my %choices = &color_font_choices();
 1180:     my @bgs = ('pgbg','tabbg','sidebg');
 1181:     my @links = ('link','alink','vlink');
 1182:     my @images = ('img');
 1183:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1184:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1185:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1186:     my (%is_custom,%designs);
 1187:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1188:     if (ref($settings) eq 'HASH') {
 1189:         if (ref($settings->{$role}) eq 'HASH') {
 1190:             if ($settings->{$role}->{'img'} ne '') {
 1191:                 $designs{'img'} = $settings->{$role}->{'img'};
 1192:                 $is_custom{'img'} = 1;
 1193:             }
 1194:             if ($settings->{$role}->{'font'} ne '') {
 1195:                 $designs{'font'} = $settings->{$role}->{'font'};
 1196:                 $is_custom{'font'} = 1;
 1197:             }
 1198:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1199:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1200:                 $is_custom{'fontmenu'} = 1;
 1201:             }
 1202:             foreach my $item (@bgs) {
 1203:                 if ($settings->{$role}->{$item} ne '') {
 1204:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1205:                     $is_custom{$item} = 1;
 1206:                 }
 1207:             }
 1208:             foreach my $item (@links) {
 1209:                 if ($settings->{$role}->{$item} ne '') {
 1210:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1211:                     $is_custom{$item} = 1;
 1212:                 }
 1213:             }
 1214:         }
 1215:     } else {
 1216:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1217:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1218:             $is_custom{'img'} = 1;
 1219:         }
 1220:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1221:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1222:             $is_custom{'fontmenu'} = 1; 
 1223:         }
 1224:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1225:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1226:             $is_custom{'font'} = 1;
 1227:         }
 1228:         foreach my $item (@bgs) {
 1229:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1230:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1231:                 $is_custom{$item} = 1;
 1232:             
 1233:             }
 1234:         }
 1235:         foreach my $item (@links) {
 1236:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1237:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1238:                 $is_custom{$item} = 1;
 1239:             }
 1240:         }
 1241:     }
 1242:     my $itemcount = 1;
 1243:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1244:     $datatable .= '</tr></table></td></tr>';
 1245:     return $datatable;
 1246: }
 1247: 
 1248: sub role_defaults {
 1249:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1250:     my %defaults;
 1251:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1252:         return %defaults;
 1253:     }
 1254:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1255:     if ($role eq 'login') {
 1256:         %defaults = (
 1257:                        font => $defaultdesign{$role.'.font'},
 1258:                     );
 1259:         if (ref($logintext) eq 'ARRAY') {
 1260:             foreach my $item (@{$logintext}) {
 1261:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1262:             }
 1263:         }
 1264:         foreach my $item (@{$images}) {
 1265:             $defaults{'showlogo'}{$item} = 1;
 1266:         }
 1267:     } else {
 1268:         %defaults = (
 1269:                        img => $defaultdesign{$role.'.img'},
 1270:                        font => $defaultdesign{$role.'.font'},
 1271:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1272:                     );
 1273:     }
 1274:     foreach my $item (@{$bgs}) {
 1275:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1276:     }
 1277:     foreach my $item (@{$links}) {
 1278:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1279:     }
 1280:     foreach my $item (@{$images}) {
 1281:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1282:     }
 1283:     return %defaults;
 1284: }
 1285: 
 1286: sub display_color_options {
 1287:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1288:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1289:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1290:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1291:     my $datatable = '<tr'.$css_class.'>'.
 1292:         '<td>'.$choices->{'font'}.'</td>';
 1293:     if (!$is_custom->{'font'}) {
 1294:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1295:     } else {
 1296:         $datatable .= '<td>&nbsp;</td>';
 1297:     }
 1298:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1299: 
 1300:     $datatable .= '<td><span class="LC_nobreak">'.
 1301:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1302:                   ' value="'.$current_color.'" />&nbsp;'.
 1303:                   '&nbsp;</td></tr>';
 1304:     unless ($role eq 'login') { 
 1305:         $datatable .= '<tr'.$css_class.'>'.
 1306:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1307:         if (!$is_custom->{'fontmenu'}) {
 1308:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1309:         } else {
 1310:             $datatable .= '<td>&nbsp;</td>';
 1311:         }
 1312: 	$current_color = $designs->{'fontmenu'} ?
 1313: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1314:         $datatable .= '<td><span class="LC_nobreak">'.
 1315:                       '<input class="colorchooser" type="text" size="10" name="'
 1316: 		      .$role.'_fontmenu"'.
 1317:                       ' value="'.$current_color.'" />&nbsp;'.
 1318:                       '&nbsp;</td></tr>';
 1319:     }
 1320:     my $switchserver = &check_switchserver($dom,$confname);
 1321:     foreach my $img (@{$images}) {
 1322: 	$itemcount ++;
 1323:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1324:         $datatable .= '<tr'.$css_class.'>'.
 1325:                       '<td>'.$choices->{$img};
 1326:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1327:         if ($role eq 'login') {
 1328:             if ($img eq 'login') {
 1329:                 $login_hdr_pick =
 1330:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1331:                 $logincolors =
 1332:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1333:                                        $designs,$defaults);
 1334:             } elsif ($img ne 'domlogo') {
 1335:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1336:             }
 1337:         }
 1338:         $datatable .= '</td>';
 1339:         if ($designs->{$img} ne '') {
 1340:             $imgfile = $designs->{$img};
 1341: 	    $img_import = ($imgfile =~ m{^/adm/});
 1342:         } else {
 1343:             $imgfile = $defaults->{$img};
 1344:         }
 1345:         if ($imgfile) {
 1346:             my ($showfile,$fullsize);
 1347:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1348:                 my $urldir = $1;
 1349:                 my $filename = $2;
 1350:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1351:                 if (@info) {
 1352:                     my $thumbfile = 'tn-'.$filename;
 1353:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1354:                     if (@thumb) {
 1355:                         $showfile = $urldir.'/'.$thumbfile;
 1356:                     } else {
 1357:                         $showfile = $imgfile;
 1358:                     }
 1359:                 } else {
 1360:                     $showfile = '';
 1361:                 }
 1362:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1363:                 $showfile = $imgfile;
 1364:                 my $imgdir = $1;
 1365:                 my $filename = $2;
 1366:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1367:                     $showfile = "/$imgdir/tn-".$filename;
 1368:                 } else {
 1369:                     my $input = $londocroot.$imgfile;
 1370:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1371:                     if (!-e $output) {
 1372:                         my ($width,$height) = &thumb_dimensions();
 1373:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1374:                         if ($fullwidth ne '' && $fullheight ne '') {
 1375:                             if ($fullwidth > $width && $fullheight > $height) { 
 1376:                                 my $size = $width.'x'.$height;
 1377:                                 system("convert -sample $size $input $output");
 1378:                                 $showfile = "/$imgdir/tn-".$filename;
 1379:                             }
 1380:                         }
 1381:                     }
 1382:                 }
 1383:             }
 1384:             if ($showfile) {
 1385:                 if ($showfile =~ m{^/(adm|res)/}) {
 1386:                     if ($showfile =~ m{^/res/}) {
 1387:                         my $local_showfile =
 1388:                             &Apache::lonnet::filelocation('',$showfile);
 1389:                         &Apache::lonnet::repcopy($local_showfile);
 1390:                     }
 1391:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1392:                 }
 1393:                 if ($imgfile) {
 1394:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1395:                         if ($imgfile =~ m{^/res/}) {
 1396:                             my $local_imgfile =
 1397:                                 &Apache::lonnet::filelocation('',$imgfile);
 1398:                             &Apache::lonnet::repcopy($local_imgfile);
 1399:                         }
 1400:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1401:                     } else {
 1402:                         $fullsize = $imgfile;
 1403:                     }
 1404:                 }
 1405:                 $datatable .= '<td>';
 1406:                 if ($img eq 'login') {
 1407:                     $datatable .= $login_hdr_pick;
 1408:                 } 
 1409:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1410:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1411:             } else {
 1412:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1413:                               &mt('Upload:').'<br />';
 1414:             }
 1415:         } else {
 1416:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1417:                           &mt('Upload:').'<br />';
 1418:         }
 1419:         if ($switchserver) {
 1420:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1421:         } else {
 1422:             if ($img ne 'login') { # suppress file selection for Log-in header
 1423:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1424:             }
 1425:         }
 1426:         $datatable .= '</td></tr>';
 1427:     }
 1428:     $itemcount ++;
 1429:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1430:     $datatable .= '<tr'.$css_class.'>'.
 1431:                   '<td>'.$choices->{'bgs'}.'</td>';
 1432:     my $bgs_def;
 1433:     foreach my $item (@{$bgs}) {
 1434:         if (!$is_custom->{$item}) {
 1435:             $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>';
 1436:         }
 1437:     }
 1438:     if ($bgs_def) {
 1439:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1440:     } else {
 1441:         $datatable .= '<td>&nbsp;</td>';
 1442:     }
 1443:     $datatable .= '<td class="LC_right_item">'.
 1444:                   '<table border="0"><tr>';
 1445: 
 1446:     foreach my $item (@{$bgs}) {
 1447:         $datatable .= '<td align="center">'.$choices->{$item};
 1448: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1449:         if ($designs->{'bgs'}{$item}) {
 1450:             $datatable .= '&nbsp;';
 1451:         }
 1452:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1453:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1454:     }
 1455:     $datatable .= '</tr></table></td></tr>';
 1456:     $itemcount ++;
 1457:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1458:     $datatable .= '<tr'.$css_class.'>'.
 1459:                   '<td>'.$choices->{'links'}.'</td>';
 1460:     my $links_def;
 1461:     foreach my $item (@{$links}) {
 1462:         if (!$is_custom->{$item}) {
 1463:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1464:         }
 1465:     }
 1466:     if ($links_def) {
 1467:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1468:     } else {
 1469:         $datatable .= '<td>&nbsp;</td>';
 1470:     }
 1471:     $datatable .= '<td class="LC_right_item">'.
 1472:                   '<table border="0"><tr>';
 1473:     foreach my $item (@{$links}) {
 1474: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1475:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1476:         if ($designs->{'links'}{$item}) {
 1477:             $datatable.='&nbsp;';
 1478:         }
 1479:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1480:                       '" /></td>';
 1481:     }
 1482:     $$rowtotal += $itemcount;
 1483:     return $datatable;
 1484: }
 1485: 
 1486: sub logo_display_options {
 1487:     my ($img,$defaults,$designs) = @_;
 1488:     my $checkedon;
 1489:     if (ref($defaults) eq 'HASH') {
 1490:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1491:             if ($defaults->{'showlogo'}{$img}) {
 1492:                 $checkedon = 'checked="checked" ';     
 1493:             }
 1494:         } 
 1495:     }
 1496:     if (ref($designs) eq 'HASH') {
 1497:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1498:             if (defined($designs->{'showlogo'}{$img})) {
 1499:                 if ($designs->{'showlogo'}{$img} == 0) {
 1500:                     $checkedon = '';
 1501:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1502:                     $checkedon = 'checked="checked" ';
 1503:                 }
 1504:             }
 1505:         }
 1506:     }
 1507:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1508:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1509:            &mt('show').'</label>'."\n";
 1510: }
 1511: 
 1512: sub login_header_options  {
 1513:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1514:     my $output = '';
 1515:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1516:         $output .= &mt('Text default(s):').'<br />';
 1517:         if (!$is_custom->{'textcol'}) {
 1518:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1519:                        '&nbsp;&nbsp;&nbsp;';
 1520:         }
 1521:         if (!$is_custom->{'bgcol'}) {
 1522:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1523:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1524:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1525:         }
 1526:         $output .= '<br />';
 1527:     }
 1528:     $output .='<br />';
 1529:     return $output;
 1530: }
 1531: 
 1532: sub login_text_colors {
 1533:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1534:     my $color_menu = '<table border="0"><tr>';
 1535:     foreach my $item (@{$logintext}) {
 1536:         $color_menu .= '<td align="center">'.$choices->{$item};
 1537:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1538:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1539:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1540:     }
 1541:     $color_menu .= '</tr></table><br />';
 1542:     return $color_menu;
 1543: }
 1544: 
 1545: sub image_changes {
 1546:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1547:     my $output;
 1548:     if ($img eq 'login') {
 1549:             # suppress image for Log-in header
 1550:     } elsif (!$is_custom) {
 1551:         if ($img ne 'domlogo') {
 1552:             $output .= &mt('Default image:').'<br />';
 1553:         } else {
 1554:             $output .= &mt('Default in use:').'<br />';
 1555:         }
 1556:     }
 1557:     if ($img eq 'login') { # suppress image for Log-in header
 1558:         $output .= '<td>'.$logincolors;
 1559:     } else {
 1560:         if ($img_import) {
 1561:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1562:         }
 1563:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1564:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1565:         if ($is_custom) {
 1566:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1567:                        '<input type="checkbox" name="'.
 1568:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1569:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1570:         } else {
 1571:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1572:         }
 1573:     }
 1574:     return $output;
 1575: }
 1576: 
 1577: sub print_quotas {
 1578:     my ($dom,$settings,$rowtotal,$action) = @_;
 1579:     my $context;
 1580:     if ($action eq 'quotas') {
 1581:         $context = 'tools';
 1582:     } else {
 1583:         $context = $action;
 1584:     }
 1585:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1586:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1587:     my $typecount = 0;
 1588:     my ($css_class,%titles);
 1589:     if ($context eq 'requestcourses') {
 1590:         @usertools = ('official','unofficial','community','textbook');
 1591:         @options =('norequest','approval','validate','autolimit');
 1592:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1593:         %titles = &courserequest_titles();
 1594:     } elsif ($context eq 'requestauthor') {
 1595:         @usertools = ('author');
 1596:         @options = ('norequest','approval','automatic');
 1597:         %titles = &authorrequest_titles();
 1598:     } else {
 1599:         @usertools = ('aboutme','blog','webdav','portfolio');
 1600:         %titles = &tool_titles();
 1601:     }
 1602:     if (ref($types) eq 'ARRAY') {
 1603:         foreach my $type (@{$types}) {
 1604:             my ($currdefquota,$currauthorquota);
 1605:             unless (($context eq 'requestcourses') ||
 1606:                     ($context eq 'requestauthor')) {
 1607:                 if (ref($settings) eq 'HASH') {
 1608:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1609:                         $currdefquota = $settings->{defaultquota}->{$type};
 1610:                     } else {
 1611:                         $currdefquota = $settings->{$type};
 1612:                     }
 1613:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1614:                         $currauthorquota = $settings->{authorquota}->{$type};
 1615:                     }
 1616:                 }
 1617:             }
 1618:             if (defined($usertypes->{$type})) {
 1619:                 $typecount ++;
 1620:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1621:                 $datatable .= '<tr'.$css_class.'>'.
 1622:                               '<td>'.$usertypes->{$type}.'</td>'.
 1623:                               '<td class="LC_left_item">';
 1624:                 if ($context eq 'requestcourses') {
 1625:                     $datatable .= '<table><tr>';
 1626:                 }
 1627:                 my %cell;  
 1628:                 foreach my $item (@usertools) {
 1629:                     if ($context eq 'requestcourses') {
 1630:                         my ($curroption,$currlimit);
 1631:                         if (ref($settings) eq 'HASH') {
 1632:                             if (ref($settings->{$item}) eq 'HASH') {
 1633:                                 $curroption = $settings->{$item}->{$type};
 1634:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1635:                                     $currlimit = $1; 
 1636:                                 }
 1637:                             }
 1638:                         }
 1639:                         if (!$curroption) {
 1640:                             $curroption = 'norequest';
 1641:                         }
 1642:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1643:                         foreach my $option (@options) {
 1644:                             my $val = $option;
 1645:                             if ($option eq 'norequest') {
 1646:                                 $val = 0;  
 1647:                             }
 1648:                             if ($option eq 'validate') {
 1649:                                 my $canvalidate = 0;
 1650:                                 if (ref($validations{$item}) eq 'HASH') { 
 1651:                                     if ($validations{$item}{$type}) {
 1652:                                         $canvalidate = 1;
 1653:                                     }
 1654:                                 }
 1655:                                 next if (!$canvalidate);
 1656:                             }
 1657:                             my $checked = '';
 1658:                             if ($option eq $curroption) {
 1659:                                 $checked = ' checked="checked"';
 1660:                             } elsif ($option eq 'autolimit') {
 1661:                                 if ($curroption =~ /^autolimit/) {
 1662:                                     $checked = ' checked="checked"';
 1663:                                 }                       
 1664:                             } 
 1665:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1666:                                   '<input type="radio" name="crsreq_'.$item.
 1667:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1668:                                   $titles{$option}.'</label>';
 1669:                             if ($option eq 'autolimit') {
 1670:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1671:                                                 $item.'_limit_'.$type.'" size="1" '.
 1672:                                                 'value="'.$currlimit.'" />';
 1673:                             }
 1674:                             $cell{$item} .= '</span> ';
 1675:                             if ($option eq 'autolimit') {
 1676:                                 $cell{$item} .= $titles{'unlimited'};
 1677:                             }
 1678:                         }
 1679:                     } elsif ($context eq 'requestauthor') {
 1680:                         my $curroption;
 1681:                         if (ref($settings) eq 'HASH') {
 1682:                             $curroption = $settings->{$type};
 1683:                         }
 1684:                         if (!$curroption) {
 1685:                             $curroption = 'norequest';
 1686:                         }
 1687:                         foreach my $option (@options) {
 1688:                             my $val = $option;
 1689:                             if ($option eq 'norequest') {
 1690:                                 $val = 0;
 1691:                             }
 1692:                             my $checked = '';
 1693:                             if ($option eq $curroption) {
 1694:                                 $checked = ' checked="checked"';
 1695:                             }
 1696:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1697:                                   '<input type="radio" name="authorreq_'.$type.
 1698:                                   '" value="'.$val.'"'.$checked.' />'.
 1699:                                   $titles{$option}.'</label></span>&nbsp; ';
 1700:                         }
 1701:                     } else {
 1702:                         my $checked = 'checked="checked" ';
 1703:                         if (ref($settings) eq 'HASH') {
 1704:                             if (ref($settings->{$item}) eq 'HASH') {
 1705:                                 if ($settings->{$item}->{$type} == 0) {
 1706:                                     $checked = '';
 1707:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1708:                                     $checked =  'checked="checked" ';
 1709:                                 }
 1710:                             }
 1711:                         }
 1712:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1713:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1714:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1715:                                       '</label></span>&nbsp; ';
 1716:                     }
 1717:                 }
 1718:                 if ($context eq 'requestcourses') {
 1719:                     $datatable .= '</tr><tr>';
 1720:                     foreach my $item (@usertools) {
 1721:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1722:                     }
 1723:                     $datatable .= '</tr></table>';
 1724:                 }
 1725:                 $datatable .= '</td>';
 1726:                 unless (($context eq 'requestcourses') ||
 1727:                         ($context eq 'requestauthor')) {
 1728:                     $datatable .= 
 1729:                               '<td class="LC_right_item">'.
 1730:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1731:                               '<input type="text" name="quota_'.$type.
 1732:                               '" value="'.$currdefquota.
 1733:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1734:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1735:                               '<input type="text" name="authorquota_'.$type.
 1736:                               '" value="'.$currauthorquota.
 1737:                               '" size="5" /></span></td>';
 1738:                 }
 1739:                 $datatable .= '</tr>';
 1740:             }
 1741:         }
 1742:     }
 1743:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1744:         $defaultquota = '20';
 1745:         $authorquota = '500';
 1746:         if (ref($settings) eq 'HASH') {
 1747:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1748:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1749:             } elsif (defined($settings->{'default'})) {
 1750:                 $defaultquota = $settings->{'default'};
 1751:             }
 1752:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1753:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1754:             }
 1755:         }
 1756:     }
 1757:     $typecount ++;
 1758:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1759:     $datatable .= '<tr'.$css_class.'>'.
 1760:                   '<td>'.$othertitle.'</td>'.
 1761:                   '<td class="LC_left_item">';
 1762:     if ($context eq 'requestcourses') {
 1763:         $datatable .= '<table><tr>';
 1764:     }
 1765:     my %defcell;
 1766:     foreach my $item (@usertools) {
 1767:         if ($context eq 'requestcourses') {
 1768:             my ($curroption,$currlimit);
 1769:             if (ref($settings) eq 'HASH') {
 1770:                 if (ref($settings->{$item}) eq 'HASH') {
 1771:                     $curroption = $settings->{$item}->{'default'};
 1772:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1773:                         $currlimit = $1;
 1774:                     }
 1775:                 }
 1776:             }
 1777:             if (!$curroption) {
 1778:                 $curroption = 'norequest';
 1779:             }
 1780:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1781:             foreach my $option (@options) {
 1782:                 my $val = $option;
 1783:                 if ($option eq 'norequest') {
 1784:                     $val = 0;
 1785:                 }
 1786:                 if ($option eq 'validate') {
 1787:                     my $canvalidate = 0;
 1788:                     if (ref($validations{$item}) eq 'HASH') {
 1789:                         if ($validations{$item}{'default'}) {
 1790:                             $canvalidate = 1;
 1791:                         }
 1792:                     }
 1793:                     next if (!$canvalidate);
 1794:                 }
 1795:                 my $checked = '';
 1796:                 if ($option eq $curroption) {
 1797:                     $checked = ' checked="checked"';
 1798:                 } elsif ($option eq 'autolimit') {
 1799:                     if ($curroption =~ /^autolimit/) {
 1800:                         $checked = ' checked="checked"';
 1801:                     }
 1802:                 }
 1803:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1804:                                   '<input type="radio" name="crsreq_'.$item.
 1805:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1806:                                   $titles{$option}.'</label>';
 1807:                 if ($option eq 'autolimit') {
 1808:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1809:                                        $item.'_limit_default" size="1" '.
 1810:                                        'value="'.$currlimit.'" />';
 1811:                 }
 1812:                 $defcell{$item} .= '</span> ';
 1813:                 if ($option eq 'autolimit') {
 1814:                     $defcell{$item} .= $titles{'unlimited'};
 1815:                 }
 1816:             }
 1817:         } elsif ($context eq 'requestauthor') {
 1818:             my $curroption;
 1819:             if (ref($settings) eq 'HASH') {
 1820:                 $curroption = $settings->{'default'};
 1821:             }
 1822:             if (!$curroption) {
 1823:                 $curroption = 'norequest';
 1824:             }
 1825:             foreach my $option (@options) {
 1826:                 my $val = $option;
 1827:                 if ($option eq 'norequest') {
 1828:                     $val = 0;
 1829:                 }
 1830:                 my $checked = '';
 1831:                 if ($option eq $curroption) {
 1832:                     $checked = ' checked="checked"';
 1833:                 }
 1834:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1835:                               '<input type="radio" name="authorreq_default"'.
 1836:                               ' value="'.$val.'"'.$checked.' />'.
 1837:                               $titles{$option}.'</label></span>&nbsp; ';
 1838:             }
 1839:         } else {
 1840:             my $checked = 'checked="checked" ';
 1841:             if (ref($settings) eq 'HASH') {
 1842:                 if (ref($settings->{$item}) eq 'HASH') {
 1843:                     if ($settings->{$item}->{'default'} == 0) {
 1844:                         $checked = '';
 1845:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1846:                         $checked = 'checked="checked" ';
 1847:                     }
 1848:                 }
 1849:             }
 1850:             $datatable .= '<span class="LC_nobreak"><label>'.
 1851:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1852:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1853:                           '</label></span>&nbsp; ';
 1854:         }
 1855:     }
 1856:     if ($context eq 'requestcourses') {
 1857:         $datatable .= '</tr><tr>';
 1858:         foreach my $item (@usertools) {
 1859:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1860:         }
 1861:         $datatable .= '</tr></table>';
 1862:     }
 1863:     $datatable .= '</td>';
 1864:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1865:         $datatable .= '<td class="LC_right_item">'.
 1866:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1867:                       '<input type="text" name="defaultquota" value="'.
 1868:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1869:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1870:                       '<input type="text" name="authorquota" value="'.
 1871:                       $authorquota.'" size="5" /></span></td>';
 1872:     }
 1873:     $datatable .= '</tr>';
 1874:     $typecount ++;
 1875:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1876:     $datatable .= '<tr'.$css_class.'>'.
 1877:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1878:     if ($context eq 'requestcourses') {
 1879:         $datatable .= &mt('(overrides affiliation, if set)').
 1880:                       '</td>'.
 1881:                       '<td class="LC_left_item">'.
 1882:                       '<table><tr>';
 1883:     } else {
 1884:         $datatable .= &mt('(overrides affiliation, if checked)').
 1885:                       '</td>'.
 1886:                       '<td class="LC_left_item" colspan="2">'.
 1887:                       '<br />';
 1888:     }
 1889:     my %advcell;
 1890:     foreach my $item (@usertools) {
 1891:         if ($context eq 'requestcourses') {
 1892:             my ($curroption,$currlimit);
 1893:             if (ref($settings) eq 'HASH') {
 1894:                 if (ref($settings->{$item}) eq 'HASH') {
 1895:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1896:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1897:                         $currlimit = $1;
 1898:                     }
 1899:                 }
 1900:             }
 1901:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1902:             my $checked = '';
 1903:             if ($curroption eq '') {
 1904:                 $checked = ' checked="checked"';
 1905:             }
 1906:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1907:                                '<input type="radio" name="crsreq_'.$item.
 1908:                                '__LC_adv" value=""'.$checked.' />'.
 1909:                                &mt('No override set').'</label></span>&nbsp; ';
 1910:             foreach my $option (@options) {
 1911:                 my $val = $option;
 1912:                 if ($option eq 'norequest') {
 1913:                     $val = 0;
 1914:                 }
 1915:                 if ($option eq 'validate') {
 1916:                     my $canvalidate = 0;
 1917:                     if (ref($validations{$item}) eq 'HASH') {
 1918:                         if ($validations{$item}{'_LC_adv'}) {
 1919:                             $canvalidate = 1;
 1920:                         }
 1921:                     }
 1922:                     next if (!$canvalidate);
 1923:                 }
 1924:                 my $checked = '';
 1925:                 if ($val eq $curroption) {
 1926:                     $checked = ' checked="checked"';
 1927:                 } elsif ($option eq 'autolimit') {
 1928:                     if ($curroption =~ /^autolimit/) {
 1929:                         $checked = ' checked="checked"';
 1930:                     }
 1931:                 }
 1932:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1933:                                   '<input type="radio" name="crsreq_'.$item.
 1934:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1935:                                   $titles{$option}.'</label>';
 1936:                 if ($option eq 'autolimit') {
 1937:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1938:                                        $item.'_limit__LC_adv" size="1" '.
 1939:                                        'value="'.$currlimit.'" />';
 1940:                 }
 1941:                 $advcell{$item} .= '</span> ';
 1942:                 if ($option eq 'autolimit') {
 1943:                     $advcell{$item} .= $titles{'unlimited'};
 1944:                 }
 1945:             }
 1946:         } elsif ($context eq 'requestauthor') {
 1947:             my $curroption;
 1948:             if (ref($settings) eq 'HASH') {
 1949:                 $curroption = $settings->{'_LC_adv'};
 1950:             }
 1951:             my $checked = '';
 1952:             if ($curroption eq '') {
 1953:                 $checked = ' checked="checked"';
 1954:             }
 1955:             $datatable .= '<span class="LC_nobreak"><label>'.
 1956:                           '<input type="radio" name="authorreq__LC_adv"'.
 1957:                           ' value=""'.$checked.' />'.
 1958:                           &mt('No override set').'</label></span>&nbsp; ';
 1959:             foreach my $option (@options) {
 1960:                 my $val = $option;
 1961:                 if ($option eq 'norequest') {
 1962:                     $val = 0;
 1963:                 }
 1964:                 my $checked = '';
 1965:                 if ($val eq $curroption) {
 1966:                     $checked = ' checked="checked"';
 1967:                 }
 1968:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1969:                               '<input type="radio" name="authorreq__LC_adv"'.
 1970:                               ' value="'.$val.'"'.$checked.' />'.
 1971:                               $titles{$option}.'</label></span>&nbsp; ';
 1972:             }
 1973:         } else {
 1974:             my $checked = 'checked="checked" ';
 1975:             if (ref($settings) eq 'HASH') {
 1976:                 if (ref($settings->{$item}) eq 'HASH') {
 1977:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1978:                         $checked = '';
 1979:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1980:                         $checked = 'checked="checked" ';
 1981:                     }
 1982:                 }
 1983:             }
 1984:             $datatable .= '<span class="LC_nobreak"><label>'.
 1985:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1986:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1987:                           '</label></span>&nbsp; ';
 1988:         }
 1989:     }
 1990:     if ($context eq 'requestcourses') {
 1991:         $datatable .= '</tr><tr>';
 1992:         foreach my $item (@usertools) {
 1993:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1994:         }
 1995:         $datatable .= '</tr></table>';
 1996:     }
 1997:     $datatable .= '</td></tr>';
 1998:     $$rowtotal += $typecount;
 1999:     return $datatable;
 2000: }
 2001: 
 2002: sub print_requestmail {
 2003:     my ($dom,$action,$settings,$rowtotal) = @_;
 2004:     my ($now,$datatable,%currapp);
 2005:     $now = time;
 2006:     if (ref($settings) eq 'HASH') {
 2007:         if (ref($settings->{'notify'}) eq 'HASH') {
 2008:             if ($settings->{'notify'}{'approval'} ne '') {
 2009:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2010:             }
 2011:         }
 2012:     }
 2013:     my $numinrow = 2;
 2014:     my $css_class;
 2015:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2016:     my $text;
 2017:     if ($action eq 'requestcourses') {
 2018:         $text = &mt('Receive notification of course requests requiring approval');
 2019:     } elsif ($action eq 'requestauthor') {
 2020:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2021:     } else {
 2022:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2023:     }
 2024:     $datatable = '<tr'.$css_class.'>'.
 2025:                  ' <td>'.$text.'</td>'.
 2026:                  ' <td class="LC_left_item">';
 2027:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2028:                                                  $action.'notifyapproval',%currapp);
 2029:     if ($numdc > 0) {
 2030:         $datatable .= $table;
 2031:     } else {
 2032:         $datatable .= &mt('There are no active Domain Coordinators');
 2033:     }
 2034:     $datatable .='</td></tr>';
 2035:     $$rowtotal += $rows;
 2036:     return $datatable;
 2037: }
 2038: 
 2039: sub print_studentcode {
 2040:     my ($settings,$rowtotal) = @_;
 2041:     my $rownum = 0; 
 2042:     my ($output,%current);
 2043:     my @crstypes = ('official','unofficial','community','textbook');
 2044:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2045:         foreach my $type (@crstypes) {
 2046:             $current{$type} = $settings->{'uniquecode'}{$type};
 2047:         }
 2048:     }
 2049:     $output .= '<tr>'.
 2050:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2051:                '<td class="LC_left_item">';
 2052:     foreach my $type (@crstypes) {
 2053:         my $check = ' ';
 2054:         if ($current{$type}) {
 2055:             $check = ' checked="checked" ';
 2056:         }
 2057:         $output .= '<span class="LC_nobreak"><label>'.
 2058:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2059:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2060:     }
 2061:     $output .= '</td></tr>';
 2062:     $$rowtotal ++;
 2063:     return $output;
 2064: }
 2065: 
 2066: sub print_textbookcourses {
 2067:     my ($dom,$settings,$rowtotal) = @_;
 2068:     my $rownum = 0;
 2069:     my $css_class;
 2070:     my $itemcount = 1;
 2071:     my $maxnum = 0;
 2072:     my $bookshash;
 2073:     if (ref($settings) eq 'HASH') {
 2074:         $bookshash = $settings->{'textbooks'};
 2075:     }
 2076:     my %ordered;
 2077:     if (ref($bookshash) eq 'HASH') {
 2078:         foreach my $item (keys(%{$bookshash})) {
 2079:             if (ref($bookshash->{$item}) eq 'HASH') {
 2080:                 my $num = $bookshash->{$item}{'order'};
 2081:                 $ordered{$num} = $item;
 2082:             }
 2083:         }
 2084:     }
 2085:     my $confname = $dom.'-domainconfig';
 2086:     my $switchserver = &check_switchserver($dom,$confname);
 2087:     my $maxnum = scalar(keys(%ordered));
 2088:     my $datatable = &textbookcourses_javascript(\%ordered);
 2089:     if (keys(%ordered)) {
 2090:         my @items = sort { $a <=> $b } keys(%ordered);
 2091:         for (my $i=0; $i<@items; $i++) {
 2092:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2093:             my $key = $ordered{$items[$i]};
 2094:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2095:             my $coursetitle = $coursehash{'description'};
 2096:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2097:             if (ref($bookshash->{$key}) eq 'HASH') {
 2098:                 $subject = $bookshash->{$key}->{'subject'};
 2099:                 $title = $bookshash->{$key}->{'title'};
 2100:                 $author = $bookshash->{$key}->{'author'};
 2101:                 $image = $bookshash->{$key}->{'image'};
 2102:                 if ($image ne '') {
 2103:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2104:                     my $imagethumb = "$path/tn-".$imagefile;
 2105:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2106:                 }
 2107:             }
 2108:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2109:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2110:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2111:             for (my $k=0; $k<=$maxnum; $k++) {
 2112:                 my $vpos = $k+1;
 2113:                 my $selstr;
 2114:                 if ($k == $i) {
 2115:                     $selstr = ' selected="selected" ';
 2116:                 }
 2117:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2118:             }
 2119:             $datatable .= '</select>'.('&nbsp;'x2).
 2120:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2121:                 &mt('Delete?').'</label></span></td>'.
 2122:                 '<td colspan="2">'.
 2123:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2124:                 ('&nbsp;'x2).
 2125:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2126:                 ('&nbsp;'x2).
 2127:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2128:                 ('&nbsp;'x2).
 2129:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2130:             if ($image) {
 2131:                 $datatable .= '<span class="LC_nobreak">'.
 2132:                               $imgsrc.
 2133:                               '<label><input type="checkbox" name="book_image_del"'.
 2134:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2135:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2136:             }
 2137:             if ($switchserver) {
 2138:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2139:             } else {
 2140:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2141:             }
 2142:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2143:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2144:                           $coursetitle.'</span></td></tr>'."\n";
 2145:             $itemcount ++;
 2146:         }
 2147:     }
 2148:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2149:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2150:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2151:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2152:                   '<select name="addbook_pos"'.$chgstr.'>';
 2153:     for (my $k=0; $k<$maxnum+1; $k++) {
 2154:         my $vpos = $k+1;
 2155:         my $selstr;
 2156:         if ($k == $maxnum) {
 2157:             $selstr = ' selected="selected" ';
 2158:         }
 2159:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2160:     }
 2161:     $datatable .= '</select>&nbsp;'."\n".
 2162:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2163:                   '<td colspan="2">'.
 2164:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2165:                   ('&nbsp;'x2).
 2166:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2167:                   ('&nbsp;'x2).
 2168:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
 2169:                   ('&nbsp;'x2).
 2170:                   '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2171:     if ($switchserver) {
 2172:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2173:     } else {
 2174:         $datatable .= '<input type="file" name="addbook_image" value="" />';
 2175:     }
 2176:     $datatable .= '</span>'."\n".
 2177:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2178:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2179:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2180:                   &Apache::loncommon::selectcourse_link
 2181:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2182:                   '</span></td>'."\n".
 2183:                   '</tr>'."\n";
 2184:     $itemcount ++;
 2185:     return $datatable;
 2186: }
 2187: 
 2188: sub textbookcourses_javascript {
 2189:     my ($textbooks) = @_;
 2190:     return unless(ref($textbooks) eq 'HASH');
 2191:     my $num = scalar(keys(%{$textbooks}));
 2192:     my @jsarray;
 2193:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2194:         push(@jsarray,$textbooks->{$item});
 2195:     }
 2196:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2197:     return <<"ENDSCRIPT";
 2198: <script type="text/javascript">
 2199: // <![CDATA[
 2200: function reorderBooks(form,item) {
 2201:     var changedVal;
 2202: $jstext 
 2203:     var newpos = 'addbook_pos';
 2204:     var current = new Array;
 2205:     var maxh = 1 + $num;
 2206:     var current = new Array;
 2207:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2208:     if (item == newpos) {
 2209:         changedVal = newitemVal;
 2210:     } else {
 2211:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2212:         current[newitemVal] = newpos;
 2213:     }
 2214:     for (var i=0; i<textbooks.length; i++) {
 2215:         var elementName = textbooks[i];
 2216:         if (elementName != item) {
 2217:             if (form.elements[elementName]) {
 2218:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2219:                 current[currVal] = elementName;
 2220:             }
 2221:         }
 2222:     }
 2223:     var oldVal;
 2224:     for (var j=0; j<maxh; j++) {
 2225:         if (current[j] == undefined) {
 2226:             oldVal = j;
 2227:         }
 2228:     }
 2229:     if (oldVal < changedVal) {
 2230:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2231:            var elementName = current[k];
 2232:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2233:         }
 2234:     } else {
 2235:         for (var k=changedVal; k<oldVal; k++) {
 2236:             var elementName = current[k];
 2237:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2238:         }
 2239:     }
 2240:     return;
 2241: }
 2242: 
 2243: // ]]>
 2244: </script>
 2245: 
 2246: ENDSCRIPT
 2247: }
 2248: 
 2249: sub print_autoenroll {
 2250:     my ($dom,$settings,$rowtotal) = @_;
 2251:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2252:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2253:     if (ref($settings) eq 'HASH') {
 2254:         if (exists($settings->{'run'})) {
 2255:             if ($settings->{'run'} eq '0') {
 2256:                 $runoff = ' checked="checked" ';
 2257:                 $runon = ' ';
 2258:             } else {
 2259:                 $runon = ' checked="checked" ';
 2260:                 $runoff = ' ';
 2261:             }
 2262:         } else {
 2263:             if ($autorun) {
 2264:                 $runon = ' checked="checked" ';
 2265:                 $runoff = ' ';
 2266:             } else {
 2267:                 $runoff = ' checked="checked" ';
 2268:                 $runon = ' ';
 2269:             }
 2270:         }
 2271:         if (exists($settings->{'co-owners'})) {
 2272:             if ($settings->{'co-owners'} eq '0') {
 2273:                 $coownersoff = ' checked="checked" ';
 2274:                 $coownerson = ' ';
 2275:             } else {
 2276:                 $coownerson = ' checked="checked" ';
 2277:                 $coownersoff = ' ';
 2278:             }
 2279:         } else {
 2280:             $coownersoff = ' checked="checked" ';
 2281:             $coownerson = ' ';
 2282:         }
 2283:         if (exists($settings->{'sender_domain'})) {
 2284:             $defdom = $settings->{'sender_domain'};
 2285:         }
 2286:     } else {
 2287:         if ($autorun) {
 2288:             $runon = ' checked="checked" ';
 2289:             $runoff = ' ';
 2290:         } else {
 2291:             $runoff = ' checked="checked" ';
 2292:             $runon = ' ';
 2293:         }
 2294:     }
 2295:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2296:     my $notif_sender;
 2297:     if (ref($settings) eq 'HASH') {
 2298:         $notif_sender = $settings->{'sender_uname'};
 2299:     }
 2300:     my $datatable='<tr class="LC_odd_row">'.
 2301:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2302:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2303:                   '<input type="radio" name="autoenroll_run"'.
 2304:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2305:                   '<label><input type="radio" name="autoenroll_run"'.
 2306:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2307:                   '</tr><tr>'.
 2308:                   '<td>'.&mt('Notification messages - sender').
 2309:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2310:                   &mt('username').':&nbsp;'.
 2311:                   '<input type="text" name="sender_uname" value="'.
 2312:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2313:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2314:                   '<tr class="LC_odd_row">'.
 2315:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2316:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2317:                   '<input type="radio" name="autoassign_coowners"'.
 2318:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2319:                   '<label><input type="radio" name="autoassign_coowners"'.
 2320:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2321:                   '</tr>';
 2322:     $$rowtotal += 3;
 2323:     return $datatable;
 2324: }
 2325: 
 2326: sub print_autoupdate {
 2327:     my ($position,$dom,$settings,$rowtotal) = @_;
 2328:     my $datatable;
 2329:     if ($position eq 'top') {
 2330:         my $updateon = ' ';
 2331:         my $updateoff = ' checked="checked" ';
 2332:         my $classlistson = ' ';
 2333:         my $classlistsoff = ' checked="checked" ';
 2334:         if (ref($settings) eq 'HASH') {
 2335:             if ($settings->{'run'} eq '1') {
 2336:                 $updateon = $updateoff;
 2337:                 $updateoff = ' ';
 2338:             }
 2339:             if ($settings->{'classlists'} eq '1') {
 2340:                 $classlistson = $classlistsoff;
 2341:                 $classlistsoff = ' ';
 2342:             }
 2343:         }
 2344:         my %title = (
 2345:                    run => 'Auto-update active?',
 2346:                    classlists => 'Update information in classlists?',
 2347:                     );
 2348:         $datatable = '<tr class="LC_odd_row">'. 
 2349:                   '<td>'.&mt($title{'run'}).'</td>'.
 2350:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2351:                   '<input type="radio" name="autoupdate_run"'.
 2352:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2353:                   '<label><input type="radio" name="autoupdate_run"'.
 2354:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2355:                   '</tr><tr>'.
 2356:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2357:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2358:                   '<label><input type="radio" name="classlists"'.
 2359:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2360:                   '<label><input type="radio" name="classlists"'.
 2361:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2362:                   '</tr>';
 2363:         $$rowtotal += 2;
 2364:     } elsif ($position eq 'middle') {
 2365:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2366:         my $numinrow = 3;
 2367:         my $locknamesettings;
 2368:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2369:                                      $dom,$numinrow,$othertitle,
 2370:                                     'lockablenames');
 2371:         $$rowtotal ++;
 2372:     } else {
 2373:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2374:         my @fields = ('lastname','firstname','middlename','generation',
 2375:                       'permanentemail','id');
 2376:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2377:         my $numrows = 0;
 2378:         if (ref($types) eq 'ARRAY') {
 2379:             if (@{$types} > 0) {
 2380:                 $datatable = 
 2381:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2382:                                          \@fields,$types,\$numrows);
 2383:                     $$rowtotal += @{$types}; 
 2384:             }
 2385:         }
 2386:         $datatable .= 
 2387:             &usertype_update_row($settings,{'default' => $othertitle},
 2388:                                  \%fieldtitles,\@fields,['default'],
 2389:                                  \$numrows);
 2390:         $$rowtotal ++;     
 2391:     }
 2392:     return $datatable;
 2393: }
 2394: 
 2395: sub print_autocreate {
 2396:     my ($dom,$settings,$rowtotal) = @_;
 2397:     my (%createon,%createoff,%currhash);
 2398:     my @types = ('xml','req');
 2399:     if (ref($settings) eq 'HASH') {
 2400:         foreach my $item (@types) {
 2401:             $createoff{$item} = ' checked="checked" ';
 2402:             $createon{$item} = ' ';
 2403:             if (exists($settings->{$item})) {
 2404:                 if ($settings->{$item}) {
 2405:                     $createon{$item} = ' checked="checked" ';
 2406:                     $createoff{$item} = ' ';
 2407:                 }
 2408:             }
 2409:         }
 2410:         if ($settings->{'xmldc'} ne '') {
 2411:             $currhash{$settings->{'xmldc'}} = 1;
 2412:         }
 2413:     } else {
 2414:         foreach my $item (@types) {
 2415:             $createoff{$item} = ' checked="checked" ';
 2416:             $createon{$item} = ' ';
 2417:         }
 2418:     }
 2419:     $$rowtotal += 2;
 2420:     my $numinrow = 2;
 2421:     my $datatable='<tr class="LC_odd_row">'.
 2422:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2423:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2424:                   '<input type="radio" name="autocreate_xml"'.
 2425:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2426:                   '<label><input type="radio" name="autocreate_xml"'.
 2427:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2428:                   '</td></tr><tr>'.
 2429:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2430:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2431:                   '<input type="radio" name="autocreate_req"'.
 2432:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2433:                   '<label><input type="radio" name="autocreate_req"'.
 2434:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2435:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2436:                                                    'autocreate_xmldc',%currhash);
 2437:     if ($numdc > 1) {
 2438:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2439:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2440:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2441:     } else {
 2442:         $datatable .= $dctable.'</td></tr>';
 2443:     }
 2444:     $$rowtotal += $rows;
 2445:     return $datatable;
 2446: }
 2447: 
 2448: sub print_directorysrch {
 2449:     my ($dom,$settings,$rowtotal) = @_;
 2450:     my $srchon = ' ';
 2451:     my $srchoff = ' checked="checked" ';
 2452:     my ($exacton,$containson,$beginson);
 2453:     my $localon = ' ';
 2454:     my $localoff = ' checked="checked" ';
 2455:     if (ref($settings) eq 'HASH') {
 2456:         if ($settings->{'available'} eq '1') {
 2457:             $srchon = $srchoff;
 2458:             $srchoff = ' ';
 2459:         }
 2460:         if ($settings->{'localonly'} eq '1') {
 2461:             $localon = $localoff;
 2462:             $localoff = ' ';
 2463:         }
 2464:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2465:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2466:                 if ($type eq 'exact') {
 2467:                     $exacton = ' checked="checked" ';
 2468:                 } elsif ($type eq 'contains') {
 2469:                     $containson = ' checked="checked" ';
 2470:                 } elsif ($type eq 'begins') {
 2471:                     $beginson = ' checked="checked" ';
 2472:                 }
 2473:             }
 2474:         } else {
 2475:             if ($settings->{'searchtypes'} eq 'exact') {
 2476:                 $exacton = ' checked="checked" ';
 2477:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2478:                 $containson = ' checked="checked" ';
 2479:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2480:                 $exacton = ' checked="checked" ';
 2481:                 $containson = ' checked="checked" ';
 2482:             }
 2483:         }
 2484:     }
 2485:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2486:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2487: 
 2488:     my $numinrow = 4;
 2489:     my $cansrchrow = 0;
 2490:     my $datatable='<tr class="LC_odd_row">'.
 2491:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2492:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2493:                   '<input type="radio" name="dirsrch_available"'.
 2494:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2495:                   '<label><input type="radio" name="dirsrch_available"'.
 2496:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2497:                   '</tr><tr>'.
 2498:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2499:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2500:                   '<input type="radio" name="dirsrch_localonly"'.
 2501:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2502:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2503:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2504:                   '</tr>';
 2505:     $$rowtotal += 2;
 2506:     if (ref($usertypes) eq 'HASH') {
 2507:         if (keys(%{$usertypes}) > 0) {
 2508:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2509:                                          $numinrow,$othertitle,'cansearch');
 2510:             $cansrchrow = 1;
 2511:         }
 2512:     }
 2513:     if ($cansrchrow) {
 2514:         $$rowtotal ++;
 2515:         $datatable .= '<tr>';
 2516:     } else {
 2517:         $datatable .= '<tr class="LC_odd_row">';
 2518:     }
 2519:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2520:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2521:     foreach my $title (@{$titleorder}) {
 2522:         if (defined($searchtitles->{$title})) {
 2523:             my $check = ' ';
 2524:             if (ref($settings) eq 'HASH') {
 2525:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2526:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2527:                         $check = ' checked="checked" ';
 2528:                     }
 2529:                 }
 2530:             }
 2531:             $datatable .= '<td class="LC_left_item">'.
 2532:                           '<span class="LC_nobreak"><label>'.
 2533:                           '<input type="checkbox" name="searchby" '.
 2534:                           'value="'.$title.'"'.$check.'/>'.
 2535:                           $searchtitles->{$title}.'</label></span></td>';
 2536:         }
 2537:     }
 2538:     $datatable .= '</tr></table></td></tr>';
 2539:     $$rowtotal ++;
 2540:     if ($cansrchrow) {
 2541:         $datatable .= '<tr class="LC_odd_row">';
 2542:     } else {
 2543:         $datatable .= '<tr>';
 2544:     }
 2545:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2546:                   '<td class="LC_left_item" colspan="2">'.
 2547:                   '<span class="LC_nobreak"><label>'.
 2548:                   '<input type="checkbox" name="searchtypes" '.
 2549:                   $exacton.' value="exact" />'.&mt('Exact match').
 2550:                   '</label>&nbsp;'.
 2551:                   '<label><input type="checkbox" name="searchtypes" '.
 2552:                   $beginson.' value="begins" />'.&mt('Begins with').
 2553:                   '</label>&nbsp;'.
 2554:                   '<label><input type="checkbox" name="searchtypes" '.
 2555:                   $containson.' value="contains" />'.&mt('Contains').
 2556:                   '</label></span></td></tr>';
 2557:     $$rowtotal ++;
 2558:     return $datatable;
 2559: }
 2560: 
 2561: sub print_contacts {
 2562:     my ($dom,$settings,$rowtotal) = @_;
 2563:     my $datatable;
 2564:     my @contacts = ('adminemail','supportemail');
 2565:     my (%checked,%to,%otheremails,%bccemails);
 2566:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2567:                     'requestsmail','updatesmail','idconflictsmail');
 2568:     foreach my $type (@mailings) {
 2569:         $otheremails{$type} = '';
 2570:     }
 2571:     $bccemails{'helpdeskmail'} = '';
 2572:     if (ref($settings) eq 'HASH') {
 2573:         foreach my $item (@contacts) {
 2574:             if (exists($settings->{$item})) {
 2575:                 $to{$item} = $settings->{$item};
 2576:             }
 2577:         }
 2578:         foreach my $type (@mailings) {
 2579:             if (exists($settings->{$type})) {
 2580:                 if (ref($settings->{$type}) eq 'HASH') {
 2581:                     foreach my $item (@contacts) {
 2582:                         if ($settings->{$type}{$item}) {
 2583:                             $checked{$type}{$item} = ' checked="checked" ';
 2584:                         }
 2585:                     }
 2586:                     $otheremails{$type} = $settings->{$type}{'others'};
 2587:                     if ($type eq 'helpdeskmail') {
 2588:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2589:                     }
 2590:                 }
 2591:             } elsif ($type eq 'lonstatusmail') {
 2592:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2593:             }
 2594:         }
 2595:     } else {
 2596:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2597:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2598:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2599:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2600:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2601:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2602:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2603:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2604:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2605:     }
 2606:     my ($titles,$short_titles) = &contact_titles();
 2607:     my $rownum = 0;
 2608:     my $css_class;
 2609:     foreach my $item (@contacts) {
 2610:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2611:         $datatable .= '<tr'.$css_class.'>'. 
 2612:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2613:                   '</span></td><td class="LC_right_item">'.
 2614:                   '<input type="text" name="'.$item.'" value="'.
 2615:                   $to{$item}.'" /></td></tr>';
 2616:         $rownum ++;
 2617:     }
 2618:     foreach my $type (@mailings) {
 2619:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2620:         $datatable .= '<tr'.$css_class.'>'.
 2621:                       '<td><span class="LC_nobreak">'.
 2622:                       $titles->{$type}.': </span></td>'.
 2623:                       '<td class="LC_left_item">'.
 2624:                       '<span class="LC_nobreak">';
 2625:         foreach my $item (@contacts) {
 2626:             $datatable .= '<label>'.
 2627:                           '<input type="checkbox" name="'.$type.'"'.
 2628:                           $checked{$type}{$item}.
 2629:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2630:                           '</label>&nbsp;';
 2631:         }
 2632:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2633:                       '<input type="text" name="'.$type.'_others" '.
 2634:                       'value="'.$otheremails{$type}.'"  />';
 2635:         if ($type eq 'helpdeskmail') {
 2636:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2637:                           '<input type="text" name="'.$type.'_bcc" '.
 2638:                           'value="'.$bccemails{$type}.'"  />';
 2639:         }
 2640:         $datatable .= '</td></tr>'."\n";
 2641:         $rownum ++;
 2642:     }
 2643:     my %choices;
 2644:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2645:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2646:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2647:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2648:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2649:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2650:     my @toggles = ('reporterrors','reportupdates');
 2651:     my %defaultchecked = ('reporterrors'  => 'on',
 2652:                           'reportupdates' => 'on');
 2653:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2654:                                                \%choices,$rownum);
 2655:     $datatable .= $reports;
 2656:     $$rowtotal += $rownum;
 2657:     return $datatable;
 2658: }
 2659: 
 2660: sub print_helpsettings {
 2661:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2662:     my ($datatable,$itemcount);
 2663:     $itemcount = 1;
 2664:     my (%choices,%defaultchecked,@toggles);
 2665:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2666:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2667:                                  &mt('LON-CAPA bug tracker'),600,500));
 2668:     %defaultchecked = ('submitbugs' => 'on');
 2669:     @toggles = ('submitbugs',);
 2670: 
 2671:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2672:                                                  \%choices,$itemcount);
 2673:     return $datatable;
 2674: }
 2675: 
 2676: sub radiobutton_prefs {
 2677:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2678:         $additional) = @_;
 2679:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2680:                    (ref($choices) eq 'HASH'));
 2681: 
 2682:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2683: 
 2684:     foreach my $item (@{$toggles}) {
 2685:         if ($defaultchecked->{$item} eq 'on') {
 2686:             $checkedon{$item} = ' checked="checked" ';
 2687:             $checkedoff{$item} = ' ';
 2688:         } elsif ($defaultchecked->{$item} eq 'off') {
 2689:             $checkedoff{$item} = ' checked="checked" ';
 2690:             $checkedon{$item} = ' ';
 2691:         }
 2692:     }
 2693:     if (ref($settings) eq 'HASH') {
 2694:         foreach my $item (@{$toggles}) {
 2695:             if ($settings->{$item} eq '1') {
 2696:                 $checkedon{$item} =  ' checked="checked" ';
 2697:                 $checkedoff{$item} = ' ';
 2698:             } elsif ($settings->{$item} eq '0') {
 2699:                 $checkedoff{$item} =  ' checked="checked" ';
 2700:                 $checkedon{$item} = ' ';
 2701:             }
 2702:         }
 2703:     }
 2704:     if ($onclick) {
 2705:         $onclick = ' onclick="'.$onclick.'"';
 2706:     }
 2707:     foreach my $item (@{$toggles}) {
 2708:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2709:         $datatable .=
 2710:             '<tr'.$css_class.'><td valign="top">'.
 2711:             '<span class="LC_nobreak">'.$choices->{$item}.
 2712:             '</span></td>'.
 2713:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2714:             '<label><input type="radio" name="'.
 2715:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2716:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2717:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2718:             '</span>'.$additional.
 2719:             '</td>'.
 2720:             '</tr>';
 2721:         $itemcount ++;
 2722:     }
 2723:     return ($datatable,$itemcount);
 2724: }
 2725: 
 2726: sub print_coursedefaults {
 2727:     my ($position,$dom,$settings,$rowtotal) = @_;
 2728:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2729:     my $itemcount = 1;
 2730:     my %choices =  &Apache::lonlocal::texthash (
 2731:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2732:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2733:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2734:         coursecredits        => 'Credits can be specified for courses',
 2735:     );
 2736:     my %staticdefaults = (
 2737:                            anonsurvey_threshold => 10,
 2738:                            uploadquota          => 500,
 2739:                          );
 2740:     if ($position eq 'top') {
 2741:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2742:         @toggles = ('canuse_pdfforms');
 2743:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2744:                                                  \%choices,$itemcount);
 2745:     } else {
 2746:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2747:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2748:             %curruploadquota);
 2749:         my $currusecredits = 0;
 2750:         my @types = ('official','unofficial','community','textbook');
 2751:         if (ref($settings) eq 'HASH') {
 2752:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2753:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2754:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2755:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2756:                 }
 2757:             }
 2758:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2759:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2760:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2761:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2762:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2763:                     ($def_textbook_credits ne '')) {
 2764:                     $currusecredits = 1;
 2765:                 }
 2766:             }
 2767:         }
 2768:         if (!$currdefresponder) {
 2769:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2770:         } elsif ($currdefresponder < 1) {
 2771:             $currdefresponder = 1;
 2772:         }
 2773:         foreach my $type (@types) {
 2774:             if ($curruploadquota{$type} eq '') {
 2775:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2776:             }
 2777:         }
 2778:         $datatable .=
 2779:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2780:                 $choices{'anonsurvey_threshold'}.
 2781:                 '</span></td>'.
 2782:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2783:                 '<input type="text" name="anonsurvey_threshold"'.
 2784:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2785:                 '</td></tr>'."\n";
 2786:         $itemcount ++;
 2787:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2788:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2789:                       $choices{'uploadquota'}.
 2790:                       '</span></td>'.
 2791:                       '<td align="right" class="LC_right_item">'.
 2792:                       '<table><tr>';
 2793:         foreach my $type (@types) {
 2794:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2795:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2796:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2797:         }
 2798:         $datatable .= '</tr></table></td></tr>'."\n";
 2799:         $itemcount ++;
 2800:         my $onclick = 'toggleCredits(this.form);';
 2801:         my $display = 'none';
 2802:         if ($currusecredits) {
 2803:             $display = 'block';
 2804:         }
 2805:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2806:                          '<span class="LC_nobreak">'.
 2807:                          &mt('Default credits for official courses [_1]',
 2808:                          '<input type="text" name="official_credits" value="'.
 2809:                          $def_official_credits.'" size="3" />').
 2810:                          '</span><br />'.
 2811:                          '<span class="LC_nobreak">'.
 2812:                          &mt('Default credits for unofficial courses [_1]',
 2813:                          '<input type="text" name="unofficial_credits" value="'.
 2814:                          $def_unofficial_credits.'" size="3" />').
 2815:                          '</span><br />'.
 2816:                          '<span class="LC_nobreak">'.
 2817:                          &mt('Default credits for textbook courses [_1]',
 2818:                          '<input type="text" name="textbook_credits" value="'.
 2819:                          $def_textbook_credits.'" size="3" />').
 2820:                          '</span></div>'."\n";
 2821:         %defaultchecked = ('coursecredits' => 'off');
 2822:         @toggles = ('coursecredits');
 2823:         my $current = {
 2824:                         'coursecredits' => $currusecredits,
 2825:                       };
 2826:         (my $table,$itemcount) =
 2827:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2828:                                \%choices,$itemcount,$onclick,$additional);
 2829:         $datatable .= $table;
 2830:         $itemcount ++;
 2831:     }
 2832:     $$rowtotal += $itemcount;
 2833:     return $datatable;
 2834: }
 2835: 
 2836: sub print_selfenrollment {
 2837:     my ($position,$dom,$settings,$rowtotal) = @_;
 2838:     my ($css_class,$datatable);
 2839:     my $itemcount = 1;
 2840:     my @types = ('official','unofficial','community','textbook');
 2841:     if (($position eq 'top') || ($position eq 'middle')) {
 2842:         my ($rowsref,$titlesref) = &get_selfenroll_titles();
 2843:         my %descs = &selfenroll_default_descs();
 2844:         my @rows;
 2845:         my $key;
 2846:         if ($position eq 'top') {
 2847:             $key = 'admin'; 
 2848:             if (ref($rowsref) eq 'ARRAY') {
 2849:                 @rows = @{$rowsref};
 2850:             }
 2851:         } elsif ($position eq 'middle') {
 2852:             $key = 'default';
 2853:             @rows = ('types','registered','approval','limit');
 2854:         }
 2855:         foreach my $row (@rows) {
 2856:             if (defined($titlesref->{$row})) {
 2857:                 $itemcount ++;
 2858:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2859:                 $datatable .= '<tr'.$css_class.'>'.
 2860:                               '<td>'.$titlesref->{$row}.'</td>'.
 2861:                               '<td class="LC_left_item">'.
 2862:                               '<table><tr>';
 2863:                 my (%current,%currentcap);
 2864:                 if (ref($settings) eq 'HASH') {
 2865:                     if (ref($settings->{$key}) eq 'HASH') {
 2866:                         foreach my $type (@types) {
 2867:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2868:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 2869:                             }
 2870:                             if (($row eq 'limit') && ($key eq 'default')) {
 2871:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 2872:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 2873:                                 }
 2874:                             }
 2875:                         }
 2876:                     }
 2877:                 }
 2878:                 my %roles = (
 2879:                              '0' => &Apache::lonnet::plaintext('dc'),
 2880:                             ); 
 2881:             
 2882:                 foreach my $type (@types) {
 2883:                     unless (($row eq 'registered') && ($key eq 'default')) {
 2884:                         $datatable .= '<th>'.&mt($type).'</th>';
 2885:                     }
 2886:                 }
 2887:                 unless (($row eq 'registered') && ($key eq 'default')) {
 2888:                     $datatable .= '</tr><tr>';
 2889:                 }
 2890:                 foreach my $type (@types) {
 2891:                     if ($type eq 'community') {
 2892:                         $roles{'1'} = &mt('Community personnel');
 2893:                     } else {
 2894:                         $roles{'1'} = &mt('Course personnel');
 2895:                     }
 2896:                     $datatable .= '<td style="vertical-align: top">';
 2897:                     if ($position eq 'top') {
 2898:                         my %checked;
 2899:                         if ($current{$type} eq '0') {
 2900:                             $checked{'0'} = ' checked="checked"';
 2901:                         } else {
 2902:                             $checked{'1'} = ' checked="checked"';
 2903:                         }
 2904:                         foreach my $role ('1','0') {
 2905:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2906:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 2907:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 2908:                                           $roles{$role}.'</label></span> ';
 2909:                         }
 2910:                     } else {
 2911:                         if ($row eq 'types') {
 2912:                             my %checked;
 2913:                             if ($current{$type} =~ /^(all|dom)$/) {
 2914:                                 $checked{$1} = ' checked="checked"';
 2915:                             } else {
 2916:                                 $checked{''} = ' checked="checked"';
 2917:                             }
 2918:                             foreach my $val ('','dom','all') {
 2919:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2920:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2921:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2922:                             }
 2923:                         } elsif ($row eq 'registered') {
 2924:                             my %checked;
 2925:                             if ($current{$type} eq '1') {
 2926:                                 $checked{'1'} = ' checked="checked"';
 2927:                             } else {
 2928:                                 $checked{'0'} = ' checked="checked"';
 2929:                             }
 2930:                             foreach my $val ('0','1') {
 2931:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2932:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2933:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2934:                             }
 2935:                         } elsif ($row eq 'approval') {
 2936:                             my %checked;
 2937:                             if ($current{$type} =~ /^([12])$/) {
 2938:                                 $checked{$1} = ' checked="checked"';
 2939:                             } else {
 2940:                                 $checked{'0'} = ' checked="checked"';
 2941:                             }
 2942:                             for my $val (0..2) {
 2943:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2944:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2945:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2946:                             }
 2947:                         } elsif ($row eq 'limit') {
 2948:                             my %checked;
 2949:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 2950:                                 $checked{$1} = ' checked="checked"';
 2951:                             } else {
 2952:                                 $checked{'none'} = ' checked="checked"';
 2953:                             }
 2954:                             my $cap;
 2955:                             if ($currentcap{$type} =~ /^\d+$/) {
 2956:                                 $cap = $currentcap{$type};
 2957:                             }
 2958:                             foreach my $val ('none','allstudents','selfenrolled') {
 2959:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 2960:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 2961:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 2962:                             }
 2963:                             $datatable .= '<br />'.
 2964:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 2965:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 2966:                                           '</span>'; 
 2967:                         }
 2968:                     }
 2969:                     $datatable .= '</td>';
 2970:                 }
 2971:                 $datatable .= '</tr>';
 2972:             }
 2973:             $datatable .= '</table></td></tr>';
 2974:         }
 2975:     } elsif ($position eq 'bottom') {
 2976:         my ($itemsref,$namesref,$fieldsref) = &selfenroll_validation_types();
 2977:         my %currvalidation;
 2978:         if (ref($settings) eq 'HASH') {
 2979:             if (ref($settings->{'validation'}) eq 'HASH') {
 2980:                 %currvalidation = %{$settings->{'validation'}}; 
 2981:             }
 2982:         }
 2983:         foreach my $item (@{$itemsref}) {
 2984:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2985:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2986:                           $namesref->{$item}.
 2987:                           '</span></td>'.
 2988:                           '<td class="LC_left_item">';
 2989:             if (($item eq 'url') || ($item eq 'button')) {
 2990:                 $datatable .= '<span class="LC_nobreak">'.
 2991:                               '<input type="text" name="selfenroll_validation_'.$item.'"'.
 2992:                               ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 2993:             } elsif ($item eq 'fields') {
 2994:                 my @currfields;
 2995:                 if (ref($currvalidation{$item}) eq 'ARRAY') {
 2996:                     @currfields = @{$currvalidation{$item}};
 2997:                 }
 2998:                 foreach my $field (@{$fieldsref}) {
 2999:                     my $check = '';
 3000:                     if (grep(/^\Q$field\E$/,@currfields)) {
 3001:                         $check = ' checked="checked"';
 3002:                     }
 3003:                     $datatable .= '<span class="LC_nobreak"><label>'.
 3004:                                   '<input type="checkbox" name="selfenroll_validation_fields"'.
 3005:                                   ' value="'.$field.'"'.$check.' />'.$field.
 3006:                                   '</label></span> ';
 3007:                 }
 3008:             } elsif ($item eq 'markup') {
 3009:                 $datatable .= '<textarea name="selfenroll_validation_markup" cols="50" rows="5" wrap="soft">'.
 3010:                               $currvalidation{$item}. 
 3011:                               '</textarea>';
 3012:             }
 3013:             $datatable .= '</td></tr>'."\n";
 3014:             $itemcount ++;
 3015:         }
 3016:     }
 3017:     $$rowtotal += $itemcount;
 3018:     return $datatable;
 3019: }
 3020: 
 3021: sub get_selfenroll_titles {
 3022:     my @row = ('types','registered','enroll_dates','access_dates','section',
 3023:                'approval','limit');
 3024:     my %lt = &Apache::lonlocal::texthash (
 3025:                 types        => 'Users allowed to self-enroll',
 3026:                 registered   => 'Registration status (official courses)' ,
 3027:                 enroll_dates => 'Dates self-enrollment available',
 3028:                 access_dates => 'Access dates for self-enrolling users',
 3029:                 section      => 'Self-enrolling users section',
 3030:                 approval     => 'Processing of requests',
 3031:                 limit        => 'Enrollment limit',
 3032:              );
 3033:     return (\@row,\%lt);
 3034: }
 3035: 
 3036: sub selfenroll_default_descs {
 3037:     my %desc = (
 3038:                  types => {
 3039:                             dom => &mt('Course domain'),
 3040:                             all => &mt('Any domain'),
 3041:                             ''  => &mt('None'),
 3042:                           },
 3043:                  limit => {
 3044:                             none         => &mt('No limit'),
 3045:                             allstudents  => &mt('Limit by total students'),
 3046:                             selfenrolled => &mt('Limit by total self-enrolled'),
 3047:                           },
 3048:                  approval => {
 3049:                                 '0' => &mt('Processed automatically'),
 3050:                                 '1' => &mt('Queued for approval'),
 3051:                                 '2' => &mt('Queued, pending validation'),
 3052:                              },
 3053:                  registered => {
 3054:                                  0 => 'No registration required',
 3055:                                  1 => 'Registered students only',
 3056:                                },
 3057:                );
 3058:     return %desc;
 3059: }
 3060: 
 3061: sub selfenroll_validation_types {
 3062:     my @items = ('url','fields','button','markup');
 3063:     my %names =  &Apache::lonlocal::texthash (
 3064:             url      => 'Web address of validation server/script',
 3065:             fields   => 'Form fields to send to validator',
 3066:             button   => 'Text for validation button',
 3067:             markup   => 'Validation description (HTML)',
 3068:     );
 3069:     my @fields = ('username','domain','uniquecode','course','token');
 3070:     return (\@items,\%names,\@fields);
 3071: }  
 3072: 
 3073: sub print_usersessions {
 3074:     my ($position,$dom,$settings,$rowtotal) = @_;
 3075:     my ($css_class,$datatable,%checked,%choices);
 3076:     my (%by_ip,%by_location,@intdoms);
 3077:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3078: 
 3079:     my @alldoms = &Apache::lonnet::all_domains();
 3080:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3081:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3082:     my %altids = &id_for_thisdom(%servers);
 3083:     my $itemcount = 1;
 3084:     if ($position eq 'top') {
 3085:         if (keys(%serverhomes) > 1) {
 3086:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3087:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 3088:         } else {
 3089:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3090:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3091:         }
 3092:     } else {
 3093:         if (keys(%by_location) == 0) {
 3094:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3095:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3096:         } else {
 3097:             my %lt = &usersession_titles();
 3098:             my $numinrow = 5;
 3099:             my $prefix;
 3100:             my @types;
 3101:             if ($position eq 'bottom') {
 3102:                 $prefix = 'remote';
 3103:                 @types = ('version','excludedomain','includedomain');
 3104:             } else {
 3105:                 $prefix = 'hosted';
 3106:                 @types = ('excludedomain','includedomain');
 3107:             }
 3108:             my (%current,%checkedon,%checkedoff);
 3109:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3110:             my @locations = sort(keys(%by_location));
 3111:             foreach my $type (@types) {
 3112:                 $checkedon{$type} = '';
 3113:                 $checkedoff{$type} = ' checked="checked"';
 3114:             }
 3115:             if (ref($settings) eq 'HASH') {
 3116:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3117:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3118:                         $current{$key} = $settings->{$prefix}{$key};
 3119:                         if ($key eq 'version') {
 3120:                             if ($current{$key} ne '') {
 3121:                                 $checkedon{$key} = ' checked="checked"';
 3122:                                 $checkedoff{$key} = '';
 3123:                             }
 3124:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3125:                             $checkedon{$key} = ' checked="checked"';
 3126:                             $checkedoff{$key} = '';
 3127:                         }
 3128:                     }
 3129:                 }
 3130:             }
 3131:             foreach my $type (@types) {
 3132:                 next if ($type ne 'version' && !@locations);
 3133:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3134:                 $datatable .= '<tr'.$css_class.'>
 3135:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3136:                                <span class="LC_nobreak">&nbsp;
 3137:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3138:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3139:                 if ($type eq 'version') {
 3140:                     my $selector = '<select name="'.$prefix.'_version">';
 3141:                     foreach my $version (@lcversions) {
 3142:                         my $selected = '';
 3143:                         if ($current{'version'} eq $version) {
 3144:                             $selected = ' selected="selected"';
 3145:                         }
 3146:                         $selector .= ' <option value="'.$version.'"'.
 3147:                                      $selected.'>'.$version.'</option>';
 3148:                     }
 3149:                     $selector .= '</select> ';
 3150:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3151:                 } else {
 3152:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3153:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3154:                                  ' />'.('&nbsp;'x2).
 3155:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3156:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3157:                                  "\n".
 3158:                                  '</div><div><table>';
 3159:                     my $rem;
 3160:                     for (my $i=0; $i<@locations; $i++) {
 3161:                         my ($showloc,$value,$checkedtype);
 3162:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3163:                             my $ip = $by_location{$locations[$i]}->[0];
 3164:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3165:                                  $value = join(':',@{$by_ip{$ip}});
 3166:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3167:                                 if (ref($current{$type}) eq 'ARRAY') {
 3168:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3169:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3170:                                             $checkedtype = ' checked="checked"';
 3171:                                             last;
 3172:                                         }
 3173:                                     }
 3174:                                 }
 3175:                             }
 3176:                         }
 3177:                         $rem = $i%($numinrow);
 3178:                         if ($rem == 0) {
 3179:                             if ($i > 0) {
 3180:                                 $datatable .= '</tr>';
 3181:                             }
 3182:                             $datatable .= '<tr>';
 3183:                         }
 3184:                         $datatable .= '<td class="LC_left_item">'.
 3185:                                       '<span class="LC_nobreak"><label>'.
 3186:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3187:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3188:                                       '</label></span></td>';
 3189:                     }
 3190:                     $rem = @locations%($numinrow);
 3191:                     my $colsleft = $numinrow - $rem;
 3192:                     if ($colsleft > 1 ) {
 3193:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3194:                                       '&nbsp;</td>';
 3195:                     } elsif ($colsleft == 1) {
 3196:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3197:                     }
 3198:                     $datatable .= '</tr></table>';
 3199:                 }
 3200:                 $datatable .= '</td></tr>';
 3201:                 $itemcount ++;
 3202:             }
 3203:         }
 3204:     }
 3205:     $$rowtotal += $itemcount;
 3206:     return $datatable;
 3207: }
 3208: 
 3209: sub build_location_hashes {
 3210:     my ($intdoms,$by_ip,$by_location) = @_;
 3211:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3212:                   (ref($by_location) eq 'HASH')); 
 3213:     my %iphost = &Apache::lonnet::get_iphost();
 3214:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3215:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3216:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3217:         foreach my $id (@{$iphost{$primary_ip}}) {
 3218:             my $intdom = &Apache::lonnet::internet_dom($id);
 3219:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3220:                 push(@{$intdoms},$intdom);
 3221:             }
 3222:         }
 3223:     }
 3224:     foreach my $ip (keys(%iphost)) {
 3225:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3226:             foreach my $id (@{$iphost{$ip}}) {
 3227:                 my $location = &Apache::lonnet::internet_dom($id);
 3228:                 if ($location) {
 3229:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3230:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3231:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3232:                             push(@{$by_ip->{$ip}},$location);
 3233:                         }
 3234:                     } else {
 3235:                         $by_ip->{$ip} = [$location];
 3236:                     }
 3237:                 }
 3238:             }
 3239:         }
 3240:     }
 3241:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3242:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3243:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3244:             my $first = $by_ip->{$ip}->[0];
 3245:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3246:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3247:                     push(@{$by_location->{$first}},$ip);
 3248:                 }
 3249:             } else {
 3250:                 $by_location->{$first} = [$ip];
 3251:             }
 3252:         }
 3253:     }
 3254:     return;
 3255: }
 3256: 
 3257: sub current_offloads_to {
 3258:     my ($dom,$settings,$servers) = @_;
 3259:     my (%spareid,%otherdomconfigs);
 3260:     if (ref($servers) eq 'HASH') {
 3261:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3262:             my $gotspares;
 3263:             if (ref($settings) eq 'HASH') {
 3264:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3265:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3266:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3267:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3268:                         $gotspares = 1;
 3269:                     }
 3270:                 }
 3271:             }
 3272:             unless ($gotspares) {
 3273:                 my $gotspares;
 3274:                 my $serverhomeID =
 3275:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3276:                 my $serverhomedom =
 3277:                     &Apache::lonnet::host_domain($serverhomeID);
 3278:                 if ($serverhomedom ne $dom) {
 3279:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3280:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3281:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3282:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3283:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3284:                                 $gotspares = 1;
 3285:                             }
 3286:                         }
 3287:                     } else {
 3288:                         $otherdomconfigs{$serverhomedom} =
 3289:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3290:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3291:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3292:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3293:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3294:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3295:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3296:                                         $gotspares = 1;
 3297:                                     }
 3298:                                 }
 3299:                             }
 3300:                         }
 3301:                     }
 3302:                 }
 3303:             }
 3304:             unless ($gotspares) {
 3305:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3306:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3307:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3308:                } else {
 3309:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3310:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3311:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3312:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3313:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3314:                     } else {
 3315:                         my %what = (
 3316:                              spareid => 1,
 3317:                         );
 3318:                         my ($result,$returnhash) = 
 3319:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3320:                         if ($result eq 'ok') { 
 3321:                             if (ref($returnhash) eq 'HASH') {
 3322:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3323:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3324:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3325:                                 }
 3326:                             }
 3327:                         }
 3328:                     }
 3329:                 }
 3330:             }
 3331:         }
 3332:     }
 3333:     return %spareid;
 3334: }
 3335: 
 3336: sub spares_row {
 3337:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3338:     my $css_class;
 3339:     my $numinrow = 4;
 3340:     my $itemcount = 1;
 3341:     my $datatable;
 3342:     my %typetitles = &sparestype_titles();
 3343:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3344:         foreach my $server (sort(keys(%{$servers}))) {
 3345:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3346:             my ($othercontrol,$serverdom);
 3347:             if ($serverhome ne $server) {
 3348:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3349:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3350:             } else {
 3351:                 $serverdom = &Apache::lonnet::host_domain($server);
 3352:                 if ($serverdom ne $dom) {
 3353:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3354:                 }
 3355:             }
 3356:             next unless (ref($spareid->{$server}) eq 'HASH');
 3357:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3358:             $datatable .= '<tr'.$css_class.'>
 3359:                            <td rowspan="2">
 3360:                             <span class="LC_nobreak">'.
 3361:                           &mt('[_1] when busy, offloads to:'
 3362:                               ,'<b>'.$server.'</b>').
 3363:                           "\n";
 3364:             my (%current,%canselect);
 3365:             my @choices = 
 3366:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3367:             foreach my $type ('primary','default') {
 3368:                 if (ref($spareid->{$server}) eq 'HASH') {
 3369:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3370:                         my @spares = @{$spareid->{$server}{$type}};
 3371:                         if (@spares > 0) {
 3372:                             if ($othercontrol) {
 3373:                                 $current{$type} = join(', ',@spares);
 3374:                             } else {
 3375:                                 $current{$type} .= '<table>';
 3376:                                 my $numspares = scalar(@spares);
 3377:                                 for (my $i=0;  $i<@spares; $i++) {
 3378:                                     my $rem = $i%($numinrow);
 3379:                                     if ($rem == 0) {
 3380:                                         if ($i > 0) {
 3381:                                             $current{$type} .= '</tr>';
 3382:                                         }
 3383:                                         $current{$type} .= '<tr>';
 3384:                                     }
 3385:                                     $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;'.
 3386:                                                        $spareid->{$server}{$type}[$i].
 3387:                                                        '</label></td>'."\n";
 3388:                                 }
 3389:                                 my $rem = @spares%($numinrow);
 3390:                                 my $colsleft = $numinrow - $rem;
 3391:                                 if ($colsleft > 1 ) {
 3392:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3393:                                                        '" class="LC_left_item">'.
 3394:                                                        '&nbsp;</td>';
 3395:                                 } elsif ($colsleft == 1) {
 3396:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3397:                                 }
 3398:                                 $current{$type} .= '</tr></table>';
 3399:                             }
 3400:                         }
 3401:                     }
 3402:                     if ($current{$type} eq '') {
 3403:                         $current{$type} = &mt('None specified');
 3404:                     }
 3405:                     if ($othercontrol) {
 3406:                         if ($type eq 'primary') {
 3407:                             $canselect{$type} = $othercontrol;
 3408:                         }
 3409:                     } else {
 3410:                         $canselect{$type} = 
 3411:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3412:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3413:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3414:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3415:                         if (@choices > 0) {
 3416:                             foreach my $lonhost (@choices) {
 3417:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3418:                             }
 3419:                         }
 3420:                         $canselect{$type} .= '</select>'."\n";
 3421:                     }
 3422:                 } else {
 3423:                     $current{$type} = &mt('Could not be determined');
 3424:                     if ($type eq 'primary') {
 3425:                         $canselect{$type} =  $othercontrol;
 3426:                     }
 3427:                 }
 3428:                 if ($type eq 'default') {
 3429:                     $datatable .= '<tr'.$css_class.'>';
 3430:                 }
 3431:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3432:                               '<td>'.$current{$type}.'</td>'."\n".
 3433:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3434:             }
 3435:             $itemcount ++;
 3436:         }
 3437:     }
 3438:     $$rowtotal += $itemcount;
 3439:     return $datatable;
 3440: }
 3441: 
 3442: sub possible_newspares {
 3443:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3444:     my $serverhostname = &Apache::lonnet::hostname($server);
 3445:     my %excluded;
 3446:     if ($serverhostname ne '') {
 3447:         %excluded = (
 3448:                        $serverhostname => 1,
 3449:                     );
 3450:     }
 3451:     if (ref($currspares) eq 'HASH') {
 3452:         foreach my $type (keys(%{$currspares})) {
 3453:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3454:                 if (@{$currspares->{$type}} > 0) {
 3455:                     foreach my $curr (@{$currspares->{$type}}) {
 3456:                         my $hostname = &Apache::lonnet::hostname($curr);
 3457:                         $excluded{$hostname} = 1;
 3458:                     }
 3459:                 }
 3460:             }
 3461:         }
 3462:     }
 3463:     my @choices;
 3464:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3465:         if (keys(%{$serverhomes}) > 1) {
 3466:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3467:                 unless ($excluded{$name}) {
 3468:                     if (exists($altids->{$serverhomes->{$name}})) {
 3469:                         push(@choices,$altids->{$serverhomes->{$name}});
 3470:                     } else {
 3471:                         push(@choices,$serverhomes->{$name});
 3472:                     }
 3473:                 }
 3474:             }
 3475:         }
 3476:     }
 3477:     return sort(@choices);
 3478: }
 3479: 
 3480: sub print_loadbalancing {
 3481:     my ($dom,$settings,$rowtotal) = @_;
 3482:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3483:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3484:     my $numinrow = 1;
 3485:     my $datatable;
 3486:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3487:     my (%currbalancer,%currtargets,%currrules,%existing);
 3488:     if (ref($settings) eq 'HASH') {
 3489:         %existing = %{$settings};
 3490:     }
 3491:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3492:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3493:                                   \%currtargets,\%currrules);
 3494:     } else {
 3495:         return;
 3496:     }
 3497:     my ($othertitle,$usertypes,$types) =
 3498:         &Apache::loncommon::sorted_inst_types($dom);
 3499:     my $rownum = 8;
 3500:     if (ref($types) eq 'ARRAY') {
 3501:         $rownum += scalar(@{$types});
 3502:     }
 3503:     my @css_class = ('LC_odd_row','LC_even_row');
 3504:     my $balnum = 0;
 3505:     my $islast;
 3506:     my (@toshow,$disabledtext);
 3507:     if (keys(%currbalancer) > 0) {
 3508:         @toshow = sort(keys(%currbalancer));
 3509:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3510:             push(@toshow,'');
 3511:         }
 3512:     } else {
 3513:         @toshow = ('');
 3514:         $disabledtext = &mt('No existing load balancer');
 3515:     }
 3516:     foreach my $lonhost (@toshow) {
 3517:         if ($balnum == scalar(@toshow)-1) {
 3518:             $islast = 1;
 3519:         } else {
 3520:             $islast = 0;
 3521:         }
 3522:         my $cssidx = $balnum%2;
 3523:         my $targets_div_style = 'display: none';
 3524:         my $disabled_div_style = 'display: block';
 3525:         my $homedom_div_style = 'display: none';
 3526:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3527:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3528:                       '<p>';
 3529:         if ($lonhost eq '') {
 3530:             $datatable .= '<span class="LC_nobreak">';
 3531:             if (keys(%currbalancer) > 0) {
 3532:                 $datatable .= &mt('Add balancer:');
 3533:             } else {
 3534:                 $datatable .= &mt('Enable balancer:');
 3535:             }
 3536:             $datatable .= '&nbsp;'.
 3537:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3538:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3539:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3540:                           '<option value="" selected="selected">'.&mt('None').
 3541:                           '</option>'."\n";
 3542:             foreach my $server (sort(keys(%servers))) {
 3543:                 next if ($currbalancer{$server});
 3544:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3545:             }
 3546:             $datatable .=
 3547:                 '</select>'."\n".
 3548:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3549:         } else {
 3550:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3551:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3552:                            &mt('Stop balancing').'</label>'.
 3553:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3554:             $targets_div_style = 'display: block';
 3555:             $disabled_div_style = 'display: none';
 3556:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3557:                 $homedom_div_style = 'display: block';
 3558:             }
 3559:         }
 3560:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3561:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3562:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3563:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3564:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3565:         my @sparestypes = ('primary','default');
 3566:         my %typetitles = &sparestype_titles();
 3567:         foreach my $sparetype (@sparestypes) {
 3568:             my $targettable;
 3569:             for (my $i=0; $i<$numspares; $i++) {
 3570:                 my $checked;
 3571:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3572:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3573:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3574:                             $checked = ' checked="checked"';
 3575:                         }
 3576:                     }
 3577:                 }
 3578:                 my ($chkboxval,$disabled);
 3579:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3580:                     $chkboxval = $spares[$i];
 3581:                 }
 3582:                 if (exists($currbalancer{$spares[$i]})) {
 3583:                     $disabled = ' disabled="disabled"';
 3584:                 }
 3585:                 $targettable .=
 3586:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3587:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3588:                     '</span></label></td>';
 3589:                 my $rem = $i%($numinrow);
 3590:                 if ($rem == 0) {
 3591:                     if (($i > 0) && ($i < $numspares-1)) {
 3592:                         $targettable .= '</tr>';
 3593:                     }
 3594:                     if ($i < $numspares-1) {
 3595:                         $targettable .= '<tr>';
 3596:                     }
 3597:                 }
 3598:             }
 3599:             if ($targettable ne '') {
 3600:                 my $rem = $numspares%($numinrow);
 3601:                 my $colsleft = $numinrow - $rem;
 3602:                 if ($colsleft > 1 ) {
 3603:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3604:                                     '&nbsp;</td>';
 3605:                 } elsif ($colsleft == 1) {
 3606:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3607:                 }
 3608:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3609:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3610:             }
 3611:         }
 3612:         $datatable .= '</div></td></tr>'.
 3613:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3614:                                            $othertitle,$usertypes,$types,\%servers,
 3615:                                            \%currbalancer,$lonhost,
 3616:                                            $targets_div_style,$homedom_div_style,
 3617:                                            $css_class[$cssidx],$balnum,$islast);
 3618:         $$rowtotal += $rownum;
 3619:         $balnum ++;
 3620:     }
 3621:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3622:     return $datatable;
 3623: }
 3624: 
 3625: sub get_loadbalancers_config {
 3626:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3627:     return unless ((ref($servers) eq 'HASH') &&
 3628:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3629:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3630:     if (keys(%{$existing}) > 0) {
 3631:         my $oldlonhost;
 3632:         foreach my $key (sort(keys(%{$existing}))) {
 3633:             if ($key eq 'lonhost') {
 3634:                 $oldlonhost = $existing->{'lonhost'};
 3635:                 $currbalancer->{$oldlonhost} = 1;
 3636:             } elsif ($key eq 'targets') {
 3637:                 if ($oldlonhost) {
 3638:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3639:                 }
 3640:             } elsif ($key eq 'rules') {
 3641:                 if ($oldlonhost) {
 3642:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3643:                 }
 3644:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3645:                 $currbalancer->{$key} = 1;
 3646:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3647:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3648:             }
 3649:         }
 3650:     } else {
 3651:         my ($balancerref,$targetsref) =
 3652:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3653:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3654:             foreach my $server (sort(keys(%{$balancerref}))) {
 3655:                 $currbalancer->{$server} = 1;
 3656:                 $currtargets->{$server} = $targetsref->{$server};
 3657:             }
 3658:         }
 3659:     }
 3660:     return;
 3661: }
 3662: 
 3663: sub loadbalancing_rules {
 3664:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3665:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3666:         $css_class,$balnum,$islast) = @_;
 3667:     my $output;
 3668:     my $num = 0;
 3669:     my ($alltypes,$othertypes,$titles) =
 3670:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3671:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3672:         foreach my $type (@{$alltypes}) {
 3673:             $num ++;
 3674:             my $current;
 3675:             if (ref($currrules) eq 'HASH') {
 3676:                 $current = $currrules->{$type};
 3677:             }
 3678:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3679:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3680:                     $current = '';
 3681:                 }
 3682:             }
 3683:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3684:                                              $servers,$currbalancer,$lonhost,$dom,
 3685:                                              $targets_div_style,$homedom_div_style,
 3686:                                              $css_class,$balnum,$num,$islast);
 3687:         }
 3688:     }
 3689:     return $output;
 3690: }
 3691: 
 3692: sub loadbalancing_titles {
 3693:     my ($dom,$intdom,$usertypes,$types) = @_;
 3694:     my %othertypes = (
 3695:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3696:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3697:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3698:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3699:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3700:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3701:                      );
 3702:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3703:     if (ref($types) eq 'ARRAY') {
 3704:         unshift(@alltypes,@{$types},'default');
 3705:     }
 3706:     my %titles;
 3707:     foreach my $type (@alltypes) {
 3708:         if ($type =~ /^_LC_/) {
 3709:             $titles{$type} = $othertypes{$type};
 3710:         } elsif ($type eq 'default') {
 3711:             $titles{$type} = &mt('All users from [_1]',$dom);
 3712:             if (ref($types) eq 'ARRAY') {
 3713:                 if (@{$types} > 0) {
 3714:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3715:                 }
 3716:             }
 3717:         } elsif (ref($usertypes) eq 'HASH') {
 3718:             $titles{$type} = $usertypes->{$type};
 3719:         }
 3720:     }
 3721:     return (\@alltypes,\%othertypes,\%titles);
 3722: }
 3723: 
 3724: sub loadbalance_rule_row {
 3725:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3726:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3727:     my @rulenames;
 3728:     my %ruletitles = &offloadtype_text();
 3729:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3730:         @rulenames = ('balancer','offloadedto');
 3731:     } else {
 3732:         @rulenames = ('default','homeserver');
 3733:         if ($type eq '_LC_external') {
 3734:             push(@rulenames,'externalbalancer');
 3735:         } else {
 3736:             push(@rulenames,'specific');
 3737:         }
 3738:         push(@rulenames,'none');
 3739:     }
 3740:     my $style = $targets_div_style;
 3741:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3742:         $style = $homedom_div_style;
 3743:     }
 3744:     my $space;
 3745:     if ($islast && $num == 1) {
 3746:         $space = '<div display="inline-block">&nbsp;</div>';
 3747:     }
 3748:     my $output =
 3749:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3750:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3751:         '<td valaign="top">'.$space.
 3752:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3753:     for (my $i=0; $i<@rulenames; $i++) {
 3754:         my $rule = $rulenames[$i];
 3755:         my ($checked,$extra);
 3756:         if ($rulenames[$i] eq 'default') {
 3757:             $rule = '';
 3758:         }
 3759:         if ($rulenames[$i] eq 'specific') {
 3760:             if (ref($servers) eq 'HASH') {
 3761:                 my $default;
 3762:                 if (($current ne '') && (exists($servers->{$current}))) {
 3763:                     $checked = ' checked="checked"';
 3764:                 }
 3765:                 unless ($checked) {
 3766:                     $default = ' selected="selected"';
 3767:                 }
 3768:                 $extra =
 3769:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3770:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3771:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3772:                     '<option value=""'.$default.'></option>'."\n";
 3773:                 foreach my $server (sort(keys(%{$servers}))) {
 3774:                     if (ref($currbalancer) eq 'HASH') {
 3775:                         next if (exists($currbalancer->{$server}));
 3776:                     }
 3777:                     my $selected;
 3778:                     if ($server eq $current) {
 3779:                         $selected = ' selected="selected"';
 3780:                     }
 3781:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3782:                 }
 3783:                 $extra .= '</select>';
 3784:             }
 3785:         } elsif ($rule eq $current) {
 3786:             $checked = ' checked="checked"';
 3787:         }
 3788:         $output .= '<span class="LC_nobreak"><label>'.
 3789:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3790:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3791:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3792:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3793:                    '</label>'.$extra.'</span><br />'."\n";
 3794:     }
 3795:     $output .= '</div></td></tr>'."\n";
 3796:     return $output;
 3797: }
 3798: 
 3799: sub offloadtype_text {
 3800:     my %ruletitles = &Apache::lonlocal::texthash (
 3801:            'default'          => 'Offloads to default destinations',
 3802:            'homeserver'       => "Offloads to user's home server",
 3803:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3804:            'specific'         => 'Offloads to specific server',
 3805:            'none'             => 'No offload',
 3806:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3807:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3808:     );
 3809:     return %ruletitles;
 3810: }
 3811: 
 3812: sub sparestype_titles {
 3813:     my %typestitles = &Apache::lonlocal::texthash (
 3814:                           'primary' => 'primary',
 3815:                           'default' => 'default',
 3816:                       );
 3817:     return %typestitles;
 3818: }
 3819: 
 3820: sub contact_titles {
 3821:     my %titles = &Apache::lonlocal::texthash (
 3822:                    'supportemail' => 'Support E-mail address',
 3823:                    'adminemail'   => 'Default Server Admin E-mail address',
 3824:                    'errormail'    => 'Error reports to be e-mailed to',
 3825:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3826:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3827:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3828:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3829:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3830:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3831:                  );
 3832:     my %short_titles = &Apache::lonlocal::texthash (
 3833:                            adminemail   => 'Admin E-mail address',
 3834:                            supportemail => 'Support E-mail',
 3835:                        );   
 3836:     return (\%titles,\%short_titles);
 3837: }
 3838: 
 3839: sub tool_titles {
 3840:     my %titles = &Apache::lonlocal::texthash (
 3841:                      aboutme    => 'Personal web page',
 3842:                      blog       => 'Blog',
 3843:                      webdav     => 'WebDAV',
 3844:                      portfolio  => 'Portfolio',
 3845:                      official   => 'Official courses (with institutional codes)',
 3846:                      unofficial => 'Unofficial courses',
 3847:                      community  => 'Communities',
 3848:                      textbook   => 'Textbook courses',
 3849:                  );
 3850:     return %titles;
 3851: }
 3852: 
 3853: sub courserequest_titles {
 3854:     my %titles = &Apache::lonlocal::texthash (
 3855:                                    official   => 'Official',
 3856:                                    unofficial => 'Unofficial',
 3857:                                    community  => 'Communities',
 3858:                                    textbook   => 'Textbook',
 3859:                                    norequest  => 'Not allowed',
 3860:                                    approval   => 'Approval by Dom. Coord.',
 3861:                                    validate   => 'With validation',
 3862:                                    autolimit  => 'Numerical limit',
 3863:                                    unlimited  => '(blank for unlimited)',
 3864:                  );
 3865:     return %titles;
 3866: }
 3867: 
 3868: sub authorrequest_titles {
 3869:     my %titles = &Apache::lonlocal::texthash (
 3870:                                    norequest  => 'Not allowed',
 3871:                                    approval   => 'Approval by Dom. Coord.',
 3872:                                    automatic  => 'Automatic approval',
 3873:                  );
 3874:     return %titles;
 3875: }
 3876: 
 3877: sub courserequest_conditions {
 3878:     my %conditions = &Apache::lonlocal::texthash (
 3879:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3880:        validate   => '(Processing of request subject to institutional validation).',
 3881:                  );
 3882:     return %conditions;
 3883: }
 3884: 
 3885: 
 3886: sub print_usercreation {
 3887:     my ($position,$dom,$settings,$rowtotal) = @_;
 3888:     my $numinrow = 4;
 3889:     my $datatable;
 3890:     if ($position eq 'top') {
 3891:         $$rowtotal ++;
 3892:         my $rowcount = 0;
 3893:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3894:         if (ref($rules) eq 'HASH') {
 3895:             if (keys(%{$rules}) > 0) {
 3896:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3897:                                                 $ruleorder,$numinrow,$rowcount);
 3898:                 $$rowtotal ++;
 3899:                 $rowcount ++;
 3900:             }
 3901:         }
 3902:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3903:         if (ref($idrules) eq 'HASH') {
 3904:             if (keys(%{$idrules}) > 0) {
 3905:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3906:                                                 $idruleorder,$numinrow,$rowcount);
 3907:                 $$rowtotal ++;
 3908:                 $rowcount ++;
 3909:             }
 3910:         }
 3911:         if ($rowcount == 0) {
 3912:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3913:             $$rowtotal ++;
 3914:             $rowcount ++;
 3915:         }
 3916:     } elsif ($position eq 'middle') {
 3917:         my @creators = ('author','course','requestcrs');
 3918:         my ($rules,$ruleorder) =
 3919:             &Apache::lonnet::inst_userrules($dom,'username');
 3920:         my %lt = &usercreation_types();
 3921:         my %checked;
 3922:         if (ref($settings) eq 'HASH') {
 3923:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3924:                 foreach my $item (@creators) {
 3925:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3926:                 }
 3927:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3928:                 foreach my $item (@creators) {
 3929:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3930:                         $checked{$item} = 'none';
 3931:                     }
 3932:                 }
 3933:             }
 3934:         }
 3935:         my $rownum = 0;
 3936:         foreach my $item (@creators) {
 3937:             $rownum ++;
 3938:             if ($checked{$item} eq '') {
 3939:                 $checked{$item} = 'any';
 3940:             }
 3941:             my $css_class;
 3942:             if ($rownum%2) {
 3943:                 $css_class = '';
 3944:             } else {
 3945:                 $css_class = ' class="LC_odd_row" ';
 3946:             }
 3947:             $datatable .= '<tr'.$css_class.'>'.
 3948:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3949:                          '</span></td><td align="right">';
 3950:             my @options = ('any');
 3951:             if (ref($rules) eq 'HASH') {
 3952:                 if (keys(%{$rules}) > 0) {
 3953:                     push(@options,('official','unofficial'));
 3954:                 }
 3955:             }
 3956:             push(@options,'none');
 3957:             foreach my $option (@options) {
 3958:                 my $type = 'radio';
 3959:                 my $check = ' ';
 3960:                 if ($checked{$item} eq $option) {
 3961:                     $check = ' checked="checked" ';
 3962:                 } 
 3963:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3964:                               '<input type="'.$type.'" name="can_createuser_'.
 3965:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3966:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3967:             }
 3968:             $datatable .= '</td></tr>';
 3969:         }
 3970:     } else {
 3971:         my @contexts = ('author','course','domain');
 3972:         my @authtypes = ('int','krb4','krb5','loc');
 3973:         my %checked;
 3974:         if (ref($settings) eq 'HASH') {
 3975:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3976:                 foreach my $item (@contexts) {
 3977:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3978:                         foreach my $auth (@authtypes) {
 3979:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3980:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3981:                             }
 3982:                         }
 3983:                     }
 3984:                 }
 3985:             }
 3986:         } else {
 3987:             foreach my $item (@contexts) {
 3988:                 foreach my $auth (@authtypes) {
 3989:                     $checked{$item}{$auth} = ' checked="checked" ';
 3990:                 }
 3991:             }
 3992:         }
 3993:         my %title = &context_names();
 3994:         my %authname = &authtype_names();
 3995:         my $rownum = 0;
 3996:         my $css_class; 
 3997:         foreach my $item (@contexts) {
 3998:             if ($rownum%2) {
 3999:                 $css_class = '';
 4000:             } else {
 4001:                 $css_class = ' class="LC_odd_row" ';
 4002:             }
 4003:             $datatable .=   '<tr'.$css_class.'>'.
 4004:                             '<td>'.$title{$item}.
 4005:                             '</td><td class="LC_left_item">'.
 4006:                             '<span class="LC_nobreak">';
 4007:             foreach my $auth (@authtypes) {
 4008:                 $datatable .= '<label>'. 
 4009:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4010:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4011:                               $authname{$auth}.'</label>&nbsp;';
 4012:             }
 4013:             $datatable .= '</span></td></tr>';
 4014:             $rownum ++;
 4015:         }
 4016:         $$rowtotal += $rownum;
 4017:     }
 4018:     return $datatable;
 4019: }
 4020: 
 4021: sub print_selfcreation {
 4022:     my ($position,$dom,$settings,$rowtotal) = @_;
 4023:     my (@selfcreate,$createsettings,$datatable);
 4024:     if (ref($settings) eq 'HASH') {
 4025:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4026:             $createsettings = $settings->{'cancreate'};
 4027:             if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 4028:                 @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 4029:             } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 4030:                 if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4031:                     @selfcreate = ('email','login','sso');
 4032:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 4033:                     @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 4034:                 }
 4035:             }
 4036:         }
 4037:     }
 4038:     my %radiohash;
 4039:     my $numinrow = 4;
 4040:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4041:     if ($position eq 'top') {
 4042:         my %choices = &Apache::lonlocal::texthash (
 4043:                                                       cancreate_login      => 'Institutional Login',
 4044:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4045:                                                   );
 4046:         my @toggles = sort(keys(%choices));
 4047:         my %defaultchecked = (
 4048:                                'cancreate_login' => 'off',
 4049:                                'cancreate_sso'   => 'off',
 4050:                              );
 4051:         my ($onclick,$itemcount);
 4052:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4053:                                                      \%choices,$itemcount,$onclick);
 4054:         $$rowtotal += $itemcount;
 4055:         
 4056:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4057: 
 4058:         if (ref($usertypes) eq 'HASH') {
 4059:             if (keys(%{$usertypes}) > 0) {
 4060:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4061:                                              $dom,$numinrow,$othertitle,
 4062:                                              'statustocreate',$$rowtotal);
 4063:                 $$rowtotal ++;
 4064:             }
 4065:         }
 4066:     } elsif ($position eq 'middle') {
 4067:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4068:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4069:         $usertypes->{'default'} = $othertitle;
 4070:         if (ref($types) eq 'ARRAY') {
 4071:             push(@{$types},'default');
 4072:             $usertypes->{'default'} = $othertitle;
 4073:             foreach my $status (@{$types}) {
 4074:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4075:                                                        $numinrow,$$rowtotal,$usertypes);
 4076:                 $$rowtotal ++;
 4077:             }
 4078:         }
 4079:     } else {
 4080:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4081:         my %choices =
 4082:             &Apache::lonlocal::texthash(
 4083:                                           email         => 'Approved automatically',
 4084:                                           emailapproval => 'Queued for approval by DC',
 4085:                                           off           => 'Not enabled',
 4086:                                        );
 4087:         $datatable .= '<tr'.$css_class.'>'.
 4088:                       '<td>'.&mt('E-mail address as username').
 4089:                       '</td><td class="LC_left_item">'.
 4090:                       '<span class="LC_nobreak">';
 4091:         foreach my $option ('email','emailapproval','off') {
 4092:             my $checked;
 4093:             if ($option eq 'email') {
 4094:                 if ($radiohash{'cancreate_email'}) {
 4095:                     $checked = 'checked="checked"';
 4096:                 }
 4097:             } elsif ($option eq 'emailapproval') {
 4098:                 if ($radiohash{'cancreate_emailapproval'}) {
 4099:                     $checked = 'checked="checked"';
 4100:                 }
 4101:             } else {
 4102:                 if ((!$radiohash{'cancreate_email'}) && (!$radiohash{'cancreate_emailapproval'})) {
 4103:                     $checked = 'checked="checked"';  
 4104:                 }
 4105:             }
 4106:             $datatable .= '<label>'.
 4107:                           '<input type="radio" name="cancreate_email" '.
 4108:                           $checked.' value="'.$option.'" />'.
 4109:                           $choices{$option}.'</label>&nbsp;';
 4110:         }
 4111:         $$rowtotal ++;
 4112:         $datatable .= '</span></td></tr>'.
 4113:                       &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4114:         $$rowtotal ++;
 4115:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4116:         $numinrow = 1;
 4117:         my ($othertitle,$usertypes,$types) =  &Apache::loncommon::sorted_inst_types($dom);
 4118:         $usertypes->{'default'} = $othertitle;
 4119:         if (ref($types) eq 'ARRAY') {
 4120:             push(@{$types},'default');
 4121:             $usertypes->{'default'} = $othertitle;
 4122:             foreach my $status (@{$types}) {
 4123:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4124:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4125:                 $$rowtotal ++;
 4126:             }
 4127:         }
 4128:         my ($emailrules,$emailruleorder) =
 4129:             &Apache::lonnet::inst_userrules($dom,'email');
 4130:         if (ref($emailrules) eq 'HASH') {
 4131:             if (keys(%{$emailrules}) > 0) {
 4132:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4133:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4134:                 $$rowtotal ++;
 4135:             }
 4136:         }
 4137:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4138:     }
 4139:     return $datatable;
 4140: }
 4141: 
 4142: sub captcha_choice {
 4143:     my ($context,$settings,$itemcount) = @_;
 4144:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4145:     my %lt = &captcha_phrases();
 4146:     $keyentry = 'hidden';
 4147:     if ($context eq 'cancreate') {
 4148:         $rowname = &mt('CAPTCHA validation');
 4149:     } elsif ($context eq 'login') {
 4150:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4151:     }
 4152:     if (ref($settings) eq 'HASH') {
 4153:         if ($settings->{'captcha'}) {
 4154:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4155:         } else {
 4156:             $checked{'original'} = ' checked="checked"';
 4157:         }
 4158:         if ($settings->{'captcha'} eq 'recaptcha') {
 4159:             $pubtext = $lt{'pub'};
 4160:             $privtext = $lt{'priv'};
 4161:             $keyentry = 'text';
 4162:         }
 4163:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4164:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4165:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4166:         }
 4167:     } else {
 4168:         $checked{'original'} = ' checked="checked"';
 4169:     }
 4170:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4171:     my $output = '<tr'.$css_class.'>'.
 4172:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4173:                  '<table><tr><td>'."\n";
 4174:     foreach my $option ('original','recaptcha','notused') {
 4175:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4176:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4177:                    $lt{$option}.'</label></span>';
 4178:         unless ($option eq 'notused') {
 4179:             $output .= ('&nbsp;'x2)."\n";
 4180:         }
 4181:     }
 4182: #
 4183: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4184: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4185: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4186: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4187: #
 4188:     $output .= '</td></tr>'."\n".
 4189:                '<tr><td>'."\n".
 4190:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4191:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4192:                $currpub.'" size="40" /></span><br />'."\n".
 4193:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4194:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4195:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4196:                '</td></tr>';
 4197:     return $output;
 4198: }
 4199: 
 4200: sub user_formats_row {
 4201:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4202:     my $output;
 4203:     my %text = (
 4204:                    'username' => 'new usernames',
 4205:                    'id'       => 'IDs',
 4206:                    'email'    => 'self-created accounts (e-mail)',
 4207:                );
 4208:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4209:     $output = '<tr '.$css_class.'>'.
 4210:               '<td><span class="LC_nobreak">';
 4211:     if ($type eq 'email') {
 4212:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4213:     } else {
 4214:         $output .= &mt("Format rules to check for $text{$type}: ");
 4215:     }
 4216:     $output .= '</span></td>'.
 4217:                '<td class="LC_left_item" colspan="2"><table>';
 4218:     my $rem;
 4219:     if (ref($ruleorder) eq 'ARRAY') {
 4220:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4221:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4222:                 my $rem = $i%($numinrow);
 4223:                 if ($rem == 0) {
 4224:                     if ($i > 0) {
 4225:                         $output .= '</tr>';
 4226:                     }
 4227:                     $output .= '<tr>';
 4228:                 }
 4229:                 my $check = ' ';
 4230:                 if (ref($settings) eq 'HASH') {
 4231:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4232:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4233:                             $check = ' checked="checked" ';
 4234:                         }
 4235:                     }
 4236:                 }
 4237:                 $output .= '<td class="LC_left_item">'.
 4238:                            '<span class="LC_nobreak"><label>'.
 4239:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4240:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4241:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4242:             }
 4243:         }
 4244:         $rem = @{$ruleorder}%($numinrow);
 4245:     }
 4246:     my $colsleft = $numinrow - $rem;
 4247:     if ($colsleft > 1 ) {
 4248:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4249:                    '&nbsp;</td>';
 4250:     } elsif ($colsleft == 1) {
 4251:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4252:     }
 4253:     $output .= '</tr></table></td></tr>';
 4254:     return $output;
 4255: }
 4256: 
 4257: sub usercreation_types {
 4258:     my %lt = &Apache::lonlocal::texthash (
 4259:                     author     => 'When adding a co-author',
 4260:                     course     => 'When adding a user to a course',
 4261:                     requestcrs => 'When requesting a course',
 4262:                     any        => 'Any',
 4263:                     official   => 'Institutional only ',
 4264:                     unofficial => 'Non-institutional only',
 4265:                     none       => 'None',
 4266:     );
 4267:     return %lt;
 4268: }
 4269: 
 4270: sub selfcreation_types {
 4271:     my %lt = &Apache::lonlocal::texthash (
 4272:                     selfcreate => 'User creates own account',
 4273:                     any        => 'Any',
 4274:                     official   => 'Institutional only ',
 4275:                     unofficial => 'Non-institutional only',
 4276:                     email      => 'E-mail address',
 4277:                     login      => 'Institutional Login',
 4278:                     sso        => 'SSO',
 4279:              );
 4280: }
 4281: 
 4282: sub authtype_names {
 4283:     my %lt = &Apache::lonlocal::texthash(
 4284:                       int    => 'Internal',
 4285:                       krb4   => 'Kerberos 4',
 4286:                       krb5   => 'Kerberos 5',
 4287:                       loc    => 'Local',
 4288:                   );
 4289:     return %lt;
 4290: }
 4291: 
 4292: sub context_names {
 4293:     my %context_title = &Apache::lonlocal::texthash(
 4294:        author => 'Creating users when an Author',
 4295:        course => 'Creating users when in a course',
 4296:        domain => 'Creating users when a Domain Coordinator',
 4297:     );
 4298:     return %context_title;
 4299: }
 4300: 
 4301: sub print_usermodification {
 4302:     my ($position,$dom,$settings,$rowtotal) = @_;
 4303:     my $numinrow = 4;
 4304:     my ($context,$datatable,$rowcount);
 4305:     if ($position eq 'top') {
 4306:         $rowcount = 0;
 4307:         $context = 'author'; 
 4308:         foreach my $role ('ca','aa') {
 4309:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4310:                                                    $numinrow,$rowcount);
 4311:             $$rowtotal ++;
 4312:             $rowcount ++;
 4313:         }
 4314:     } elsif ($position eq 'bottom') {
 4315:         $context = 'course';
 4316:         $rowcount = 0;
 4317:         foreach my $role ('st','ep','ta','in','cr') {
 4318:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4319:                                                    $numinrow,$rowcount);
 4320:             $$rowtotal ++;
 4321:             $rowcount ++;
 4322:         }
 4323:     }
 4324:     return $datatable;
 4325: }
 4326: 
 4327: sub print_defaults {
 4328:     my ($dom,$settings,$rowtotal) = @_;
 4329:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4330:                  'datelocale_def','portal_def');
 4331:     my %defaults;
 4332:     if (ref($settings) eq 'HASH') {
 4333:         %defaults = %{$settings};
 4334:     } else {
 4335:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4336:         foreach my $item (@items) {
 4337:             $defaults{$item} = $domdefaults{$item};
 4338:         }
 4339:     }
 4340:     my $titles = &defaults_titles($dom);
 4341:     my $rownum = 0;
 4342:     my ($datatable,$css_class);
 4343:     foreach my $item (@items) {
 4344:         if ($rownum%2) {
 4345:             $css_class = '';
 4346:         } else {
 4347:             $css_class = ' class="LC_odd_row" ';
 4348:         }
 4349:         $datatable .= '<tr'.$css_class.'>'.
 4350:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 4351:                   '</span></td><td class="LC_right_item">';
 4352:         if ($item eq 'auth_def') {
 4353:             my @authtypes = ('internal','krb4','krb5','localauth');
 4354:             my %shortauth = (
 4355:                              internal => 'int',
 4356:                              krb4 => 'krb4',
 4357:                              krb5 => 'krb5',
 4358:                              localauth  => 'loc'
 4359:                            );
 4360:             my %authnames = &authtype_names();
 4361:             foreach my $auth (@authtypes) {
 4362:                 my $checked = ' ';
 4363:                 if ($defaults{$item} eq $auth) {
 4364:                     $checked = ' checked="checked" ';
 4365:                 }
 4366:                 $datatable .= '<label><input type="radio" name="'.$item.
 4367:                               '" value="'.$auth.'"'.$checked.'/>'.
 4368:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4369:             }
 4370:         } elsif ($item eq 'timezone_def') {
 4371:             my $includeempty = 1;
 4372:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4373:         } elsif ($item eq 'datelocale_def') {
 4374:             my $includeempty = 1;
 4375:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4376:         } elsif ($item eq 'lang_def') {
 4377:             my %langchoices = &get_languages_hash();
 4378:             $langchoices{''} = 'No language preference';
 4379:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4380:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4381:                                                           \%langchoices);
 4382:         } else {
 4383:             my $size;
 4384:             if ($item eq 'portal_def') {
 4385:                 $size = ' size="25"';
 4386:             }
 4387:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 4388:                           $defaults{$item}.'"'.$size.' />';
 4389:         }
 4390:         $datatable .= '</td></tr>';
 4391:         $rownum ++;
 4392:     }
 4393:     $$rowtotal += $rownum;
 4394:     return $datatable;
 4395: }
 4396: 
 4397: sub get_languages_hash {
 4398:     my %langchoices;
 4399:     foreach my $id (&Apache::loncommon::languageids()) {
 4400:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4401:         if ($code ne '') {
 4402:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4403:         }
 4404:     }
 4405:     return %langchoices;
 4406: }
 4407: 
 4408: sub defaults_titles {
 4409:     my ($dom) = @_;
 4410:     my %titles = &Apache::lonlocal::texthash (
 4411:                    'auth_def'      => 'Default authentication type',
 4412:                    'auth_arg_def'  => 'Default authentication argument',
 4413:                    'lang_def'      => 'Default language',
 4414:                    'timezone_def'  => 'Default timezone',
 4415:                    'datelocale_def' => 'Default locale for dates',
 4416:                    'portal_def'     => 'Portal/Default URL',
 4417:                  );
 4418:     if ($dom) {
 4419:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4420:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4421:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4422:         $protocol = 'http' if ($protocol ne 'https');
 4423:         if ($uint_dom) {
 4424:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4425:                                          $uint_dom);
 4426:         }
 4427:     }
 4428:     return (\%titles);
 4429: }
 4430: 
 4431: sub print_scantronformat {
 4432:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4433:     my $itemcount = 1;
 4434:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4435:         %confhash);
 4436:     my $switchserver = &check_switchserver($dom,$confname);
 4437:     my %lt = &Apache::lonlocal::texthash (
 4438:                 default => 'Default bubblesheet format file error',
 4439:                 custom  => 'Custom bubblesheet format file error',
 4440:              );
 4441:     my %scantronfiles = (
 4442:         default => 'default.tab',
 4443:         custom => 'custom.tab',
 4444:     );
 4445:     foreach my $key (keys(%scantronfiles)) {
 4446:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4447:                               .$scantronfiles{$key};
 4448:     }
 4449:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4450:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4451:         if (!$switchserver) {
 4452:             my $servadm = $r->dir_config('lonAdmEMail');
 4453:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4454:             if ($configuserok eq 'ok') {
 4455:                 if ($author_ok eq 'ok') {
 4456:                     my %legacyfile = (
 4457:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4458:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4459:                     );
 4460:                     my %md5chk;
 4461:                     foreach my $type (keys(%legacyfile)) {
 4462:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4463:                         chomp($md5chk{$type});
 4464:                     }
 4465:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4466:                         foreach my $type (keys(%legacyfile)) {
 4467:                             ($scantronurls{$type},my $error) = 
 4468:                                 &legacy_scantronformat($r,$dom,$confname,
 4469:                                                  $type,$legacyfile{$type},
 4470:                                                  $scantronurls{$type},
 4471:                                                  $scantronfiles{$type});
 4472:                             if ($error ne '') {
 4473:                                 $error{$type} = $error;
 4474:                             }
 4475:                         }
 4476:                         if (keys(%error) == 0) {
 4477:                             $is_custom = 1;
 4478:                             $confhash{'scantron'}{'scantronformat'} = 
 4479:                                 $scantronurls{'custom'};
 4480:                             my $putresult = 
 4481:                                 &Apache::lonnet::put_dom('configuration',
 4482:                                                          \%confhash,$dom);
 4483:                             if ($putresult ne 'ok') {
 4484:                                 $error{'custom'} = 
 4485:                                     '<span class="LC_error">'.
 4486:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4487:                             }
 4488:                         }
 4489:                     } else {
 4490:                         ($scantronurls{'default'},my $error) =
 4491:                             &legacy_scantronformat($r,$dom,$confname,
 4492:                                           'default',$legacyfile{'default'},
 4493:                                           $scantronurls{'default'},
 4494:                                           $scantronfiles{'default'});
 4495:                         if ($error eq '') {
 4496:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4497:                             my $putresult =
 4498:                                 &Apache::lonnet::put_dom('configuration',
 4499:                                                          \%confhash,$dom);
 4500:                             if ($putresult ne 'ok') {
 4501:                                 $error{'default'} =
 4502:                                     '<span class="LC_error">'.
 4503:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4504:                             }
 4505:                         } else {
 4506:                             $error{'default'} = $error;
 4507:                         }
 4508:                     }
 4509:                 }
 4510:             }
 4511:         } else {
 4512:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4513:         }
 4514:     }
 4515:     if (ref($settings) eq 'HASH') {
 4516:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4517:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4518:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4519:                 $scantronurl = '';
 4520:             } else {
 4521:                 $scantronurl = $settings->{'scantronformat'};
 4522:             }
 4523:             $is_custom = 1;
 4524:         } else {
 4525:             $scantronurl = $scantronurls{'default'};
 4526:         }
 4527:     } else {
 4528:         if ($is_custom) {
 4529:             $scantronurl = $scantronurls{'custom'};
 4530:         } else {
 4531:             $scantronurl = $scantronurls{'default'};
 4532:         }
 4533:     }
 4534:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4535:     $datatable .= '<tr'.$css_class.'>';
 4536:     if (!$is_custom) {
 4537:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4538:                       '<span class="LC_nobreak">';
 4539:         if ($scantronurl) {
 4540:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4541:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4542:         } else {
 4543:             $datatable = &mt('File unavailable for display');
 4544:         }
 4545:         $datatable .= '</span></td>';
 4546:         if (keys(%error) == 0) { 
 4547:             $datatable .= '<td valign="bottom">';
 4548:             if (!$switchserver) {
 4549:                 $datatable .= &mt('Upload:').'<br />';
 4550:             }
 4551:         } else {
 4552:             my $errorstr;
 4553:             foreach my $key (sort(keys(%error))) {
 4554:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4555:             }
 4556:             $datatable .= '<td>'.$errorstr;
 4557:         }
 4558:     } else {
 4559:         if (keys(%error) > 0) {
 4560:             my $errorstr;
 4561:             foreach my $key (sort(keys(%error))) {
 4562:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4563:             } 
 4564:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4565:         } elsif ($scantronurl) {
 4566:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4567:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4568:             $datatable .= '<td><span class="LC_nobreak">'.
 4569:                           $link.
 4570:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4571:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4572:                           '<td><span class="LC_nobreak">&nbsp;'.
 4573:                           &mt('Replace:').'</span><br />';
 4574:         }
 4575:     }
 4576:     if (keys(%error) == 0) {
 4577:         if ($switchserver) {
 4578:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4579:         } else {
 4580:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4581:                          '<input type="file" name="scantronformat" /></span>';
 4582:         }
 4583:     }
 4584:     $datatable .= '</td></tr>';
 4585:     $$rowtotal ++;
 4586:     return $datatable;
 4587: }
 4588: 
 4589: sub legacy_scantronformat {
 4590:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4591:     my ($url,$error);
 4592:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4593:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4594:         (my $result,$url) =
 4595:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4596:                          '','',$newfile);
 4597:         if ($result ne 'ok') {
 4598:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4599:         }
 4600:     }
 4601:     return ($url,$error);
 4602: }
 4603: 
 4604: sub print_coursecategories {
 4605:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4606:     my $datatable;
 4607:     if ($position eq 'top') {
 4608:         my $toggle_cats_crs = ' ';
 4609:         my $toggle_cats_dom = ' checked="checked" ';
 4610:         my $can_cat_crs = ' ';
 4611:         my $can_cat_dom = ' checked="checked" ';
 4612:         my $toggle_catscomm_comm = ' ';
 4613:         my $toggle_catscomm_dom = ' checked="checked" ';
 4614:         my $can_catcomm_comm = ' ';
 4615:         my $can_catcomm_dom = ' checked="checked" ';
 4616: 
 4617:         if (ref($settings) eq 'HASH') {
 4618:             if ($settings->{'togglecats'} eq 'crs') {
 4619:                 $toggle_cats_crs = $toggle_cats_dom;
 4620:                 $toggle_cats_dom = ' ';
 4621:             }
 4622:             if ($settings->{'categorize'} eq 'crs') {
 4623:                 $can_cat_crs = $can_cat_dom;
 4624:                 $can_cat_dom = ' ';
 4625:             }
 4626:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4627:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4628:                 $toggle_catscomm_dom = ' ';
 4629:             }
 4630:             if ($settings->{'categorizecomm'} eq 'comm') {
 4631:                 $can_catcomm_comm = $can_catcomm_dom;
 4632:                 $can_catcomm_dom = ' ';
 4633:             }
 4634:         }
 4635:         my %title = &Apache::lonlocal::texthash (
 4636:                      togglecats     => 'Show/Hide a course in catalog',
 4637:                      togglecatscomm => 'Show/Hide a community in catalog',
 4638:                      categorize     => 'Assign a category to a course',
 4639:                      categorizecomm => 'Assign a category to a community',
 4640:                     );
 4641:         my %level = &Apache::lonlocal::texthash (
 4642:                      dom  => 'Set in Domain',
 4643:                      crs  => 'Set in Course',
 4644:                      comm => 'Set in Community',
 4645:                     );
 4646:         $datatable = '<tr class="LC_odd_row">'.
 4647:                   '<td>'.$title{'togglecats'}.'</td>'.
 4648:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4649:                   '<input type="radio" name="togglecats"'.
 4650:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4651:                   '<label><input type="radio" name="togglecats"'.
 4652:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4653:                   '</tr><tr>'.
 4654:                   '<td>'.$title{'categorize'}.'</td>'.
 4655:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4656:                   '<label><input type="radio" name="categorize"'.
 4657:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4658:                   '<label><input type="radio" name="categorize"'.
 4659:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4660:                   '</tr><tr class="LC_odd_row">'.
 4661:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4662:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4663:                   '<input type="radio" name="togglecatscomm"'.
 4664:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4665:                   '<label><input type="radio" name="togglecatscomm"'.
 4666:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4667:                   '</tr><tr>'.
 4668:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4669:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4670:                   '<label><input type="radio" name="categorizecomm"'.
 4671:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4672:                   '<label><input type="radio" name="categorizecomm"'.
 4673:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4674:                   '</tr>';
 4675:         $$rowtotal += 4;
 4676:     } else {
 4677:         my $css_class;
 4678:         my $itemcount = 1;
 4679:         my $cathash; 
 4680:         if (ref($settings) eq 'HASH') {
 4681:             $cathash = $settings->{'cats'};
 4682:         }
 4683:         if (ref($cathash) eq 'HASH') {
 4684:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4685:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4686:                                                    \%allitems,\%idx,\@jsarray);
 4687:             my $maxdepth = scalar(@cats);
 4688:             my $colattrib = '';
 4689:             if ($maxdepth > 2) {
 4690:                 $colattrib = ' colspan="2" ';
 4691:             }
 4692:             my @path;
 4693:             if (@cats > 0) {
 4694:                 if (ref($cats[0]) eq 'ARRAY') {
 4695:                     my $numtop = @{$cats[0]};
 4696:                     my $maxnum = $numtop;
 4697:                     my %default_names = (
 4698:                           instcode    => &mt('Official courses'),
 4699:                           communities => &mt('Communities'),
 4700:                     );
 4701: 
 4702:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4703:                         ($cathash->{'instcode::0'} eq '') ||
 4704:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4705:                         ($cathash->{'communities::0'} eq '')) {
 4706:                         $maxnum ++;
 4707:                     }
 4708:                     my $lastidx;
 4709:                     for (my $i=0; $i<$numtop; $i++) {
 4710:                         my $parent = $cats[0][$i];
 4711:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4712:                         my $item = &escape($parent).'::0';
 4713:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4714:                         $lastidx = $idx{$item};
 4715:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4716:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4717:                         for (my $k=0; $k<=$maxnum; $k++) {
 4718:                             my $vpos = $k+1;
 4719:                             my $selstr;
 4720:                             if ($k == $i) {
 4721:                                 $selstr = ' selected="selected" ';
 4722:                             }
 4723:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4724:                         }
 4725:                         $datatable .= '</select></span></td><td>';
 4726:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4727:                             $datatable .=  '<span class="LC_nobreak">'
 4728:                                            .$default_names{$parent}.'</span>';
 4729:                             if ($parent eq 'instcode') {
 4730:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4731:                                               .&mt('with institutional codes')
 4732:                                               .')</span></td><td'.$colattrib.'>';
 4733:                             } else {
 4734:                                 $datatable .= '<table><tr><td>';
 4735:                             }
 4736:                             $datatable .= '<span class="LC_nobreak">'
 4737:                                           .'<label><input type="radio" name="'
 4738:                                           .$parent.'" value="1" checked="checked" />'
 4739:                                           .&mt('Display').'</label>';
 4740:                             if ($parent eq 'instcode') {
 4741:                                 $datatable .= '&nbsp;';
 4742:                             } else {
 4743:                                 $datatable .= '</span></td></tr><tr><td>'
 4744:                                               .'<span class="LC_nobreak">';
 4745:                             }
 4746:                             $datatable .= '<label><input type="radio" name="'
 4747:                                           .$parent.'" value="0" />'
 4748:                                           .&mt('Do not display').'</label></span>';
 4749:                             if ($parent eq 'communities') {
 4750:                                 $datatable .= '</td></tr></table>';
 4751:                             }
 4752:                             $datatable .= '</td>';
 4753:                         } else {
 4754:                             $datatable .= $parent
 4755:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4756:                                           .'<input type="checkbox" name="deletecategory" '
 4757:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4758:                         }
 4759:                         my $depth = 1;
 4760:                         push(@path,$parent);
 4761:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4762:                         pop(@path);
 4763:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4764:                         $itemcount ++;
 4765:                     }
 4766:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4767:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4768:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4769:                     for (my $k=0; $k<=$maxnum; $k++) {
 4770:                         my $vpos = $k+1;
 4771:                         my $selstr;
 4772:                         if ($k == $numtop) {
 4773:                             $selstr = ' selected="selected" ';
 4774:                         }
 4775:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4776:                     }
 4777:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4778:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4779:                                   .'</tr>'."\n";
 4780:                     $itemcount ++;
 4781:                     foreach my $default ('instcode','communities') {
 4782:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4783:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4784:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4785:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4786:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4787:                             for (my $k=0; $k<=$maxnum; $k++) {
 4788:                                 my $vpos = $k+1;
 4789:                                 my $selstr;
 4790:                                 if ($k == $maxnum) {
 4791:                                     $selstr = ' selected="selected" ';
 4792:                                 }
 4793:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4794:                             }
 4795:                             $datatable .= '</select></span></td>'.
 4796:                                           '<td><span class="LC_nobreak">'.
 4797:                                           $default_names{$default}.'</span>';
 4798:                             if ($default eq 'instcode') {
 4799:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4800:                                               .&mt('with institutional codes').')</span>';
 4801:                             }
 4802:                             $datatable .= '</td>'
 4803:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4804:                                           .&mt('Display').'</label>&nbsp;'
 4805:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4806:                                           .&mt('Do not display').'</label></span></td></tr>';
 4807:                         }
 4808:                     }
 4809:                 }
 4810:             } else {
 4811:                 $datatable .= &initialize_categories($itemcount);
 4812:             }
 4813:         } else {
 4814:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4815:                           .&initialize_categories($itemcount);
 4816:         }
 4817:         $$rowtotal += $itemcount;
 4818:     }
 4819:     return $datatable;
 4820: }
 4821: 
 4822: sub print_serverstatuses {
 4823:     my ($dom,$settings,$rowtotal) = @_;
 4824:     my $datatable;
 4825:     my @pages = &serverstatus_pages();
 4826:     my (%namedaccess,%machineaccess);
 4827:     foreach my $type (@pages) {
 4828:         $namedaccess{$type} = '';
 4829:         $machineaccess{$type}= '';
 4830:     }
 4831:     if (ref($settings) eq 'HASH') {
 4832:         foreach my $type (@pages) {
 4833:             if (exists($settings->{$type})) {
 4834:                 if (ref($settings->{$type}) eq 'HASH') {
 4835:                     foreach my $key (keys(%{$settings->{$type}})) {
 4836:                         if ($key eq 'namedusers') {
 4837:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4838:                         } elsif ($key eq 'machines') {
 4839:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4840:                         }
 4841:                     }
 4842:                 }
 4843:             }
 4844:         }
 4845:     }
 4846:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4847:     my $rownum = 0;
 4848:     my $css_class;
 4849:     foreach my $type (@pages) {
 4850:         $rownum ++;
 4851:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4852:         $datatable .= '<tr'.$css_class.'>'.
 4853:                       '<td><span class="LC_nobreak">'.
 4854:                       $titles->{$type}.'</span></td>'.
 4855:                       '<td class="LC_left_item">'.
 4856:                       '<input type="text" name="'.$type.'_namedusers" '.
 4857:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4858:                       '<td class="LC_right_item">'.
 4859:                       '<span class="LC_nobreak">'.
 4860:                       '<input type="text" name="'.$type.'_machines" '.
 4861:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4862:                       '</td></tr>'."\n";
 4863:     }
 4864:     $$rowtotal += $rownum;
 4865:     return $datatable;
 4866: }
 4867: 
 4868: sub serverstatus_pages {
 4869:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4870:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4871:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 4872:             'uniquecodes','diskusage');
 4873: }
 4874: 
 4875: sub coursecategories_javascript {
 4876:     my ($settings) = @_;
 4877:     my ($output,$jstext,$cathash);
 4878:     if (ref($settings) eq 'HASH') {
 4879:         $cathash = $settings->{'cats'};
 4880:     }
 4881:     if (ref($cathash) eq 'HASH') {
 4882:         my (@cats,@jsarray,%idx);
 4883:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4884:         if (@jsarray > 0) {
 4885:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4886:             for (my $i=0; $i<@jsarray; $i++) {
 4887:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4888:                     my $catstr = join('","',@{$jsarray[$i]});
 4889:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4890:                 }
 4891:             }
 4892:         }
 4893:     } else {
 4894:         $jstext  = '    var categories = Array(1);'."\n".
 4895:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4896:     }
 4897:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4898:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4899:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4900:     $output = <<"ENDSCRIPT";
 4901: <script type="text/javascript">
 4902: // <![CDATA[
 4903: function reorderCats(form,parent,item,idx) {
 4904:     var changedVal;
 4905: $jstext
 4906:     var newpos = 'addcategory_pos';
 4907:     var current = new Array;
 4908:     if (parent == '') {
 4909:         var has_instcode = 0;
 4910:         var maxtop = categories[idx].length;
 4911:         for (var j=0; j<maxtop; j++) {
 4912:             if (categories[idx][j] == 'instcode::0') {
 4913:                 has_instcode == 1;
 4914:             }
 4915:         }
 4916:         if (has_instcode == 0) {
 4917:             categories[idx][maxtop] = 'instcode_pos';
 4918:         }
 4919:     } else {
 4920:         newpos += '_'+parent;
 4921:     }
 4922:     var maxh = 1 + categories[idx].length;
 4923:     var current = new Array;
 4924:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4925:     if (item == newpos) {
 4926:         changedVal = newitemVal;
 4927:     } else {
 4928:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4929:         current[newitemVal] = newpos;
 4930:     }
 4931:     for (var i=0; i<categories[idx].length; i++) {
 4932:         var elementName = categories[idx][i];
 4933:         if (elementName != item) {
 4934:             if (form.elements[elementName]) {
 4935:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4936:                 current[currVal] = elementName;
 4937:             }
 4938:         }
 4939:     }
 4940:     var oldVal;
 4941:     for (var j=0; j<maxh; j++) {
 4942:         if (current[j] == undefined) {
 4943:             oldVal = j;
 4944:         }
 4945:     }
 4946:     if (oldVal < changedVal) {
 4947:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4948:            var elementName = current[k];
 4949:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4950:         }
 4951:     } else {
 4952:         for (var k=changedVal; k<oldVal; k++) {
 4953:             var elementName = current[k];
 4954:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4955:         }
 4956:     }
 4957:     return;
 4958: }
 4959: 
 4960: function categoryCheck(form) {
 4961:     if (form.elements['addcategory_name'].value == 'instcode') {
 4962:         alert('$instcode_reserved\\n$choose_again');
 4963:         return false;
 4964:     }
 4965:     if (form.elements['addcategory_name'].value == 'communities') {
 4966:         alert('$communities_reserved\\n$choose_again');
 4967:         return false;
 4968:     }
 4969:     return true;
 4970: }
 4971: 
 4972: // ]]>
 4973: </script>
 4974: 
 4975: ENDSCRIPT
 4976:     return $output;
 4977: }
 4978: 
 4979: sub initialize_categories {
 4980:     my ($itemcount) = @_;
 4981:     my ($datatable,$css_class,$chgstr);
 4982:     my %default_names = (
 4983:                       instcode    => 'Official courses (with institutional codes)',
 4984:                       communities => 'Communities',
 4985:                         );
 4986:     my $select0 = ' selected="selected"';
 4987:     my $select1 = '';
 4988:     foreach my $default ('instcode','communities') {
 4989:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4990:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4991:         if ($default eq 'communities') {
 4992:             $select1 = $select0;
 4993:             $select0 = '';
 4994:         }
 4995:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4996:                      .'<select name="'.$default.'_pos">'
 4997:                      .'<option value="0"'.$select0.'>1</option>'
 4998:                      .'<option value="1"'.$select1.'>2</option>'
 4999:                      .'<option value="2">3</option></select>&nbsp;'
 5000:                      .$default_names{$default}
 5001:                      .'</span></td><td><span class="LC_nobreak">'
 5002:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5003:                      .&mt('Display').'</label>&nbsp;<label>'
 5004:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5005:                  .'</label></span></td></tr>';
 5006:         $itemcount ++;
 5007:     }
 5008:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5009:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5010:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5011:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5012:                   .'<option value="0">1</option>'
 5013:                   .'<option value="1">2</option>'
 5014:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5015:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5016:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5017:     return $datatable;
 5018: }
 5019: 
 5020: sub build_category_rows {
 5021:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5022:     my ($text,$name,$item,$chgstr);
 5023:     if (ref($cats) eq 'ARRAY') {
 5024:         my $maxdepth = scalar(@{$cats});
 5025:         if (ref($cats->[$depth]) eq 'HASH') {
 5026:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5027:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5028:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5029:                 $text .= '<td><table class="LC_data_table">';
 5030:                 my ($idxnum,$parent_name,$parent_item);
 5031:                 my $higher = $depth - 1;
 5032:                 if ($higher == 0) {
 5033:                     $parent_name = &escape($parent).'::'.$higher;
 5034:                 } else {
 5035:                     if (ref($path) eq 'ARRAY') {
 5036:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5037:                     }
 5038:                 }
 5039:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5040:                 for (my $j=0; $j<=$numchildren; $j++) {
 5041:                     if ($j < $numchildren) {
 5042:                         $name = $cats->[$depth]{$parent}[$j];
 5043:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5044:                         $idxnum = $idx->{$item};
 5045:                     } else {
 5046:                         $name = $parent_name;
 5047:                         $item = $parent_item;
 5048:                     }
 5049:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5050:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5051:                     for (my $i=0; $i<=$numchildren; $i++) {
 5052:                         my $vpos = $i+1;
 5053:                         my $selstr;
 5054:                         if ($j == $i) {
 5055:                             $selstr = ' selected="selected" ';
 5056:                         }
 5057:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5058:                     }
 5059:                     $text .= '</select>&nbsp;';
 5060:                     if ($j < $numchildren) {
 5061:                         my $deeper = $depth+1;
 5062:                         $text .= $name.'&nbsp;'
 5063:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5064:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5065:                         if(ref($path) eq 'ARRAY') {
 5066:                             push(@{$path},$name);
 5067:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5068:                             pop(@{$path});
 5069:                         }
 5070:                     } else {
 5071:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5072:                         if ($j == $numchildren) {
 5073:                             $text .= $name;
 5074:                         } else {
 5075:                             $text .= $item;
 5076:                         }
 5077:                         $text .= '" value="" />';
 5078:                     }
 5079:                     $text .= '</td></tr>';
 5080:                 }
 5081:                 $text .= '</table></td>';
 5082:             } else {
 5083:                 my $higher = $depth-1;
 5084:                 if ($higher == 0) {
 5085:                     $name = &escape($parent).'::'.$higher;
 5086:                 } else {
 5087:                     if (ref($path) eq 'ARRAY') {
 5088:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5089:                     }
 5090:                 }
 5091:                 my $colspan;
 5092:                 if ($parent ne 'instcode') {
 5093:                     $colspan = $maxdepth - $depth - 1;
 5094:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5095:                 }
 5096:             }
 5097:         }
 5098:     }
 5099:     return $text;
 5100: }
 5101: 
 5102: sub modifiable_userdata_row {
 5103:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5104:     my ($role,$rolename,$statustype);
 5105:     $role = $item;
 5106:     if ($context eq 'cancreate') {
 5107:         if ($item =~ /^emailusername_(.+)$/) {
 5108:             $statustype = $1;
 5109:             $role = 'emailusername';
 5110:             if (ref($usertypes) eq 'HASH') {
 5111:                 if ($usertypes->{$statustype}) {
 5112:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5113:                 } else {
 5114:                     $rolename = &mt('Data provided by user');
 5115:                 }
 5116:             }
 5117:         }
 5118:     } elsif ($context eq 'selfcreate') {
 5119:         if (ref($usertypes) eq 'HASH') {
 5120:             $rolename = $usertypes->{$role};
 5121:         } else {
 5122:             $rolename = $role;
 5123:         }
 5124:     } else {
 5125:         if ($role eq 'cr') {
 5126:             $rolename = &mt('Custom role');
 5127:         } else {
 5128:             $rolename = &Apache::lonnet::plaintext($role);
 5129:         }
 5130:     }
 5131:     my (@fields,%fieldtitles);
 5132:     if (ref($fieldsref) eq 'ARRAY') {
 5133:         @fields = @{$fieldsref};
 5134:     } else {
 5135:         @fields = ('lastname','firstname','middlename','generation',
 5136:                    'permanentemail','id');
 5137:     }
 5138:     if ((ref($titlesref) eq 'HASH')) {
 5139:         %fieldtitles = %{$titlesref};
 5140:     } else {
 5141:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5142:     }
 5143:     my $output;
 5144:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5145:     $output = '<tr '.$css_class.'>'.
 5146:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5147:               '<td class="LC_left_item" colspan="2"><table>';
 5148:     my $rem;
 5149:     my %checks;
 5150:     if (ref($settings) eq 'HASH') {
 5151:         if (ref($settings->{$context}) eq 'HASH') {
 5152:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5153:                 my $hashref = $settings->{$context}->{$role};
 5154:                 if ($role eq 'emailusername') {
 5155:                     if ($statustype) {
 5156:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5157:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5158:                             if (ref($hashref) eq 'HASH') { 
 5159:                                 foreach my $field (@fields) {
 5160:                                     if ($hashref->{$field}) {
 5161:                                         $checks{$field} = $hashref->{$field};
 5162:                                     }
 5163:                                 }
 5164:                             }
 5165:                         }
 5166:                     }
 5167:                 } else {
 5168:                     if (ref($hashref) eq 'HASH') {
 5169:                         foreach my $field (@fields) {
 5170:                             if ($hashref->{$field}) {
 5171:                                 $checks{$field} = ' checked="checked" ';
 5172:                             }
 5173:                         }
 5174:                     }
 5175:                 }
 5176:             }
 5177:         }
 5178:     }
 5179:      
 5180:     for (my $i=0; $i<@fields; $i++) {
 5181:         my $rem = $i%($numinrow);
 5182:         if ($rem == 0) {
 5183:             if ($i > 0) {
 5184:                 $output .= '</tr>';
 5185:             }
 5186:             $output .= '<tr>';
 5187:         }
 5188:         my $check = ' ';
 5189:         unless ($role eq 'emailusername') {
 5190:             if (exists($checks{$fields[$i]})) {
 5191:                 $check = $checks{$fields[$i]}
 5192:             } else {
 5193:                 if ($role eq 'st') {
 5194:                     if (ref($settings) ne 'HASH') {
 5195:                         $check = ' checked="checked" '; 
 5196:                     }
 5197:                 }
 5198:             }
 5199:         }
 5200:         $output .= '<td class="LC_left_item">'.
 5201:                    '<span class="LC_nobreak">';
 5202:         if ($role eq 'emailusername') {
 5203:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5204:                 $checks{$fields[$i]} = 'omit';
 5205:             }
 5206:             foreach my $option ('required','optional','omit') {
 5207:                 my $checked='';
 5208:                 if ($checks{$fields[$i]} eq $option) {
 5209:                     $checked='checked="checked" ';
 5210:                 }
 5211:                 $output .= '<label>'.
 5212:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5213:                            &mt($option).'</label>'.('&nbsp;' x2);
 5214:             }
 5215:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5216:         } else {
 5217:             $output .= '<label>'.
 5218:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5219:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5220:                        '</label>';
 5221:         }
 5222:         $output .= '</span></td>';
 5223:         $rem = @fields%($numinrow);
 5224:     }
 5225:     my $colsleft = $numinrow - $rem;
 5226:     if ($colsleft > 1 ) {
 5227:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5228:                    '&nbsp;</td>';
 5229:     } elsif ($colsleft == 1) {
 5230:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5231:     }
 5232:     $output .= '</tr></table></td></tr>';
 5233:     return $output;
 5234: }
 5235: 
 5236: sub insttypes_row {
 5237:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5238:     my %lt = &Apache::lonlocal::texthash (
 5239:                       cansearch => 'Users allowed to search',
 5240:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5241:                       lockablenames => 'User preference to lock name',
 5242:              );
 5243:     my $showdom;
 5244:     if ($context eq 'cansearch') {
 5245:         $showdom = ' ('.$dom.')';
 5246:     }
 5247:     my $class = 'LC_left_item';
 5248:     if ($context eq 'statustocreate') {
 5249:         $class = 'LC_right_item';
 5250:     }
 5251:     my $css_class = ' class="LC_odd_row"';
 5252:     if ($rownum ne '') { 
 5253:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5254:     }
 5255:     my $output = '<tr'.$css_class.'>'.
 5256:                  '<td>'.$lt{$context}.$showdom.
 5257:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5258:     my $rem;
 5259:     if (ref($types) eq 'ARRAY') {
 5260:         for (my $i=0; $i<@{$types}; $i++) {
 5261:             if (defined($usertypes->{$types->[$i]})) {
 5262:                 my $rem = $i%($numinrow);
 5263:                 if ($rem == 0) {
 5264:                     if ($i > 0) {
 5265:                         $output .= '</tr>';
 5266:                     }
 5267:                     $output .= '<tr>';
 5268:                 }
 5269:                 my $check = ' ';
 5270:                 if (ref($settings) eq 'HASH') {
 5271:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5272:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5273:                             $check = ' checked="checked" ';
 5274:                         }
 5275:                     } elsif ($context eq 'statustocreate') {
 5276:                         $check = ' checked="checked" ';
 5277:                     }
 5278:                 }
 5279:                 $output .= '<td class="LC_left_item">'.
 5280:                            '<span class="LC_nobreak"><label>'.
 5281:                            '<input type="checkbox" name="'.$context.'" '.
 5282:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5283:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5284:             }
 5285:         }
 5286:         $rem = @{$types}%($numinrow);
 5287:     }
 5288:     my $colsleft = $numinrow - $rem;
 5289:     if (($rem == 0) && (@{$types} > 0)) {
 5290:         $output .= '<tr>';
 5291:     }
 5292:     if ($colsleft > 1) {
 5293:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5294:     } else {
 5295:         $output .= '<td class="LC_left_item">';
 5296:     }
 5297:     my $defcheck = ' ';
 5298:     if (ref($settings) eq 'HASH') {  
 5299:         if (ref($settings->{$context}) eq 'ARRAY') {
 5300:             if (grep(/^default$/,@{$settings->{$context}})) {
 5301:                 $defcheck = ' checked="checked" ';
 5302:             }
 5303:         } elsif ($context eq 'statustocreate') {
 5304:             $defcheck = ' checked="checked" ';
 5305:         }
 5306:     }
 5307:     $output .= '<span class="LC_nobreak"><label>'.
 5308:                '<input type="checkbox" name="'.$context.'" '.
 5309:                'value="default"'.$defcheck.'/>'.
 5310:                $othertitle.'</label></span></td>'.
 5311:                '</tr></table></td></tr>';
 5312:     return $output;
 5313: }
 5314: 
 5315: sub sorted_searchtitles {
 5316:     my %searchtitles = &Apache::lonlocal::texthash(
 5317:                          'uname' => 'username',
 5318:                          'lastname' => 'last name',
 5319:                          'lastfirst' => 'last name, first name',
 5320:                      );
 5321:     my @titleorder = ('uname','lastname','lastfirst');
 5322:     return (\%searchtitles,\@titleorder);
 5323: }
 5324: 
 5325: sub sorted_searchtypes {
 5326:     my %srchtypes_desc = (
 5327:                            exact    => 'is exact match',
 5328:                            contains => 'contains ..',
 5329:                            begins   => 'begins with ..',
 5330:                          );
 5331:     my @srchtypeorder = ('exact','begins','contains');
 5332:     return (\%srchtypes_desc,\@srchtypeorder);
 5333: }
 5334: 
 5335: sub usertype_update_row {
 5336:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5337:     my $datatable;
 5338:     my $numinrow = 4;
 5339:     foreach my $type (@{$types}) {
 5340:         if (defined($usertypes->{$type})) {
 5341:             $$rownums ++;
 5342:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5343:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5344:                           '</td><td class="LC_left_item"><table>';
 5345:             for (my $i=0; $i<@{$fields}; $i++) {
 5346:                 my $rem = $i%($numinrow);
 5347:                 if ($rem == 0) {
 5348:                     if ($i > 0) {
 5349:                         $datatable .= '</tr>';
 5350:                     }
 5351:                     $datatable .= '<tr>';
 5352:                 }
 5353:                 my $check = ' ';
 5354:                 if (ref($settings) eq 'HASH') {
 5355:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5356:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5357:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5358:                                 $check = ' checked="checked" ';
 5359:                             }
 5360:                         }
 5361:                     }
 5362:                 }
 5363: 
 5364:                 if ($i == @{$fields}-1) {
 5365:                     my $colsleft = $numinrow - $rem;
 5366:                     if ($colsleft > 1) {
 5367:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5368:                     } else {
 5369:                         $datatable .= '<td>';
 5370:                     }
 5371:                 } else {
 5372:                     $datatable .= '<td>';
 5373:                 }
 5374:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5375:                               '<input type="checkbox" name="updateable_'.$type.
 5376:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5377:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5378:             }
 5379:             $datatable .= '</tr></table></td></tr>';
 5380:         }
 5381:     }
 5382:     return $datatable;
 5383: }
 5384: 
 5385: sub modify_login {
 5386:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5387:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5388:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5389:     %title = ( coursecatalog => 'Display course catalog',
 5390:                adminmail => 'Display administrator E-mail address',
 5391:                helpdesk  => 'Display "Contact Helpdesk" link',
 5392:                newuser => 'Link for visitors to create a user account',
 5393:                loginheader => 'Log-in box header');
 5394:     @offon = ('off','on');
 5395:     if (ref($domconfig{login}) eq 'HASH') {
 5396:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5397:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5398:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5399:             }
 5400:         }
 5401:     }
 5402:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5403:                                            \%domconfig,\%loginhash);
 5404:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5405:     foreach my $item (@toggles) {
 5406:         $loginhash{login}{$item} = $env{'form.'.$item};
 5407:     }
 5408:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5409:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5410:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5411:                                          \%loginhash);
 5412:     }
 5413: 
 5414:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5415:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5416:     if (keys(%servers) > 1) {
 5417:         foreach my $lonhost (keys(%servers)) {
 5418:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5419:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5420:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5421:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5422:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5423:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5424:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5425:                         $changes{'loginvia'}{$lonhost} = 1;
 5426:                     } else {
 5427:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5428:                         $changes{'loginvia'}{$lonhost} = 1;
 5429:                     }
 5430:                 } else {
 5431:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5432:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5433:                         $changes{'loginvia'}{$lonhost} = 1;
 5434:                     }
 5435:                 }
 5436:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5437:                     foreach my $item (@loginvia_attribs) {
 5438:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5439:                     }
 5440:                 } else {
 5441:                     foreach my $item (@loginvia_attribs) {
 5442:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5443:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5444:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5445:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5446:                                 $new = '/';
 5447:                             }
 5448:                         }
 5449:                         if (($item eq 'custompath') && 
 5450:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5451:                             $new = '';
 5452:                         }
 5453:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5454:                             $changes{'loginvia'}{$lonhost} = 1;
 5455:                         }
 5456:                         if ($item eq 'exempt') {
 5457:                             $new =~ s/^\s+//;
 5458:                             $new =~ s/\s+$//;
 5459:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5460:                             my @okips;
 5461:                             foreach my $ip (@poss_ips) {
 5462:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5463:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5464:                                         push(@okips,$ip); 
 5465:                                     }
 5466:                                 }
 5467:                             }
 5468:                             if (@okips > 0) {
 5469:                                 $new = join(',',@okips); 
 5470:                             } else {
 5471:                                 $new = ''; 
 5472:                             }
 5473:                         }
 5474:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5475:                     }
 5476:                 }
 5477:             } else {
 5478:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5479:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5480:                     $changes{'loginvia'}{$lonhost} = 1;
 5481:                     foreach my $item (@loginvia_attribs) {
 5482:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5483:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5484:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5485:                                 $new = '/';
 5486:                             }
 5487:                         }
 5488:                         if (($item eq 'custompath') && 
 5489:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5490:                             $new = '';
 5491:                         }
 5492:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5493:                     }
 5494:                 }
 5495:             }
 5496:         }
 5497:     }
 5498: 
 5499:     my $servadm = $r->dir_config('lonAdmEMail');
 5500:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5501:     if (ref($domconfig{'login'}) eq 'HASH') {
 5502:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5503:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5504:                 if ($lang eq 'nolang') {
 5505:                     push(@currlangs,$lang);
 5506:                 } elsif (defined($langchoices{$lang})) {
 5507:                     push(@currlangs,$lang);
 5508:                 } else {
 5509:                     next;
 5510:                 }
 5511:             }
 5512:         }
 5513:     }
 5514:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5515:     if (@currlangs > 0) {
 5516:         foreach my $lang (@currlangs) {
 5517:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5518:                 $changes{'helpurl'}{$lang} = 1;
 5519:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5520:                 $changes{'helpurl'}{$lang} = 1;
 5521:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5522:                 push(@newlangs,$lang);
 5523:             } else {
 5524:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5525:             }
 5526:         }
 5527:     }
 5528:     unless (grep(/^nolang$/,@currlangs)) {
 5529:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5530:             $changes{'helpurl'}{'nolang'} = 1;
 5531:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5532:             push(@newlangs,'nolang');
 5533:         }
 5534:     }
 5535:     if ($env{'form.loginhelpurl_add_lang'}) {
 5536:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5537:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5538:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5539:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5540:         }
 5541:     }
 5542:     if ((@newlangs > 0) || ($addedfile)) {
 5543:         my $error;
 5544:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5545:         if ($configuserok eq 'ok') {
 5546:             if ($switchserver) {
 5547:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5548:             } elsif ($author_ok eq 'ok') {
 5549:                 my @allnew = @newlangs;
 5550:                 if ($addedfile ne '') {
 5551:                     push(@allnew,$addedfile);
 5552:                 }
 5553:                 foreach my $lang (@allnew) {
 5554:                     my $formelem = 'loginhelpurl_'.$lang;
 5555:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5556:                         $formelem = 'loginhelpurl_add_file';
 5557:                     }
 5558:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5559:                                                                "help/$lang",'','',$newfile{$lang});
 5560:                     if ($result eq 'ok') {
 5561:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5562:                         $changes{'helpurl'}{$lang} = 1;
 5563:                     } else {
 5564:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5565:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5566:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5567:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5568: 
 5569:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5570:                         }
 5571:                     }
 5572:                 }
 5573:             } else {
 5574:                 $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);
 5575:             }
 5576:         } else {
 5577:             $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);
 5578:         }
 5579:         if ($error) {
 5580:             &Apache::lonnet::logthis($error);
 5581:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5582:         }
 5583:     }
 5584:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5585: 
 5586:     my $defaulthelpfile = '/adm/loginproblems.html';
 5587:     my $defaulttext = &mt('Default in use');
 5588: 
 5589:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5590:                                              $dom);
 5591:     if ($putresult eq 'ok') {
 5592:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5593:         my %defaultchecked = (
 5594:                     'coursecatalog' => 'on',
 5595:                     'helpdesk'      => 'on',
 5596:                     'adminmail'     => 'off',
 5597:                     'newuser'       => 'off',
 5598:         );
 5599:         if (ref($domconfig{'login'}) eq 'HASH') {
 5600:             foreach my $item (@toggles) {
 5601:                 if ($defaultchecked{$item} eq 'on') { 
 5602:                     if (($domconfig{'login'}{$item} eq '0') &&
 5603:                         ($env{'form.'.$item} eq '1')) {
 5604:                         $changes{$item} = 1;
 5605:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5606:                               $domconfig{'login'}{$item} eq '1') &&
 5607:                              ($env{'form.'.$item} eq '0')) {
 5608:                         $changes{$item} = 1;
 5609:                     }
 5610:                 } elsif ($defaultchecked{$item} eq 'off') {
 5611:                     if (($domconfig{'login'}{$item} eq '1') &&
 5612:                         ($env{'form.'.$item} eq '0')) {
 5613:                         $changes{$item} = 1;
 5614:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5615:                               $domconfig{'login'}{$item} eq '0') &&
 5616:                              ($env{'form.'.$item} eq '1')) {
 5617:                         $changes{$item} = 1;
 5618:                     }
 5619:                 }
 5620:             }
 5621:         }
 5622:         if (keys(%changes) > 0 || $colchgtext) {
 5623:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5624:             if (ref($lastactref) eq 'HASH') {
 5625:                 $lastactref->{'domainconfig'} = 1;
 5626:             }
 5627:             $resulttext = &mt('Changes made:').'<ul>';
 5628:             foreach my $item (sort(keys(%changes))) {
 5629:                 if ($item eq 'loginvia') {
 5630:                     if (ref($changes{$item}) eq 'HASH') {
 5631:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5632:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5633:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5634:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5635:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5636:                                     $protocol = 'http' if ($protocol ne 'https');
 5637:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5638: 
 5639:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5640:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5641:                                     } else {
 5642:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5643:                                     }
 5644:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5645:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5646:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5647:                                     }
 5648:                                     $resulttext .= '</li>';
 5649:                                 } else {
 5650:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5651:                                 }
 5652:                             } else {
 5653:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5654:                             }
 5655:                         }
 5656:                         $resulttext .= '</ul></li>';
 5657:                     }
 5658:                 } elsif ($item eq 'helpurl') {
 5659:                     if (ref($changes{$item}) eq 'HASH') {
 5660:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5661:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5662:                                 my ($chg,$link);
 5663:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5664:                                 if ($lang eq 'nolang') {
 5665:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5666:                                 } else {
 5667:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5668:                                 }
 5669:                                 $resulttext .= '<li>'.$chg.'</li>';
 5670:                             } else {
 5671:                                 my $chg;
 5672:                                 if ($lang eq 'nolang') {
 5673:                                     $chg = &mt('custom log-in help file for no preferred language');
 5674:                                 } else {
 5675:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5676:                                 }
 5677:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5678:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5679:                                                       '?inhibitmenu=yes',$chg,600,500).
 5680:                                                '</li>';
 5681:                             }
 5682:                         }
 5683:                     }
 5684:                 } elsif ($item eq 'captcha') {
 5685:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5686:                         my $chgtxt;
 5687:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5688:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5689:                         } else {
 5690:                             my %captchas = &captcha_phrases();
 5691:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5692:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5693:                             } else {
 5694:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5695:                             }
 5696:                         }
 5697:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5698:                     }
 5699:                 } elsif ($item eq 'recaptchakeys') {
 5700:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5701:                         my ($privkey,$pubkey);
 5702:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5703:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5704:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5705:                         }
 5706:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5707:                         if (!$pubkey) {
 5708:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5709:                         } else {
 5710:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5711:                         }
 5712:                         if (!$privkey) {
 5713:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5714:                         } else {
 5715:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5716:                         }
 5717:                         $chgtxt .= '</ul>';
 5718:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5719:                     }
 5720:                 } else {
 5721:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5722:                 }
 5723:             }
 5724:             $resulttext .= $colchgtext.'</ul>';
 5725:         } else {
 5726:             $resulttext = &mt('No changes made to log-in page settings');
 5727:         }
 5728:     } else {
 5729:         $resulttext = '<span class="LC_error">'.
 5730: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5731:     }
 5732:     if ($errors) {
 5733:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5734:                        $errors.'</ul>';
 5735:     }
 5736:     return $resulttext;
 5737: }
 5738: 
 5739: sub color_font_choices {
 5740:     my %choices =
 5741:         &Apache::lonlocal::texthash (
 5742:             img => "Header",
 5743:             bgs => "Background colors",
 5744:             links => "Link colors",
 5745:             images => "Images",
 5746:             font => "Font color",
 5747:             fontmenu => "Font menu",
 5748:             pgbg => "Page",
 5749:             tabbg => "Header",
 5750:             sidebg => "Border",
 5751:             link => "Link",
 5752:             alink => "Active link",
 5753:             vlink => "Visited link",
 5754:         );
 5755:     return %choices;
 5756: }
 5757: 
 5758: sub modify_rolecolors {
 5759:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5760:     my ($resulttext,%rolehash);
 5761:     $rolehash{'rolecolors'} = {};
 5762:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5763:         if ($domconfig{'rolecolors'} eq '') {
 5764:             $domconfig{'rolecolors'} = {};
 5765:         }
 5766:     }
 5767:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5768:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5769:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5770:                                              $dom);
 5771:     if ($putresult eq 'ok') {
 5772:         if (keys(%changes) > 0) {
 5773:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5774:             if (ref($lastactref) eq 'HASH') {
 5775:                 $lastactref->{'domainconfig'} = 1;
 5776:             }
 5777:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5778:                                              $rolehash{'rolecolors'});
 5779:         } else {
 5780:             $resulttext = &mt('No changes made to default color schemes');
 5781:         }
 5782:     } else {
 5783:         $resulttext = '<span class="LC_error">'.
 5784: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5785:     }
 5786:     if ($errors) {
 5787:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5788:                        $errors.'</ul>';
 5789:     }
 5790:     return $resulttext;
 5791: }
 5792: 
 5793: sub modify_colors {
 5794:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5795:     my (%changes,%choices);
 5796:     my @bgs;
 5797:     my @links = ('link','alink','vlink');
 5798:     my @logintext;
 5799:     my @images;
 5800:     my $servadm = $r->dir_config('lonAdmEMail');
 5801:     my $errors;
 5802:     my %defaults;
 5803:     foreach my $role (@{$roles}) {
 5804:         if ($role eq 'login') {
 5805:             %choices = &login_choices();
 5806:             @logintext = ('textcol','bgcol');
 5807:         } else {
 5808:             %choices = &color_font_choices();
 5809:         }
 5810:         if ($role eq 'login') {
 5811:             @images = ('img','logo','domlogo','login');
 5812:             @bgs = ('pgbg','mainbg','sidebg');
 5813:         } else {
 5814:             @images = ('img');
 5815:             @bgs = ('pgbg','tabbg','sidebg');
 5816:         }
 5817:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5818:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5819:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5820:         }
 5821:         if ($role eq 'login') {
 5822:             foreach my $item (@logintext) {
 5823:                 unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'logintext'}{$item}) {
 5824:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5825:                 }
 5826:             }
 5827:         } else {
 5828:             unless($env{'form.'.$role.'_fontmenu'} eq $defaults{'fontmenu'}) {
 5829:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5830:             }
 5831:         }
 5832:         foreach my $item (@bgs) {
 5833:             unless ($env{'form.'.$role.'_'.$item} eq $defaults{'bgs'}{$item} ) {
 5834:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5835:             }
 5836:         }
 5837:         foreach my $item (@links) {
 5838:             unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'links'}{$item}) {
 5839:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5840:             }
 5841:         }
 5842:         my ($configuserok,$author_ok,$switchserver) = 
 5843:             &config_check($dom,$confname,$servadm);
 5844:         my ($width,$height) = &thumb_dimensions();
 5845:         if (ref($domconfig->{$role}) ne 'HASH') {
 5846:             $domconfig->{$role} = {};
 5847:         }
 5848:         foreach my $img (@images) {
 5849:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5850:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5851:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5852:                 } else { 
 5853:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5854:                 }
 5855:             } 
 5856: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5857: 		 && !defined($domconfig->{$role}{$img})
 5858: 		 && !$env{'form.'.$role.'_del_'.$img}
 5859: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5860: 		# import the old configured image from the .tab setting
 5861: 		# if they haven't provided a new one 
 5862: 		$domconfig->{$role}{$img} = 
 5863: 		    $env{'form.'.$role.'_import_'.$img};
 5864: 	    }
 5865:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5866:                 my $error;
 5867:                 if ($configuserok eq 'ok') {
 5868:                     if ($switchserver) {
 5869:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5870:                     } else {
 5871:                         if ($author_ok eq 'ok') {
 5872:                             my ($result,$logourl) = 
 5873:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5874:                                            $dom,$confname,$img,$width,$height);
 5875:                             if ($result eq 'ok') {
 5876:                                 $confhash->{$role}{$img} = $logourl;
 5877:                                 $changes{$role}{'images'}{$img} = 1;
 5878:                             } else {
 5879:                                 $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);
 5880:                             }
 5881:                         } else {
 5882:                             $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);
 5883:                         }
 5884:                     }
 5885:                 } else {
 5886:                     $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);
 5887:                 }
 5888:                 if ($error) {
 5889:                     &Apache::lonnet::logthis($error);
 5890:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5891:                 }
 5892:             } elsif ($domconfig->{$role}{$img} ne '') {
 5893:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5894:                     my $error;
 5895:                     if ($configuserok eq 'ok') {
 5896: # is confname an author?
 5897:                         if ($switchserver eq '') {
 5898:                             if ($author_ok eq 'ok') {
 5899:                                 my ($result,$logourl) = 
 5900:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5901:                                             $dom,$confname,$img,$width,$height);
 5902:                                 if ($result eq 'ok') {
 5903:                                     $confhash->{$role}{$img} = $logourl;
 5904: 				    $changes{$role}{'images'}{$img} = 1;
 5905:                                 }
 5906:                             }
 5907:                         }
 5908:                     }
 5909:                 }
 5910:             }
 5911:         }
 5912:         if (ref($domconfig) eq 'HASH') {
 5913:             if (ref($domconfig->{$role}) eq 'HASH') {
 5914:                 foreach my $img (@images) {
 5915:                     if ($domconfig->{$role}{$img} ne '') {
 5916:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5917:                             $confhash->{$role}{$img} = '';
 5918:                             $changes{$role}{'images'}{$img} = 1;
 5919:                         } else {
 5920:                             if ($confhash->{$role}{$img} eq '') {
 5921:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5922:                             }
 5923:                         }
 5924:                     } else {
 5925:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5926:                             $confhash->{$role}{$img} = '';
 5927:                             $changes{$role}{'images'}{$img} = 1;
 5928:                         } 
 5929:                     }
 5930:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5931:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5932:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5933:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5934:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5935:                             }
 5936:                         } else {
 5937:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5938:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5939:                             }
 5940:                         }
 5941:                     }
 5942:                 }
 5943:                 if ($domconfig->{$role}{'font'} ne '') {
 5944:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5945:                         $changes{$role}{'font'} = 1;
 5946:                     }
 5947:                 } else {
 5948:                     if ($confhash->{$role}{'font'}) {
 5949:                         $changes{$role}{'font'} = 1;
 5950:                     }
 5951:                 }
 5952:                 if ($role ne 'login') {
 5953:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5954:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5955:                             $changes{$role}{'fontmenu'} = 1;
 5956:                         }
 5957:                     } else {
 5958:                         if ($confhash->{$role}{'fontmenu'}) {
 5959:                             $changes{$role}{'fontmenu'} = 1;
 5960:                         }
 5961:                     }
 5962:                 }
 5963:                 foreach my $item (@bgs) {
 5964:                     if ($domconfig->{$role}{$item} ne '') {
 5965:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5966:                             $changes{$role}{'bgs'}{$item} = 1;
 5967:                         } 
 5968:                     } else {
 5969:                         if ($confhash->{$role}{$item}) {
 5970:                             $changes{$role}{'bgs'}{$item} = 1;
 5971:                         }
 5972:                     }
 5973:                 }
 5974:                 foreach my $item (@links) {
 5975:                     if ($domconfig->{$role}{$item} ne '') {
 5976:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5977:                             $changes{$role}{'links'}{$item} = 1;
 5978:                         }
 5979:                     } else {
 5980:                         if ($confhash->{$role}{$item}) {
 5981:                             $changes{$role}{'links'}{$item} = 1;
 5982:                         }
 5983:                     }
 5984:                 }
 5985:                 foreach my $item (@logintext) {
 5986:                     if ($domconfig->{$role}{$item} ne '') {
 5987:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5988:                             $changes{$role}{'logintext'}{$item} = 1;
 5989:                         }
 5990:                     } else {
 5991:                         if ($confhash->{$role}{$item}) {
 5992:                             $changes{$role}{'logintext'}{$item} = 1;
 5993:                         }
 5994:                     }
 5995:                 }
 5996:             } else {
 5997:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5998:                                         \@logintext,$confhash,\%changes); 
 5999:             }
 6000:         } else {
 6001:             &default_change_checker($role,\@images,\@links,\@bgs,
 6002:                                     \@logintext,$confhash,\%changes); 
 6003:         }
 6004:     }
 6005:     return ($errors,%changes);
 6006: }
 6007: 
 6008: sub config_check {
 6009:     my ($dom,$confname,$servadm) = @_;
 6010:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6011:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6012:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6013:                                                    $confname,$servadm);
 6014:     if ($configuserok eq 'ok') {
 6015:         $switchserver = &check_switchserver($dom,$confname);
 6016:         if ($switchserver eq '') {
 6017:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6018:         }
 6019:     }
 6020:     return ($configuserok,$author_ok,$switchserver);
 6021: }
 6022: 
 6023: sub default_change_checker {
 6024:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6025:     foreach my $item (@{$links}) {
 6026:         if ($confhash->{$role}{$item}) {
 6027:             $changes->{$role}{'links'}{$item} = 1;
 6028:         }
 6029:     }
 6030:     foreach my $item (@{$bgs}) {
 6031:         if ($confhash->{$role}{$item}) {
 6032:             $changes->{$role}{'bgs'}{$item} = 1;
 6033:         }
 6034:     }
 6035:     foreach my $item (@{$logintext}) {
 6036:         if ($confhash->{$role}{$item}) {
 6037:             $changes->{$role}{'logintext'}{$item} = 1;
 6038:         }
 6039:     }
 6040:     foreach my $img (@{$images}) {
 6041:         if ($env{'form.'.$role.'_del_'.$img}) {
 6042:             $confhash->{$role}{$img} = '';
 6043:             $changes->{$role}{'images'}{$img} = 1;
 6044:         }
 6045:         if ($role eq 'login') {
 6046:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6047:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6048:             }
 6049:         }
 6050:     }
 6051:     if ($confhash->{$role}{'font'}) {
 6052:         $changes->{$role}{'font'} = 1;
 6053:     }
 6054: }
 6055: 
 6056: sub display_colorchgs {
 6057:     my ($dom,$changes,$roles,$confhash) = @_;
 6058:     my (%choices,$resulttext);
 6059:     if (!grep(/^login$/,@{$roles})) {
 6060:         $resulttext = &mt('Changes made:').'<br />';
 6061:     }
 6062:     foreach my $role (@{$roles}) {
 6063:         if ($role eq 'login') {
 6064:             %choices = &login_choices();
 6065:         } else {
 6066:             %choices = &color_font_choices();
 6067:         }
 6068:         if (ref($changes->{$role}) eq 'HASH') {
 6069:             if ($role ne 'login') {
 6070:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6071:             }
 6072:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6073:                 if ($role ne 'login') {
 6074:                     $resulttext .= '<ul>';
 6075:                 }
 6076:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6077:                     if ($role ne 'login') {
 6078:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6079:                     }
 6080:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6081:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6082:                             if ($confhash->{$role}{$key}{$item}) {
 6083:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6084:                             } else {
 6085:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6086:                             }
 6087:                         } elsif ($confhash->{$role}{$item} eq '') {
 6088:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6089:                         } else {
 6090:                             my $newitem = $confhash->{$role}{$item};
 6091:                             if ($key eq 'images') {
 6092:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6093:                             }
 6094:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6095:                         }
 6096:                     }
 6097:                     if ($role ne 'login') {
 6098:                         $resulttext .= '</ul></li>';
 6099:                     }
 6100:                 } else {
 6101:                     if ($confhash->{$role}{$key} eq '') {
 6102:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6103:                     } else {
 6104:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6105:                     }
 6106:                 }
 6107:                 if ($role ne 'login') {
 6108:                     $resulttext .= '</ul>';
 6109:                 }
 6110:             }
 6111:         }
 6112:     }
 6113:     return $resulttext;
 6114: }
 6115: 
 6116: sub thumb_dimensions {
 6117:     return ('200','50');
 6118: }
 6119: 
 6120: sub check_dimensions {
 6121:     my ($inputfile) = @_;
 6122:     my ($fullwidth,$fullheight);
 6123:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6124:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6125:             my $imageinfo = <PIPE>;
 6126:             if (!close(PIPE)) {
 6127:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6128:             }
 6129:             chomp($imageinfo);
 6130:             my ($fullsize) = 
 6131:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6132:             if ($fullsize) {
 6133:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6134:             }
 6135:         }
 6136:     }
 6137:     return ($fullwidth,$fullheight);
 6138: }
 6139: 
 6140: sub check_configuser {
 6141:     my ($uhome,$dom,$confname,$servadm) = @_;
 6142:     my ($configuserok,%currroles);
 6143:     if ($uhome eq 'no_host') {
 6144:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6145:         my $configpass = &LONCAPA::Enrollment::create_password();
 6146:         $configuserok = 
 6147:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6148:                              $configpass,'','','','','',undef,$servadm);
 6149:     } else {
 6150:         $configuserok = 'ok';
 6151:         %currroles = 
 6152:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6153:     }
 6154:     return ($configuserok,%currroles);
 6155: }
 6156: 
 6157: sub check_authorstatus {
 6158:     my ($dom,$confname,%currroles) = @_;
 6159:     my $author_ok;
 6160:     if (!$currroles{':'.$dom.':au'}) {
 6161:         my $start = time;
 6162:         my $end = 0;
 6163:         $author_ok = 
 6164:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6165:                                         'au',$end,$start,'','','domconfig');
 6166:     } else {
 6167:         $author_ok = 'ok';
 6168:     }
 6169:     return $author_ok;
 6170: }
 6171: 
 6172: sub publishlogo {
 6173:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6174:     my ($output,$fname,$logourl);
 6175:     if ($action eq 'upload') {
 6176:         $fname=$env{'form.'.$formname.'.filename'};
 6177:         chop($env{'form.'.$formname});
 6178:     } else {
 6179:         ($fname) = ($formname =~ /([^\/]+)$/);
 6180:     }
 6181:     if ($savefileas ne '') {
 6182:         $fname = $savefileas;
 6183:     }
 6184:     $fname=&Apache::lonnet::clean_filename($fname);
 6185: # See if there is anything left
 6186:     unless ($fname) { return ('error: no uploaded file'); }
 6187:     $fname="$subdir/$fname";
 6188:     my $docroot=$r->dir_config('lonDocRoot');
 6189:     my $filepath="$docroot/priv";
 6190:     my $relpath = "$dom/$confname";
 6191:     my ($fnamepath,$file,$fetchthumb);
 6192:     $file=$fname;
 6193:     if ($fname=~m|/|) {
 6194:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6195:     }
 6196:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6197:     my $count;
 6198:     for ($count=5;$count<=$#parts;$count++) {
 6199:         $filepath.="/$parts[$count]";
 6200:         if ((-e $filepath)!=1) {
 6201:             mkdir($filepath,02770);
 6202:         }
 6203:     }
 6204:     # Check for bad extension and disallow upload
 6205:     if ($file=~/\.(\w+)$/ &&
 6206:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6207:         $output = 
 6208:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6209:     } elsif ($file=~/\.(\w+)$/ &&
 6210:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6211:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6212:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6213:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6214:     } elsif (-d "$filepath/$file") {
 6215:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6216:     } else {
 6217:         my $source = $filepath.'/'.$file;
 6218:         my $logfile;
 6219:         if (!open($logfile,">>$source".'.log')) {
 6220:             return (&mt('No write permission to Authoring Space'));
 6221:         }
 6222:         print $logfile
 6223: "\n================= Publish ".localtime()." ================\n".
 6224: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6225: # Save the file
 6226:         if (!open(FH,'>'.$source)) {
 6227:             &Apache::lonnet::logthis('Failed to create '.$source);
 6228:             return (&mt('Failed to create file'));
 6229:         }
 6230:         if ($action eq 'upload') {
 6231:             if (!print FH ($env{'form.'.$formname})) {
 6232:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6233:                 return (&mt('Failed to write file'));
 6234:             }
 6235:         } else {
 6236:             my $original = &Apache::lonnet::filelocation('',$formname);
 6237:             if(!copy($original,$source)) {
 6238:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6239:                 return (&mt('Failed to write file'));
 6240:             }
 6241:         }
 6242:         close(FH);
 6243:         chmod(0660, $source); # Permissions to rw-rw---.
 6244: 
 6245:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6246:         my $copyfile=$targetdir.'/'.$file;
 6247: 
 6248:         my @parts=split(/\//,$targetdir);
 6249:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6250:         for (my $count=5;$count<=$#parts;$count++) {
 6251:             $path.="/$parts[$count]";
 6252:             if (!-e $path) {
 6253:                 print $logfile "\nCreating directory ".$path;
 6254:                 mkdir($path,02770);
 6255:             }
 6256:         }
 6257:         my $versionresult;
 6258:         if (-e $copyfile) {
 6259:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6260:         } else {
 6261:             $versionresult = 'ok';
 6262:         }
 6263:         if ($versionresult eq 'ok') {
 6264:             if (copy($source,$copyfile)) {
 6265:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6266:                 $output = 'ok';
 6267:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6268:                 push(@{$modified_urls},[$copyfile,$source]);
 6269:                 my $metaoutput = 
 6270:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6271:                 unless ($registered_cleanup) {
 6272:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6273:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6274:                     $registered_cleanup=1;
 6275:                 }
 6276:             } else {
 6277:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6278:                 $output = &mt('Failed to copy file to RES space').", $!";
 6279:             }
 6280:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6281:                 my $inputfile = $filepath.'/'.$file;
 6282:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6283:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6284:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6285:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6286:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6287:                         system("convert -sample $thumbsize $inputfile $outfile");
 6288:                         chmod(0660, $filepath.'/tn-'.$file);
 6289:                         if (-e $outfile) {
 6290:                             my $copyfile=$targetdir.'/tn-'.$file;
 6291:                             if (copy($outfile,$copyfile)) {
 6292:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6293:                                 my $thumb_metaoutput = 
 6294:                                     &write_metadata($dom,$confname,$formname,
 6295:                                                     $targetdir,'tn-'.$file,$logfile);
 6296:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6297:                                 unless ($registered_cleanup) {
 6298:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6299:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6300:                                     $registered_cleanup=1;
 6301:                                 }
 6302:                             } else {
 6303:                                 print $logfile "\nUnable to write ".$copyfile.
 6304:                                                ':'.$!."\n";
 6305:                             }
 6306:                         }
 6307:                     }
 6308:                 }
 6309:             }
 6310:         } else {
 6311:             $output = $versionresult;
 6312:         }
 6313:     }
 6314:     return ($output,$logourl);
 6315: }
 6316: 
 6317: sub logo_versioning {
 6318:     my ($targetdir,$file,$logfile) = @_;
 6319:     my $target = $targetdir.'/'.$file;
 6320:     my ($maxversion,$fn,$extn,$output);
 6321:     $maxversion = 0;
 6322:     if ($file =~ /^(.+)\.(\w+)$/) {
 6323:         $fn=$1;
 6324:         $extn=$2;
 6325:     }
 6326:     opendir(DIR,$targetdir);
 6327:     while (my $filename=readdir(DIR)) {
 6328:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6329:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6330:         }
 6331:     }
 6332:     $maxversion++;
 6333:     print $logfile "\nCreating old version ".$maxversion."\n";
 6334:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6335:     if (copy($target,$copyfile)) {
 6336:         print $logfile "Copied old target to ".$copyfile."\n";
 6337:         $copyfile=$copyfile.'.meta';
 6338:         if (copy($target.'.meta',$copyfile)) {
 6339:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6340:             $output = 'ok';
 6341:         } else {
 6342:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6343:             $output = &mt('Failed to copy old meta').", $!, ";
 6344:         }
 6345:     } else {
 6346:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6347:         $output = &mt('Failed to copy old target').", $!, ";
 6348:     }
 6349:     return $output;
 6350: }
 6351: 
 6352: sub write_metadata {
 6353:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6354:     my (%metadatafields,%metadatakeys,$output);
 6355:     $metadatafields{'title'}=$formname;
 6356:     $metadatafields{'creationdate'}=time;
 6357:     $metadatafields{'lastrevisiondate'}=time;
 6358:     $metadatafields{'copyright'}='public';
 6359:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6360:                                          $env{'user.domain'};
 6361:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6362:     $metadatafields{'domain'}=$dom;
 6363:     {
 6364:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6365:         my $mfh;
 6366:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6367:             foreach (sort(keys(%metadatafields))) {
 6368:                 unless ($_=~/\./) {
 6369:                     my $unikey=$_;
 6370:                     $unikey=~/^([A-Za-z]+)/;
 6371:                     my $tag=$1;
 6372:                     $tag=~tr/A-Z/a-z/;
 6373:                     print $mfh "\n\<$tag";
 6374:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6375:                         my $value=$metadatafields{$unikey.'.'.$_};
 6376:                         $value=~s/\"/\'\'/g;
 6377:                         print $mfh ' '.$_.'="'.$value.'"';
 6378:                     }
 6379:                     print $mfh '>'.
 6380:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6381:                             .'</'.$tag.'>';
 6382:                 }
 6383:             }
 6384:             $output = 'ok';
 6385:             print $logfile "\nWrote metadata";
 6386:             close($mfh);
 6387:         } else {
 6388:             print $logfile "\nFailed to open metadata file";
 6389:             $output = &mt('Could not write metadata');
 6390:         }
 6391:     }
 6392:     return $output;
 6393: }
 6394: 
 6395: sub notifysubscribed {
 6396:     foreach my $targetsource (@{$modified_urls}){
 6397:         next unless (ref($targetsource) eq 'ARRAY');
 6398:         my ($target,$source)=@{$targetsource};
 6399:         if ($source ne '') {
 6400:             if (open(my $logfh,'>>'.$source.'.log')) {
 6401:                 print $logfh "\nCleanup phase: Notifications\n";
 6402:                 my @subscribed=&subscribed_hosts($target);
 6403:                 foreach my $subhost (@subscribed) {
 6404:                     print $logfh "\nNotifying host ".$subhost.':';
 6405:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6406:                     print $logfh $reply;
 6407:                 }
 6408:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6409:                 foreach my $subhost (@subscribedmeta) {
 6410:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6411:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6412:                                                         $subhost);
 6413:                     print $logfh $reply;
 6414:                 }
 6415:                 print $logfh "\n============ Done ============\n";
 6416:                 close($logfh);
 6417:             }
 6418:         }
 6419:     }
 6420:     return OK;
 6421: }
 6422: 
 6423: sub subscribed_hosts {
 6424:     my ($target) = @_;
 6425:     my @subscribed;
 6426:     if (open(my $fh,"<$target.subscription")) {
 6427:         while (my $subline=<$fh>) {
 6428:             if ($subline =~ /^($match_lonid):/) {
 6429:                 my $host = $1;
 6430:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6431:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6432:                         push(@subscribed,$host);
 6433:                     }
 6434:                 }
 6435:             }
 6436:         }
 6437:     }
 6438:     return @subscribed;
 6439: }
 6440: 
 6441: sub check_switchserver {
 6442:     my ($dom,$confname) = @_;
 6443:     my ($allowed,$switchserver);
 6444:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6445:     if ($home eq 'no_host') {
 6446:         $home = &Apache::lonnet::domain($dom,'primary');
 6447:     }
 6448:     my @ids=&Apache::lonnet::current_machine_ids();
 6449:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6450:     if (!$allowed) {
 6451: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6452:     }
 6453:     return $switchserver;
 6454: }
 6455: 
 6456: sub modify_quotas {
 6457:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6458:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6459:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6460:         $author_ok,$switchserver,$errors);
 6461:     if ($action eq 'quotas') {
 6462:         $context = 'tools'; 
 6463:     } else {
 6464:         $context = $action;
 6465:     }
 6466:     if ($context eq 'requestcourses') {
 6467:         @usertools = ('official','unofficial','community','textbook');
 6468:         @options =('norequest','approval','validate','autolimit');
 6469:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6470:         %titles = &courserequest_titles();
 6471:         $toolregexp = join('|',@usertools);
 6472:         %conditions = &courserequest_conditions();
 6473:         $confname = $dom.'-domainconfig';
 6474:         my $servadm = $r->dir_config('lonAdmEMail');
 6475:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6476:     } elsif ($context eq 'requestauthor') {
 6477:         @usertools = ('author');
 6478:         %titles = &authorrequest_titles();
 6479:     } else {
 6480:         @usertools = ('aboutme','blog','webdav','portfolio');
 6481:         %titles = &tool_titles();
 6482:     }
 6483:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6484:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6485:     foreach my $key (keys(%env)) {
 6486:         if ($context eq 'requestcourses') {
 6487:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6488:                 my $item = $1;
 6489:                 my $type = $2;
 6490:                 if ($type =~ /^limit_(.+)/) {
 6491:                     $limithash{$item}{$1} = $env{$key};
 6492:                 } else {
 6493:                     $confhash{$item}{$type} = $env{$key};
 6494:                 }
 6495:             }
 6496:         } elsif ($context eq 'requestauthor') {
 6497:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6498:                 $confhash{$1} = $env{$key};
 6499:             }
 6500:         } else {
 6501:             if ($key =~ /^form\.quota_(.+)$/) {
 6502:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6503:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6504:                 $confhash{'authorquota'}{$1} = $env{$key};
 6505:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6506:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6507:             }
 6508:         }
 6509:     }
 6510:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6511:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6512:         @approvalnotify = sort(@approvalnotify);
 6513:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6514:         my @crstypes = ('official','unofficial','community','textbook');
 6515:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6516:         foreach my $type (@hasuniquecode) {
 6517:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6518:                 $confhash{'uniquecode'}{$type} = 1;
 6519:             }
 6520:         }
 6521:         my ($newbook,@allpos);
 6522:         if ($context eq 'requestcourses') {
 6523:             if ($env{'form.addbook'}) {
 6524:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6525:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6526:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6527:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6528:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6529:                                    '</span></li>';
 6530:                     } else {
 6531:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6532:                         my $position = $env{'form.addbook_pos'};
 6533:                         $position =~ s/\D+//g;
 6534:                         if ($position ne '') {
 6535:                             $allpos[$position] = $newbook;
 6536:                         }
 6537:                     }
 6538:                 } else {
 6539:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6540:                                '</span></li>';
 6541:                 }
 6542:             }
 6543:         }
 6544:         if (ref($domconfig{$action}) eq 'HASH') {
 6545:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6546:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6547:                     $changes{'notify'}{'approval'} = 1;
 6548:                 }
 6549:             } else {
 6550:                 if ($confhash{'notify'}{'approval'}) {
 6551:                     $changes{'notify'}{'approval'} = 1;
 6552:                 }
 6553:             }
 6554:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6555:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6556:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6557:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6558:                             $changes{'uniquecode'} = 1;
 6559:                         }
 6560:                     }
 6561:                     unless ($changes{'uniquecode'}) {
 6562:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6563:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6564:                                 $changes{'uniquecode'} = 1;
 6565:                             }
 6566:                         }
 6567:                     }
 6568:                } else {
 6569:                    $changes{'uniquecode'} = 1;
 6570:                }
 6571:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6572:                 $changes{'uniquecode'} = 1;
 6573:             }
 6574:             if ($context eq 'requestcourses') {
 6575:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6576:                     my %deletions;
 6577:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6578:                     if (@todelete) {
 6579:                         map { $deletions{$_} = 1; } @todelete;
 6580:                     }
 6581:                     my %imgdeletions;
 6582:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6583:                     if (@todeleteimages) {
 6584:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6585:                     }
 6586:                     my $maxnum = $env{'form.book_maxnum'};
 6587:                     for (my $i=0; $i<=$maxnum; $i++) {
 6588:                         my $key = $env{'form.book_id_'.$i};
 6589:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6590:                             if ($deletions{$key}) {
 6591:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6592:                                     #FIXME need to obsolete item in RES space
 6593:                                 }
 6594:                                 next;
 6595:                             } else {
 6596:                                 my $newpos = $env{'form.'.$key};
 6597:                                 $newpos =~ s/\D+//g;
 6598:                                 foreach my $item ('subject','title','author') {
 6599:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6600:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6601:                                         $changes{'textbooks'}{$key} = 1;
 6602:                                     }
 6603:                                 }
 6604:                                 $allpos[$newpos] = $key;
 6605:                             }
 6606:                             if ($imgdeletions{$key}) {
 6607:                                 $changes{'textbooks'}{$key} = 1;
 6608:                                 #FIXME need to obsolete item in RES space
 6609:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6610:                                 my ($cdom,$cnum) = split(/_/,$key);
 6611:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6612:                                                                               $cdom,$cnum,$configuserok,
 6613:                                                                               $switchserver,$author_ok);
 6614:                                 if ($imgurl) {
 6615:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6616:                                     $changes{'textbooks'}{$key} = 1; 
 6617:                                 }
 6618:                                 if ($error) {
 6619:                                     &Apache::lonnet::logthis($error);
 6620:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6621:                                 } 
 6622:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6623:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6624:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6625:                             }
 6626:                         }
 6627:                     }
 6628:                 }
 6629:             }
 6630:         } else {
 6631:             if ($confhash{'notify'}{'approval'}) {
 6632:                 $changes{'notify'}{'approval'} = 1;
 6633:             }
 6634:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6635:                 $changes{'uniquecode'} = 1;
 6636:             }
 6637:         }
 6638:         if ($context eq 'requestcourses') {
 6639:             if ($newbook) {
 6640:                 $changes{'textbooks'}{$newbook} = 1;
 6641:                 foreach my $item ('subject','title','author') {
 6642:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6643:                     if ($env{'form.addbook_'.$item}) {
 6644:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6645:                     }
 6646:                 }
 6647:                 if ($env{'form.addbook_image.filename'} ne '') {
 6648:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6649:                     my ($imageurl,$error) = 
 6650:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6651:                                                 $switchserver,$author_ok);
 6652:                     if ($imageurl) {
 6653:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6654:                     }
 6655:                     if ($error) {
 6656:                         &Apache::lonnet::logthis($error);
 6657:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6658:                     }
 6659:                 }
 6660:             }
 6661:             if (@allpos > 0) {
 6662:                 my $idx = 0;
 6663:                 foreach my $item (@allpos) {
 6664:                     if ($item ne '') {
 6665:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6666:                         if (ref($domconfig{$action}) eq 'HASH') {
 6667:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6668:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6669:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6670:                                         $changes{'textbooks'}{$item} = 1;
 6671:                                     }
 6672:                                 }
 6673:                             }
 6674:                         }
 6675:                         $idx ++;
 6676:                     }
 6677:                 }
 6678:             }
 6679:         }
 6680:     } else {
 6681:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 6682:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 6683:     }
 6684:     foreach my $item (@usertools) {
 6685:         foreach my $type (@{$types},'default','_LC_adv') {
 6686:             my $unset; 
 6687:             if ($context eq 'requestcourses') {
 6688:                 $unset = '0';
 6689:                 if ($type eq '_LC_adv') {
 6690:                     $unset = '';
 6691:                 }
 6692:                 if ($confhash{$item}{$type} eq 'autolimit') {
 6693:                     $confhash{$item}{$type} .= '=';
 6694:                     unless ($limithash{$item}{$type} =~ /\D/) {
 6695:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 6696:                     }
 6697:                 }
 6698:             } elsif ($context eq 'requestauthor') {
 6699:                 $unset = '0';
 6700:                 if ($type eq '_LC_adv') {
 6701:                     $unset = '';
 6702:                 }
 6703:             } else {
 6704:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 6705:                     $confhash{$item}{$type} = 1;
 6706:                 } else {
 6707:                     $confhash{$item}{$type} = 0;
 6708:                 }
 6709:             }
 6710:             if (ref($domconfig{$action}) eq 'HASH') {
 6711:                 if ($action eq 'requestauthor') {
 6712:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 6713:                         $changes{$type} = 1;
 6714:                     }
 6715:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 6716:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 6717:                         $changes{$item}{$type} = 1;
 6718:                     }
 6719:                 } else {
 6720:                     if ($context eq 'requestcourses') {
 6721:                         if ($confhash{$item}{$type} ne $unset) {
 6722:                             $changes{$item}{$type} = 1;
 6723:                         }
 6724:                     } else {
 6725:                         if (!$confhash{$item}{$type}) {
 6726:                             $changes{$item}{$type} = 1;
 6727:                         }
 6728:                     }
 6729:                 }
 6730:             } else {
 6731:                 if ($context eq 'requestcourses') {
 6732:                     if ($confhash{$item}{$type} ne $unset) {
 6733:                         $changes{$item}{$type} = 1;
 6734:                     }
 6735:                 } elsif ($context eq 'requestauthor') {
 6736:                     if ($confhash{$type} ne $unset) {
 6737:                         $changes{$type} = 1;
 6738:                     }
 6739:                 } else {
 6740:                     if (!$confhash{$item}{$type}) {
 6741:                         $changes{$item}{$type} = 1;
 6742:                     }
 6743:                 }
 6744:             }
 6745:         }
 6746:     }
 6747:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6748:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 6749:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6750:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 6751:                     if (exists($confhash{'defaultquota'}{$key})) {
 6752:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 6753:                             $changes{'defaultquota'}{$key} = 1;
 6754:                         }
 6755:                     } else {
 6756:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 6757:                     }
 6758:                 }
 6759:             } else {
 6760:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 6761:                     if (exists($confhash{'defaultquota'}{$key})) {
 6762:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 6763:                             $changes{'defaultquota'}{$key} = 1;
 6764:                         }
 6765:                     } else {
 6766:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 6767:                     }
 6768:                 }
 6769:             }
 6770:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6771:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 6772:                     if (exists($confhash{'authorquota'}{$key})) {
 6773:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 6774:                             $changes{'authorquota'}{$key} = 1;
 6775:                         }
 6776:                     } else {
 6777:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 6778:                     }
 6779:                 }
 6780:             }
 6781:         }
 6782:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 6783:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 6784:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6785:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6786:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 6787:                             $changes{'defaultquota'}{$key} = 1;
 6788:                         }
 6789:                     } else {
 6790:                         if (!exists($domconfig{'quotas'}{$key})) {
 6791:                             $changes{'defaultquota'}{$key} = 1;
 6792:                         }
 6793:                     }
 6794:                 } else {
 6795:                     $changes{'defaultquota'}{$key} = 1;
 6796:                 }
 6797:             }
 6798:         }
 6799:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6800:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6801:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6802:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6803:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6804:                             $changes{'authorquota'}{$key} = 1;
 6805:                         }
 6806:                     } else {
 6807:                         $changes{'authorquota'}{$key} = 1;
 6808:                     }
 6809:                 } else {
 6810:                     $changes{'authorquota'}{$key} = 1;
 6811:                 }
 6812:             }
 6813:         }
 6814:     }
 6815: 
 6816:     if ($context eq 'requestauthor') {
 6817:         $domdefaults{'requestauthor'} = \%confhash;
 6818:     } else {
 6819:         foreach my $key (keys(%confhash)) {
 6820:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 6821:                 $domdefaults{$key} = $confhash{$key};
 6822:             }
 6823:         }
 6824:     }
 6825: 
 6826:     my %quotahash = (
 6827:                       $action => { %confhash }
 6828:                     );
 6829:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6830:                                              $dom);
 6831:     if ($putresult eq 'ok') {
 6832:         if (keys(%changes) > 0) {
 6833:             my $cachetime = 24*60*60;
 6834:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6835:             if (ref($lastactref) eq 'HASH') {
 6836:                 $lastactref->{'domdefaults'} = 1;
 6837:             }
 6838:             $resulttext = &mt('Changes made:').'<ul>';
 6839:             unless (($context eq 'requestcourses') ||
 6840:                     ($context eq 'requestauthor')) {
 6841:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6842:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6843:                     foreach my $type (@{$types},'default') {
 6844:                         if (defined($changes{'defaultquota'}{$type})) {
 6845:                             my $typetitle = $usertypes->{$type};
 6846:                             if ($type eq 'default') {
 6847:                                 $typetitle = $othertitle;
 6848:                             }
 6849:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6850:                         }
 6851:                     }
 6852:                     $resulttext .= '</ul></li>';
 6853:                 }
 6854:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6855:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 6856:                     foreach my $type (@{$types},'default') {
 6857:                         if (defined($changes{'authorquota'}{$type})) {
 6858:                             my $typetitle = $usertypes->{$type};
 6859:                             if ($type eq 'default') {
 6860:                                 $typetitle = $othertitle;
 6861:                             }
 6862:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6863:                         }
 6864:                     }
 6865:                     $resulttext .= '</ul></li>';
 6866:                 }
 6867:             }
 6868:             my %newenv;
 6869:             foreach my $item (@usertools) {
 6870:                 my (%haschgs,%inconf);
 6871:                 if ($context eq 'requestauthor') {
 6872:                     %haschgs = %changes;
 6873:                     %inconf = %confhash;
 6874:                 } else {
 6875:                     if (ref($changes{$item}) eq 'HASH') {
 6876:                         %haschgs = %{$changes{$item}};
 6877:                     }
 6878:                     if (ref($confhash{$item}) eq 'HASH') {
 6879:                         %inconf = %{$confhash{$item}};
 6880:                     }
 6881:                 }
 6882:                 if (keys(%haschgs) > 0) {
 6883:                     my $newacc = 
 6884:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6885:                                                           $env{'user.domain'},
 6886:                                                           $item,'reload',$context);
 6887:                     if (($context eq 'requestcourses') ||
 6888:                         ($context eq 'requestauthor')) {
 6889:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6890:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6891:                         }
 6892:                     } else {
 6893:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6894:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6895:                         }
 6896:                     }
 6897:                     unless ($context eq 'requestauthor') {
 6898:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6899:                     }
 6900:                     foreach my $type (@{$types},'default','_LC_adv') {
 6901:                         if ($haschgs{$type}) {
 6902:                             my $typetitle = $usertypes->{$type};
 6903:                             if ($type eq 'default') {
 6904:                                 $typetitle = $othertitle;
 6905:                             } elsif ($type eq '_LC_adv') {
 6906:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6907:                             }
 6908:                             if ($inconf{$type}) {
 6909:                                 if ($context eq 'requestcourses') {
 6910:                                     my $cond;
 6911:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6912:                                         if ($1 eq '') {
 6913:                                             $cond = &mt('(Automatic processing of any request).');
 6914:                                         } else {
 6915:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6916:                                         }
 6917:                                     } else { 
 6918:                                         $cond = $conditions{$inconf{$type}};
 6919:                                     }
 6920:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6921:                                 } elsif ($context eq 'requestauthor') {
 6922:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6923:                                                              $titles{$inconf{$type}},$typetitle);
 6924: 
 6925:                                 } else {
 6926:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 6927:                                 }
 6928:                             } else {
 6929:                                 if ($type eq '_LC_adv') {
 6930:                                     if ($inconf{$type} eq '0') {
 6931:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6932:                                     } else { 
 6933:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 6934:                                     }
 6935:                                 } else {
 6936:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6937:                                 }
 6938:                             }
 6939:                         }
 6940:                     }
 6941:                     unless ($context eq 'requestauthor') {
 6942:                         $resulttext .= '</ul></li>';
 6943:                     }
 6944:                 }
 6945:             }
 6946:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 6947:                 if (ref($changes{'notify'}) eq 'HASH') {
 6948:                     if ($changes{'notify'}{'approval'}) {
 6949:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6950:                             if ($confhash{'notify'}{'approval'}) {
 6951:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6952:                             } else {
 6953:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6954:                             }
 6955:                         }
 6956:                     }
 6957:                 }
 6958:             }
 6959:             if ($action eq 'requestcourses') {
 6960:                 my @offon = ('off','on');
 6961:                 if ($changes{'uniquecode'}) {
 6962:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6963:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 6964:                         $resulttext .= '<li>'.
 6965:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 6966:                                        '</li>';
 6967:                     } else {
 6968:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 6969:                                        '</li>';
 6970:                     }
 6971:                 }
 6972:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 6973:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 6974:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 6975:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 6976:                         my $coursetitle = $coursehash{'description'};
 6977:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 6978:                         $resulttext .= '<li>';
 6979:                         foreach my $item ('subject','title','author') {
 6980:                             my $name = $item.':';
 6981:                             $name =~ s/^(\w)/\U$1/;
 6982:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 6983:                         }
 6984:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 6985:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 6986:                             $resulttext .= ' '.&mt('Image: [_1]',
 6987:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 6988:                                                ' alt="Textbook cover" />').'<br />';
 6989:                         }
 6990:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 6991:                     }
 6992:                     $resulttext .= '</ul></li>';
 6993:                 }
 6994:             }
 6995:             $resulttext .= '</ul>';
 6996:             if (keys(%newenv)) {
 6997:                 &Apache::lonnet::appenv(\%newenv);
 6998:             }
 6999:         } else {
 7000:             if ($context eq 'requestcourses') {
 7001:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7002:             } elsif ($context eq 'requestauthor') {
 7003:                 $resulttext = &mt('No changes made to rights to request author space.');
 7004:             } else {
 7005:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7006:             }
 7007:         }
 7008:     } else {
 7009:         $resulttext = '<span class="LC_error">'.
 7010: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7011:     }
 7012:     if ($errors) {
 7013:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7014:                        '<ul>'.$errors.'</ul></p>';
 7015:     }
 7016:     return $resulttext;
 7017: }
 7018: 
 7019: sub process_textbook_image {
 7020:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 7021:     my $filename = $env{'form.'.$caller.'.filename'};
 7022:     my ($error,$url);
 7023:     my ($width,$height) = (50,50);
 7024:     if ($configuserok eq 'ok') {
 7025:         if ($switchserver) {
 7026:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7027:                          $switchserver);
 7028:         } elsif ($author_ok eq 'ok') {
 7029:             my ($result,$imageurl) =
 7030:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7031:                              "textbooks/$dom/$cnum/cover",$width,$height);
 7032:             if ($result eq 'ok') {
 7033:                 $url = $imageurl;
 7034:             } else {
 7035:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7036:             }
 7037:         } else {
 7038:             $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);
 7039:         }
 7040:     } else {
 7041:         $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);
 7042:     }
 7043:     return ($url,$error);
 7044: }
 7045: 
 7046: sub modify_autoenroll {
 7047:     my ($dom,$lastactref,%domconfig) = @_;
 7048:     my ($resulttext,%changes);
 7049:     my %currautoenroll;
 7050:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7051:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7052:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7053:         }
 7054:     }
 7055:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7056:     my %title = ( run => 'Auto-enrollment active',
 7057:                   sender => 'Sender for notification messages',
 7058:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7059:     my @offon = ('off','on');
 7060:     my $sender_uname = $env{'form.sender_uname'};
 7061:     my $sender_domain = $env{'form.sender_domain'};
 7062:     if ($sender_domain eq '') {
 7063:         $sender_uname = '';
 7064:     } elsif ($sender_uname eq '') {
 7065:         $sender_domain = '';
 7066:     }
 7067:     my $coowners = $env{'form.autoassign_coowners'};
 7068:     my %autoenrollhash =  (
 7069:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7070:                                        'sender_uname' => $sender_uname,
 7071:                                        'sender_domain' => $sender_domain,
 7072:                                        'co-owners' => $coowners,
 7073:                                 }
 7074:                      );
 7075:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7076:                                              $dom);
 7077:     if ($putresult eq 'ok') {
 7078:         if (exists($currautoenroll{'run'})) {
 7079:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7080:                  $changes{'run'} = 1;
 7081:              }
 7082:         } elsif ($autorun) {
 7083:             if ($env{'form.autoenroll_run'} ne '1') {
 7084:                  $changes{'run'} = 1;
 7085:             }
 7086:         }
 7087:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7088:             $changes{'sender'} = 1;
 7089:         }
 7090:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7091:             $changes{'sender'} = 1;
 7092:         }
 7093:         if ($currautoenroll{'co-owners'} ne '') {
 7094:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7095:                 $changes{'coowners'} = 1;
 7096:             }
 7097:         } elsif ($coowners) {
 7098:             $changes{'coowners'} = 1;
 7099:         }      
 7100:         if (keys(%changes) > 0) {
 7101:             $resulttext = &mt('Changes made:').'<ul>';
 7102:             if ($changes{'run'}) {
 7103:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7104:             }
 7105:             if ($changes{'sender'}) {
 7106:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7107:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7108:                 } else {
 7109:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7110:                 }
 7111:             }
 7112:             if ($changes{'coowners'}) {
 7113:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7114:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7115:                 if (ref($lastactref) eq 'HASH') {
 7116:                     $lastactref->{'domainconfig'} = 1;
 7117:                 }
 7118:             }
 7119:             $resulttext .= '</ul>';
 7120:         } else {
 7121:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7122:         }
 7123:     } else {
 7124:         $resulttext = '<span class="LC_error">'.
 7125: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7126:     }
 7127:     return $resulttext;
 7128: }
 7129: 
 7130: sub modify_autoupdate {
 7131:     my ($dom,%domconfig) = @_;
 7132:     my ($resulttext,%currautoupdate,%fields,%changes);
 7133:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7134:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7135:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7136:         }
 7137:     }
 7138:     my @offon = ('off','on');
 7139:     my %title = &Apache::lonlocal::texthash (
 7140:                    run => 'Auto-update:',
 7141:                    classlists => 'Updates to user information in classlists?'
 7142:                 );
 7143:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7144:     my %fieldtitles = &Apache::lonlocal::texthash (
 7145:                         id => 'Student/Employee ID',
 7146:                         permanentemail => 'E-mail address',
 7147:                         lastname => 'Last Name',
 7148:                         firstname => 'First Name',
 7149:                         middlename => 'Middle Name',
 7150:                         generation => 'Generation',
 7151:                       );
 7152:     $othertitle = &mt('All users');
 7153:     if (keys(%{$usertypes}) >  0) {
 7154:         $othertitle = &mt('Other users');
 7155:     }
 7156:     foreach my $key (keys(%env)) {
 7157:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7158:             my ($usertype,$item) = ($1,$2);
 7159:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7160:                 if ($usertype eq 'default') {   
 7161:                     push(@{$fields{$1}},$2);
 7162:                 } elsif (ref($types) eq 'ARRAY') {
 7163:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7164:                         push(@{$fields{$1}},$2);
 7165:                     }
 7166:                 }
 7167:             }
 7168:         }
 7169:     }
 7170:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 7171:     @lockablenames = sort(@lockablenames);
 7172:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 7173:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7174:         if (@changed) {
 7175:             $changes{'lockablenames'} = 1;
 7176:         }
 7177:     } else {
 7178:         if (@lockablenames) {
 7179:             $changes{'lockablenames'} = 1;
 7180:         }
 7181:     }
 7182:     my %updatehash = (
 7183:                       autoupdate => { run => $env{'form.autoupdate_run'},
 7184:                                       classlists => $env{'form.classlists'},
 7185:                                       fields => {%fields},
 7186:                                       lockablenames => \@lockablenames,
 7187:                                     }
 7188:                      );
 7189:     foreach my $key (keys(%currautoupdate)) {
 7190:         if (($key eq 'run') || ($key eq 'classlists')) {
 7191:             if (exists($updatehash{autoupdate}{$key})) {
 7192:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 7193:                     $changes{$key} = 1;
 7194:                 }
 7195:             }
 7196:         } elsif ($key eq 'fields') {
 7197:             if (ref($currautoupdate{$key}) eq 'HASH') {
 7198:                 foreach my $item (@{$types},'default') {
 7199:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 7200:                         my $change = 0;
 7201:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 7202:                             if (!exists($fields{$item})) {
 7203:                                 $change = 1;
 7204:                                 last;
 7205:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 7206:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 7207:                                     $change = 1;
 7208:                                     last;
 7209:                                 }
 7210:                             }
 7211:                         }
 7212:                         if ($change) {
 7213:                             push(@{$changes{$key}},$item);
 7214:                         }
 7215:                     } 
 7216:                 }
 7217:             }
 7218:         } elsif ($key eq 'lockablenames') {
 7219:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 7220:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 7221:                 if (@changed) {
 7222:                     $changes{'lockablenames'} = 1;
 7223:                 }
 7224:             } else {
 7225:                 if (@lockablenames) {
 7226:                     $changes{'lockablenames'} = 1;
 7227:                 }
 7228:             }
 7229:         }
 7230:     }
 7231:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 7232:         if (@lockablenames) {
 7233:             $changes{'lockablenames'} = 1;
 7234:         }
 7235:     }
 7236:     foreach my $item (@{$types},'default') {
 7237:         if (defined($fields{$item})) {
 7238:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 7239:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 7240:                     my $change = 0;
 7241:                     if (ref($fields{$item}) eq 'ARRAY') {
 7242:                         foreach my $type (@{$fields{$item}}) {
 7243:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 7244:                                 $change = 1;
 7245:                                 last;
 7246:                             }
 7247:                         }
 7248:                     }
 7249:                     if ($change) {
 7250:                         push(@{$changes{'fields'}},$item);
 7251:                     }
 7252:                 } else {
 7253:                     push(@{$changes{'fields'}},$item);
 7254:                 }
 7255:             } else {
 7256:                 push(@{$changes{'fields'}},$item);
 7257:             }
 7258:         }
 7259:     }
 7260:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7261:                                              $dom);
 7262:     if ($putresult eq 'ok') {
 7263:         if (keys(%changes) > 0) {
 7264:             $resulttext = &mt('Changes made:').'<ul>';
 7265:             foreach my $key (sort(keys(%changes))) {
 7266:                 if ($key eq 'lockablenames') {
 7267:                     $resulttext .= '<li>';
 7268:                     if (@lockablenames) {
 7269:                         $usertypes->{'default'} = $othertitle;
 7270:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7271:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7272:                     } else {
 7273:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7274:                     }
 7275:                     $resulttext .= '</li>';
 7276:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7277:                     foreach my $item (@{$changes{$key}}) {
 7278:                         my @newvalues;
 7279:                         foreach my $type (@{$fields{$item}}) {
 7280:                             push(@newvalues,$fieldtitles{$type});
 7281:                         }
 7282:                         my $newvaluestr;
 7283:                         if (@newvalues > 0) {
 7284:                             $newvaluestr = join(', ',@newvalues);
 7285:                         } else {
 7286:                             $newvaluestr = &mt('none');
 7287:                         }
 7288:                         if ($item eq 'default') {
 7289:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7290:                         } else {
 7291:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7292:                         }
 7293:                     }
 7294:                 } else {
 7295:                     my $newvalue;
 7296:                     if ($key eq 'run') {
 7297:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7298:                     } else {
 7299:                         $newvalue = $offon[$env{'form.'.$key}];
 7300:                     }
 7301:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7302:                 }
 7303:             }
 7304:             $resulttext .= '</ul>';
 7305:         } else {
 7306:             $resulttext = &mt('No changes made to autoupdates');
 7307:         }
 7308:     } else {
 7309:         $resulttext = '<span class="LC_error">'.
 7310: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7311:     }
 7312:     return $resulttext;
 7313: }
 7314: 
 7315: sub modify_autocreate {
 7316:     my ($dom,%domconfig) = @_;
 7317:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7318:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7319:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7320:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7321:         }
 7322:     }
 7323:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7324:                  req => 'Auto-creation of validated requests for official courses',
 7325:                  xmldc => 'Identity of course creator of courses from XML files',
 7326:                );
 7327:     my @types = ('xml','req');
 7328:     foreach my $item (@types) {
 7329:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7330:         $newvals{$item} =~ s/\D//g;
 7331:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7332:     }
 7333:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7334:     my %domcoords = &get_active_dcs($dom);
 7335:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7336:         $newvals{'xmldc'} = '';
 7337:     } 
 7338:     %autocreatehash =  (
 7339:                         autocreate => { xml => $newvals{'xml'},
 7340:                                         req => $newvals{'req'},
 7341:                                       }
 7342:                        );
 7343:     if ($newvals{'xmldc'} ne '') {
 7344:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7345:     }
 7346:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7347:                                              $dom);
 7348:     if ($putresult eq 'ok') {
 7349:         my @items = @types;
 7350:         if ($newvals{'xml'}) {
 7351:             push(@items,'xmldc');
 7352:         }
 7353:         foreach my $item (@items) {
 7354:             if (exists($currautocreate{$item})) {
 7355:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7356:                     $changes{$item} = 1;
 7357:                 }
 7358:             } elsif ($newvals{$item}) {
 7359:                 $changes{$item} = 1;
 7360:             }
 7361:         }
 7362:         if (keys(%changes) > 0) {
 7363:             my @offon = ('off','on'); 
 7364:             $resulttext = &mt('Changes made:').'<ul>';
 7365:             foreach my $item (@types) {
 7366:                 if ($changes{$item}) {
 7367:                     my $newtxt = $offon[$newvals{$item}];
 7368:                     $resulttext .= '<li>'.
 7369:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7370:                                        '<b>','</b>').
 7371:                                    '</li>';
 7372:                 }
 7373:             }
 7374:             if ($changes{'xmldc'}) {
 7375:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7376:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7377:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7378:             }
 7379:             $resulttext .= '</ul>';
 7380:         } else {
 7381:             $resulttext = &mt('No changes made to auto-creation settings');
 7382:         }
 7383:     } else {
 7384:         $resulttext = '<span class="LC_error">'.
 7385:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7386:     }
 7387:     return $resulttext;
 7388: }
 7389: 
 7390: sub modify_directorysrch {
 7391:     my ($dom,%domconfig) = @_;
 7392:     my ($resulttext,%changes);
 7393:     my %currdirsrch;
 7394:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7395:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7396:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7397:         }
 7398:     }
 7399:     my %title = ( available => 'Directory search available',
 7400:                   localonly => 'Other domains can search',
 7401:                   searchby => 'Search types',
 7402:                   searchtypes => 'Search latitude');
 7403:     my @offon = ('off','on');
 7404:     my @otherdoms = ('Yes','No');
 7405: 
 7406:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7407:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7408:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7409: 
 7410:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7411:     if (keys(%{$usertypes}) == 0) {
 7412:         @cansearch = ('default');
 7413:     } else {
 7414:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7415:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7416:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7417:                     push(@{$changes{'cansearch'}},$type);
 7418:                 }
 7419:             }
 7420:             foreach my $type (@cansearch) {
 7421:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7422:                     push(@{$changes{'cansearch'}},$type);
 7423:                 }
 7424:             }
 7425:         } else {
 7426:             push(@{$changes{'cansearch'}},@cansearch);
 7427:         }
 7428:     }
 7429: 
 7430:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7431:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7432:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7433:                 push(@{$changes{'searchby'}},$by);
 7434:             }
 7435:         }
 7436:         foreach my $by (@searchby) {
 7437:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7438:                 push(@{$changes{'searchby'}},$by);
 7439:             }
 7440:         }
 7441:     } else {
 7442:         push(@{$changes{'searchby'}},@searchby);
 7443:     }
 7444: 
 7445:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7446:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7447:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7448:                 push(@{$changes{'searchtypes'}},$type);
 7449:             }
 7450:         }
 7451:         foreach my $type (@searchtypes) {
 7452:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7453:                 push(@{$changes{'searchtypes'}},$type);
 7454:             }
 7455:         }
 7456:     } else {
 7457:         if (exists($currdirsrch{'searchtypes'})) {
 7458:             foreach my $type (@searchtypes) {  
 7459:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7460:                     push(@{$changes{'searchtypes'}},$type);
 7461:                 }
 7462:             }
 7463:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7464:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7465:             }   
 7466:         } else {
 7467:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7468:         }
 7469:     }
 7470: 
 7471:     my %dirsrch_hash =  (
 7472:             directorysrch => { available => $env{'form.dirsrch_available'},
 7473:                                cansearch => \@cansearch,
 7474:                                localonly => $env{'form.dirsrch_localonly'},
 7475:                                searchby => \@searchby,
 7476:                                searchtypes => \@searchtypes,
 7477:                              }
 7478:             );
 7479:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7480:                                              $dom);
 7481:     if ($putresult eq 'ok') {
 7482:         if (exists($currdirsrch{'available'})) {
 7483:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7484:                  $changes{'available'} = 1;
 7485:              }
 7486:         } else {
 7487:             if ($env{'form.dirsrch_available'} eq '1') {
 7488:                 $changes{'available'} = 1;
 7489:             }
 7490:         }
 7491:         if (exists($currdirsrch{'localonly'})) {
 7492:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7493:                  $changes{'localonly'} = 1;
 7494:              }
 7495:         } else {
 7496:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7497:                 $changes{'localonly'} = 1;
 7498:             }
 7499:         }
 7500:         if (keys(%changes) > 0) {
 7501:             $resulttext = &mt('Changes made:').'<ul>';
 7502:             if ($changes{'available'}) {
 7503:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7504:             }
 7505:             if ($changes{'localonly'}) {
 7506:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7507:             }
 7508: 
 7509:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7510:                 my $chgtext;
 7511:                 if (ref($usertypes) eq 'HASH') {
 7512:                     if (keys(%{$usertypes}) > 0) {
 7513:                         foreach my $type (@{$types}) {
 7514:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7515:                                 $chgtext .= $usertypes->{$type}.'; ';
 7516:                             }
 7517:                         }
 7518:                         if (grep(/^default$/,@cansearch)) {
 7519:                             $chgtext .= $othertitle;
 7520:                         } else {
 7521:                             $chgtext =~ s/\; $//;
 7522:                         }
 7523:                         $resulttext .=
 7524:                             '<li>'.
 7525:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7526:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7527:                             '</li>';
 7528:                     }
 7529:                 }
 7530:             }
 7531:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7532:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7533:                 my $chgtext;
 7534:                 foreach my $type (@{$titleorder}) {
 7535:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7536:                         if (defined($searchtitles->{$type})) {
 7537:                             $chgtext .= $searchtitles->{$type}.'; ';
 7538:                         }
 7539:                     }
 7540:                 }
 7541:                 $chgtext =~ s/\; $//;
 7542:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7543:             }
 7544:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7545:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7546:                 my $chgtext;
 7547:                 foreach my $type (@{$srchtypeorder}) {
 7548:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7549:                         if (defined($srchtypes_desc->{$type})) {
 7550:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7551:                         }
 7552:                     }
 7553:                 }
 7554:                 $chgtext =~ s/\; $//;
 7555:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7556:             }
 7557:             $resulttext .= '</ul>';
 7558:         } else {
 7559:             $resulttext = &mt('No changes made to institution directory search settings');
 7560:         }
 7561:     } else {
 7562:         $resulttext = '<span class="LC_error">'.
 7563:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7564:     }
 7565:     return $resulttext;
 7566: }
 7567: 
 7568: sub modify_contacts {
 7569:     my ($dom,$lastactref,%domconfig) = @_;
 7570:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7571:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7572:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7573:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7574:         }
 7575:     }
 7576:     my (%others,%to,%bcc);
 7577:     my @contacts = ('supportemail','adminemail');
 7578:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7579:                     'requestsmail','updatesmail','idconflictsmail');
 7580:     my @toggles = ('reporterrors','reportupdates');
 7581:     foreach my $type (@mailings) {
 7582:         @{$newsetting{$type}} = 
 7583:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7584:         foreach my $item (@contacts) {
 7585:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7586:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7587:             } else {
 7588:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7589:             }
 7590:         }  
 7591:         $others{$type} = $env{'form.'.$type.'_others'};
 7592:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7593:         if ($type eq 'helpdeskmail') {
 7594:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 7595:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 7596:         }
 7597:     }
 7598:     foreach my $item (@contacts) {
 7599:         $to{$item} = $env{'form.'.$item};
 7600:         $contacts_hash{'contacts'}{$item} = $to{$item};
 7601:     }
 7602:     foreach my $item (@toggles) {
 7603:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 7604:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 7605:         }
 7606:     }
 7607:     if (keys(%currsetting) > 0) {
 7608:         foreach my $item (@contacts) {
 7609:             if ($to{$item} ne $currsetting{$item}) {
 7610:                 $changes{$item} = 1;
 7611:             }
 7612:         }
 7613:         foreach my $type (@mailings) {
 7614:             foreach my $item (@contacts) {
 7615:                 if (ref($currsetting{$type}) eq 'HASH') {
 7616:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 7617:                         push(@{$changes{$type}},$item);
 7618:                     }
 7619:                 } else {
 7620:                     push(@{$changes{$type}},@{$newsetting{$type}});
 7621:                 }
 7622:             }
 7623:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 7624:                 push(@{$changes{$type}},'others');
 7625:             }
 7626:             if ($type eq 'helpdeskmail') {   
 7627:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 7628:                     push(@{$changes{$type}},'bcc'); 
 7629:                 }
 7630:             }
 7631:         }
 7632:     } else {
 7633:         my %default;
 7634:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 7635:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 7636:         $default{'errormail'} = 'adminemail';
 7637:         $default{'packagesmail'} = 'adminemail';
 7638:         $default{'helpdeskmail'} = 'supportemail';
 7639:         $default{'lonstatusmail'} = 'adminemail';
 7640:         $default{'requestsmail'} = 'adminemail';
 7641:         $default{'updatesmail'} = 'adminemail';
 7642:         foreach my $item (@contacts) {
 7643:            if ($to{$item} ne $default{$item}) {
 7644:               $changes{$item} = 1;
 7645:            }
 7646:         }
 7647:         foreach my $type (@mailings) {
 7648:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 7649:                
 7650:                 push(@{$changes{$type}},@{$newsetting{$type}});
 7651:             }
 7652:             if ($others{$type} ne '') {
 7653:                 push(@{$changes{$type}},'others');
 7654:             }
 7655:             if ($type eq 'helpdeskmail') {
 7656:                 if ($bcc{$type} ne '') {
 7657:                     push(@{$changes{$type}},'bcc');
 7658:                 }
 7659:             }
 7660:         }
 7661:     }
 7662:     foreach my $item (@toggles) {
 7663:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 7664:             $changes{$item} = 1;
 7665:         } elsif ((!$env{'form.'.$item}) &&
 7666:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 7667:             $changes{$item} = 1;
 7668:         }
 7669:     }
 7670:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 7671:                                              $dom);
 7672:     if ($putresult eq 'ok') {
 7673:         if (keys(%changes) > 0) {
 7674:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7675:             if (ref($lastactref) eq 'HASH') {
 7676:                 $lastactref->{'domainconfig'} = 1;
 7677:             }
 7678:             my ($titles,$short_titles)  = &contact_titles();
 7679:             $resulttext = &mt('Changes made:').'<ul>';
 7680:             foreach my $item (@contacts) {
 7681:                 if ($changes{$item}) {
 7682:                     $resulttext .= '<li>'.$titles->{$item}.
 7683:                                     &mt(' set to: ').
 7684:                                     '<span class="LC_cusr_emph">'.
 7685:                                     $to{$item}.'</span></li>';
 7686:                 }
 7687:             }
 7688:             foreach my $type (@mailings) {
 7689:                 if (ref($changes{$type}) eq 'ARRAY') {
 7690:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 7691:                     my @text;
 7692:                     foreach my $item (@{$newsetting{$type}}) {
 7693:                         push(@text,$short_titles->{$item});
 7694:                     }
 7695:                     if ($others{$type} ne '') {
 7696:                         push(@text,$others{$type});
 7697:                     }
 7698:                     $resulttext .= '<span class="LC_cusr_emph">'.
 7699:                                    join(', ',@text).'</span>';
 7700:                     if ($type eq 'helpdeskmail') {
 7701:                         if ($bcc{$type} ne '') {
 7702:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 7703:                         }
 7704:                     }
 7705:                     $resulttext .= '</li>';
 7706:                 }
 7707:             }
 7708:             my @offon = ('off','on');
 7709:             if ($changes{'reporterrors'}) {
 7710:                 $resulttext .= '<li>'.
 7711:                                &mt('E-mail error reports to [_1] set to "'.
 7712:                                    $offon[$env{'form.reporterrors'}].'".',
 7713:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7714:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 7715:                                '</li>';
 7716:             }
 7717:             if ($changes{'reportupdates'}) {
 7718:                 $resulttext .= '<li>'.
 7719:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 7720:                                     $offon[$env{'form.reportupdates'}].'".',
 7721:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7722:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 7723:                                 '</li>';
 7724:             }
 7725:             $resulttext .= '</ul>';
 7726:         } else {
 7727:             $resulttext = &mt('No changes made to contact information');
 7728:         }
 7729:     } else {
 7730:         $resulttext = '<span class="LC_error">'.
 7731:             &mt('An error occurred: [_1].',$putresult).'</span>';
 7732:     }
 7733:     return $resulttext;
 7734: }
 7735: 
 7736: sub modify_usercreation {
 7737:     my ($dom,%domconfig) = @_;
 7738:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 7739:     my $warningmsg;
 7740:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7741:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7742:             if ($key eq 'cancreate') {
 7743:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7744:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7745:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7746:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 7747:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7748:                         } else {
 7749:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7750:                         }
 7751:                     }
 7752:                 }
 7753:             } elsif ($key eq 'email_rule') {
 7754:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7755:             } else {
 7756:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7757:             }
 7758:         }
 7759:     }
 7760:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 7761:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 7762:     my @contexts = ('author','course','requestcrs');
 7763:     foreach my $item(@contexts) {
 7764:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 7765:     }
 7766:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7767:         foreach my $item (@contexts) {
 7768:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 7769:                 push(@{$changes{'cancreate'}},$item);
 7770:             }
 7771:         }
 7772:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 7773:         foreach my $item (@contexts) {
 7774:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 7775:                 if ($cancreate{$item} ne 'any') {
 7776:                     push(@{$changes{'cancreate'}},$item);
 7777:                 }
 7778:             } else {
 7779:                 if ($cancreate{$item} ne 'none') {
 7780:                     push(@{$changes{'cancreate'}},$item);
 7781:                 }
 7782:             }
 7783:         }
 7784:     } else {
 7785:         foreach my $item (@contexts)  {
 7786:             push(@{$changes{'cancreate'}},$item);
 7787:         }
 7788:     }
 7789: 
 7790:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 7791:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 7792:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 7793:                 push(@{$changes{'username_rule'}},$type);
 7794:             }
 7795:         }
 7796:         foreach my $type (@username_rule) {
 7797:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 7798:                 push(@{$changes{'username_rule'}},$type);
 7799:             }
 7800:         }
 7801:     } else {
 7802:         push(@{$changes{'username_rule'}},@username_rule);
 7803:     }
 7804: 
 7805:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 7806:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7807:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7808:                 push(@{$changes{'id_rule'}},$type);
 7809:             }
 7810:         }
 7811:         foreach my $type (@id_rule) {
 7812:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7813:                 push(@{$changes{'id_rule'}},$type);
 7814:             }
 7815:         }
 7816:     } else {
 7817:         push(@{$changes{'id_rule'}},@id_rule);
 7818:     }
 7819: 
 7820:     my @authen_contexts = ('author','course','domain');
 7821:     my @authtypes = ('int','krb4','krb5','loc');
 7822:     my %authhash;
 7823:     foreach my $item (@authen_contexts) {
 7824:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7825:         foreach my $auth (@authtypes) {
 7826:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7827:                 $authhash{$item}{$auth} = 1;
 7828:             } else {
 7829:                 $authhash{$item}{$auth} = 0;
 7830:             }
 7831:         }
 7832:     }
 7833:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7834:         foreach my $item (@authen_contexts) {
 7835:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7836:                 foreach my $auth (@authtypes) {
 7837:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7838:                         push(@{$changes{'authtypes'}},$item);
 7839:                         last;
 7840:                     }
 7841:                 }
 7842:             }
 7843:         }
 7844:     } else {
 7845:         foreach my $item (@authen_contexts) {
 7846:             push(@{$changes{'authtypes'}},$item);
 7847:         }
 7848:     }
 7849: 
 7850:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 7851:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 7852:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 7853:     $save_usercreate{'id_rule'} = \@id_rule;
 7854:     $save_usercreate{'username_rule'} = \@username_rule,
 7855:     $save_usercreate{'authtypes'} = \%authhash;
 7856: 
 7857:     my %usercreation_hash =  (
 7858:         usercreation     => \%save_usercreate,
 7859:     );
 7860: 
 7861:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7862:                                              $dom);
 7863: 
 7864:     if ($putresult eq 'ok') {
 7865:         if (keys(%changes) > 0) {
 7866:             $resulttext = &mt('Changes made:').'<ul>';
 7867:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7868:                 my %lt = &usercreation_types();
 7869:                 foreach my $type (@{$changes{'cancreate'}}) {
 7870:                     my $chgtext = $lt{$type}.', ';
 7871:                     if ($cancreate{$type} eq 'none') {
 7872:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7873:                     } elsif ($cancreate{$type} eq 'any') {
 7874:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7875:                     } elsif ($cancreate{$type} eq 'official') {
 7876:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7877:                     } elsif ($cancreate{$type} eq 'unofficial') {
 7878:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7879:                     }
 7880:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7881:                 }
 7882:             }
 7883:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7884:                 my ($rules,$ruleorder) = 
 7885:                     &Apache::lonnet::inst_userrules($dom,'username');
 7886:                 my $chgtext = '<ul>';
 7887:                 foreach my $type (@username_rule) {
 7888:                     if (ref($rules->{$type}) eq 'HASH') {
 7889:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7890:                     }
 7891:                 }
 7892:                 $chgtext .= '</ul>';
 7893:                 if (@username_rule > 0) {
 7894:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7895:                 } else {
 7896:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7897:                 }
 7898:             }
 7899:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7900:                 my ($idrules,$idruleorder) = 
 7901:                     &Apache::lonnet::inst_userrules($dom,'id');
 7902:                 my $chgtext = '<ul>';
 7903:                 foreach my $type (@id_rule) {
 7904:                     if (ref($idrules->{$type}) eq 'HASH') {
 7905:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7906:                     }
 7907:                 }
 7908:                 $chgtext .= '</ul>';
 7909:                 if (@id_rule > 0) {
 7910:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7911:                 } else {
 7912:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7913:                 }
 7914:             }
 7915:             my %authname = &authtype_names();
 7916:             my %context_title = &context_names();
 7917:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7918:                 my $chgtext = '<ul>';
 7919:                 foreach my $type (@{$changes{'authtypes'}}) {
 7920:                     my @allowed;
 7921:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7922:                     foreach my $auth (@authtypes) {
 7923:                         if ($authhash{$type}{$auth}) {
 7924:                             push(@allowed,$authname{$auth});
 7925:                         }
 7926:                     }
 7927:                     if (@allowed > 0) {
 7928:                         $chgtext .= join(', ',@allowed).'</li>';
 7929:                     } else {
 7930:                         $chgtext .= &mt('none').'</li>';
 7931:                     }
 7932:                 }
 7933:                 $chgtext .= '</ul>';
 7934:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7935:                 $resulttext .= '</li>';
 7936:             }
 7937:             $resulttext .= '</ul>';
 7938:         } else {
 7939:             $resulttext = &mt('No changes made to user creation settings');
 7940:         }
 7941:     } else {
 7942:         $resulttext = '<span class="LC_error">'.
 7943:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7944:     }
 7945:     if ($warningmsg ne '') {
 7946:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7947:     }
 7948:     return $resulttext;
 7949: }
 7950: 
 7951: sub modify_selfcreation {
 7952:     my ($dom,%domconfig) = @_;
 7953:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 7954:     my (%save_usercreate,%save_usermodify);
 7955:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7956:     if (ref($types) eq 'ARRAY') {
 7957:         $usertypes->{'default'} = $othertitle;
 7958:         push(@{$types},'default');
 7959:     }
 7960: #
 7961: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 7962: #
 7963:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7964:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7965:             if ($key eq 'cancreate') {
 7966:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7967:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7968:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7969:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 7970:                             ($item eq 'emailusername') || ($item eq 'notify')) {
 7971:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7972:                         } else {
 7973:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7974:                         }
 7975:                     }
 7976:                 }
 7977:             } elsif ($key eq 'email_rule') {
 7978:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7979:             } else {
 7980:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7981:             }
 7982:         }
 7983:     }
 7984: #
 7985: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 7986: #
 7987:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7988:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7989:             if ($key eq 'selfcreate') {
 7990:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 7991:             } else {
 7992:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 7993:             }
 7994:         }
 7995:     }
 7996: 
 7997:     my @contexts = ('selfcreate');
 7998:     @{$cancreate{'selfcreate'}} = ();
 7999:     %{$cancreate{'emailusername'}} = ();
 8000:     @{$cancreate{'statustocreate'}} = ();
 8001:     my %selfcreatetypes = (
 8002:                              sso   => 'users authenticated by institutional single sign on',
 8003:                              login => 'users authenticated by institutional log-in',
 8004:                              email => 'users who provide a valid e-mail address for use as username (automatic creation)',
 8005:                              emailapproval => 'users who provide a valid e-mail address for use as username (queued for Domain Coordinator review)',
 8006:                           );
 8007: #
 8008: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8009: # is permitted.
 8010: #
 8011:     foreach my $item ('login','sso','email') {
 8012:         if ($item eq 'email') {
 8013:             if ($env{'form.cancreate_email'} eq 'email') {
 8014:                 push(@{$cancreate{'selfcreate'}},'email');
 8015:             } elsif ($env{'form.cancreate_email'} eq 'emailapproval') {
 8016:                 push(@{$cancreate{'selfcreate'}},'emailapproval');
 8017:             }
 8018:         } else {
 8019:             if ($env{'form.cancreate_'.$item}) {
 8020:                 push(@{$cancreate{'selfcreate'}},$item);
 8021:             }
 8022:         }
 8023:     }
 8024:     my (@email_rule,%userinfo,%savecaptcha);
 8025:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8026: #
 8027: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8028: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8029: #
 8030:     if (($env{'form.cancreate_email'} eq 'email') || ($env{'form.cancreate_email'} eq 'emailapproval')) {
 8031:         push(@contexts,'emailusername');
 8032:         if (ref($types) eq 'ARRAY') {
 8033:             foreach my $type (@{$types}) {
 8034:                 if (ref($infofields) eq 'ARRAY') {
 8035:                     foreach my $field (@{$infofields}) {
 8036:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8037:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8038:                         }
 8039:                     }
 8040:                 }
 8041:             }
 8042:         }
 8043: #
 8044: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8045: # queued requests for self-creation of account using e-mail address as username
 8046: #
 8047: 
 8048:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8049:         @approvalnotify = sort(@approvalnotify);
 8050:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8051:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8052:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8053:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8054:                     push(@{$changes{'cancreate'}},'notify');
 8055:                 }
 8056:             } else {
 8057:                 if ($cancreate{'notify'}{'approval'}) {
 8058:                     push(@{$changes{'cancreate'}},'notify');
 8059:                 }
 8060:             }
 8061:         } elsif ($cancreate{'notify'}{'approval'}) {
 8062:             push(@{$changes{'cancreate'}},'notify');
 8063:         }
 8064: 
 8065: #
 8066: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8067: #
 8068:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8069:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8070:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8071:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8072:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8073:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8074:                         push(@{$changes{'email_rule'}},$type);
 8075:                     }
 8076:                 }
 8077:             }
 8078:             if (@email_rule > 0) {
 8079:                 foreach my $type (@email_rule) {
 8080:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8081:                         push(@{$changes{'email_rule'}},$type);
 8082:                     }
 8083:                 }
 8084:             }
 8085:         } elsif (@email_rule > 0) {
 8086:             push(@{$changes{'email_rule'}},@email_rule);
 8087:         }
 8088:     }
 8089: #  
 8090: # Check if domain default is set appropriately, if selef-creation of accounts is to be available for
 8091: # institutional log-in.
 8092: #
 8093:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8094:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8095:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8096:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8097:             $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.').' '.
 8098:                           &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.');
 8099:         }
 8100:     }
 8101:     my @fields = ('lastname','firstname','middlename','generation',
 8102:                   'permanentemail','id');
 8103:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8104: #
 8105: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8106: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8107: # may self-create accounts 
 8108: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8109: # which the user may supply, if institutional data is unavailable.
 8110: #
 8111:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8112:         if (ref($types) eq 'ARRAY') {
 8113:             if (@{$types} > 1) {
 8114:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8115:                 push(@contexts,'statustocreate');
 8116:             } else {
 8117:                 undef($cancreate{'statustocreate'});
 8118:             } 
 8119:             foreach my $type (@{$types}) {
 8120:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8121:                 foreach my $field (@fields) {
 8122:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8123:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8124:                     } else {
 8125:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8126:                     }
 8127:                 }
 8128:             }
 8129:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8130:                 foreach my $type (@{$types}) {
 8131:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8132:                         foreach my $field (@fields) {
 8133:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8134:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8135:                                 push(@{$changes{'selfcreate'}},$type);
 8136:                                 last;
 8137:                             }
 8138:                         }
 8139:                     }
 8140:                 }
 8141:             } else {
 8142:                 foreach my $type (@{$types}) {
 8143:                     push(@{$changes{'selfcreate'}},$type);
 8144:                 }
 8145:             }
 8146:         }
 8147:     }
 8148:     foreach my $item (@contexts) {
 8149:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 8150:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 8151:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 8152:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 8153:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8154:                             push(@{$changes{'cancreate'}},$item);
 8155:                         }
 8156:                     }
 8157:                 }
 8158:             }
 8159:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8160:                 foreach my $type (@{$cancreate{$item}}) {
 8161:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 8162:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8163:                             push(@{$changes{'cancreate'}},$item);
 8164:                         }
 8165:                     }
 8166:                 }
 8167:             }
 8168:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 8169:             if (ref($cancreate{$item}) eq 'HASH') {
 8170:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 8171:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 8172:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 8173:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 8174:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8175:                                     push(@{$changes{'cancreate'}},$item);
 8176:                                 }
 8177:                             }
 8178:                         }
 8179:                     } else {
 8180:                         if (!$cancreate{$item}{$curr}) {
 8181:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8182:                                 push(@{$changes{'cancreate'}},$item);
 8183:                             }
 8184:                         }
 8185:                     }
 8186:                 }
 8187:                 foreach my $field (keys(%{$cancreate{$item}})) {
 8188:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 8189:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 8190:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 8191:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 8192:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8193:                                         push(@{$changes{'cancreate'}},$item);
 8194:                                     }
 8195:                                 }
 8196:                             } else {
 8197:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8198:                                     push(@{$changes{'cancreate'}},$item);
 8199:                                 }
 8200:                             }
 8201:                         }
 8202:                     } else {
 8203:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 8204:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8205:                                 push(@{$changes{'cancreate'}},$item);
 8206:                             }
 8207:                         }
 8208:                     }
 8209:                 }
 8210:             }
 8211:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 8212:             if (ref($cancreate{$item}) eq 'ARRAY') {
 8213:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 8214:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8215:                         push(@{$changes{'cancreate'}},$item);
 8216:                     }
 8217:                 }
 8218:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 8219:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 8220:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8221:                         push(@{$changes{'cancreate'}},$item);
 8222:                     }
 8223:                 }
 8224:             }
 8225:         } elsif ($item eq 'emailusername') {
 8226:             if (ref($cancreate{$item}) eq 'HASH') {
 8227:                 foreach my $type (keys(%{$cancreate{$item}})) {
 8228:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 8229:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 8230:                             if ($cancreate{$item}{$type}{$field}) {
 8231:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 8232:                                     push(@{$changes{'cancreate'}},$item);
 8233:                                 }
 8234:                                 last;
 8235:                             }
 8236:                         }
 8237:                     }
 8238:                 }
 8239:             }
 8240:         }
 8241:     }
 8242: #
 8243: # Populate %save_usercreate hash with updates to self-creation configuration.
 8244: #
 8245:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 8246:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 8247:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 8248:     if (ref($cancreate{'notify'}) eq 'HASH') {
 8249:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 8250:     }
 8251:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8252:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 8253:     }
 8254:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 8255:     $save_usercreate{'emailrule'} = \@email_rule;
 8256: 
 8257:     my %userconfig_hash = (
 8258:             usercreation     => \%save_usercreate,
 8259:             usermodification => \%save_usermodify,
 8260:     );
 8261:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8262:                                              $dom);
 8263: #
 8264: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8265: #
 8266:     if ($putresult eq 'ok') {
 8267:         if (keys(%changes) > 0) {
 8268:             $resulttext = &mt('Changes made:').'<ul>';
 8269:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8270:                 my %lt = &selfcreation_types();
 8271:                 foreach my $type (@{$changes{'cancreate'}}) {
 8272:                     my $chgtext;
 8273:                     if ($type eq 'selfcreate') {
 8274:                         if (@{$cancreate{$type}} == 0) {
 8275:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8276:                         } else {
 8277:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8278:                                         '<ul>';
 8279:                             foreach my $case (@{$cancreate{$type}}) {
 8280:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8281:                             }
 8282:                             $chgtext .= '</ul>';
 8283:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8284:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8285:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8286:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8287:                                             $chgtext .= '<br />'.
 8288:                                                         '<span class="LC_warning">'.
 8289:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8290:                                                         '</span>';
 8291:                                         }
 8292:                                     }
 8293:                                 }
 8294:                             }
 8295:                         }
 8296:                     } elsif ($type eq 'statustocreate') {
 8297:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8298:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8299:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8300:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8301:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8302:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8303:                                         $chgtext .= '<br />'.
 8304:                                                     '<span class="LC_warning">'.
 8305:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8306:                                                     '</span>';
 8307:                                     }
 8308:                                 } elsif (ref($usertypes) eq 'HASH') {
 8309:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8310:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8311:                                     } else {
 8312:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8313:                                     }
 8314:                                     $chgtext .= '<ul>';
 8315:                                     foreach my $case (@{$cancreate{$type}}) {
 8316:                                         if ($case eq 'default') {
 8317:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8318:                                         } else {
 8319:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8320:                                         }
 8321:                                     }
 8322:                                     $chgtext .= '</ul>';
 8323:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8324:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8325:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8326:                                                     '</span>';
 8327:                                     }
 8328:                                 }
 8329:                             } else {
 8330:                                 if (@{$cancreate{$type}} == 0) {
 8331:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8332:                                 } else {
 8333:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8334:                                 }
 8335:                             }
 8336:                         }
 8337:                     } elsif ($type eq 'captcha') {
 8338:                         if ($savecaptcha{$type} eq 'notused') {
 8339:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8340:                         } else {
 8341:                             my %captchas = &captcha_phrases();
 8342:                             if ($captchas{$savecaptcha{$type}}) {
 8343:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8344:                             } else {
 8345:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8346:                             }
 8347:                         }
 8348:                     } elsif ($type eq 'recaptchakeys') {
 8349:                         my ($privkey,$pubkey);
 8350:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8351:                             $pubkey = $savecaptcha{$type}{'public'};
 8352:                             $privkey = $savecaptcha{$type}{'private'};
 8353:                         }
 8354:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8355:                         if (!$pubkey) {
 8356:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8357:                         } else {
 8358:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8359:                         }
 8360:                         if (!$privkey) {
 8361:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8362:                         } else {
 8363:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8364:                         }
 8365:                         $chgtext .= '</ul>';
 8366:                     } elsif ($type eq 'emailusername') {
 8367:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8368:                             if (ref($types) eq 'ARRAY') {
 8369:                                 foreach my $type (@{$types}) {
 8370:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8371:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8372:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',$usertypes->{$type}).
 8373:                                                     '<ul>';
 8374:                                             foreach my $field (@{$infofields}) {
 8375:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8376:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8377:                                                 }
 8378:                                             }
 8379:                                         }
 8380:                                         $chgtext .= '</ul>';
 8381:                                     } else {
 8382:                                         $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 />';
 8383:                                     }
 8384:                                 }
 8385:                             }
 8386:                         }
 8387:                     } elsif ($type eq 'notify') {
 8388:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8389:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8390:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8391:                                 if ($cancreate{'notify'}{'approval'}) {
 8392:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8393:                                 }
 8394:                             }
 8395:                         }
 8396:                     }
 8397:                     if ($chgtext) {
 8398:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8399:                     }
 8400:                 }
 8401:             }
 8402:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8403:                 my ($emailrules,$emailruleorder) =
 8404:                     &Apache::lonnet::inst_userrules($dom,'email');
 8405:                 my $chgtext = '<ul>';
 8406:                 foreach my $type (@email_rule) {
 8407:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8408:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8409:                     }
 8410:                 }
 8411:                 $chgtext .= '</ul>';
 8412:                 if (@email_rule > 0) {
 8413:                     $resulttext .= '<li>'.
 8414:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8415:                                        $chgtext.
 8416:                                    '</li>';
 8417:                 } else {
 8418:                     $resulttext .= '<li>'.
 8419:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8420:                                    '</li>';
 8421:                 }
 8422:             }
 8423:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8424:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8425:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8426:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8427:                     my $typename = $type;
 8428:                     if (ref($usertypes) eq 'HASH') {
 8429:                         if ($usertypes->{$type} ne '') {
 8430:                             $typename = $usertypes->{$type};
 8431:                         }
 8432:                     }
 8433:                     my @modifiable;
 8434:                     $resulttext .= '<li>'.
 8435:                                     &mt('Self-creation of account by users with status: [_1]',
 8436:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 8437:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 8438:                     foreach my $field (@fields) {
 8439:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 8440:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 8441:                         }
 8442:                     }
 8443:                     if (@modifiable > 0) {
 8444:                         $resulttext .= join(', ',@modifiable);
 8445:                     } else {
 8446:                         $resulttext .= &mt('none');
 8447:                     }
 8448:                     $resulttext .= '</li>';
 8449:                 }
 8450:                 $resulttext .= '</ul></li>';
 8451:             }
 8452:             $resulttext .= '</ul>';
 8453:         } else {
 8454:             $resulttext = &mt('No changes made to self-creation settings');
 8455:         }
 8456:     } else {
 8457:         $resulttext = '<span class="LC_error">'.
 8458:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8459:     }
 8460:     if ($warningmsg ne '') {
 8461:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8462:     }
 8463:     return $resulttext;
 8464: }
 8465: 
 8466: sub process_captcha {
 8467:     my ($container,$changes,$newsettings,$current) = @_;
 8468:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 8469:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 8470:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 8471:         $newsettings->{'captcha'} = 'original';
 8472:     }
 8473:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 8474:         if ($container eq 'cancreate') {
 8475:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8476:                 push(@{$changes->{'cancreate'}},'captcha');
 8477:             } elsif (!defined($changes->{'cancreate'})) {
 8478:                 $changes->{'cancreate'} = ['captcha'];
 8479:             }
 8480:         } else {
 8481:             $changes->{'captcha'} = 1;
 8482:         }
 8483:     }
 8484:     my ($newpub,$newpriv,$currpub,$currpriv);
 8485:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 8486:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 8487:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 8488:         $newpub =~ s/\W//g;
 8489:         $newpriv =~ s/\W//g;
 8490:         $newsettings->{'recaptchakeys'} = {
 8491:                                              public  => $newpub,
 8492:                                              private => $newpriv,
 8493:                                           };
 8494:     }
 8495:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 8496:         $currpub = $current->{'recaptchakeys'}{'public'};
 8497:         $currpriv = $current->{'recaptchakeys'}{'private'};
 8498:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 8499:             $newsettings->{'recaptchakeys'} = {
 8500:                                                  public  => '',
 8501:                                                  private => '',
 8502:                                               }
 8503:         }
 8504:     }
 8505:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 8506:         if ($container eq 'cancreate') {
 8507:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8508:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 8509:             } elsif (!defined($changes->{'cancreate'})) {
 8510:                 $changes->{'cancreate'} = ['recaptchakeys'];
 8511:             }
 8512:         } else {
 8513:             $changes->{'recaptchakeys'} = 1;
 8514:         }
 8515:     }
 8516:     return;
 8517: }
 8518: 
 8519: sub modify_usermodification {
 8520:     my ($dom,%domconfig) = @_;
 8521:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 8522:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8523:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8524:             if ($key eq 'selfcreate') {
 8525:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 8526:             } else {  
 8527:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 8528:             }
 8529:         }
 8530:     }
 8531:     my @contexts = ('author','course');
 8532:     my %context_title = (
 8533:                            author => 'In author context',
 8534:                            course => 'In course context',
 8535:                         );
 8536:     my @fields = ('lastname','firstname','middlename','generation',
 8537:                   'permanentemail','id');
 8538:     my %roles = (
 8539:                   author => ['ca','aa'],
 8540:                   course => ['st','ep','ta','in','cr'],
 8541:                 );
 8542:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8543:     foreach my $context (@contexts) {
 8544:         foreach my $role (@{$roles{$context}}) {
 8545:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 8546:             foreach my $item (@fields) {
 8547:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 8548:                     $modifyhash{$context}{$role}{$item} = 1;
 8549:                 } else {
 8550:                     $modifyhash{$context}{$role}{$item} = 0;
 8551:                 }
 8552:             }
 8553:         }
 8554:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 8555:             foreach my $role (@{$roles{$context}}) {
 8556:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 8557:                     foreach my $field (@fields) {
 8558:                         if ($modifyhash{$context}{$role}{$field} ne 
 8559:                                 $curr_usermodification{$context}{$role}{$field}) {
 8560:                             push(@{$changes{$context}},$role);
 8561:                             last;
 8562:                         }
 8563:                     }
 8564:                 }
 8565:             }
 8566:         } else {
 8567:             foreach my $context (@contexts) {
 8568:                 foreach my $role (@{$roles{$context}}) {
 8569:                     push(@{$changes{$context}},$role);
 8570:                 }
 8571:             }
 8572:         }
 8573:     }
 8574:     my %usermodification_hash =  (
 8575:                                    usermodification => \%modifyhash,
 8576:                                  );
 8577:     my $putresult = &Apache::lonnet::put_dom('configuration',
 8578:                                              \%usermodification_hash,$dom);
 8579:     if ($putresult eq 'ok') {
 8580:         if (keys(%changes) > 0) {
 8581:             $resulttext = &mt('Changes made: ').'<ul>';
 8582:             foreach my $context (@contexts) {
 8583:                 if (ref($changes{$context}) eq 'ARRAY') {
 8584:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 8585:                     if (ref($changes{$context}) eq 'ARRAY') {
 8586:                         foreach my $role (@{$changes{$context}}) {
 8587:                             my $rolename;
 8588:                             if ($role eq 'cr') {
 8589:                                 $rolename = &mt('Custom');
 8590:                             } else {
 8591:                                 $rolename = &Apache::lonnet::plaintext($role);
 8592:                             }
 8593:                             my @modifiable;
 8594:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 8595:                             foreach my $field (@fields) {
 8596:                                 if ($modifyhash{$context}{$role}{$field}) {
 8597:                                     push(@modifiable,$fieldtitles{$field});
 8598:                                 }
 8599:                             }
 8600:                             if (@modifiable > 0) {
 8601:                                 $resulttext .= join(', ',@modifiable);
 8602:                             } else {
 8603:                                 $resulttext .= &mt('none'); 
 8604:                             }
 8605:                             $resulttext .= '</li>';
 8606:                         }
 8607:                         $resulttext .= '</ul></li>';
 8608:                     }
 8609:                 }
 8610:             }
 8611:             $resulttext .= '</ul>';
 8612:         } else {
 8613:             $resulttext = &mt('No changes made to user modification settings');
 8614:         }
 8615:     } else {
 8616:         $resulttext = '<span class="LC_error">'.
 8617:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8618:     }
 8619:     return $resulttext;
 8620: }
 8621: 
 8622: sub modify_defaults {
 8623:     my ($dom,$lastactref,%domconfig) = @_;
 8624:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 8625:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8626:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 8627:     my @authtypes = ('internal','krb4','krb5','localauth');
 8628:     foreach my $item (@items) {
 8629:         $newvalues{$item} = $env{'form.'.$item};
 8630:         if ($item eq 'auth_def') {
 8631:             if ($newvalues{$item} ne '') {
 8632:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 8633:                     push(@errors,$item);
 8634:                 }
 8635:             }
 8636:         } elsif ($item eq 'lang_def') {
 8637:             if ($newvalues{$item} ne '') {
 8638:                 if ($newvalues{$item} =~ /^(\w+)/) {
 8639:                     my $langcode = $1;
 8640:                     if ($langcode ne 'x_chef') {
 8641:                         if (code2language($langcode) eq '') {
 8642:                             push(@errors,$item);
 8643:                         }
 8644:                     }
 8645:                 } else {
 8646:                     push(@errors,$item);
 8647:                 }
 8648:             }
 8649:         } elsif ($item eq 'timezone_def') {
 8650:             if ($newvalues{$item} ne '') {
 8651:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 8652:                     push(@errors,$item);   
 8653:                 }
 8654:             }
 8655:         } elsif ($item eq 'datelocale_def') {
 8656:             if ($newvalues{$item} ne '') {
 8657:                 my @datelocale_ids = DateTime::Locale->ids();
 8658:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 8659:                     push(@errors,$item);
 8660:                 }
 8661:             }
 8662:         } elsif ($item eq 'portal_def') {
 8663:             if ($newvalues{$item} ne '') {
 8664:                 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])\/?$/) {
 8665:                     push(@errors,$item);
 8666:                 }
 8667:             }
 8668:         }
 8669:         if (grep(/^\Q$item\E$/,@errors)) {
 8670:             $newvalues{$item} = $domdefaults{$item};
 8671:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 8672:             $changes{$item} = 1;
 8673:         }
 8674:         $domdefaults{$item} = $newvalues{$item};
 8675:     }
 8676:     my %defaults_hash = (
 8677:                          defaults => \%newvalues,
 8678:                         );
 8679:     my $title = &defaults_titles();
 8680:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 8681:                                              $dom);
 8682:     if ($putresult eq 'ok') {
 8683:         if (keys(%changes) > 0) {
 8684:             $resulttext = &mt('Changes made:').'<ul>';
 8685:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 8686:             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";
 8687:             foreach my $item (sort(keys(%changes))) {
 8688:                 my $value = $env{'form.'.$item};
 8689:                 if ($value eq '') {
 8690:                     $value = &mt('none');
 8691:                 } elsif ($item eq 'auth_def') {
 8692:                     my %authnames = &authtype_names();
 8693:                     my %shortauth = (
 8694:                              internal => 'int',
 8695:                              krb4 => 'krb4',
 8696:                              krb5 => 'krb5',
 8697:                              localauth  => 'loc',
 8698:                     );
 8699:                     $value = $authnames{$shortauth{$value}};
 8700:                 }
 8701:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 8702:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 8703:             }
 8704:             $resulttext .= '</ul>';
 8705:             $mailmsgtext .= "\n";
 8706:             my $cachetime = 24*60*60;
 8707:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8708:             if (ref($lastactref) eq 'HASH') {
 8709:                 $lastactref->{'domdefaults'} = 1;
 8710:             }
 8711:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 8712:                 my $notify = 1;
 8713:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 8714:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 8715:                         $notify = 0;
 8716:                     }
 8717:                 }
 8718:                 if ($notify) {
 8719:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 8720:                                                "LON-CAPA Domain Settings Change - $dom",
 8721:                                                $mailmsgtext);
 8722:                 }
 8723:             }
 8724:         } else {
 8725:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 8726:         }
 8727:     } else {
 8728:         $resulttext = '<span class="LC_error">'.
 8729:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8730:     }
 8731:     if (@errors > 0) {
 8732:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 8733:         foreach my $item (@errors) {
 8734:             $resulttext .= ' "'.$title->{$item}.'",';
 8735:         }
 8736:         $resulttext =~ s/,$//;
 8737:     }
 8738:     return $resulttext;
 8739: }
 8740: 
 8741: sub modify_scantron {
 8742:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8743:     my ($resulttext,%confhash,%changes,$errors);
 8744:     my $custom = 'custom.tab';
 8745:     my $default = 'default.tab';
 8746:     my $servadm = $r->dir_config('lonAdmEMail');
 8747:     my ($configuserok,$author_ok,$switchserver) = 
 8748:         &config_check($dom,$confname,$servadm);
 8749:     if ($env{'form.scantronformat.filename'} ne '') {
 8750:         my $error;
 8751:         if ($configuserok eq 'ok') {
 8752:             if ($switchserver) {
 8753:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 8754:             } else {
 8755:                 if ($author_ok eq 'ok') {
 8756:                     my ($result,$scantronurl) =
 8757:                         &publishlogo($r,'upload','scantronformat',$dom,
 8758:                                      $confname,'scantron','','',$custom);
 8759:                     if ($result eq 'ok') {
 8760:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 8761:                         $changes{'scantronformat'} = 1;
 8762:                     } else {
 8763:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 8764:                     }
 8765:                 } else {
 8766:                     $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);
 8767:                 }
 8768:             }
 8769:         } else {
 8770:             $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);
 8771:         }
 8772:         if ($error) {
 8773:             &Apache::lonnet::logthis($error);
 8774:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8775:         }
 8776:     }
 8777:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 8778:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 8779:             if ($env{'form.scantronformat_del'}) {
 8780:                 $confhash{'scantron'}{'scantronformat'} = '';
 8781:                 $changes{'scantronformat'} = 1;
 8782:             }
 8783:         }
 8784:     }
 8785:     if (keys(%confhash) > 0) {
 8786:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 8787:                                                  $dom);
 8788:         if ($putresult eq 'ok') {
 8789:             if (keys(%changes) > 0) {
 8790:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 8791:                     $resulttext = &mt('Changes made:').'<ul>';
 8792:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 8793:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 8794:                     } else {
 8795:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 8796:                     }
 8797:                     $resulttext .= '</ul>';
 8798:                 } else {
 8799:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 8800:                 }
 8801:                 $resulttext .= '</ul>';
 8802:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8803:                 if (ref($lastactref) eq 'HASH') {
 8804:                     $lastactref->{'domainconfig'} = 1;
 8805:                 }
 8806:             } else {
 8807:                 $resulttext = &mt('No changes made to bubblesheet format file');
 8808:             }
 8809:         } else {
 8810:             $resulttext = '<span class="LC_error">'.
 8811:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 8812:         }
 8813:     } else {
 8814:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 8815:     }
 8816:     if ($errors) {
 8817:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8818:                        $errors.'</ul>';
 8819:     }
 8820:     return $resulttext;
 8821: }
 8822: 
 8823: sub modify_coursecategories {
 8824:     my ($dom,%domconfig) = @_;
 8825:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 8826:         $cathash);
 8827:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 8828:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8829:         $cathash = $domconfig{'coursecategories'}{'cats'};
 8830:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 8831:             $changes{'togglecats'} = 1;
 8832:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 8833:         }
 8834:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 8835:             $changes{'categorize'} = 1;
 8836:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 8837:         }
 8838:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 8839:             $changes{'togglecatscomm'} = 1;
 8840:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 8841:         }
 8842:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 8843:             $changes{'categorizecomm'} = 1;
 8844:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 8845:         }
 8846:     } else {
 8847:         $changes{'togglecats'} = 1;
 8848:         $changes{'categorize'} = 1;
 8849:         $changes{'togglecatscomm'} = 1;
 8850:         $changes{'categorizecomm'} = 1;
 8851:         $domconfig{'coursecategories'} = {
 8852:                                              togglecats => $env{'form.togglecats'},
 8853:                                              categorize => $env{'form.categorize'},
 8854:                                              togglecatscomm => $env{'form.togglecatscomm'},
 8855:                                              categorizecomm => $env{'form.categorizecomm'},
 8856:                                          };
 8857:     }
 8858:     if (ref($cathash) eq 'HASH') {
 8859:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 8860:             push (@deletecategory,'instcode::0');
 8861:         }
 8862:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 8863:             push(@deletecategory,'communities::0');
 8864:         }
 8865:     }
 8866:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 8867:     if (ref($cathash) eq 'HASH') {
 8868:         if (@deletecategory > 0) {
 8869:             #FIXME Need to remove category from all courses using a deleted category 
 8870:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 8871:             foreach my $item (@deletecategory) {
 8872:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 8873:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 8874:                     $deletions{$item} = 1;
 8875:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 8876:                 }
 8877:             }
 8878:         }
 8879:         foreach my $item (keys(%{$cathash})) {
 8880:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8881:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 8882:                 $reorderings{$item} = 1;
 8883:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 8884:             }
 8885:             if ($env{'form.addcategory_name_'.$item} ne '') {
 8886:                 my $newcat = $env{'form.addcategory_name_'.$item};
 8887:                 my $newdepth = $depth+1;
 8888:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8889:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 8890:                 $adds{$newitem} = 1; 
 8891:             }
 8892:             if ($env{'form.subcat_'.$item} ne '') {
 8893:                 my $newcat = $env{'form.subcat_'.$item};
 8894:                 my $newdepth = $depth+1;
 8895:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8896:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 8897:                 $adds{$newitem} = 1;
 8898:             }
 8899:         }
 8900:     }
 8901:     if ($env{'form.instcode'} eq '1') {
 8902:         if (ref($cathash) eq 'HASH') {
 8903:             my $newitem = 'instcode::0';
 8904:             if ($cathash->{$newitem} eq '') {  
 8905:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8906:                 $adds{$newitem} = 1;
 8907:             }
 8908:         } else {
 8909:             my $newitem = 'instcode::0';
 8910:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8911:             $adds{$newitem} = 1;
 8912:         }
 8913:     }
 8914:     if ($env{'form.communities'} eq '1') {
 8915:         if (ref($cathash) eq 'HASH') {
 8916:             my $newitem = 'communities::0';
 8917:             if ($cathash->{$newitem} eq '') {
 8918:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8919:                 $adds{$newitem} = 1;
 8920:             }
 8921:         } else {
 8922:             my $newitem = 'communities::0';
 8923:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8924:             $adds{$newitem} = 1;
 8925:         }
 8926:     }
 8927:     if ($env{'form.addcategory_name'} ne '') {
 8928:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 8929:             ($env{'form.addcategory_name'} ne 'communities')) {
 8930:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 8931:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 8932:             $adds{$newitem} = 1;
 8933:         }
 8934:     }
 8935:     my $putresult;
 8936:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8937:         if (keys(%deletions) > 0) {
 8938:             foreach my $key (keys(%deletions)) {
 8939:                 if ($predelallitems{$key} ne '') {
 8940:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 8941:                 }
 8942:             }
 8943:         }
 8944:         my (@chkcats,@chktrails,%chkallitems);
 8945:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 8946:         if (ref($chkcats[0]) eq 'ARRAY') {
 8947:             my $depth = 0;
 8948:             my $chg = 0;
 8949:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 8950:                 my $name = $chkcats[0][$i];
 8951:                 my $item;
 8952:                 if ($name eq '') {
 8953:                     $chg ++;
 8954:                 } else {
 8955:                     $item = &escape($name).'::0';
 8956:                     if ($chg) {
 8957:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 8958:                     }
 8959:                     $depth ++; 
 8960:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 8961:                     $depth --;
 8962:                 }
 8963:             }
 8964:         }
 8965:     }
 8966:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8967:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 8968:         if ($putresult eq 'ok') {
 8969:             my %title = (
 8970:                          togglecats     => 'Show/Hide a course in catalog',
 8971:                          categorize     => 'Assign a category to a course',
 8972:                          togglecatscomm => 'Show/Hide a community in catalog',
 8973:                          categorizecomm => 'Assign a category to a community',
 8974:                         );
 8975:             my %level = (
 8976:                          dom  => 'set in Domain ("Modify Course/Community")',
 8977:                          crs  => 'set in Course ("Course Configuration")',
 8978:                          comm => 'set in Community ("Community Configuration")',
 8979:                         );
 8980:             $resulttext = &mt('Changes made:').'<ul>';
 8981:             if ($changes{'togglecats'}) {
 8982:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 8983:             }
 8984:             if ($changes{'categorize'}) {
 8985:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 8986:             }
 8987:             if ($changes{'togglecatscomm'}) {
 8988:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 8989:             }
 8990:             if ($changes{'categorizecomm'}) {
 8991:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 8992:             }
 8993:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8994:                 my $cathash;
 8995:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8996:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 8997:                 } else {
 8998:                     $cathash = {};
 8999:                 } 
 9000:                 my (@cats,@trails,%allitems);
 9001:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 9002:                 if (keys(%deletions) > 0) {
 9003:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 9004:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 9005:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 9006:                     }
 9007:                     $resulttext .= '</ul></li>';
 9008:                 }
 9009:                 if (keys(%reorderings) > 0) {
 9010:                     my %sort_by_trail;
 9011:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 9012:                     foreach my $key (keys(%reorderings)) {
 9013:                         if ($allitems{$key} ne '') {
 9014:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9015:                         }
 9016:                     }
 9017:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9018:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9019:                     }
 9020:                     $resulttext .= '</ul></li>';
 9021:                 }
 9022:                 if (keys(%adds) > 0) {
 9023:                     my %sort_by_trail;
 9024:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 9025:                     foreach my $key (keys(%adds)) {
 9026:                         if ($allitems{$key} ne '') {
 9027:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 9028:                         }
 9029:                     }
 9030:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 9031:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 9032:                     }
 9033:                     $resulttext .= '</ul></li>';
 9034:                 }
 9035:             }
 9036:             $resulttext .= '</ul>';
 9037:         } else {
 9038:             $resulttext = '<span class="LC_error">'.
 9039:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9040:         }
 9041:     } else {
 9042:         $resulttext = &mt('No changes made to course and community categories');
 9043:     }
 9044:     return $resulttext;
 9045: }
 9046: 
 9047: sub modify_serverstatuses {
 9048:     my ($dom,%domconfig) = @_;
 9049:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 9050:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 9051:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 9052:     }
 9053:     my @pages = &serverstatus_pages();
 9054:     foreach my $type (@pages) {
 9055:         $newserverstatus{$type}{'namedusers'} = '';
 9056:         $newserverstatus{$type}{'machines'} = '';
 9057:         if (defined($env{'form.'.$type.'_namedusers'})) {
 9058:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 9059:             my @okusers;
 9060:             foreach my $user (@users) {
 9061:                 my ($uname,$udom) = split(/:/,$user);
 9062:                 if (($udom =~ /^$match_domain$/) &&   
 9063:                     (&Apache::lonnet::domain($udom)) &&
 9064:                     ($uname =~ /^$match_username$/)) {
 9065:                     if (!grep(/^\Q$user\E/,@okusers)) {
 9066:                         push(@okusers,$user);
 9067:                     }
 9068:                 }
 9069:             }
 9070:             if (@okusers > 0) {
 9071:                  @okusers = sort(@okusers);
 9072:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 9073:             }
 9074:         }
 9075:         if (defined($env{'form.'.$type.'_machines'})) {
 9076:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 9077:             my @okmachines;
 9078:             foreach my $ip (@machines) {
 9079:                 my @parts = split(/\./,$ip);
 9080:                 next if (@parts < 4);
 9081:                 my $badip = 0;
 9082:                 for (my $i=0; $i<4; $i++) {
 9083:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 9084:                         $badip = 1;
 9085:                         last;
 9086:                     }
 9087:                 }
 9088:                 if (!$badip) {
 9089:                     push(@okmachines,$ip);     
 9090:                 }
 9091:             }
 9092:             @okmachines = sort(@okmachines);
 9093:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 9094:         }
 9095:     }
 9096:     my %serverstatushash =  (
 9097:                                 serverstatuses => \%newserverstatus,
 9098:                             );
 9099:     foreach my $type (@pages) {
 9100:         foreach my $setting ('namedusers','machines') {
 9101:             my (@current,@new);
 9102:             if (ref($currserverstatus{$type}) eq 'HASH') {
 9103:                 if ($currserverstatus{$type}{$setting} ne '') { 
 9104:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 9105:                 }
 9106:             }
 9107:             if ($newserverstatus{$type}{$setting} ne '') {
 9108:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 9109:             }
 9110:             if (@current > 0) {
 9111:                 if (@new > 0) {
 9112:                     foreach my $item (@current) {
 9113:                         if (!grep(/^\Q$item\E$/,@new)) {
 9114:                             $changes{$type}{$setting} = 1;
 9115:                             last;
 9116:                         }
 9117:                     }
 9118:                     foreach my $item (@new) {
 9119:                         if (!grep(/^\Q$item\E$/,@current)) {
 9120:                             $changes{$type}{$setting} = 1;
 9121:                             last;
 9122:                         }
 9123:                     }
 9124:                 } else {
 9125:                     $changes{$type}{$setting} = 1;
 9126:                 }
 9127:             } elsif (@new > 0) {
 9128:                 $changes{$type}{$setting} = 1;
 9129:             }
 9130:         }
 9131:     }
 9132:     if (keys(%changes) > 0) {
 9133:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9134:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9135:                                                  \%serverstatushash,$dom);
 9136:         if ($putresult eq 'ok') {
 9137:             $resulttext .= &mt('Changes made:').'<ul>';
 9138:             foreach my $type (@pages) {
 9139:                 if (ref($changes{$type}) eq 'HASH') {
 9140:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 9141:                     if ($changes{$type}{'namedusers'}) {
 9142:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 9143:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 9144:                         } else {
 9145:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 9146:                         }
 9147:                     }
 9148:                     if ($changes{$type}{'machines'}) {
 9149:                         if ($newserverstatus{$type}{'machines'} eq '') {
 9150:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 9151:                         } else {
 9152:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 9153:                         }
 9154: 
 9155:                     }
 9156:                     $resulttext .= '</ul></li>';
 9157:                 }
 9158:             }
 9159:             $resulttext .= '</ul>';
 9160:         } else {
 9161:             $resulttext = '<span class="LC_error">'.
 9162:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 9163: 
 9164:         }
 9165:     } else {
 9166:         $resulttext = &mt('No changes made to access to server status pages');
 9167:     }
 9168:     return $resulttext;
 9169: }
 9170: 
 9171: sub modify_helpsettings {
 9172:     my ($r,$dom,$confname,%domconfig) = @_;
 9173:     my ($resulttext,$errors,%changes,%helphash);
 9174:     my %defaultchecked = ('submitbugs' => 'on');
 9175:     my @offon = ('off','on');
 9176:     my @toggles = ('submitbugs');
 9177:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 9178:         foreach my $item (@toggles) {
 9179:             if ($defaultchecked{$item} eq 'on') { 
 9180:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9181:                     if ($env{'form.'.$item} eq '0') {
 9182:                         $changes{$item} = 1;
 9183:                     }
 9184:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9185:                     $changes{$item} = 1;
 9186:                 }
 9187:             } elsif ($defaultchecked{$item} eq 'off') {
 9188:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 9189:                     if ($env{'form.'.$item} eq '1') {
 9190:                         $changes{$item} = 1;
 9191:                     }
 9192:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 9193:                     $changes{$item} = 1;
 9194:                 }
 9195:             }
 9196:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 9197:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 9198:             }
 9199:         }
 9200:     }
 9201:     my $putresult;
 9202:     if (keys(%changes) > 0) {
 9203:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 9204:         if ($putresult eq 'ok') {
 9205:             $resulttext = &mt('Changes made:').'<ul>';
 9206:             foreach my $item (sort(keys(%changes))) {
 9207:                 if ($item eq 'submitbugs') {
 9208:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 9209:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 9210:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 9211:                 }
 9212:             }
 9213:             $resulttext .= '</ul>';
 9214:         } else {
 9215:             $resulttext = &mt('No changes made to help settings');
 9216:             $errors .= '<li><span class="LC_error">'.
 9217:                        &mt('An error occurred storing the settings: [_1]',
 9218:                            $putresult).'</span></li>';
 9219:         }
 9220:     }
 9221:     if ($errors) {
 9222:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9223:                        $errors.'</ul>';
 9224:     }
 9225:     return $resulttext;
 9226: }
 9227: 
 9228: sub modify_coursedefaults {
 9229:     my ($dom,$lastactref,%domconfig) = @_;
 9230:     my ($resulttext,$errors,%changes,%defaultshash);
 9231:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 9232:     my @toggles = ('canuse_pdfforms');
 9233:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 9234:                    'uploadquota_community','uploadquota_textbook');
 9235:     my @types = ('official','unofficial','community','textbook');
 9236:     my %staticdefaults = (
 9237:                            anonsurvey_threshold => 10,
 9238:                            uploadquota          => 500,
 9239:                          );
 9240: 
 9241:     $defaultshash{'coursedefaults'} = {};
 9242: 
 9243:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 9244:         if ($domconfig{'coursedefaults'} eq '') {
 9245:             $domconfig{'coursedefaults'} = {};
 9246:         }
 9247:     }
 9248: 
 9249:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 9250:         foreach my $item (@toggles) {
 9251:             if ($defaultchecked{$item} eq 'on') {
 9252:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9253:                     ($env{'form.'.$item} eq '0')) {
 9254:                     $changes{$item} = 1;
 9255:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9256:                     $changes{$item} = 1;
 9257:                 }
 9258:             } elsif ($defaultchecked{$item} eq 'off') {
 9259:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9260:                     ($env{'form.'.$item} eq '1')) {
 9261:                     $changes{$item} = 1;
 9262:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9263:                     $changes{$item} = 1;
 9264:                 }
 9265:             }
 9266:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 9267:         }
 9268:         foreach my $item (@numbers) {
 9269:             my ($currdef,$newdef);
 9270:             $newdef = $env{'form.'.$item};
 9271:             if ($item eq 'anonsurvey_threshold') {
 9272:                 $currdef = $domconfig{'coursedefaults'}{$item};
 9273:                 $newdef =~ s/\D//g;
 9274:                 if ($newdef eq '' || $newdef < 1) {
 9275:                     $newdef = 1;
 9276:                 }
 9277:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 9278:             } else {
 9279:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 9280:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9281:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 9282:                 }
 9283:                 $newdef =~ s/[^\w.\-]//g;
 9284:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 9285:             }
 9286:             if ($currdef ne $newdef) {
 9287:                 my $staticdef;
 9288:                 if ($item eq 'anonsurvey_threshold') {
 9289:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 9290:                         $changes{$item} = 1;
 9291:                     }
 9292:                 } else {
 9293:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 9294:                         $changes{'uploadquota'} = 1;
 9295:                     }
 9296:                 }
 9297:             }
 9298:         }
 9299: 
 9300:         my $officialcreds = $env{'form.official_credits'};
 9301:         $officialcreds =~ s/[^\d.]+//g;
 9302:         my $unofficialcreds = $env{'form.unofficial_credits'};
 9303:         $unofficialcreds =~ s/[^\d.]+//g;
 9304:         my $textbookcreds = $env{'form.textbook_credits'};
 9305:         $textbookcreds =~ s/[^\d.]+//g;
 9306:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 9307:                 ($env{'form.coursecredits'} eq '1')) {
 9308:                 $changes{'coursecredits'} = 1;
 9309:         } else {
 9310:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 9311:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 9312:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 9313:                 $changes{'coursecredits'} = 1;
 9314:             }
 9315:         }
 9316:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 9317:             official   => $officialcreds,
 9318:             unofficial => $unofficialcreds,
 9319:             textbook   => $textbookcreds,
 9320:         }
 9321:     }
 9322:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9323:                                              $dom);
 9324:     if ($putresult eq 'ok') {
 9325:         if (keys(%changes) > 0) {
 9326:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9327:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || 
 9328:                 ($changes{'uploadquota'})) { 
 9329:                 if ($changes{'canuse_pdfforms'}) {
 9330:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 9331:                 }
 9332:                 if ($changes{'coursecredits'}) {
 9333:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9334:                         $domdefaults{'officialcredits'} =
 9335:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 9336:                         $domdefaults{'unofficialcredits'} =
 9337:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 9338:                         $domdefaults{'textbookcredits'} =
 9339:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 9340:                     }
 9341:                 }
 9342:                 if ($changes{'uploadquota'}) {
 9343:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9344:                         foreach my $type (@types) {
 9345:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 9346:                         }
 9347:                     }
 9348:                 }
 9349:                 my $cachetime = 24*60*60;
 9350:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9351:                 if (ref($lastactref) eq 'HASH') {
 9352:                     $lastactref->{'domdefaults'} = 1;
 9353:                 }
 9354:             }
 9355:             $resulttext = &mt('Changes made:').'<ul>';
 9356:             foreach my $item (sort(keys(%changes))) {
 9357:                 if ($item eq 'canuse_pdfforms') {
 9358:                     if ($env{'form.'.$item} eq '1') {
 9359:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 9360:                     } else {
 9361:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 9362:                     }
 9363:                 } elsif ($item eq 'anonsurvey_threshold') {
 9364:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 9365:                 } elsif ($item eq 'uploadquota') {
 9366:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9367:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 9368:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 9369:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 9370:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
 9371: 
 9372:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 9373:                                        '</ul>'.
 9374:                                        '</li>';
 9375:                     } else {
 9376:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 9377:                     }
 9378:                 } elsif ($item eq 'coursecredits') {
 9379:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9380:                         if (($domdefaults{'officialcredits'} eq '') &&
 9381:                             ($domdefaults{'unofficialcredits'} eq '') &&
 9382:                             ($domdefaults{'textbookcredits'} eq '')) {
 9383:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9384:                         } else {
 9385:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 9386:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 9387:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 9388:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
 9389:                                            '</ul>'.
 9390:                                            '</li>';
 9391:                         }
 9392:                     } else {
 9393:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9394:                     }
 9395:                 }
 9396:             }
 9397:             $resulttext .= '</ul>';
 9398:         } else {
 9399:             $resulttext = &mt('No changes made to course defaults');
 9400:         }
 9401:     } else {
 9402:         $resulttext = '<span class="LC_error">'.
 9403:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9404:     }
 9405:     return $resulttext;
 9406: }
 9407: 
 9408: sub modify_selfenrollment {
 9409:     my ($dom,$lastactref,%domconfig) = @_;
 9410:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
 9411:     my @types = ('official','unofficial','community','textbook');
 9412:     my %titles = &tool_titles();
 9413:     my %descs = &selfenroll_default_descs();
 9414:     ($ordered{'admin'},my $titlesref) = &get_selfenroll_titles();
 9415:     $ordered{'default'} = ['types','registered','approval','limit'];
 9416: 
 9417:     my (%roles,%shown,%toplevel);
 9418:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
 9419: 
 9420:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
 9421:         if ($domconfig{'selfenrollment'} eq '') {
 9422:             $domconfig{'selfenrollment'} = {};
 9423:         }
 9424:     }
 9425:     %toplevel = (
 9426:                   admin      => 'Configuration Rights',
 9427:                   default    => 'Default settings',
 9428:                   validation => 'Validation of self-enrollment requests',
 9429:                 );
 9430:     my ($itemsref,$namesref,$fieldsref) = &selfenroll_validation_types();
 9431: 
 9432:     if (ref($ordered{'admin'}) eq 'ARRAY') {
 9433:         foreach my $item (@{$ordered{'admin'}}) {
 9434:             foreach my $type (@types) {
 9435:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
 9436:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
 9437:                 } else {
 9438:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
 9439:                 }
 9440:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 9441:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 9442:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
 9443:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
 9444:                             push(@{$changes{'admin'}{$type}},$item);
 9445:                         }
 9446:                     } else {
 9447:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
 9448:                             push(@{$changes{'admin'}{$type}},$item);
 9449:                         }
 9450:                     }
 9451:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
 9452:                     push(@{$changes{'admin'}{$type}},$item);
 9453:                 }
 9454:             }
 9455:         }
 9456:     }
 9457: 
 9458:     foreach my $item (@{$ordered{'default'}}) {
 9459:         foreach my $type (@types) {
 9460:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
 9461:             if ($item eq 'types') {
 9462:                 unless (($value eq 'all') || ($value eq 'dom')) {
 9463:                     $value = '';
 9464:                 }
 9465:             } elsif ($item eq 'registered') {
 9466:                 unless ($value eq '1') {
 9467:                     $value = 0;
 9468:                 }
 9469:             } elsif ($item eq 'approval') {
 9470:                 unless ($value =~ /^[012]$/) {
 9471:                     $value = 0;
 9472:                 }
 9473:             } else {
 9474:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
 9475:                     $value = 'none';
 9476:                 }
 9477:             }
 9478:             $selfenrollhash{'default'}{$type}{$item} = $value;
 9479:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 9480:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 9481:                     if ($selfenrollhash{'default'}{$type}{$item} ne
 9482:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
 9483:                          push(@{$changes{'default'}{$type}},$item);
 9484:                     }
 9485:                 } else {
 9486:                     push(@{$changes{'default'}{$type}},$item);
 9487:                 }
 9488:             } else {
 9489:                 push(@{$changes{'default'}{$type}},$item);
 9490:             }
 9491:             if ($item eq 'limit') {
 9492:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
 9493:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
 9494:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
 9495:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
 9496:                     }
 9497:                 } else {
 9498:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
 9499:                 }
 9500:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 9501:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
 9502:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
 9503:                          push(@{$changes{'default'}{$type}},'cap');
 9504:                     }
 9505:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
 9506:                     push(@{$changes{'default'}{$type}},'cap');
 9507:                 }
 9508:             }
 9509:         }
 9510:     }
 9511: 
 9512:     foreach my $item (@{$itemsref}) {
 9513:         if ($item eq 'fields') {
 9514:             my @changed;
 9515:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
 9516:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
 9517:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
 9518:             }
 9519:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
 9520:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
 9521:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
 9522:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
 9523:                 } else {
 9524:                     @changed = @{$selfenrollhash{'validation'}{$item}};
 9525:                 }
 9526:             } else {
 9527:                 @changed = @{$selfenrollhash{'validation'}{$item}};
 9528:             }
 9529:             if (@changed) {
 9530:                 if ($selfenrollhash{'validation'}{$item}) { 
 9531:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
 9532:                 } else {
 9533:                     $changes{'validation'}{$item} = &mt('None');
 9534:                 }
 9535:             }
 9536:         } else {
 9537:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
 9538:             if ($item eq 'markup') {
 9539:                if ($env{'form.selfenroll_validation_'.$item}) {
 9540:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9541:                }
 9542:             }
 9543:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
 9544:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
 9545:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
 9546:                 }
 9547:             }
 9548:         }
 9549:     }
 9550: 
 9551:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
 9552:                                              $dom);
 9553:     if ($putresult eq 'ok') {
 9554:         if (keys(%changes) > 0) {
 9555:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9556:             $resulttext = &mt('Changes made:').'<ul>';
 9557:             foreach my $key ('admin','default','validation') {
 9558:                 if (ref($changes{$key}) eq 'HASH') {
 9559:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
 9560:                     if ($key eq 'validation') {
 9561:                         foreach my $item (@{$itemsref}) {
 9562:                             if (exists($changes{$key}{$item})) {
 9563:                                 if ($item eq 'markup') {
 9564:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
 9565:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
 9566:                                 } else {  
 9567:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
 9568:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
 9569:                                 }
 9570:                             }
 9571:                         }
 9572:                     } else {
 9573:                         foreach my $type (@types) {
 9574:                             if ($type eq 'community') {
 9575:                                 $roles{'1'} = &mt('Community personnel');
 9576:                             } else {
 9577:                                 $roles{'1'} = &mt('Course personnel');
 9578:                             }
 9579:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
 9580:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
 9581:                                 foreach my $item (@{$ordered{$key}}) {
 9582:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
 9583:                                         $resulttext .= '<li>';
 9584:                                         if ($key eq 'admin') {
 9585:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
 9586:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
 9587:                                         } else {
 9588:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
 9589:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
 9590:                                         }
 9591:                                         $resulttext .= '</li>';
 9592:                                     }
 9593:                                 }
 9594:                                 $resulttext .= '</ul></li>';
 9595:                             }
 9596:                         }
 9597:                         $resulttext .= '</ul></li>'; 
 9598:                     }
 9599:                 }
 9600:             }
 9601:             $resulttext .= '</ul>';
 9602:         } else {
 9603:             $resulttext = &mt('No changes made to self-enrollment settings');
 9604:         }
 9605:     } else {
 9606:         $resulttext = '<span class="LC_error">'.
 9607:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9608:     }
 9609:     return $resulttext;
 9610: }
 9611: 
 9612: sub modify_usersessions {
 9613:     my ($dom,$lastactref,%domconfig) = @_;
 9614:     my @hostingtypes = ('version','excludedomain','includedomain');
 9615:     my @offloadtypes = ('primary','default');
 9616:     my %types = (
 9617:                   remote => \@hostingtypes,
 9618:                   hosted => \@hostingtypes,
 9619:                   spares => \@offloadtypes,
 9620:                 );
 9621:     my @prefixes = ('remote','hosted','spares');
 9622:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 9623:     my (%by_ip,%by_location,@intdoms);
 9624:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 9625:     my @locations = sort(keys(%by_location));
 9626:     my (%defaultshash,%changes);
 9627:     foreach my $prefix (@prefixes) {
 9628:         $defaultshash{'usersessions'}{$prefix} = {};
 9629:     }
 9630:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9631:     my $resulttext;
 9632:     my %iphost = &Apache::lonnet::get_iphost();
 9633:     foreach my $prefix (@prefixes) {
 9634:         next if ($prefix eq 'spares');
 9635:         foreach my $type (@{$types{$prefix}}) {
 9636:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 9637:             if ($type eq 'version') {
 9638:                 my $value = $env{'form.'.$prefix.'_'.$type};
 9639:                 my $okvalue;
 9640:                 if ($value ne '') {
 9641:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 9642:                         $okvalue = $value;
 9643:                     }
 9644:                 }
 9645:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9646:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9647:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 9648:                             if ($inuse == 0) {
 9649:                                 $changes{$prefix}{$type} = 1;
 9650:                             } else {
 9651:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 9652:                                     $changes{$prefix}{$type} = 1;
 9653:                                 }
 9654:                                 if ($okvalue ne '') {
 9655:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9656:                                 } 
 9657:                             }
 9658:                         } else {
 9659:                             if (($inuse == 1) && ($okvalue ne '')) {
 9660:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9661:                                 $changes{$prefix}{$type} = 1;
 9662:                             }
 9663:                         }
 9664:                     } else {
 9665:                         if (($inuse == 1) && ($okvalue ne '')) {
 9666:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9667:                             $changes{$prefix}{$type} = 1;
 9668:                         }
 9669:                     }
 9670:                 } else {
 9671:                     if (($inuse == 1) && ($okvalue ne '')) {
 9672:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9673:                         $changes{$prefix}{$type} = 1;
 9674:                     }
 9675:                 }
 9676:             } else {
 9677:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 9678:                 my @okvals;
 9679:                 foreach my $val (@vals) {
 9680:                     if ($val =~ /:/) {
 9681:                         my @items = split(/:/,$val);
 9682:                         foreach my $item (@items) {
 9683:                             if (ref($by_location{$item}) eq 'ARRAY') {
 9684:                                 push(@okvals,$item);
 9685:                             }
 9686:                         }
 9687:                     } else {
 9688:                         if (ref($by_location{$val}) eq 'ARRAY') {
 9689:                             push(@okvals,$val);
 9690:                         }
 9691:                     }
 9692:                 }
 9693:                 @okvals = sort(@okvals);
 9694:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9695:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9696:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9697:                             if ($inuse == 0) {
 9698:                                 $changes{$prefix}{$type} = 1; 
 9699:                             } else {
 9700:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9701:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 9702:                                 if (@changed > 0) {
 9703:                                     $changes{$prefix}{$type} = 1;
 9704:                                 }
 9705:                             }
 9706:                         } else {
 9707:                             if ($inuse == 1) {
 9708:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9709:                                 $changes{$prefix}{$type} = 1;
 9710:                             }
 9711:                         } 
 9712:                     } else {
 9713:                         if ($inuse == 1) {
 9714:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9715:                             $changes{$prefix}{$type} = 1;
 9716:                         }
 9717:                     }
 9718:                 } else {
 9719:                     if ($inuse == 1) {
 9720:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9721:                         $changes{$prefix}{$type} = 1;
 9722:                     }
 9723:                 }
 9724:             }
 9725:         }
 9726:     }
 9727: 
 9728:     my @alldoms = &Apache::lonnet::all_domains();
 9729:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9730:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 9731:     my $savespares;
 9732: 
 9733:     foreach my $lonhost (sort(keys(%servers))) {
 9734:         my $serverhomeID =
 9735:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 9736:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 9737:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 9738:         my %spareschg;
 9739:         foreach my $type (@{$types{'spares'}}) {
 9740:             my @okspares;
 9741:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 9742:             foreach my $server (@checked) {
 9743:                 if (&Apache::lonnet::hostname($server) ne '') {
 9744:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 9745:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 9746:                             push(@okspares,$server);
 9747:                         }
 9748:                     }
 9749:                 }
 9750:             }
 9751:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 9752:             my $newspare;
 9753:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 9754:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 9755:                     $newspare = $new;
 9756:                 }
 9757:             }
 9758:             my @spares;
 9759:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 9760:                 @spares = sort(@okspares,$newspare);
 9761:             } else {
 9762:                 @spares = sort(@okspares);
 9763:             }
 9764:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 9765:             if (ref($spareid{$lonhost}) eq 'HASH') {
 9766:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 9767:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 9768:                     if (@diffs > 0) {
 9769:                         $spareschg{$type} = 1;
 9770:                     }
 9771:                 }
 9772:             }
 9773:         }
 9774:         if (keys(%spareschg) > 0) {
 9775:             $changes{'spares'}{$lonhost} = \%spareschg;
 9776:         }
 9777:     }
 9778: 
 9779:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9780:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 9781:             if (ref($changes{'spares'}) eq 'HASH') {
 9782:                 if (keys(%{$changes{'spares'}}) > 0) {
 9783:                     $savespares = 1;
 9784:                 }
 9785:             }
 9786:         } else {
 9787:             $savespares = 1;
 9788:         }
 9789:     }
 9790: 
 9791:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 9792:     if ((keys(%changes) > 0) || ($savespares)) {
 9793:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9794:                                                  $dom);
 9795:         if ($putresult eq 'ok') {
 9796:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9797:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 9798:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 9799:                 }
 9800:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 9801:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 9802:                 }
 9803:             }
 9804:             my $cachetime = 24*60*60;
 9805:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9806:             if (ref($lastactref) eq 'HASH') {
 9807:                 $lastactref->{'domdefaults'} = 1;
 9808:             }
 9809:             if (keys(%changes) > 0) {
 9810:                 my %lt = &usersession_titles();
 9811:                 $resulttext = &mt('Changes made:').'<ul>';
 9812:                 foreach my $prefix (@prefixes) {
 9813:                     if (ref($changes{$prefix}) eq 'HASH') {
 9814:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 9815:                         if ($prefix eq 'spares') {
 9816:                             if (ref($changes{$prefix}) eq 'HASH') {
 9817:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 9818:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 9819:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 9820:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 9821:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 9822:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 9823:                                         foreach my $type (@{$types{$prefix}}) {
 9824:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 9825:                                                 my $offloadto = &mt('None');
 9826:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 9827:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 9828:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 9829:                                                     }
 9830:                                                 }
 9831:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 9832:                                             }
 9833:                                         }
 9834:                                     }
 9835:                                     $resulttext .= '</li>';
 9836:                                 }
 9837:                             }
 9838:                         } else {
 9839:                             foreach my $type (@{$types{$prefix}}) {
 9840:                                 if (defined($changes{$prefix}{$type})) {
 9841:                                     my $newvalue;
 9842:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9843:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 9844:                                             if ($type eq 'version') {
 9845:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 9846:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9847:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 9848:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 9849:                                                 }
 9850:                                             }
 9851:                                         }
 9852:                                     }
 9853:                                     if ($newvalue eq '') {
 9854:                                         if ($type eq 'version') {
 9855:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 9856:                                         } else {
 9857:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 9858:                                         }
 9859:                                     } else {
 9860:                                         if ($type eq 'version') {
 9861:                                             $newvalue .= ' '.&mt('(or later)'); 
 9862:                                         }
 9863:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 9864:                                     }
 9865:                                 }
 9866:                             }
 9867:                         }
 9868:                         $resulttext .= '</ul>';
 9869:                     }
 9870:                 }
 9871:                 $resulttext .= '</ul>';
 9872:             } else {
 9873:                 $resulttext = $nochgmsg;
 9874:             }
 9875:         } else {
 9876:             $resulttext = '<span class="LC_error">'.
 9877:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9878:         }
 9879:     } else {
 9880:         $resulttext = $nochgmsg;
 9881:     }
 9882:     return $resulttext;
 9883: }
 9884: 
 9885: sub modify_loadbalancing {
 9886:     my ($dom,%domconfig) = @_;
 9887:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9888:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9889:     my ($othertitle,$usertypes,$types) =
 9890:         &Apache::loncommon::sorted_inst_types($dom);
 9891:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9892:     my @sparestypes = ('primary','default');
 9893:     my %typetitles = &sparestype_titles();
 9894:     my $resulttext;
 9895:     my (%currbalancer,%currtargets,%currrules,%existing);
 9896:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 9897:         %existing = %{$domconfig{'loadbalancing'}};
 9898:     }
 9899:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 9900:                               \%currtargets,\%currrules);
 9901:     my ($saveloadbalancing,%defaultshash,%changes);
 9902:     my ($alltypes,$othertypes,$titles) =
 9903:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9904:     my %ruletitles = &offloadtype_text();
 9905:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 9906:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 9907:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 9908:         if ($balancer eq '') {
 9909:             next;
 9910:         }
 9911:         if (!exists($servers{$balancer})) {
 9912:             if (exists($currbalancer{$balancer})) {
 9913:                 push(@{$changes{'delete'}},$balancer);
 9914:             }
 9915:             next;
 9916:         }
 9917:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 9918:             push(@{$changes{'delete'}},$balancer);
 9919:             next;
 9920:         }
 9921:         if (!exists($currbalancer{$balancer})) {
 9922:             push(@{$changes{'add'}},$balancer);
 9923:         }
 9924:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 9925:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 9926:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 9927:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 9928:             $saveloadbalancing = 1;
 9929:         }
 9930:         foreach my $sparetype (@sparestypes) {
 9931:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 9932:             my @offloadto;
 9933:             foreach my $target (@targets) {
 9934:                 if (($servers{$target}) && ($target ne $balancer)) {
 9935:                     if ($sparetype eq 'default') {
 9936:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 9937:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 9938:                         }
 9939:                     }
 9940:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 9941:                         push(@offloadto,$target);
 9942:                     }
 9943:                 }
 9944:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 9945:             }
 9946:         }
 9947:         if (ref($currtargets{$balancer}) eq 'HASH') {
 9948:             foreach my $sparetype (@sparestypes) {
 9949:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 9950:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 9951:                     if (@targetdiffs > 0) {
 9952:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9953:                     }
 9954:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9955:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9956:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9957:                     }
 9958:                 }
 9959:             }
 9960:         } else {
 9961:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 9962:                 foreach my $sparetype (@sparestypes) {
 9963:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9964:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9965:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 9966:                         }
 9967:                     }
 9968:                 }
 9969:             }
 9970:         }
 9971:         my $ishomedom;
 9972:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 9973:             $ishomedom = 1;
 9974:         }
 9975:         if (ref($alltypes) eq 'ARRAY') {
 9976:             foreach my $type (@{$alltypes}) {
 9977:                 my $rule;
 9978:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 9979:                          (!$ishomedom)) {
 9980:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 9981:                 }
 9982:                 if ($rule eq 'specific') {
 9983:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 9984:                 }
 9985:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 9986:                 if (ref($currrules{$balancer}) eq 'HASH') {
 9987:                     if ($rule ne $currrules{$balancer}{$type}) {
 9988:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9989:                     }
 9990:                 } elsif ($rule ne '') {
 9991:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9992:                 }
 9993:             }
 9994:         }
 9995:     }
 9996:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 9997:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 9998:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 9999:             $defaultshash{'loadbalancing'} = {};
10000:         }
10001:         my $putresult = &Apache::lonnet::put_dom('configuration',
10002:                                                  \%defaultshash,$dom);
10003:         if ($putresult eq 'ok') {
10004:             if (keys(%changes) > 0) {
10005:                 if (ref($changes{'delete'}) eq 'ARRAY') {
10006:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
10007:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
10008:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10009:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10010:                     }
10011:                 }
10012:                 if (ref($changes{'add'}) eq 'ARRAY') {
10013:                     foreach my $balancer (sort(@{$changes{'add'}})) {
10014:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
10015:                     }
10016:                 }
10017:                 if (ref($changes{'curr'}) eq 'HASH') {
10018:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
10019:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
10020:                             if ($changes{'curr'}{$balancer}{'targets'}) {
10021:                                 my %offloadstr;
10022:                                 foreach my $sparetype (@sparestypes) {
10023:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
10024:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
10025:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
10026:                                         }
10027:                                     }
10028:                                 }
10029:                                 if (keys(%offloadstr) == 0) {
10030:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
10031:                                 } else {
10032:                                     my $showoffload;
10033:                                     foreach my $sparetype (@sparestypes) {
10034:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
10035:                                         if (defined($offloadstr{$sparetype})) {
10036:                                             $showoffload .= $offloadstr{$sparetype};
10037:                                         } else {
10038:                                             $showoffload .= &mt('None');
10039:                                         }
10040:                                         $showoffload .= ('&nbsp;'x3);
10041:                                     }
10042:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
10043:                                 }
10044:                             }
10045:                         }
10046:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
10047:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
10048:                                 foreach my $type (@{$alltypes}) {
10049:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
10050:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
10051:                                         my $balancetext;
10052:                                         if ($rule eq '') {
10053:                                             $balancetext =  $ruletitles{'default'};
10054:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
10055:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
10056:                                             $balancetext =  $ruletitles{$rule};
10057:                                         } else {
10058:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
10059:                                         }
10060:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
10061:                                     }
10062:                                 }
10063:                             }
10064:                         }
10065:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
10066:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
10067:                     }
10068:                 }
10069:                 if ($resulttext ne '') {
10070:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
10071:                 } else {
10072:                     $resulttext = $nochgmsg;
10073:                 }
10074:             } else {
10075:                 $resulttext = $nochgmsg;
10076:             }
10077:         } else {
10078:             $resulttext = '<span class="LC_error">'.
10079:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10080:         }
10081:     } else {
10082:         $resulttext = $nochgmsg;
10083:     }
10084:     return $resulttext;
10085: }
10086: 
10087: sub recurse_check {
10088:     my ($chkcats,$categories,$depth,$name) = @_;
10089:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
10090:         my $chg = 0;
10091:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
10092:             my $category = $chkcats->[$depth]{$name}[$j];
10093:             my $item;
10094:             if ($category eq '') {
10095:                 $chg ++;
10096:             } else {
10097:                 my $deeper = $depth + 1;
10098:                 $item = &escape($category).':'.&escape($name).':'.$depth;
10099:                 if ($chg) {
10100:                     $categories->{$item} -= $chg;
10101:                 }
10102:                 &recurse_check($chkcats,$categories,$deeper,$category);
10103:                 $deeper --;
10104:             }
10105:         }
10106:     }
10107:     return;
10108: }
10109: 
10110: sub recurse_cat_deletes {
10111:     my ($item,$coursecategories,$deletions) = @_;
10112:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10113:     my $subdepth = $depth + 1;
10114:     if (ref($coursecategories) eq 'HASH') {
10115:         foreach my $subitem (keys(%{$coursecategories})) {
10116:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
10117:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
10118:                 delete($coursecategories->{$subitem});
10119:                 $deletions->{$subitem} = 1;
10120:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
10121:             }
10122:         }
10123:     }
10124:     return;
10125: }
10126: 
10127: sub get_active_dcs {
10128:     my ($dom) = @_;
10129:     my $now = time;
10130:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
10131:     my %domcoords;
10132:     my $numdcs = 0;
10133:     foreach my $server (keys(%dompersonnel)) {
10134:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
10135:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
10136:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
10137:         }
10138:     }
10139:     return %domcoords;
10140: }
10141: 
10142: sub active_dc_picker {
10143:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
10144:     my %domcoords = &get_active_dcs($dom); 
10145:     my @domcoord = keys(%domcoords);
10146:     if (keys(%currhash)) {
10147:         foreach my $dc (keys(%currhash)) {
10148:             unless (exists($domcoords{$dc})) {
10149:                 push(@domcoord,$dc);
10150:             }
10151:         }
10152:     }
10153:     @domcoord = sort(@domcoord);
10154:     my $numdcs = scalar(@domcoord);
10155:     my $rows = 0;
10156:     my $table;
10157:     if ($numdcs > 1) {
10158:         $table = '<table>';
10159:         for (my $i=0; $i<@domcoord; $i++) {
10160:             my $rem = $i%($numinrow);
10161:             if ($rem == 0) {
10162:                 if ($i > 0) {
10163:                     $table .= '</tr>';
10164:                 }
10165:                 $table .= '<tr>';
10166:                 $rows ++;
10167:             }
10168:             my $check = '';
10169:             if ($inputtype eq 'radio') {
10170:                 if (keys(%currhash) == 0) {
10171:                     if (!$i) {
10172:                         $check = ' checked="checked"';
10173:                     }
10174:                 } elsif (exists($currhash{$domcoord[$i]})) {
10175:                     $check = ' checked="checked"';
10176:                 }
10177:             } else {
10178:                 if (exists($currhash{$domcoord[$i]})) {
10179:                     $check = ' checked="checked"';
10180:                 }
10181:             }
10182:             if ($i == @domcoord - 1) {
10183:                 my $colsleft = $numinrow - $rem;
10184:                 if ($colsleft > 1) {
10185:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
10186:                 } else {
10187:                     $table .= '<td class="LC_left_item">';
10188:                 }
10189:             } else {
10190:                 $table .= '<td class="LC_left_item">';
10191:             }
10192:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
10193:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10194:             $table .= '<span class="LC_nobreak"><label>'.
10195:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
10196:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
10197:             if ($user ne $dcname.':'.$dcdom) {
10198:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10199:             }
10200:             $table .= '</label></span></td>';
10201:         }
10202:         $table .= '</tr></table>';
10203:     } elsif ($numdcs == 1) {
10204:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
10205:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
10206:         if ($inputtype eq 'radio') {
10207:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
10208:             if ($user ne $dcname.':'.$dcdom) {
10209:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10210:             }
10211:         } else {
10212:             my $check;
10213:             if (exists($currhash{$domcoord[0]})) {
10214:                 $check = ' checked="checked"';
10215:             }
10216:             $table .= '<span class="LC_nobreak"><label>'.
10217:                       '<input type="checkbox" name="'.$name.'" '.
10218:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
10219:             if ($user ne $dcname.':'.$dcdom) {
10220:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
10221:             }
10222:             $table .= '</label></span>';
10223:             $rows ++;
10224:         }
10225:     }
10226:     return ($numdcs,$table,$rows);
10227: }
10228: 
10229: sub usersession_titles {
10230:     return &Apache::lonlocal::texthash(
10231:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
10232:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
10233:                spares => 'Servers offloaded to, when busy',
10234:                version => 'LON-CAPA version requirement',
10235:                excludedomain => 'Allow all, but exclude specific domains',
10236:                includedomain => 'Deny all, but include specific domains',
10237:                primary => 'Primary (checked first)',
10238:                default => 'Default',
10239:            );
10240: }
10241: 
10242: sub id_for_thisdom {
10243:     my (%servers) = @_;
10244:     my %altids;
10245:     foreach my $server (keys(%servers)) {
10246:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
10247:         if ($serverhome ne $server) {
10248:             $altids{$serverhome} = $server;
10249:         }
10250:     }
10251:     return %altids;
10252: }
10253: 
10254: sub count_servers {
10255:     my ($currbalancer,%servers) = @_;
10256:     my (@spares,$numspares);
10257:     foreach my $lonhost (sort(keys(%servers))) {
10258:         next if ($currbalancer eq $lonhost);
10259:         push(@spares,$lonhost);
10260:     }
10261:     if ($currbalancer) {
10262:         $numspares = scalar(@spares);
10263:     } else {
10264:         $numspares = scalar(@spares) - 1;
10265:     }
10266:     return ($numspares,@spares);
10267: }
10268: 
10269: sub lonbalance_targets_js {
10270:     my ($dom,$types,$servers,$settings) = @_;
10271:     my $select = &mt('Select');
10272:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
10273:     if (ref($servers) eq 'HASH') {
10274:         $alltargets = join("','",sort(keys(%{$servers})));
10275:         my @homedoms;
10276:         foreach my $server (sort(keys(%{$servers}))) {
10277:             if (&Apache::lonnet::host_domain($server) eq $dom) {
10278:                 push(@homedoms,'1');
10279:             } else {
10280:                 push(@homedoms,'0');
10281:             }
10282:         }
10283:         $allishome = join("','",@homedoms);
10284:     }
10285:     if (ref($types) eq 'ARRAY') {
10286:         if (@{$types} > 0) {
10287:             @alltypes = @{$types};
10288:         }
10289:     }
10290:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
10291:     $allinsttypes = join("','",@alltypes);
10292:     my (%currbalancer,%currtargets,%currrules,%existing);
10293:     if (ref($settings) eq 'HASH') {
10294:         %existing = %{$settings};
10295:     }
10296:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
10297:                               \%currtargets,\%currrules);
10298:     my $balancers = join("','",sort(keys(%currbalancer)));
10299:     return <<"END";
10300: 
10301: <script type="text/javascript">
10302: // <![CDATA[
10303: 
10304: currBalancers = new Array('$balancers');
10305: 
10306: function toggleTargets(balnum) {
10307:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10308:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
10309:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
10310:     var prevbalancer = prevhostitem.value;
10311:     var baltotal = document.getElementById('loadbalancing_total').value;
10312:     prevhostitem.value = balancer;
10313:     if (prevbalancer != '') {
10314:         var prevIdx = currBalancers.indexOf(prevbalancer);
10315:         if (prevIdx != -1) {
10316:             currBalancers.splice(prevIdx,1);
10317:         }
10318:     }
10319:     if (balancer == '') {
10320:         hideSpares(balnum);
10321:     } else {
10322:         var currIdx = currBalancers.indexOf(balancer);
10323:         if (currIdx == -1) {
10324:             currBalancers.push(balancer);
10325:         }
10326:         var homedoms = new Array('$allishome');
10327:         var ishomedom = homedoms[lonhostitem.selectedIndex];
10328:         showSpares(balancer,ishomedom,balnum);
10329:     }
10330:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
10331:     return;
10332: }
10333: 
10334: function showSpares(balancer,ishomedom,balnum) {
10335:     var alltargets = new Array('$alltargets');
10336:     var insttypes = new Array('$allinsttypes');
10337:     var offloadtypes = new Array('primary','default');
10338: 
10339:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
10340:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
10341:  
10342:     for (var i=0; i<offloadtypes.length; i++) {
10343:         var count = 0;
10344:         for (var j=0; j<alltargets.length; j++) {
10345:             if (alltargets[j] != balancer) {
10346:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
10347:                 item.value = alltargets[j];
10348:                 item.style.textAlign='left';
10349:                 item.style.textFace='normal';
10350:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
10351:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
10352:                     item.disabled = '';
10353:                 } else {
10354:                     item.disabled = 'disabled';
10355:                     item.checked = false;
10356:                 }
10357:                 count ++;
10358:             }
10359:         }
10360:     }
10361:     for (var k=0; k<insttypes.length; k++) {
10362:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
10363:             if (ishomedom == 1) {
10364:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
10365:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
10366:             } else {
10367:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
10368:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
10369:             }
10370:         } else {
10371:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
10372:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
10373:         }
10374:         if ((insttypes[k] != '_LC_external') && 
10375:             ((insttypes[k] != '_LC_internetdom') ||
10376:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
10377:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
10378:             item.options.length = 0;
10379:             item.options[0] = new Option("","",true,true);
10380:             var idx = 0;
10381:             for (var m=0; m<alltargets.length; m++) {
10382:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
10383:                     idx ++;
10384:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10385:                 }
10386:             }
10387:         }
10388:     }
10389:     return;
10390: }
10391: 
10392: function hideSpares(balnum) {
10393:     var alltargets = new Array('$alltargets');
10394:     var insttypes = new Array('$allinsttypes');
10395:     var offloadtypes = new Array('primary','default');
10396: 
10397:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
10398:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
10399: 
10400:     var total = alltargets.length - 1;
10401:     for (var i=0; i<offloadtypes; i++) {
10402:         for (var j=0; j<total; j++) {
10403:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
10404:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
10405:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
10406:         }
10407:     }
10408:     for (var k=0; k<insttypes.length; k++) {
10409:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
10410:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
10411:         if (insttypes[k] != '_LC_external') {
10412:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
10413:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
10414:         }
10415:     }
10416:     return;
10417: }
10418: 
10419: function checkOffloads(item,balnum,type) {
10420:     var alltargets = new Array('$alltargets');
10421:     var offloadtypes = new Array('primary','default');
10422:     if (item.checked) {
10423:         var total = alltargets.length - 1;
10424:         var other;
10425:         if (type == offloadtypes[0]) {
10426:             other = offloadtypes[1];
10427:         } else {
10428:             other = offloadtypes[0];
10429:         }
10430:         for (var i=0; i<total; i++) {
10431:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
10432:             if (server == item.value) {
10433:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
10434:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
10435:                 }
10436:             }
10437:         }
10438:     }
10439:     return;
10440: }
10441: 
10442: function singleServerToggle(balnum,type) {
10443:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
10444:     if (offloadtoSelIdx == 0) {
10445:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
10446:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
10447: 
10448:     } else {
10449:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
10450:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
10451:     }
10452:     return;
10453: }
10454: 
10455: function balanceruleChange(formname,balnum,type) {
10456:     if (type == '_LC_external') {
10457:         return;
10458:     }
10459:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
10460:     for (var i=0; i<typesRules.length; i++) {
10461:         if (formname.elements[typesRules[i]].checked) {
10462:             if (formname.elements[typesRules[i]].value != 'specific') {
10463:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
10464:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
10465:             } else {
10466:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
10467:             }
10468:         }
10469:     }
10470:     return;
10471: }
10472: 
10473: function balancerDeleteChange(balnum) {
10474:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10475:     var baltotal = document.getElementById('loadbalancing_total').value;
10476:     var addtarget;
10477:     var removetarget;
10478:     var action = 'delete';
10479:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
10480:         var lonhost = hostitem.value;
10481:         var currIdx = currBalancers.indexOf(lonhost);
10482:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
10483:             if (currIdx != -1) {
10484:                 currBalancers.splice(currIdx,1);
10485:             }
10486:             addtarget = lonhost;
10487:         } else {
10488:             if (currIdx == -1) {
10489:                 currBalancers.push(lonhost);
10490:             }
10491:             removetarget = lonhost;
10492:             action = 'undelete';
10493:         }
10494:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
10495:     }
10496:     return;
10497: }
10498: 
10499: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
10500:     if (baltotal > 1) {
10501:         var offloadtypes = new Array('primary','default');
10502:         var alltargets = new Array('$alltargets');
10503:         var insttypes = new Array('$allinsttypes');
10504:         for (var i=0; i<baltotal; i++) {
10505:             if (i != balnum) {
10506:                 for (var j=0; j<offloadtypes.length; j++) {
10507:                     var total = alltargets.length - 1;
10508:                     for (var k=0; k<total; k++) {
10509:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
10510:                         var server = serveritem.value;
10511:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
10512:                             if (server == addtarget) {
10513:                                 serveritem.disabled = '';
10514:                             }
10515:                         }
10516:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10517:                             if (server == removetarget) {
10518:                                 serveritem.disabled = 'disabled';
10519:                                 serveritem.checked = false;
10520:                             }
10521:                         }
10522:                     }
10523:                 }
10524:                 for (var j=0; j<insttypes.length; j++) {
10525:                     if (insttypes[j] != '_LC_external') {
10526:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
10527:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
10528:                             var currSel = singleserver.selectedIndex;
10529:                             var currVal = singleserver.options[currSel].value;
10530:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
10531:                                 var numoptions = singleserver.options.length;
10532:                                 var needsnew = 1;
10533:                                 for (var k=0; k<numoptions; k++) {
10534:                                     if (singleserver.options[k] == addtarget) {
10535:                                         needsnew = 0;
10536:                                         break;
10537:                                     }
10538:                                 }
10539:                                 if (needsnew == 1) {
10540:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
10541:                                 }
10542:                             }
10543:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10544:                                 singleserver.options.length = 0;
10545:                                 if ((currVal) && (currVal != removetarget)) {
10546:                                     singleserver.options[0] = new Option("","",false,false);
10547:                                 } else {
10548:                                     singleserver.options[0] = new Option("","",true,true);
10549:                                 }
10550:                                 var idx = 0;
10551:                                 for (var m=0; m<alltargets.length; m++) {
10552:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
10553:                                         idx ++;
10554:                                         if (currVal == alltargets[m]) {
10555:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
10556:                                         } else {
10557:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10558:                                         }
10559:                                     }
10560:                                 }
10561:                             }
10562:                         }
10563:                     }
10564:                 }
10565:             }
10566:         }
10567:     }
10568:     return;
10569: }
10570: 
10571: // ]]>
10572: </script>
10573: 
10574: END
10575: }
10576: 
10577: sub new_spares_js {
10578:     my @sparestypes = ('primary','default');
10579:     my $types = join("','",@sparestypes);
10580:     my $select = &mt('Select');
10581:     return <<"END";
10582: 
10583: <script type="text/javascript">
10584: // <![CDATA[
10585: 
10586: function updateNewSpares(formname,lonhost) {
10587:     var types = new Array('$types');
10588:     var include = new Array();
10589:     var exclude = new Array();
10590:     for (var i=0; i<types.length; i++) {
10591:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
10592:         for (var j=0; j<spareboxes.length; j++) {
10593:             if (formname.elements[spareboxes[j]].checked) {
10594:                 exclude.push(formname.elements[spareboxes[j]].value);
10595:             } else {
10596:                 include.push(formname.elements[spareboxes[j]].value);
10597:             }
10598:         }
10599:     }
10600:     for (var i=0; i<types.length; i++) {
10601:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
10602:         var selIdx = newSpare.selectedIndex;
10603:         var currnew = newSpare.options[selIdx].value;
10604:         var okSpares = new Array();
10605:         for (var j=0; j<newSpare.options.length; j++) {
10606:             var possible = newSpare.options[j].value;
10607:             if (possible != '') {
10608:                 if (exclude.indexOf(possible) == -1) {
10609:                     okSpares.push(possible);
10610:                 } else {
10611:                     if (currnew == possible) {
10612:                         selIdx = 0;
10613:                     }
10614:                 }
10615:             }
10616:         }
10617:         for (var k=0; k<include.length; k++) {
10618:             if (okSpares.indexOf(include[k]) == -1) {
10619:                 okSpares.push(include[k]);
10620:             }
10621:         }
10622:         okSpares.sort();
10623:         newSpare.options.length = 0;
10624:         if (selIdx == 0) {
10625:             newSpare.options[0] = new Option("$select","",true,true);
10626:         } else {
10627:             newSpare.options[0] = new Option("$select","",false,false);
10628:         }
10629:         for (var m=0; m<okSpares.length; m++) {
10630:             var idx = m+1;
10631:             var selThis = 0;
10632:             if (selIdx != 0) {
10633:                 if (okSpares[m] == currnew) {
10634:                     selThis = 1;
10635:                 }
10636:             }
10637:             if (selThis == 1) {
10638:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
10639:             } else {
10640:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
10641:             }
10642:         }
10643:     }
10644:     return;
10645: }
10646: 
10647: function checkNewSpares(lonhost,type) {
10648:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
10649:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
10650:     if (chosen != '') { 
10651:         var othertype;
10652:         var othernewSpare;
10653:         if (type == 'primary') {
10654:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
10655:         }
10656:         if (type == 'default') {
10657:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
10658:         }
10659:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
10660:             othernewSpare.selectedIndex = 0;
10661:         }
10662:     }
10663:     return;
10664: }
10665: 
10666: // ]]>
10667: </script>
10668: 
10669: END
10670: 
10671: }
10672: 
10673: sub common_domprefs_js {
10674:     return <<"END";
10675: 
10676: <script type="text/javascript">
10677: // <![CDATA[
10678: 
10679: function getIndicesByName(formname,item) {
10680:     var group = new Array();
10681:     for (var i=0;i<formname.elements.length;i++) {
10682:         if (formname.elements[i].name == item) {
10683:             group.push(formname.elements[i].id);
10684:         }
10685:     }
10686:     return group;
10687: }
10688: 
10689: // ]]>
10690: </script>
10691: 
10692: END
10693: 
10694: }
10695: 
10696: sub recaptcha_js {
10697:     my %lt = &captcha_phrases();
10698:     return <<"END";
10699: 
10700: <script type="text/javascript">
10701: // <![CDATA[
10702: 
10703: function updateCaptcha(caller,context) {
10704:     var privitem;
10705:     var pubitem;
10706:     var privtext;
10707:     var pubtext;
10708:     if (document.getElementById(context+'_recaptchapub')) {
10709:         pubitem = document.getElementById(context+'_recaptchapub');
10710:     } else {
10711:         return;
10712:     }
10713:     if (document.getElementById(context+'_recaptchapriv')) {
10714:         privitem = document.getElementById(context+'_recaptchapriv');
10715:     } else {
10716:         return;
10717:     }
10718:     if (document.getElementById(context+'_recaptchapubtxt')) {
10719:         pubtext = document.getElementById(context+'_recaptchapubtxt');
10720:     } else {
10721:         return;
10722:     }
10723:     if (document.getElementById(context+'_recaptchaprivtxt')) {
10724:         privtext = document.getElementById(context+'_recaptchaprivtxt');
10725:     } else {
10726:         return;
10727:     }
10728:     if (caller.checked) {
10729:         if (caller.value == 'recaptcha') {
10730:             pubitem.type = 'text';
10731:             privitem.type = 'text';
10732:             pubitem.size = '40';
10733:             privitem.size = '40';
10734:             pubtext.innerHTML = "$lt{'pub'}";
10735:             privtext.innerHTML = "$lt{'priv'}";
10736:         } else {
10737:             pubitem.type = 'hidden';
10738:             privitem.type = 'hidden';
10739:             pubtext.innerHTML = '';
10740:             privtext.innerHTML = '';
10741:         }
10742:     }
10743:     return;
10744: }
10745: 
10746: // ]]>
10747: </script>
10748: 
10749: END
10750: 
10751: }
10752: 
10753: sub credits_js {
10754:     return <<"END";
10755: 
10756: <script type="text/javascript">
10757: // <![CDATA[
10758: 
10759: function toggleCredits(domForm) {
10760:     if (document.getElementById('credits')) {
10761:         creditsitem = document.getElementById('credits');
10762:         var creditsLength = domForm.coursecredits.length;
10763:         if (creditsLength) {
10764:             var currval;
10765:             for (var i=0; i<creditsLength; i++) {
10766:                 if (domForm.coursecredits[i].checked) {
10767:                    currval = domForm.coursecredits[i].value;
10768:                 }
10769:             }
10770:             if (currval == 1) {
10771:                 creditsitem.style.display = 'block';
10772:             } else {
10773:                 creditsitem.style.display = 'none';
10774:             }
10775:         }
10776:     }
10777:     return;
10778: }
10779: 
10780: // ]]>
10781: </script>
10782: 
10783: END
10784: 
10785: }
10786: 
10787: sub captcha_phrases {
10788:     return &Apache::lonlocal::texthash (
10789:                  priv => 'Private key',
10790:                  pub  => 'Public key',
10791:                  original  => 'original (CAPTCHA)',
10792:                  recaptcha => 'successor (ReCAPTCHA)',
10793:                  notused   => 'unused',
10794:     );
10795: }
10796: 
10797: sub devalidate_remote_domconfs {
10798:     my ($dom,$cachekeys) = @_;
10799:     return unless (ref($cachekeys) eq 'HASH');
10800:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10801:     my %thismachine;
10802:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
10803:     my @posscached = ('domainconfig','domdefaults');
10804:     if (keys(%servers) > 1) {
10805:         foreach my $server (keys(%servers)) {
10806:             next if ($thismachine{$server});
10807:             my @cached;
10808:             foreach my $name (@posscached) {
10809:                 if ($cachekeys->{$name}) {
10810:                     push(@cached,&escape($name).':'.&escape($dom));
10811:                 }
10812:             }
10813:             if (@cached) {
10814:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
10815:             }
10816:         }
10817:     }
10818:     return;
10819: }
10820: 
10821: 1;

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