File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.104: download - view: text, annotated - select for diffs
Sun Feb 9 05:18:20 2020 UTC (4 years, 3 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  Backport 1.369, 1.370.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.104 2020/02/09 05:18:20 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: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ##############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, and textbook).  In each case the radio buttons 
  108: allow the selection of one of four values:
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use File::Copy;
  174: use Locale::Language;
  175: use DateTime::TimeZone;
  176: use DateTime::Locale;
  177: 
  178: my $registered_cleanup;
  179: my $modified_urls;
  180: 
  181: sub handler {
  182:     my $r=shift;
  183:     if ($r->header_only) {
  184:         &Apache::loncommon::content_type($r,'text/html');
  185:         $r->send_http_header;
  186:         return OK;
  187:     }
  188: 
  189:     my $context = 'domain';
  190:     my $dom = $env{'request.role.domain'};
  191:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  192:     if (&Apache::lonnet::allowed('mau',$dom)) {
  193:         &Apache::loncommon::content_type($r,'text/html');
  194:         $r->send_http_header;
  195:     } else {
  196:         $env{'user.error.msg'}=
  197:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  198:         return HTTP_NOT_ACCEPTABLE;
  199:     }
  200: 
  201:     $registered_cleanup=0;
  202:     @{$modified_urls}=();
  203: 
  204:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  205:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  206:                                             ['phase','actions']);
  207:     my $phase = 'pickactions';
  208:     if ( exists($env{'form.phase'}) ) {
  209:         $phase = $env{'form.phase'};
  210:     }
  211:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  212:     my %domconfig =
  213:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  214:                 'quotas','autoenroll','autoupdate','autocreate',
  215:                 'directorysrch','usercreation','usermodification',
  216:                 'contacts','defaults','scantron','coursecategories',
  217:                 'serverstatuses','requestcourses','helpsettings',
  218:                 'coursedefaults','usersessions','loadbalancing',
  219:                 'requestauthor','selfenrollment','inststatus','passwords'],$dom);
  220:     my @prefs_order = ('rolecolors','login','defaults','passwords','quotas','autoenroll',
  221:                        'autoupdate','autocreate','directorysrch','contacts',
  222:                        'usercreation','selfcreation','usermodification','scantron',
  223:                        'requestcourses','requestauthor','coursecategories',
  224:                        'serverstatuses','helpsettings','coursedefaults',
  225:                        'selfenrollment','usersessions');
  226:     my %existing;
  227:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  228:         %existing = %{$domconfig{'loadbalancing'}};
  229:     }
  230:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  231:         push(@prefs_order,'loadbalancing');
  232:     }
  233:     my %prefs = (
  234:         'rolecolors' =>
  235:                    { text => 'Default color schemes',
  236:                      help => 'Domain_Configuration_Color_Schemes',
  237:                      header => [{col1 => 'Student Settings',
  238:                                  col2 => '',},
  239:                                 {col1 => 'Coordinator Settings',
  240:                                  col2 => '',},
  241:                                 {col1 => 'Author Settings',
  242:                                  col2 => '',},
  243:                                 {col1 => 'Administrator Settings',
  244:                                  col2 => '',}],
  245:                       print => \&print_rolecolors,
  246:                       modify => \&modify_rolecolors,
  247:                     },
  248:         'login' =>
  249:                     { text => 'Log-in page options',
  250:                       help => 'Domain_Configuration_Login_Page',
  251:                       header => [{col1 => 'Log-in Page Items',
  252:                                   col2 => '',},
  253:                                  {col1 => 'Log-in Help',
  254:                                   col2 => 'Value'},
  255:                                  {col1 => 'Custom HTML in document head',
  256:                                   col2 => 'Value'}],
  257:                       print => \&print_login,
  258:                       modify => \&modify_login,
  259:                     },
  260:         'defaults' => 
  261:                     { text => 'Default authentication/language/timezone/portal/types',
  262:                       help => 'Domain_Configuration_LangTZAuth',
  263:                       header => [{col1 => 'Setting',
  264:                                   col2 => 'Value'},
  265:                                  {col1 => 'Institutional user types',
  266:                                   col2 => 'Name displayed'}],
  267:                       print => \&print_defaults,
  268:                       modify => \&modify_defaults,
  269:                     },
  270:         'passwords' =>
  271:                     { text => 'Passwords (Internal authentication)',
  272:                       help => 'Domain_Configuration_Passwords',
  273:                       header => [{col1 => 'Resetting Forgotten Password',
  274:                                   col2 => 'Settings'},
  275:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  276:                                   col2 => 'Settings'},
  277:                                  {col1 => 'Rules for LON-CAPA Passwords',
  278:                                   col2 => 'Settings'},
  279:                                  {col1 => 'Course Owner Changing Student Passwords',
  280:                                   col2 => 'Settings'}],
  281:                       print => \&print_passwords,
  282:                       modify => \&modify_passwords,
  283:                     },
  284:         'quotas' => 
  285:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  286:                       help => 'Domain_Configuration_Quotas',
  287:                       header => [{col1 => 'User affiliation',
  288:                                   col2 => 'Available tools',
  289:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  290:                       print => \&print_quotas,
  291:                       modify => \&modify_quotas,
  292:                     },
  293:         'autoenroll' =>
  294:                    { text => 'Auto-enrollment settings',
  295:                      help => 'Domain_Configuration_Auto_Enrollment',
  296:                      header => [{col1 => 'Configuration setting',
  297:                                  col2 => 'Value(s)'}],
  298:                      print => \&print_autoenroll,
  299:                      modify => \&modify_autoenroll,
  300:                    },
  301:         'autoupdate' => 
  302:                    { text => 'Auto-update settings',
  303:                      help => 'Domain_Configuration_Auto_Updates',
  304:                      header => [{col1 => 'Setting',
  305:                                  col2 => 'Value',},
  306:                                 {col1 => 'Setting',
  307:                                  col2 => 'Affiliation'},
  308:                                 {col1 => 'User population',
  309:                                  col2 => 'Updatable user data'}],
  310:                      print => \&print_autoupdate,
  311:                      modify => \&modify_autoupdate,
  312:                   },
  313:         'autocreate' => 
  314:                   { text => 'Auto-course creation settings',
  315:                      help => 'Domain_Configuration_Auto_Creation',
  316:                      header => [{col1 => 'Configuration Setting',
  317:                                  col2 => 'Value',}],
  318:                      print => \&print_autocreate,
  319:                      modify => \&modify_autocreate,
  320:                   },
  321:         'directorysrch' => 
  322:                   { text => 'Directory searches',
  323:                     help => 'Domain_Configuration_InstDirectory_Search',
  324:                     header => [{col1 => 'Institutional Directory Setting',
  325:                                 col2 => 'Value',},
  326:                                {col1 => 'LON-CAPA Directory Setting',
  327:                                 col2 => 'Value',}],
  328:                     print => \&print_directorysrch,
  329:                     modify => \&modify_directorysrch,
  330:                   },
  331:         'contacts' =>
  332:                   { text => 'E-mail addresses and helpform',
  333:                     help => 'Domain_Configuration_Contact_Info',
  334:                     header => [{col1 => 'Default e-mail addresses',
  335:                                 col2 => 'Value',},
  336:                                {col1 => 'Recipient(s) for notifications',
  337:                                 col2 => 'Value',},
  338:                                {col1 => 'Ask helpdesk form settings',
  339:                                 col2 => 'Value',},],
  340:                     print => \&print_contacts,
  341:                     modify => \&modify_contacts,
  342:                   },
  343:         'usercreation' => 
  344:                   { text => 'User creation',
  345:                     help => 'Domain_Configuration_User_Creation',
  346:                     header => [{col1 => 'Format rule type',
  347:                                 col2 => 'Format rules in force'},
  348:                                {col1 => 'User account creation',
  349:                                 col2 => 'Usernames which may be created',},
  350:                                {col1 => 'Context',
  351:                                 col2 => 'Assignable authentication types'}],
  352:                     print => \&print_usercreation,
  353:                     modify => \&modify_usercreation,
  354:                   },
  355:         'selfcreation' => 
  356:                   { text => 'Users self-creating accounts',
  357:                     help => 'Domain_Configuration_Self_Creation', 
  358:                     header => [{col1 => 'Self-creation with institutional username',
  359:                                 col2 => 'Enabled?'},
  360:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  361:                                 col2 => 'Information user can enter'},
  362:                                {col1 => 'Self-creation with e-mail verification',
  363:                                 col2 => 'Settings'}],
  364:                     print => \&print_selfcreation,
  365:                     modify => \&modify_selfcreation,
  366:                   },
  367:         'usermodification' =>
  368:                   { text => 'User modification',
  369:                     help => 'Domain_Configuration_User_Modification',
  370:                     header => [{col1 => 'Target user has role',
  371:                                 col2 => 'User information updatable in author context'},
  372:                                {col1 => 'Target user has role',
  373:                                 col2 => 'User information updatable in course context'}],
  374:                     print => \&print_usermodification,
  375:                     modify => \&modify_usermodification,
  376:                   },
  377:         'scantron' =>
  378:                   { text => 'Bubblesheet format',
  379:                     help => 'Domain_Configuration_Scantron_Format',
  380:                     header => [ {col1 => 'Bubblesheet format file',
  381:                                  col2 => ''},
  382:                                 {col1 => 'Bubblesheet data upload formats',
  383:                                  col2 => 'Settings'}],
  384:                     print => \&print_scantron,
  385:                     modify => \&modify_scantron,
  386:                   },
  387:         'requestcourses' => 
  388:                  {text => 'Request creation of courses',
  389:                   help => 'Domain_Configuration_Request_Courses',
  390:                   header => [{col1 => 'User affiliation',
  391:                               col2 => 'Availability/Processing of requests',},
  392:                              {col1 => 'Setting',
  393:                               col2 => 'Value'},
  394:                              {col1 => 'Available textbooks',
  395:                               col2 => ''},
  396:                              {col1 => 'Available templates',
  397:                               col2 => ''},
  398:                              {col1 => 'Validation (not official courses)',
  399:                               col2 => 'Value'},],
  400:                   print => \&print_quotas,
  401:                   modify => \&modify_quotas,
  402:                  },
  403:         'requestauthor' =>
  404:                  {text => 'Request Authoring Space',
  405:                   help => 'Domain_Configuration_Request_Author',
  406:                   header => [{col1 => 'User affiliation',
  407:                               col2 => 'Availability/Processing of requests',},
  408:                              {col1 => 'Setting',
  409:                               col2 => 'Value'}],
  410:                   print => \&print_quotas,
  411:                   modify => \&modify_quotas,
  412:                  },
  413:         'coursecategories' =>
  414:                   { text => 'Cataloging of courses/communities',
  415:                     help => 'Domain_Configuration_Cataloging_Courses',
  416:                     header => [{col1 => 'Catalog type/availability',
  417:                                 col2 => '',},
  418:                                {col1 => 'Category settings for standard catalog',
  419:                                 col2 => '',},
  420:                                {col1 => 'Categories',
  421:                                 col2 => '',
  422:                                }],
  423:                     print => \&print_coursecategories,
  424:                     modify => \&modify_coursecategories,
  425:                   },
  426:         'serverstatuses' =>
  427:                  {text   => 'Access to server status pages',
  428:                   help   => 'Domain_Configuration_Server_Status',
  429:                   header => [{col1 => 'Status Page',
  430:                               col2 => 'Other named users',
  431:                               col3 => 'Specific IPs',
  432:                             }],
  433:                   print => \&print_serverstatuses,
  434:                   modify => \&modify_serverstatuses,
  435:                  },
  436:         'helpsettings' =>
  437:                  {text   => 'Support settings',
  438:                   help   => 'Domain_Configuration_Help_Settings',
  439:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  440:                               col2 => 'Value'},
  441:                              {col1 => 'Helpdesk Roles',
  442:                               col2 => 'Settings'},],
  443:                   print  => \&print_helpsettings,
  444:                   modify => \&modify_helpsettings,
  445:                  },
  446:         'coursedefaults' => 
  447:                  {text => 'Course/Community defaults',
  448:                   help => 'Domain_Configuration_Course_Defaults',
  449:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  450:                               col2 => 'Value',},
  451:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  452:                               col2 => 'Value',},],
  453:                   print => \&print_coursedefaults,
  454:                   modify => \&modify_coursedefaults,
  455:                  },
  456:         'selfenrollment' => 
  457:                  {text   => 'Self-enrollment in Course/Community',
  458:                   help   => 'Domain_Configuration_Selfenrollment',
  459:                   header => [{col1 => 'Configuration Rights',
  460:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  461:                              {col1 => 'Defaults',
  462:                               col2 => 'Value'},
  463:                              {col1 => 'Self-enrollment validation (optional)',
  464:                               col2 => 'Value'},],
  465:                   print => \&print_selfenrollment,
  466:                   modify => \&modify_selfenrollment,
  467:                  },
  468:         'usersessions' =>
  469:                  {text  => 'User session hosting/offloading',
  470:                   help  => 'Domain_Configuration_User_Sessions',
  471:                   header => [{col1 => 'Domain server',
  472:                               col2 => 'Servers to offload sessions to when busy'},
  473:                              {col1 => 'Hosting of users from other domains',
  474:                               col2 => 'Rules'},
  475:                              {col1 => "Hosting domain's own users elsewhere",
  476:                               col2 => 'Rules'}],
  477:                   print => \&print_usersessions,
  478:                   modify => \&modify_usersessions,
  479:                  },
  480:         'loadbalancing' =>
  481:                  {text  => 'Dedicated Load Balancer(s)',
  482:                   help  => 'Domain_Configuration_Load_Balancing',
  483:                   header => [{col1 => 'Balancers',
  484:                               col2 => 'Default destinations',
  485:                               col3 => 'User affiliation',
  486:                               col4 => 'Overrides'},
  487:                             ],
  488:                   print => \&print_loadbalancing,
  489:                   modify => \&modify_loadbalancing,
  490:                  },
  491:     );
  492:     if (keys(%servers) > 1) {
  493:         $prefs{'login'}  = { text   => 'Log-in page options',
  494:                              help   => 'Domain_Configuration_Login_Page',
  495:                             header => [{col1 => 'Log-in Service',
  496:                                         col2 => 'Server Setting',},
  497:                                        {col1 => 'Log-in Page Items',
  498:                                         col2 => ''},
  499:                                        {col1 => 'Log-in Help',
  500:                                         col2 => 'Value'},
  501:                                        {col1 => 'Custom HTML in document head',
  502:                                         col2 => 'Value'}],
  503:                             print => \&print_login,
  504:                             modify => \&modify_login,
  505:                            };
  506:     }
  507: 
  508:     my @roles = ('student','coordinator','author','admin');
  509:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  510:     &Apache::lonhtmlcommon::add_breadcrumb
  511:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  512:       text=>"Settings to display/modify"});
  513:     my $confname = $dom.'-domainconfig';
  514: 
  515:     if ($phase eq 'process') {
  516:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  517:                                                               \%prefs,\%domconfig,$confname,\@roles);
  518:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  519:             $r->rflush();
  520:             &devalidate_remote_domconfs($dom,$result);
  521:         }
  522:     } elsif ($phase eq 'display') {
  523:         my $js = &recaptcha_js().
  524:                  &toggle_display_js();
  525:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  526:             my ($othertitle,$usertypes,$types) =
  527:                 &Apache::loncommon::sorted_inst_types($dom);
  528:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  529:                                           $domconfig{'loadbalancing'}).
  530:                    &new_spares_js().
  531:                    &common_domprefs_js().
  532:                    &Apache::loncommon::javascript_array_indexof();
  533:         }
  534:         if (grep(/^requestcourses$/,@actions)) {
  535:             my $javascript_validations;
  536:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  537:             $js .= <<END;
  538: <script type="text/javascript">
  539: $javascript_validations
  540: </script>
  541: $coursebrowserjs
  542: END
  543:         }
  544:         if (grep(/^selfcreation$/,@actions)) {
  545:             $js .= &selfcreate_javascript();
  546:         }
  547:         if (grep(/^contacts$/,@actions)) {
  548:             $js .= &contacts_javascript();
  549:         }
  550:         if (grep(/^scantron$/,@actions)) {
  551:             $js .= &scantron_javascript();
  552:         }
  553:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  554:     } else {
  555: # check if domconfig user exists for the domain.
  556:         my $servadm = $r->dir_config('lonAdmEMail');
  557:         my ($configuserok,$author_ok,$switchserver) =
  558:             &config_check($dom,$confname,$servadm);
  559:         unless ($configuserok eq 'ok') {
  560:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  561:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  562:                           $confname).
  563:                       '<br />'
  564:             );
  565:             if ($switchserver) {
  566:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  567:                           '<br />'.
  568:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  569:                           '<br />'.
  570:                           &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).
  571:                           '<br />'.
  572:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  573:                 );
  574:             } else {
  575:                 $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.').
  576:                           '<br />'.
  577:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  578:                 );
  579:             }
  580:             $r->print(&Apache::loncommon::end_page());
  581:             return OK;
  582:         }
  583:         if (keys(%domconfig) == 0) {
  584:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  585:             my @ids=&Apache::lonnet::current_machine_ids();
  586:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  587:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  588:                 my @loginimages = ('img','logo','domlogo','login');
  589:                 my $custom_img_count = 0;
  590:                 foreach my $img (@loginimages) {
  591:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  592:                         $custom_img_count ++;
  593:                     }
  594:                 }
  595:                 foreach my $role (@roles) {
  596:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  597:                         $custom_img_count ++;
  598:                     }
  599:                 }
  600:                 if ($custom_img_count > 0) {
  601:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  602:                     my $switch_server = &check_switchserver($dom,$confname);
  603:                     $r->print(
  604:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  605:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  606:     &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 />'.
  607:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  608:                     if ($switch_server) {
  609:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  610:                     }
  611:                     $r->print(&Apache::loncommon::end_page());
  612:                     return OK;
  613:                 }
  614:             }
  615:         }
  616:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  617:     }
  618:     return OK;
  619: }
  620: 
  621: sub process_changes {
  622:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  623:     my %domconfig;
  624:     if (ref($values) eq 'HASH') {
  625:         %domconfig = %{$values};
  626:     }
  627:     my $output;
  628:     if ($action eq 'login') {
  629:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  630:     } elsif ($action eq 'rolecolors') {
  631:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  632:                                      $lastactref,%domconfig);
  633:     } elsif ($action eq 'quotas') {
  634:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  635:     } elsif ($action eq 'autoenroll') {
  636:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  637:     } elsif ($action eq 'autoupdate') {
  638:         $output = &modify_autoupdate($dom,%domconfig);
  639:     } elsif ($action eq 'autocreate') {
  640:         $output = &modify_autocreate($dom,%domconfig);
  641:     } elsif ($action eq 'directorysrch') {
  642:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  643:     } elsif ($action eq 'usercreation') {
  644:         $output = &modify_usercreation($dom,%domconfig);
  645:     } elsif ($action eq 'selfcreation') {
  646:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  647:     } elsif ($action eq 'usermodification') {
  648:         $output = &modify_usermodification($dom,%domconfig);
  649:     } elsif ($action eq 'contacts') {
  650:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  651:     } elsif ($action eq 'defaults') {
  652:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  653:     } elsif ($action eq 'scantron') {
  654:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  655:     } elsif ($action eq 'coursecategories') {
  656:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  657:     } elsif ($action eq 'serverstatuses') {
  658:         $output = &modify_serverstatuses($dom,%domconfig);
  659:     } elsif ($action eq 'requestcourses') {
  660:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  661:     } elsif ($action eq 'requestauthor') {
  662:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  663:     } elsif ($action eq 'helpsettings') {
  664:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  665:     } elsif ($action eq 'coursedefaults') {
  666:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  667:     } elsif ($action eq 'selfenrollment') {
  668:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  669:     } elsif ($action eq 'usersessions') {
  670:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  671:     } elsif ($action eq 'loadbalancing') {
  672:         $output = &modify_loadbalancing($dom,%domconfig);
  673:     } elsif ($action eq 'passwords') {
  674:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  675:     }
  676:     return $output;
  677: }
  678: 
  679: sub print_config_box {
  680:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  681:     my $rowtotal = 0;
  682:     my $output;
  683:     if ($action eq 'coursecategories') {
  684:         $output = &coursecategories_javascript($settings);
  685:     } elsif ($action eq 'defaults') {
  686:         $output = &defaults_javascript($settings); 
  687:     } elsif ($action eq 'passwords') {
  688:         $output = &passwords_javascript();
  689:     } elsif ($action eq 'helpsettings') {
  690:         my (%privs,%levelscurrent);
  691:         my %full=();
  692:         my %levels=(
  693:                      course => {},
  694:                      domain => {},
  695:                      system => {},
  696:                    );
  697:         my $context = 'domain';
  698:         my $crstype = 'Course';
  699:         my $formname = 'display';
  700:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  701:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  702:         $output =
  703:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  704:                                                       \@templateroles);
  705:     }
  706:     $output .=
  707:          '<table class="LC_nested_outer">
  708:           <tr>
  709:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  710:            &mt($item->{text}).'&nbsp;'.
  711:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  712:           '</tr>';
  713:     $rowtotal ++;
  714:     my $numheaders = 1;
  715:     if (ref($item->{'header'}) eq 'ARRAY') {
  716:         $numheaders = scalar(@{$item->{'header'}});
  717:     }
  718:     if ($numheaders > 1) {
  719:         my $colspan = '';
  720:         my $rightcolspan = '';
  721:         my $leftnobr = '';  
  722:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  723:             ($action eq 'directorysrch') ||
  724:             (($action eq 'login') && ($numheaders < 4))) {
  725:             $colspan = ' colspan="2"';
  726:         }
  727:         if ($action eq 'usersessions') {
  728:             $rightcolspan = ' colspan="3"'; 
  729:         }
  730:         if ($action eq 'passwords') {
  731:             $leftnobr = ' LC_nobreak';
  732:         }
  733:         $output .= '
  734:           <tr>
  735:            <td>
  736:             <table class="LC_nested">
  737:              <tr class="LC_info_row">
  738:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  739:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  740:              </tr>';
  741:         $rowtotal ++;
  742:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  743:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  744:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  745:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  746:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  747:         } elsif ($action eq 'passwords') {
  748:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  749:         } elsif ($action eq 'coursecategories') {
  750:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  751:         } elsif ($action eq 'scantron') {
  752:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  753:         } elsif ($action eq 'login') {
  754:             if ($numheaders == 4) {
  755:                 $colspan = ' colspan="2"';
  756:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  757:             } else {
  758:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  759:             }
  760:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  761:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  762:         } elsif ($action eq 'rolecolors') {
  763:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  764:         }
  765:         $output .= '
  766:            </table>
  767:           </td>
  768:          </tr>
  769:          <tr>
  770:            <td>
  771:             <table class="LC_nested">
  772:              <tr class="LC_info_row">
  773:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  774:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  775:              </tr>';
  776:             $rowtotal ++;
  777:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  778:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  779:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  780:             ($action eq 'contacts') || ($action eq 'passwords')) {
  781:             if ($action eq 'coursecategories') {
  782:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  783:                 $colspan = ' colspan="2"';
  784:             } elsif ($action eq 'passwords') {
  785:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  786:             } else {
  787:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  788:             }
  789:             $output .= '
  790:            </table>
  791:           </td>
  792:          </tr>
  793:          <tr>
  794:            <td>
  795:             <table class="LC_nested">
  796:              <tr class="LC_info_row">
  797:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  798:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  799:              </tr>'."\n";
  800:             if ($action eq 'coursecategories') {
  801:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  802:             } elsif ($action eq 'passwords') {
  803:                 $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal).'
  804:              </tr>
  805:             </table>
  806:            </td>
  807:           </tr>
  808:           <tr>
  809:            <td>
  810:             <table class="LC_nested">
  811:              <tr class="LC_info_row">
  812:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  813:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n".
  814:               $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal).'
  815:             </table>
  816:           </td>
  817:          </tr>
  818:          <tr>';
  819:             } else {
  820:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  821:             }
  822:             $rowtotal ++;
  823:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  824:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  825:                  ($action eq 'helpsettings')) {
  826:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  827:         } elsif ($action eq 'scantron') {
  828:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
  829:         } elsif ($action eq 'login') {
  830:             if ($numheaders == 4) {
  831:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  832:            </table>
  833:           </td>
  834:          </tr>
  835:          <tr>
  836:            <td>
  837:             <table class="LC_nested">
  838:              <tr class="LC_info_row">
  839:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  840:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  841:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  842:                 $rowtotal ++;
  843:             } else {
  844:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  845:             }
  846:             $output .= '
  847:            </table>
  848:           </td>
  849:          </tr>
  850:          <tr>
  851:            <td>
  852:             <table class="LC_nested">
  853:              <tr class="LC_info_row">';
  854:             if ($numheaders == 4) {
  855:                 $output .= '
  856:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  857:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  858:              </tr>';
  859:             } else {
  860:                 $output .= '
  861:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  862:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  863:              </tr>';
  864:             }
  865:             $rowtotal ++;
  866:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  867:         } elsif ($action eq 'requestcourses') {
  868:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  869:             $rowtotal ++;
  870:             $output .= &print_studentcode($settings,\$rowtotal).'
  871:            </table>
  872:           </td>
  873:          </tr>
  874:          <tr>
  875:            <td>
  876:             <table class="LC_nested">
  877:              <tr class="LC_info_row">
  878:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  879:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  880:                        &textbookcourses_javascript($settings).
  881:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  882:             </table>
  883:            </td>
  884:           </tr>
  885:          <tr>
  886:            <td>
  887:             <table class="LC_nested">
  888:              <tr class="LC_info_row">
  889:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  890:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  891:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  892:             </table>
  893:            </td>
  894:           </tr>
  895:           <tr>
  896:            <td>
  897:             <table class="LC_nested">
  898:              <tr class="LC_info_row">
  899:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  900:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  901:              </tr>'.
  902:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  903:         } elsif ($action eq 'requestauthor') {
  904:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  905:             $rowtotal ++;
  906:         } elsif ($action eq 'rolecolors') {
  907:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  908:            </table>
  909:           </td>
  910:          </tr>
  911:          <tr>
  912:            <td>
  913:             <table class="LC_nested">
  914:              <tr class="LC_info_row">
  915:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  916:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  917:               <td class="LC_right_item" valign="top">'.
  918:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  919:              </tr>'.
  920:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  921:            </table>
  922:           </td>
  923:          </tr>
  924:          <tr>
  925:            <td>
  926:             <table class="LC_nested">
  927:              <tr class="LC_info_row">
  928:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  929:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  930:              </tr>'.
  931:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  932:             $rowtotal += 2;
  933:         }
  934:     } else {
  935:         $output .= '
  936:           <tr>
  937:            <td>
  938:             <table class="LC_nested">
  939:              <tr class="LC_info_row">';
  940:         if ($action eq 'login') {
  941:             $output .= '  
  942:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  943:         } elsif ($action eq 'serverstatuses') {
  944:             $output .= '
  945:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  946:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  947: 
  948:         } else {
  949:             $output .= '
  950:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  951:         }
  952:         if (defined($item->{'header'}->[0]->{'col3'})) {
  953:             $output .= '<td class="LC_left_item" valign="top">'.
  954:                        &mt($item->{'header'}->[0]->{'col2'});
  955:             if ($action eq 'serverstatuses') {
  956:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  957:             } 
  958:         } else {
  959:             $output .= '<td class="LC_right_item" valign="top">'.
  960:                        &mt($item->{'header'}->[0]->{'col2'});
  961:         }
  962:         $output .= '</td>';
  963:         if ($item->{'header'}->[0]->{'col3'}) {
  964:             if (defined($item->{'header'}->[0]->{'col4'})) {
  965:                 $output .= '<td class="LC_left_item" valign="top">'.
  966:                             &mt($item->{'header'}->[0]->{'col3'});
  967:             } else {
  968:                 $output .= '<td class="LC_right_item" valign="top">'.
  969:                            &mt($item->{'header'}->[0]->{'col3'});
  970:             }
  971:             if ($action eq 'serverstatuses') {
  972:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  973:             }
  974:             $output .= '</td>';
  975:         }
  976:         if ($item->{'header'}->[0]->{'col4'}) {
  977:             $output .= '<td class="LC_right_item" valign="top">'.
  978:                        &mt($item->{'header'}->[0]->{'col4'});
  979:         }
  980:         $output .= '</tr>';
  981:         $rowtotal ++;
  982:         if ($action eq 'quotas') {
  983:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  984:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
  985:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  986:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  987:         }
  988:     }
  989:     $output .= '
  990:    </table>
  991:   </td>
  992:  </tr>
  993: </table><br />';
  994:     return ($output,$rowtotal);
  995: }
  996: 
  997: sub print_login {
  998:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  999:     my ($css_class,$datatable);
 1000:     my %choices = &login_choices();
 1001: 
 1002:     if ($caller eq 'service') {
 1003:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1004:         my $choice = $choices{'disallowlogin'};
 1005:         $css_class = ' class="LC_odd_row"';
 1006:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1007:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1008:                       '<th>'.$choices{'server'}.'</th>'.
 1009:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1010:                       '<th>'.$choices{'custompath'}.'</th>'.
 1011:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1012:         my %disallowed;
 1013:         if (ref($settings) eq 'HASH') {
 1014:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1015:                %disallowed = %{$settings->{'loginvia'}};
 1016:             }
 1017:         }
 1018:         foreach my $lonhost (sort(keys(%servers))) {
 1019:             my $direct = 'selected="selected"';
 1020:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1021:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1022:                     $direct = '';
 1023:                 }
 1024:             }
 1025:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1026:                           '<td><select name="'.$lonhost.'_server">'.
 1027:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1028:                           '</option>';
 1029:             foreach my $hostid (sort(keys(%servers))) {
 1030:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1031:                 my $selected = '';
 1032:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1033:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1034:                         $selected = 'selected="selected"';
 1035:                     }
 1036:                 }
 1037:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1038:                               $servers{$hostid}.'</option>';
 1039:             }
 1040:             $datatable .= '</select></td>'.
 1041:                           '<td><select name="'.$lonhost.'_serverpath">';
 1042:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1043:                 my $pathname = $path;
 1044:                 if ($path eq 'custom') {
 1045:                     $pathname = &mt('Custom Path').' ->';
 1046:                 }
 1047:                 my $selected = '';
 1048:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1049:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1050:                         $selected = 'selected="selected"';
 1051:                     }
 1052:                 } elsif ($path eq '') {
 1053:                     $selected = 'selected="selected"';
 1054:                 }
 1055:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1056:             }
 1057:             $datatable .= '</select></td>';
 1058:             my ($custom,$exempt);
 1059:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1060:                 $custom = $disallowed{$lonhost}{'custompath'};
 1061:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1062:             }
 1063:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1064:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1065:                           '</tr>';
 1066:         }
 1067:         $datatable .= '</table></td></tr>';
 1068:         return $datatable;
 1069:     } elsif ($caller eq 'page') {
 1070:         my %defaultchecked = ( 
 1071:                                'coursecatalog' => 'on',
 1072:                                'helpdesk'      => 'on',
 1073:                                'adminmail'     => 'off',
 1074:                                'newuser'       => 'off',
 1075:                              );
 1076:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1077:         my (%checkedon,%checkedoff);
 1078:         foreach my $item (@toggles) {
 1079:             if ($defaultchecked{$item} eq 'on') { 
 1080:                 $checkedon{$item} = ' checked="checked" ';
 1081:                 $checkedoff{$item} = ' ';
 1082:             } elsif ($defaultchecked{$item} eq 'off') {
 1083:                 $checkedoff{$item} = ' checked="checked" ';
 1084:                 $checkedon{$item} = ' ';
 1085:             }
 1086:         }
 1087:         my @images = ('img','logo','domlogo','login');
 1088:         my @logintext = ('textcol','bgcol');
 1089:         my @bgs = ('pgbg','mainbg','sidebg');
 1090:         my @links = ('link','alink','vlink');
 1091:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1092:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1093:         my (%is_custom,%designs);
 1094:         my %defaults = (
 1095:                        font => $defaultdesign{'login.font'},
 1096:                        );
 1097:         foreach my $item (@images) {
 1098:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1099:             $defaults{'showlogo'}{$item} = 1;
 1100:         }
 1101:         foreach my $item (@bgs) {
 1102:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1103:         }
 1104:         foreach my $item (@logintext) {
 1105:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1106:         }
 1107:         foreach my $item (@links) {
 1108:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1109:         }
 1110:         if (ref($settings) eq 'HASH') {
 1111:             foreach my $item (@toggles) {
 1112:                 if ($settings->{$item} eq '1') {
 1113:                     $checkedon{$item} =  ' checked="checked" ';
 1114:                     $checkedoff{$item} = ' ';
 1115:                 } elsif ($settings->{$item} eq '0') {
 1116:                     $checkedoff{$item} =  ' checked="checked" ';
 1117:                     $checkedon{$item} = ' ';
 1118:                 }
 1119:             }
 1120:             foreach my $item (@images) {
 1121:                 if (defined($settings->{$item})) {
 1122:                     $designs{$item} = $settings->{$item};
 1123:                     $is_custom{$item} = 1;
 1124:                 }
 1125:                 if (defined($settings->{'showlogo'}{$item})) {
 1126:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1127:                 }
 1128:             }
 1129:             foreach my $item (@logintext) {
 1130:                 if ($settings->{$item} ne '') {
 1131:                     $designs{'logintext'}{$item} = $settings->{$item};
 1132:                     $is_custom{$item} = 1;
 1133:                 }
 1134:             }
 1135:             if ($settings->{'font'} ne '') {
 1136:                 $designs{'font'} = $settings->{'font'};
 1137:                 $is_custom{'font'} = 1;
 1138:             }
 1139:             foreach my $item (@bgs) {
 1140:                 if ($settings->{$item} ne '') {
 1141:                     $designs{'bgs'}{$item} = $settings->{$item};
 1142:                     $is_custom{$item} = 1;
 1143:                 }
 1144:             }
 1145:             foreach my $item (@links) {
 1146:                 if ($settings->{$item} ne '') {
 1147:                     $designs{'links'}{$item} = $settings->{$item};
 1148:                     $is_custom{$item} = 1;
 1149:                 }
 1150:             }
 1151:         } else {
 1152:             if ($designhash{$dom.'.login.font'} ne '') {
 1153:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1154:                 $is_custom{'font'} = 1;
 1155:             }
 1156:             foreach my $item (@images) {
 1157:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1158:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1159:                     $is_custom{$item} = 1;
 1160:                 }
 1161:             }
 1162:             foreach my $item (@bgs) {
 1163:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1164:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1165:                     $is_custom{$item} = 1;
 1166:                 }
 1167:             }
 1168:             foreach my $item (@links) {
 1169:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1170:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1171:                     $is_custom{$item} = 1;
 1172:                 }
 1173:             }
 1174:         }
 1175:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1176:                                                       logo => 'Institution Logo',
 1177:                                                       domlogo => 'Domain Logo',
 1178:                                                       login => 'Login box');
 1179:         my $itemcount = 1;
 1180:         foreach my $item (@toggles) {
 1181:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1182:             $datatable .=  
 1183:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1184:                 '</td><td>'.
 1185:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1186:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1187:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1188:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1189:                 '</tr>';
 1190:             $itemcount ++;
 1191:         }
 1192:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1193:         $datatable .= '</tr></table></td></tr>';
 1194:     } elsif ($caller eq 'help') {
 1195:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1196:         my $switchserver = &check_switchserver($dom,$confname);
 1197:         my $itemcount = 1;
 1198:         $defaulturl = '/adm/loginproblems.html';
 1199:         $defaulttype = 'default';
 1200:         %lt = &Apache::lonlocal::texthash (
 1201:                      del     => 'Delete?',
 1202:                      rep     => 'Replace:',
 1203:                      upl     => 'Upload:',
 1204:                      default => 'Default',
 1205:                      custom  => 'Custom',
 1206:                                              );
 1207:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1208:         my @currlangs;
 1209:         if (ref($settings) eq 'HASH') {
 1210:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1211:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1212:                     next if ($settings->{'helpurl'}{$key} eq '');
 1213:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1214:                     $type{$key} = 'custom';
 1215:                     unless ($key eq 'nolang') {
 1216:                         push(@currlangs,$key);
 1217:                     }
 1218:                 }
 1219:             } elsif ($settings->{'helpurl'} ne '') {
 1220:                 $type{'nolang'} = 'custom';
 1221:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1222:             }
 1223:         }
 1224:         foreach my $lang ('nolang',sort(@currlangs)) {
 1225:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1226:             $datatable .= '<tr'.$css_class.'>';
 1227:             if ($url{$lang} eq '') {
 1228:                 $url{$lang} = $defaulturl;
 1229:             }
 1230:             if ($type{$lang} eq '') {
 1231:                 $type{$lang} = $defaulttype;
 1232:             }
 1233:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1234:             if ($lang eq 'nolang') {
 1235:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1236:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1237:             } else {
 1238:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1239:                                   $langchoices{$lang},
 1240:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1241:             }
 1242:             $datatable .= '</span></td>'."\n".
 1243:                           '<td class="LC_left_item">';
 1244:             if ($type{$lang} eq 'custom') {
 1245:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1246:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1247:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1248:             } else {
 1249:                 $datatable .= $lt{'upl'};
 1250:             }
 1251:             $datatable .='<br />';
 1252:             if ($switchserver) {
 1253:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1254:             } else {
 1255:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1256:             }
 1257:             $datatable .= '</td></tr>';
 1258:             $itemcount ++;
 1259:         }
 1260:         my @addlangs;
 1261:         foreach my $lang (sort(keys(%langchoices))) {
 1262:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1263:             push(@addlangs,$lang);
 1264:         }
 1265:         if (@addlangs > 0) {
 1266:             my %toadd;
 1267:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1268:             $toadd{''} = &mt('Select');
 1269:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1270:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1271:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1272:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1273:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1274:             if ($switchserver) {
 1275:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1276:             } else {
 1277:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1278:             }
 1279:             $datatable .= '</td></tr>';
 1280:             $itemcount ++;
 1281:         }
 1282:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1283:     } elsif ($caller eq 'headtag') {
 1284:         my %domservers = &Apache::lonnet::get_servers($dom);
 1285:         my $choice = $choices{'headtag'};
 1286:         $css_class = ' class="LC_odd_row"';
 1287:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1288:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1289:                       '<th>'.$choices{'current'}.'</th>'.
 1290:                       '<th>'.$choices{'action'}.'</th>'.
 1291:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1292:         my (%currurls,%currexempt);
 1293:         if (ref($settings) eq 'HASH') {
 1294:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1295:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1296:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1297:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1298:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1299:                     }
 1300:                 }
 1301:             }
 1302:         }
 1303:         my %lt = &Apache::lonlocal::texthash(
 1304:                                                del  => 'Delete?',
 1305:                                                rep  => 'Replace:',
 1306:                                                upl  => 'Upload:',
 1307:                                                curr => 'View contents',
 1308:                                                none => 'None',
 1309:         );
 1310:         my $switchserver = &check_switchserver($dom,$confname);
 1311:         foreach my $lonhost (sort(keys(%domservers))) {
 1312:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1313:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1314:             if ($currurls{$lonhost}) {
 1315:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1316:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1317:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1318:                               '">'.$lt{'curr'}.'</a></td>'.
 1319:                               '<td><span class="LC_nobreak"><label>'.
 1320:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1321:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1322:             } else {
 1323:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1324:             }
 1325:             $datatable .='<br />';
 1326:             if ($switchserver) {
 1327:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1328:             } else {
 1329:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1330:             }
 1331:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1332:         }
 1333:         $datatable .= '</table></td></tr>';
 1334:     }
 1335:     return $datatable;
 1336: }
 1337: 
 1338: sub login_choices {
 1339:     my %choices =
 1340:         &Apache::lonlocal::texthash (
 1341:             coursecatalog => 'Display Course/Community Catalog link?',
 1342:             adminmail     => "Display Administrator's E-mail Address?",
 1343:             helpdesk      => 'Display "Contact Helpdesk" link',
 1344:             disallowlogin => "Login page requests redirected",
 1345:             hostid        => "Server",
 1346:             server        => "Redirect to:",
 1347:             serverpath    => "Path",
 1348:             custompath    => "Custom", 
 1349:             exempt        => "Exempt IP(s)",
 1350:             directlogin   => "No redirect",
 1351:             newuser       => "Link to create a user account",
 1352:             img           => "Header",
 1353:             logo          => "Main Logo",
 1354:             domlogo       => "Domain Logo",
 1355:             login         => "Log-in Header", 
 1356:             textcol       => "Text color",
 1357:             bgcol         => "Box color",
 1358:             bgs           => "Background colors",
 1359:             links         => "Link colors",
 1360:             font          => "Font color",
 1361:             pgbg          => "Header",
 1362:             mainbg        => "Page",
 1363:             sidebg        => "Login box",
 1364:             link          => "Link",
 1365:             alink         => "Active link",
 1366:             vlink         => "Visited link",
 1367:             headtag       => "Custom markup",
 1368:             action        => "Action",
 1369:             current       => "Current",
 1370:         );
 1371:     return %choices;
 1372: }
 1373: 
 1374: sub print_rolecolors {
 1375:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1376:     my %choices = &color_font_choices();
 1377:     my @bgs = ('pgbg','tabbg','sidebg');
 1378:     my @links = ('link','alink','vlink');
 1379:     my @images = ('img');
 1380:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1381:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1382:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1383:     my (%is_custom,%designs);
 1384:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1385:     if (ref($settings) eq 'HASH') {
 1386:         if (ref($settings->{$role}) eq 'HASH') {
 1387:             if ($settings->{$role}->{'img'} ne '') {
 1388:                 $designs{'img'} = $settings->{$role}->{'img'};
 1389:                 $is_custom{'img'} = 1;
 1390:             }
 1391:             if ($settings->{$role}->{'font'} ne '') {
 1392:                 $designs{'font'} = $settings->{$role}->{'font'};
 1393:                 $is_custom{'font'} = 1;
 1394:             }
 1395:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1396:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1397:                 $is_custom{'fontmenu'} = 1;
 1398:             }
 1399:             foreach my $item (@bgs) {
 1400:                 if ($settings->{$role}->{$item} ne '') {
 1401:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1402:                     $is_custom{$item} = 1;
 1403:                 }
 1404:             }
 1405:             foreach my $item (@links) {
 1406:                 if ($settings->{$role}->{$item} ne '') {
 1407:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1408:                     $is_custom{$item} = 1;
 1409:                 }
 1410:             }
 1411:         }
 1412:     } else {
 1413:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1414:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1415:             $is_custom{'img'} = 1;
 1416:         }
 1417:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1418:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1419:             $is_custom{'fontmenu'} = 1; 
 1420:         }
 1421:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1422:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1423:             $is_custom{'font'} = 1;
 1424:         }
 1425:         foreach my $item (@bgs) {
 1426:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1427:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1428:                 $is_custom{$item} = 1;
 1429:             
 1430:             }
 1431:         }
 1432:         foreach my $item (@links) {
 1433:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1434:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1435:                 $is_custom{$item} = 1;
 1436:             }
 1437:         }
 1438:     }
 1439:     my $itemcount = 1;
 1440:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1441:     $datatable .= '</tr></table></td></tr>';
 1442:     return $datatable;
 1443: }
 1444: 
 1445: sub role_defaults {
 1446:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1447:     my %defaults;
 1448:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1449:         return %defaults;
 1450:     }
 1451:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1452:     if ($role eq 'login') {
 1453:         %defaults = (
 1454:                        font => $defaultdesign{$role.'.font'},
 1455:                     );
 1456:         if (ref($logintext) eq 'ARRAY') {
 1457:             foreach my $item (@{$logintext}) {
 1458:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1459:             }
 1460:         }
 1461:         foreach my $item (@{$images}) {
 1462:             $defaults{'showlogo'}{$item} = 1;
 1463:         }
 1464:     } else {
 1465:         %defaults = (
 1466:                        img => $defaultdesign{$role.'.img'},
 1467:                        font => $defaultdesign{$role.'.font'},
 1468:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1469:                     );
 1470:     }
 1471:     foreach my $item (@{$bgs}) {
 1472:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1473:     }
 1474:     foreach my $item (@{$links}) {
 1475:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1476:     }
 1477:     foreach my $item (@{$images}) {
 1478:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1479:     }
 1480:     return %defaults;
 1481: }
 1482: 
 1483: sub display_color_options {
 1484:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1485:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1486:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1487:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1488:     my $datatable = '<tr'.$css_class.'>'.
 1489:         '<td>'.$choices->{'font'}.'</td>';
 1490:     if (!$is_custom->{'font'}) {
 1491:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1492:     } else {
 1493:         $datatable .= '<td>&nbsp;</td>';
 1494:     }
 1495:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1496: 
 1497:     $datatable .= '<td><span class="LC_nobreak">'.
 1498:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1499:                   ' value="'.$current_color.'" />&nbsp;'.
 1500:                   '&nbsp;</span></td></tr>';
 1501:     unless ($role eq 'login') { 
 1502:         $datatable .= '<tr'.$css_class.'>'.
 1503:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1504:         if (!$is_custom->{'fontmenu'}) {
 1505:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1506:         } else {
 1507:             $datatable .= '<td>&nbsp;</td>';
 1508:         }
 1509: 	$current_color = $designs->{'fontmenu'} ?
 1510: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1511:         $datatable .= '<td><span class="LC_nobreak">'.
 1512:                       '<input class="colorchooser" type="text" size="10" name="'
 1513: 		      .$role.'_fontmenu"'.
 1514:                       ' value="'.$current_color.'" />&nbsp;'.
 1515:                       '&nbsp;</span></td></tr>';
 1516:     }
 1517:     my $switchserver = &check_switchserver($dom,$confname);
 1518:     foreach my $img (@{$images}) {
 1519: 	$itemcount ++;
 1520:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1521:         $datatable .= '<tr'.$css_class.'>'.
 1522:                       '<td>'.$choices->{$img};
 1523:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1524:         if ($role eq 'login') {
 1525:             if ($img eq 'login') {
 1526:                 $login_hdr_pick =
 1527:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1528:                 $logincolors =
 1529:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1530:                                        $designs,$defaults);
 1531:             } elsif ($img ne 'domlogo') {
 1532:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1533:             }
 1534:         }
 1535:         $datatable .= '</td>';
 1536:         if ($designs->{$img} ne '') {
 1537:             $imgfile = $designs->{$img};
 1538: 	    $img_import = ($imgfile =~ m{^/adm/});
 1539:         } else {
 1540:             $imgfile = $defaults->{$img};
 1541:         }
 1542:         if ($imgfile) {
 1543:             my ($showfile,$fullsize);
 1544:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1545:                 my $urldir = $1;
 1546:                 my $filename = $2;
 1547:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1548:                 if (@info) {
 1549:                     my $thumbfile = 'tn-'.$filename;
 1550:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1551:                     if (@thumb) {
 1552:                         $showfile = $urldir.'/'.$thumbfile;
 1553:                     } else {
 1554:                         $showfile = $imgfile;
 1555:                     }
 1556:                 } else {
 1557:                     $showfile = '';
 1558:                 }
 1559:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1560:                 $showfile = $imgfile;
 1561:                 my $imgdir = $1;
 1562:                 my $filename = $2;
 1563:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1564:                     $showfile = "/$imgdir/tn-".$filename;
 1565:                 } else {
 1566:                     my $input = $londocroot.$imgfile;
 1567:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1568:                     if (!-e $output) {
 1569:                         my ($width,$height) = &thumb_dimensions();
 1570:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1571:                         if ($fullwidth ne '' && $fullheight ne '') {
 1572:                             if ($fullwidth > $width && $fullheight > $height) { 
 1573:                                 my $size = $width.'x'.$height;
 1574:                                 my @args = ('convert','-sample',$size,$input,$output);
 1575:                                 system({$args[0]} @args);
 1576:                                 $showfile = "/$imgdir/tn-".$filename;
 1577:                             }
 1578:                         }
 1579:                     }
 1580:                 }
 1581:             }
 1582:             if ($showfile) {
 1583:                 if ($showfile =~ m{^/(adm|res)/}) {
 1584:                     if ($showfile =~ m{^/res/}) {
 1585:                         my $local_showfile =
 1586:                             &Apache::lonnet::filelocation('',$showfile);
 1587:                         &Apache::lonnet::repcopy($local_showfile);
 1588:                     }
 1589:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1590:                 }
 1591:                 if ($imgfile) {
 1592:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1593:                         if ($imgfile =~ m{^/res/}) {
 1594:                             my $local_imgfile =
 1595:                                 &Apache::lonnet::filelocation('',$imgfile);
 1596:                             &Apache::lonnet::repcopy($local_imgfile);
 1597:                         }
 1598:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1599:                     } else {
 1600:                         $fullsize = $imgfile;
 1601:                     }
 1602:                 }
 1603:                 $datatable .= '<td>';
 1604:                 if ($img eq 'login') {
 1605:                     $datatable .= $login_hdr_pick;
 1606:                 } 
 1607:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1608:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1609:             } else {
 1610:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1611:                               &mt('Upload:').'<br />';
 1612:             }
 1613:         } else {
 1614:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1615:                           &mt('Upload:').'<br />';
 1616:         }
 1617:         if ($switchserver) {
 1618:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1619:         } else {
 1620:             if ($img ne 'login') { # suppress file selection for Log-in header
 1621:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1622:             }
 1623:         }
 1624:         $datatable .= '</td></tr>';
 1625:     }
 1626:     $itemcount ++;
 1627:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1628:     $datatable .= '<tr'.$css_class.'>'.
 1629:                   '<td>'.$choices->{'bgs'}.'</td>';
 1630:     my $bgs_def;
 1631:     foreach my $item (@{$bgs}) {
 1632:         if (!$is_custom->{$item}) {
 1633:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span class="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1634:         }
 1635:     }
 1636:     if ($bgs_def) {
 1637:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1638:     } else {
 1639:         $datatable .= '<td>&nbsp;</td>';
 1640:     }
 1641:     $datatable .= '<td class="LC_right_item">'.
 1642:                   '<table border="0"><tr>';
 1643: 
 1644:     foreach my $item (@{$bgs}) {
 1645:         $datatable .= '<td align="center">'.$choices->{$item};
 1646: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1647:         if ($designs->{'bgs'}{$item}) {
 1648:             $datatable .= '&nbsp;';
 1649:         }
 1650:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1651:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1652:     }
 1653:     $datatable .= '</tr></table></td></tr>';
 1654:     $itemcount ++;
 1655:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1656:     $datatable .= '<tr'.$css_class.'>'.
 1657:                   '<td>'.$choices->{'links'}.'</td>';
 1658:     my $links_def;
 1659:     foreach my $item (@{$links}) {
 1660:         if (!$is_custom->{$item}) {
 1661:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1662:         }
 1663:     }
 1664:     if ($links_def) {
 1665:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1666:     } else {
 1667:         $datatable .= '<td>&nbsp;</td>';
 1668:     }
 1669:     $datatable .= '<td class="LC_right_item">'.
 1670:                   '<table border="0"><tr>';
 1671:     foreach my $item (@{$links}) {
 1672: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1673:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1674:         if ($designs->{'links'}{$item}) {
 1675:             $datatable.='&nbsp;';
 1676:         }
 1677:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1678:                       '" /></td>';
 1679:     }
 1680:     $$rowtotal += $itemcount;
 1681:     return $datatable;
 1682: }
 1683: 
 1684: sub logo_display_options {
 1685:     my ($img,$defaults,$designs) = @_;
 1686:     my $checkedon;
 1687:     if (ref($defaults) eq 'HASH') {
 1688:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1689:             if ($defaults->{'showlogo'}{$img}) {
 1690:                 $checkedon = 'checked="checked" ';     
 1691:             }
 1692:         } 
 1693:     }
 1694:     if (ref($designs) eq 'HASH') {
 1695:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1696:             if (defined($designs->{'showlogo'}{$img})) {
 1697:                 if ($designs->{'showlogo'}{$img} == 0) {
 1698:                     $checkedon = '';
 1699:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1700:                     $checkedon = 'checked="checked" ';
 1701:                 }
 1702:             }
 1703:         }
 1704:     }
 1705:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1706:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1707:            &mt('show').'</label>'."\n";
 1708: }
 1709: 
 1710: sub login_header_options  {
 1711:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1712:     my $output = '';
 1713:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1714:         $output .= &mt('Text default(s):').'<br />';
 1715:         if (!$is_custom->{'textcol'}) {
 1716:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1717:                        '&nbsp;&nbsp;&nbsp;';
 1718:         }
 1719:         if (!$is_custom->{'bgcol'}) {
 1720:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1721:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1722:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1723:         }
 1724:         $output .= '<br />';
 1725:     }
 1726:     $output .='<br />';
 1727:     return $output;
 1728: }
 1729: 
 1730: sub login_text_colors {
 1731:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1732:     my $color_menu = '<table border="0"><tr>';
 1733:     foreach my $item (@{$logintext}) {
 1734:         $color_menu .= '<td align="center">'.$choices->{$item};
 1735:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1736:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1737:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1738:     }
 1739:     $color_menu .= '</tr></table><br />';
 1740:     return $color_menu;
 1741: }
 1742: 
 1743: sub image_changes {
 1744:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1745:     my $output;
 1746:     if ($img eq 'login') {
 1747:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1748:     } elsif (!$is_custom) {
 1749:         if ($img ne 'domlogo') {
 1750:             $output = &mt('Default image:').'<br />';
 1751:         } else {
 1752:             $output = &mt('Default in use:').'<br />';
 1753:         }
 1754:     }
 1755:     if ($img ne 'login') {
 1756:         if ($img_import) {
 1757:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1758:         }
 1759:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1760:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1761:         if ($is_custom) {
 1762:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1763:                        '<input type="checkbox" name="'.
 1764:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1765:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1766:         } else {
 1767:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1768:         }
 1769:     }
 1770:     return $output;
 1771: }
 1772: 
 1773: sub print_quotas {
 1774:     my ($dom,$settings,$rowtotal,$action) = @_;
 1775:     my $context;
 1776:     if ($action eq 'quotas') {
 1777:         $context = 'tools';
 1778:     } else {
 1779:         $context = $action;
 1780:     }
 1781:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1782:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1783:     my $typecount = 0;
 1784:     my ($css_class,%titles);
 1785:     if ($context eq 'requestcourses') {
 1786:         @usertools = ('official','unofficial','community','textbook');
 1787:         @options =('norequest','approval','validate','autolimit');
 1788:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1789:         %titles = &courserequest_titles();
 1790:     } elsif ($context eq 'requestauthor') {
 1791:         @usertools = ('author');
 1792:         @options = ('norequest','approval','automatic');
 1793:         %titles = &authorrequest_titles();
 1794:     } else {
 1795:         @usertools = ('aboutme','blog','webdav','portfolio');
 1796:         %titles = &tool_titles();
 1797:     }
 1798:     if (ref($types) eq 'ARRAY') {
 1799:         foreach my $type (@{$types}) {
 1800:             my ($currdefquota,$currauthorquota);
 1801:             unless (($context eq 'requestcourses') ||
 1802:                     ($context eq 'requestauthor')) {
 1803:                 if (ref($settings) eq 'HASH') {
 1804:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1805:                         $currdefquota = $settings->{defaultquota}->{$type};
 1806:                     } else {
 1807:                         $currdefquota = $settings->{$type};
 1808:                     }
 1809:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1810:                         $currauthorquota = $settings->{authorquota}->{$type};
 1811:                     }
 1812:                 }
 1813:             }
 1814:             if (defined($usertypes->{$type})) {
 1815:                 $typecount ++;
 1816:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1817:                 $datatable .= '<tr'.$css_class.'>'.
 1818:                               '<td>'.$usertypes->{$type}.'</td>'.
 1819:                               '<td class="LC_left_item">';
 1820:                 if ($context eq 'requestcourses') {
 1821:                     $datatable .= '<table><tr>';
 1822:                 }
 1823:                 my %cell;  
 1824:                 foreach my $item (@usertools) {
 1825:                     if ($context eq 'requestcourses') {
 1826:                         my ($curroption,$currlimit);
 1827:                         if (ref($settings) eq 'HASH') {
 1828:                             if (ref($settings->{$item}) eq 'HASH') {
 1829:                                 $curroption = $settings->{$item}->{$type};
 1830:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1831:                                     $currlimit = $1; 
 1832:                                 }
 1833:                             }
 1834:                         }
 1835:                         if (!$curroption) {
 1836:                             $curroption = 'norequest';
 1837:                         }
 1838:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1839:                         foreach my $option (@options) {
 1840:                             my $val = $option;
 1841:                             if ($option eq 'norequest') {
 1842:                                 $val = 0;  
 1843:                             }
 1844:                             if ($option eq 'validate') {
 1845:                                 my $canvalidate = 0;
 1846:                                 if (ref($validations{$item}) eq 'HASH') { 
 1847:                                     if ($validations{$item}{$type}) {
 1848:                                         $canvalidate = 1;
 1849:                                     }
 1850:                                 }
 1851:                                 next if (!$canvalidate);
 1852:                             }
 1853:                             my $checked = '';
 1854:                             if ($option eq $curroption) {
 1855:                                 $checked = ' checked="checked"';
 1856:                             } elsif ($option eq 'autolimit') {
 1857:                                 if ($curroption =~ /^autolimit/) {
 1858:                                     $checked = ' checked="checked"';
 1859:                                 }                       
 1860:                             } 
 1861:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1862:                                   '<input type="radio" name="crsreq_'.$item.
 1863:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1864:                                   $titles{$option}.'</label>';
 1865:                             if ($option eq 'autolimit') {
 1866:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1867:                                                 $item.'_limit_'.$type.'" size="1" '.
 1868:                                                 'value="'.$currlimit.'" />';
 1869:                             }
 1870:                             $cell{$item} .= '</span> ';
 1871:                             if ($option eq 'autolimit') {
 1872:                                 $cell{$item} .= $titles{'unlimited'};
 1873:                             }
 1874:                         }
 1875:                     } elsif ($context eq 'requestauthor') {
 1876:                         my $curroption;
 1877:                         if (ref($settings) eq 'HASH') {
 1878:                             $curroption = $settings->{$type};
 1879:                         }
 1880:                         if (!$curroption) {
 1881:                             $curroption = 'norequest';
 1882:                         }
 1883:                         foreach my $option (@options) {
 1884:                             my $val = $option;
 1885:                             if ($option eq 'norequest') {
 1886:                                 $val = 0;
 1887:                             }
 1888:                             my $checked = '';
 1889:                             if ($option eq $curroption) {
 1890:                                 $checked = ' checked="checked"';
 1891:                             }
 1892:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1893:                                   '<input type="radio" name="authorreq_'.$type.
 1894:                                   '" value="'.$val.'"'.$checked.' />'.
 1895:                                   $titles{$option}.'</label></span>&nbsp; ';
 1896:                         }
 1897:                     } else {
 1898:                         my $checked = 'checked="checked" ';
 1899:                         if (ref($settings) eq 'HASH') {
 1900:                             if (ref($settings->{$item}) eq 'HASH') {
 1901:                                 if ($settings->{$item}->{$type} == 0) {
 1902:                                     $checked = '';
 1903:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1904:                                     $checked =  'checked="checked" ';
 1905:                                 }
 1906:                             }
 1907:                         }
 1908:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1909:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1910:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1911:                                       '</label></span>&nbsp; ';
 1912:                     }
 1913:                 }
 1914:                 if ($context eq 'requestcourses') {
 1915:                     $datatable .= '</tr><tr>';
 1916:                     foreach my $item (@usertools) {
 1917:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1918:                     }
 1919:                     $datatable .= '</tr></table>';
 1920:                 }
 1921:                 $datatable .= '</td>';
 1922:                 unless (($context eq 'requestcourses') ||
 1923:                         ($context eq 'requestauthor')) {
 1924:                     $datatable .= 
 1925:                               '<td class="LC_right_item">'.
 1926:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1927:                               '<input type="text" name="quota_'.$type.
 1928:                               '" value="'.$currdefquota.
 1929:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1930:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1931:                               '<input type="text" name="authorquota_'.$type.
 1932:                               '" value="'.$currauthorquota.
 1933:                               '" size="5" /></span></td>';
 1934:                 }
 1935:                 $datatable .= '</tr>';
 1936:             }
 1937:         }
 1938:     }
 1939:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1940:         $defaultquota = '20';
 1941:         $authorquota = '500';
 1942:         if (ref($settings) eq 'HASH') {
 1943:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1944:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1945:             } elsif (defined($settings->{'default'})) {
 1946:                 $defaultquota = $settings->{'default'};
 1947:             }
 1948:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1949:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1950:             }
 1951:         }
 1952:     }
 1953:     $typecount ++;
 1954:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1955:     $datatable .= '<tr'.$css_class.'>'.
 1956:                   '<td>'.$othertitle.'</td>'.
 1957:                   '<td class="LC_left_item">';
 1958:     if ($context eq 'requestcourses') {
 1959:         $datatable .= '<table><tr>';
 1960:     }
 1961:     my %defcell;
 1962:     foreach my $item (@usertools) {
 1963:         if ($context eq 'requestcourses') {
 1964:             my ($curroption,$currlimit);
 1965:             if (ref($settings) eq 'HASH') {
 1966:                 if (ref($settings->{$item}) eq 'HASH') {
 1967:                     $curroption = $settings->{$item}->{'default'};
 1968:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1969:                         $currlimit = $1;
 1970:                     }
 1971:                 }
 1972:             }
 1973:             if (!$curroption) {
 1974:                 $curroption = 'norequest';
 1975:             }
 1976:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1977:             foreach my $option (@options) {
 1978:                 my $val = $option;
 1979:                 if ($option eq 'norequest') {
 1980:                     $val = 0;
 1981:                 }
 1982:                 if ($option eq 'validate') {
 1983:                     my $canvalidate = 0;
 1984:                     if (ref($validations{$item}) eq 'HASH') {
 1985:                         if ($validations{$item}{'default'}) {
 1986:                             $canvalidate = 1;
 1987:                         }
 1988:                     }
 1989:                     next if (!$canvalidate);
 1990:                 }
 1991:                 my $checked = '';
 1992:                 if ($option eq $curroption) {
 1993:                     $checked = ' checked="checked"';
 1994:                 } elsif ($option eq 'autolimit') {
 1995:                     if ($curroption =~ /^autolimit/) {
 1996:                         $checked = ' checked="checked"';
 1997:                     }
 1998:                 }
 1999:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2000:                                   '<input type="radio" name="crsreq_'.$item.
 2001:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2002:                                   $titles{$option}.'</label>';
 2003:                 if ($option eq 'autolimit') {
 2004:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2005:                                        $item.'_limit_default" size="1" '.
 2006:                                        'value="'.$currlimit.'" />';
 2007:                 }
 2008:                 $defcell{$item} .= '</span> ';
 2009:                 if ($option eq 'autolimit') {
 2010:                     $defcell{$item} .= $titles{'unlimited'};
 2011:                 }
 2012:             }
 2013:         } elsif ($context eq 'requestauthor') {
 2014:             my $curroption;
 2015:             if (ref($settings) eq 'HASH') {
 2016:                 $curroption = $settings->{'default'};
 2017:             }
 2018:             if (!$curroption) {
 2019:                 $curroption = 'norequest';
 2020:             }
 2021:             foreach my $option (@options) {
 2022:                 my $val = $option;
 2023:                 if ($option eq 'norequest') {
 2024:                     $val = 0;
 2025:                 }
 2026:                 my $checked = '';
 2027:                 if ($option eq $curroption) {
 2028:                     $checked = ' checked="checked"';
 2029:                 }
 2030:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2031:                               '<input type="radio" name="authorreq_default"'.
 2032:                               ' value="'.$val.'"'.$checked.' />'.
 2033:                               $titles{$option}.'</label></span>&nbsp; ';
 2034:             }
 2035:         } else {
 2036:             my $checked = 'checked="checked" ';
 2037:             if (ref($settings) eq 'HASH') {
 2038:                 if (ref($settings->{$item}) eq 'HASH') {
 2039:                     if ($settings->{$item}->{'default'} == 0) {
 2040:                         $checked = '';
 2041:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2042:                         $checked = 'checked="checked" ';
 2043:                     }
 2044:                 }
 2045:             }
 2046:             $datatable .= '<span class="LC_nobreak"><label>'.
 2047:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2048:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2049:                           '</label></span>&nbsp; ';
 2050:         }
 2051:     }
 2052:     if ($context eq 'requestcourses') {
 2053:         $datatable .= '</tr><tr>';
 2054:         foreach my $item (@usertools) {
 2055:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2056:         }
 2057:         $datatable .= '</tr></table>';
 2058:     }
 2059:     $datatable .= '</td>';
 2060:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2061:         $datatable .= '<td class="LC_right_item">'.
 2062:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2063:                       '<input type="text" name="defaultquota" value="'.
 2064:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2065:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2066:                       '<input type="text" name="authorquota" value="'.
 2067:                       $authorquota.'" size="5" /></span></td>';
 2068:     }
 2069:     $datatable .= '</tr>';
 2070:     $typecount ++;
 2071:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2072:     $datatable .= '<tr'.$css_class.'>'.
 2073:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2074:     if ($context eq 'requestcourses') {
 2075:         $datatable .= &mt('(overrides affiliation, if set)').
 2076:                       '</td>'.
 2077:                       '<td class="LC_left_item">'.
 2078:                       '<table><tr>';
 2079:     } else {
 2080:         $datatable .= &mt('(overrides affiliation, if checked)').
 2081:                       '</td>'.
 2082:                       '<td class="LC_left_item" colspan="2">'.
 2083:                       '<br />';
 2084:     }
 2085:     my %advcell;
 2086:     foreach my $item (@usertools) {
 2087:         if ($context eq 'requestcourses') {
 2088:             my ($curroption,$currlimit);
 2089:             if (ref($settings) eq 'HASH') {
 2090:                 if (ref($settings->{$item}) eq 'HASH') {
 2091:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2092:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2093:                         $currlimit = $1;
 2094:                     }
 2095:                 }
 2096:             }
 2097:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2098:             my $checked = '';
 2099:             if ($curroption eq '') {
 2100:                 $checked = ' checked="checked"';
 2101:             }
 2102:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2103:                                '<input type="radio" name="crsreq_'.$item.
 2104:                                '__LC_adv" value=""'.$checked.' />'.
 2105:                                &mt('No override set').'</label></span>&nbsp; ';
 2106:             foreach my $option (@options) {
 2107:                 my $val = $option;
 2108:                 if ($option eq 'norequest') {
 2109:                     $val = 0;
 2110:                 }
 2111:                 if ($option eq 'validate') {
 2112:                     my $canvalidate = 0;
 2113:                     if (ref($validations{$item}) eq 'HASH') {
 2114:                         if ($validations{$item}{'_LC_adv'}) {
 2115:                             $canvalidate = 1;
 2116:                         }
 2117:                     }
 2118:                     next if (!$canvalidate);
 2119:                 }
 2120:                 my $checked = '';
 2121:                 if ($val eq $curroption) {
 2122:                     $checked = ' checked="checked"';
 2123:                 } elsif ($option eq 'autolimit') {
 2124:                     if ($curroption =~ /^autolimit/) {
 2125:                         $checked = ' checked="checked"';
 2126:                     }
 2127:                 }
 2128:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2129:                                   '<input type="radio" name="crsreq_'.$item.
 2130:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2131:                                   $titles{$option}.'</label>';
 2132:                 if ($option eq 'autolimit') {
 2133:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2134:                                        $item.'_limit__LC_adv" size="1" '.
 2135:                                        'value="'.$currlimit.'" />';
 2136:                 }
 2137:                 $advcell{$item} .= '</span> ';
 2138:                 if ($option eq 'autolimit') {
 2139:                     $advcell{$item} .= $titles{'unlimited'};
 2140:                 }
 2141:             }
 2142:         } elsif ($context eq 'requestauthor') {
 2143:             my $curroption;
 2144:             if (ref($settings) eq 'HASH') {
 2145:                 $curroption = $settings->{'_LC_adv'};
 2146:             }
 2147:             my $checked = '';
 2148:             if ($curroption eq '') {
 2149:                 $checked = ' checked="checked"';
 2150:             }
 2151:             $datatable .= '<span class="LC_nobreak"><label>'.
 2152:                           '<input type="radio" name="authorreq__LC_adv"'.
 2153:                           ' value=""'.$checked.' />'.
 2154:                           &mt('No override set').'</label></span>&nbsp; ';
 2155:             foreach my $option (@options) {
 2156:                 my $val = $option;
 2157:                 if ($option eq 'norequest') {
 2158:                     $val = 0;
 2159:                 }
 2160:                 my $checked = '';
 2161:                 if ($val eq $curroption) {
 2162:                     $checked = ' checked="checked"';
 2163:                 }
 2164:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2165:                               '<input type="radio" name="authorreq__LC_adv"'.
 2166:                               ' value="'.$val.'"'.$checked.' />'.
 2167:                               $titles{$option}.'</label></span>&nbsp; ';
 2168:             }
 2169:         } else {
 2170:             my $checked = 'checked="checked" ';
 2171:             if (ref($settings) eq 'HASH') {
 2172:                 if (ref($settings->{$item}) eq 'HASH') {
 2173:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2174:                         $checked = '';
 2175:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2176:                         $checked = 'checked="checked" ';
 2177:                     }
 2178:                 }
 2179:             }
 2180:             $datatable .= '<span class="LC_nobreak"><label>'.
 2181:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2182:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2183:                           '</label></span>&nbsp; ';
 2184:         }
 2185:     }
 2186:     if ($context eq 'requestcourses') {
 2187:         $datatable .= '</tr><tr>';
 2188:         foreach my $item (@usertools) {
 2189:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2190:         }
 2191:         $datatable .= '</tr></table>';
 2192:     }
 2193:     $datatable .= '</td></tr>';
 2194:     $$rowtotal += $typecount;
 2195:     return $datatable;
 2196: }
 2197: 
 2198: sub print_requestmail {
 2199:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2200:     my ($now,$datatable,%currapp);
 2201:     $now = time;
 2202:     if (ref($settings) eq 'HASH') {
 2203:         if (ref($settings->{'notify'}) eq 'HASH') {
 2204:             if ($settings->{'notify'}{'approval'} ne '') {
 2205:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2206:             }
 2207:         }
 2208:     }
 2209:     my $numinrow = 2;
 2210:     my $css_class;
 2211:     if ($$rowtotal%2) {
 2212:         $css_class = 'LC_odd_row';
 2213:     }
 2214:     if ($customcss) {
 2215:         $css_class .= " $customcss";
 2216:     }
 2217:     $css_class =~ s/^\s+//;
 2218:     if ($css_class) {
 2219:         $css_class = ' class="'.$css_class.'"';
 2220:     }
 2221:     if ($rowstyle) {
 2222:         $css_class .= ' style="'.$rowstyle.'"';
 2223:     }
 2224:     my $text;
 2225:     if ($action eq 'requestcourses') {
 2226:         $text = &mt('Receive notification of course requests requiring approval');
 2227:     } elsif ($action eq 'requestauthor') {
 2228:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2229:     } else {
 2230:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2231:     }
 2232:     $datatable = '<tr'.$css_class.'>'.
 2233:                  ' <td>'.$text.'</td>'.
 2234:                  ' <td class="LC_left_item">';
 2235:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2236:                                                  $action.'notifyapproval',%currapp);
 2237:     if ($numdc > 0) {
 2238:         $datatable .= $table;
 2239:     } else {
 2240:         $datatable .= &mt('There are no active Domain Coordinators');
 2241:     }
 2242:     $datatable .='</td></tr>';
 2243:     return $datatable;
 2244: }
 2245: 
 2246: sub print_studentcode {
 2247:     my ($settings,$rowtotal) = @_;
 2248:     my $rownum = 0; 
 2249:     my ($output,%current);
 2250:     my @crstypes = ('official','unofficial','community','textbook');
 2251:     if (ref($settings) eq 'HASH') {
 2252:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2253:             foreach my $type (@crstypes) {
 2254:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2255:             }
 2256:         }
 2257:     }
 2258:     $output .= '<tr>'.
 2259:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2260:                '<td class="LC_left_item">';
 2261:     foreach my $type (@crstypes) {
 2262:         my $check = ' ';
 2263:         if ($current{$type}) {
 2264:             $check = ' checked="checked" ';
 2265:         }
 2266:         $output .= '<span class="LC_nobreak"><label>'.
 2267:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2268:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2269:     }
 2270:     $output .= '</td></tr>';
 2271:     $$rowtotal ++;
 2272:     return $output;
 2273: }
 2274: 
 2275: sub print_textbookcourses {
 2276:     my ($dom,$type,$settings,$rowtotal) = @_;
 2277:     my $rownum = 0;
 2278:     my $css_class;
 2279:     my $itemcount = 1;
 2280:     my $maxnum = 0;
 2281:     my $bookshash;
 2282:     if (ref($settings) eq 'HASH') {
 2283:         $bookshash = $settings->{$type};
 2284:     }
 2285:     my %ordered;
 2286:     if (ref($bookshash) eq 'HASH') {
 2287:         foreach my $item (keys(%{$bookshash})) {
 2288:             if (ref($bookshash->{$item}) eq 'HASH') {
 2289:                 my $num = $bookshash->{$item}{'order'};
 2290:                 $ordered{$num} = $item;
 2291:             }
 2292:         }
 2293:     }
 2294:     my $confname = $dom.'-domainconfig';
 2295:     my $switchserver = &check_switchserver($dom,$confname);
 2296:     my $maxnum = scalar(keys(%ordered));
 2297:     my $datatable;
 2298:     if (keys(%ordered)) {
 2299:         my @items = sort { $a <=> $b } keys(%ordered);
 2300:         for (my $i=0; $i<@items; $i++) {
 2301:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2302:             my $key = $ordered{$items[$i]};
 2303:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2304:             my $coursetitle = $coursehash{'description'};
 2305:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2306:             if (ref($bookshash->{$key}) eq 'HASH') {
 2307:                 $subject = $bookshash->{$key}->{'subject'};
 2308:                 $title = $bookshash->{$key}->{'title'};
 2309:                 if ($type eq 'textbooks') {
 2310:                     $publisher = $bookshash->{$key}->{'publisher'};
 2311:                     $author = $bookshash->{$key}->{'author'};
 2312:                     $image = $bookshash->{$key}->{'image'};
 2313:                     if ($image ne '') {
 2314:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2315:                         my $imagethumb = "$path/tn-".$imagefile;
 2316:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2317:                     }
 2318:                 }
 2319:             }
 2320:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2321:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2322:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2323:             for (my $k=0; $k<=$maxnum; $k++) {
 2324:                 my $vpos = $k+1;
 2325:                 my $selstr;
 2326:                 if ($k == $i) {
 2327:                     $selstr = ' selected="selected" ';
 2328:                 }
 2329:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2330:             }
 2331:             $datatable .= '</select>'.('&nbsp;'x2).
 2332:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2333:                 &mt('Delete?').'</label></span></td>'.
 2334:                 '<td colspan="2">'.
 2335:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2336:                 ('&nbsp;'x2).
 2337:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2338:             if ($type eq 'textbooks') {
 2339:                 $datatable .= ('&nbsp;'x2).
 2340:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2341:                               ('&nbsp;'x2).
 2342:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2343:                               ('&nbsp;'x2).
 2344:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2345:                 if ($image) {
 2346:                     $datatable .= '<span class="LC_nobreak">'.
 2347:                                   $imgsrc.
 2348:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2349:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2350:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2351:                 }
 2352:                 if ($switchserver) {
 2353:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2354:                 } else {
 2355:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2356:                 }
 2357:             }
 2358:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2359:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2360:                           $coursetitle.'</span></td></tr>'."\n";
 2361:             $itemcount ++;
 2362:         }
 2363:     }
 2364:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2365:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2366:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2367:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2368:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2369:     for (my $k=0; $k<$maxnum+1; $k++) {
 2370:         my $vpos = $k+1;
 2371:         my $selstr;
 2372:         if ($k == $maxnum) {
 2373:             $selstr = ' selected="selected" ';
 2374:         }
 2375:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2376:     }
 2377:     $datatable .= '</select>&nbsp;'."\n".
 2378:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2379:                   '<td colspan="2">'.
 2380:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2381:                   ('&nbsp;'x2).
 2382:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2383:                   ('&nbsp;'x2);
 2384:     if ($type eq 'textbooks') {
 2385:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2386:                       ('&nbsp;'x2).
 2387:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2388:                       ('&nbsp;'x2).
 2389:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2390:         if ($switchserver) {
 2391:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2392:         } else {
 2393:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2394:         }
 2395:         $datatable .= '</span>'."\n";
 2396:     }
 2397:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2398:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2399:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2400:                   &Apache::loncommon::selectcourse_link
 2401:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2402:                   '</span></td>'."\n".
 2403:                   '</tr>'."\n";
 2404:     $itemcount ++;
 2405:     return $datatable;
 2406: }
 2407: 
 2408: sub textbookcourses_javascript {
 2409:     my ($settings) = @_;
 2410:     return unless(ref($settings) eq 'HASH');
 2411:     my (%ordered,%total,%jstext);
 2412:     foreach my $type ('textbooks','templates') {
 2413:         $total{$type} = 0;
 2414:         if (ref($settings->{$type}) eq 'HASH') {
 2415:             foreach my $item (keys(%{$settings->{$type}})) {
 2416:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2417:                     my $num = $settings->{$type}->{$item}{'order'};
 2418:                     $ordered{$type}{$num} = $item;
 2419:                 }
 2420:             }
 2421:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2422:         }
 2423:         my @jsarray = ();
 2424:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2425:             push(@jsarray,$ordered{$type}{$item});
 2426:         }
 2427:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2428:     }
 2429:     return <<"ENDSCRIPT";
 2430: <script type="text/javascript">
 2431: // <![CDATA[
 2432: function reorderBooks(form,item,caller) {
 2433:     var changedVal;
 2434: $jstext{'textbooks'};
 2435: $jstext{'templates'};
 2436:     var newpos;
 2437:     var maxh;
 2438:     if (caller == 'textbooks') {  
 2439:         newpos = 'textbooks_addbook_pos';
 2440:         maxh = 1 + $total{'textbooks'};
 2441:     } else {
 2442:         newpos = 'templates_addbook_pos';
 2443:         maxh = 1 + $total{'templates'};
 2444:     }
 2445:     var current = new Array;
 2446:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2447:     if (item == newpos) {
 2448:         changedVal = newitemVal;
 2449:     } else {
 2450:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2451:         current[newitemVal] = newpos;
 2452:     }
 2453:     if (caller == 'textbooks') {
 2454:         for (var i=0; i<textbooks.length; i++) {
 2455:             var elementName = 'textbooks_'+textbooks[i];
 2456:             if (elementName != item) {
 2457:                 if (form.elements[elementName]) {
 2458:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2459:                     current[currVal] = elementName;
 2460:                 }
 2461:             }
 2462:         }
 2463:     }
 2464:     if (caller == 'templates') {
 2465:         for (var i=0; i<templates.length; i++) {
 2466:             var elementName = 'templates_'+templates[i];
 2467:             if (elementName != item) {
 2468:                 if (form.elements[elementName]) {
 2469:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2470:                     current[currVal] = elementName;
 2471:                 }
 2472:             }
 2473:         }
 2474:     }
 2475:     var oldVal;
 2476:     for (var j=0; j<maxh; j++) {
 2477:         if (current[j] == undefined) {
 2478:             oldVal = j;
 2479:         }
 2480:     }
 2481:     if (oldVal < changedVal) {
 2482:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2483:            var elementName = current[k];
 2484:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2485:         }
 2486:     } else {
 2487:         for (var k=changedVal; k<oldVal; k++) {
 2488:             var elementName = current[k];
 2489:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2490:         }
 2491:     }
 2492:     return;
 2493: }
 2494: 
 2495: // ]]>
 2496: </script>
 2497: 
 2498: ENDSCRIPT
 2499: }
 2500: 
 2501: sub print_autoenroll {
 2502:     my ($dom,$settings,$rowtotal) = @_;
 2503:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2504:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2505:     if (ref($settings) eq 'HASH') {
 2506:         if (exists($settings->{'run'})) {
 2507:             if ($settings->{'run'} eq '0') {
 2508:                 $runoff = ' checked="checked" ';
 2509:                 $runon = ' ';
 2510:             } else {
 2511:                 $runon = ' checked="checked" ';
 2512:                 $runoff = ' ';
 2513:             }
 2514:         } else {
 2515:             if ($autorun) {
 2516:                 $runon = ' checked="checked" ';
 2517:                 $runoff = ' ';
 2518:             } else {
 2519:                 $runoff = ' checked="checked" ';
 2520:                 $runon = ' ';
 2521:             }
 2522:         }
 2523:         if (exists($settings->{'co-owners'})) {
 2524:             if ($settings->{'co-owners'} eq '0') {
 2525:                 $coownersoff = ' checked="checked" ';
 2526:                 $coownerson = ' ';
 2527:             } else {
 2528:                 $coownerson = ' checked="checked" ';
 2529:                 $coownersoff = ' ';
 2530:             }
 2531:         } else {
 2532:             $coownersoff = ' checked="checked" ';
 2533:             $coownerson = ' ';
 2534:         }
 2535:         if (exists($settings->{'sender_domain'})) {
 2536:             $defdom = $settings->{'sender_domain'};
 2537:         }
 2538:         if (exists($settings->{'autofailsafe'})) {
 2539:             $failsafe = $settings->{'autofailsafe'};
 2540:         }
 2541:     } else {
 2542:         if ($autorun) {
 2543:             $runon = ' checked="checked" ';
 2544:             $runoff = ' ';
 2545:         } else {
 2546:             $runoff = ' checked="checked" ';
 2547:             $runon = ' ';
 2548:         }
 2549:     }
 2550:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2551:     my $notif_sender;
 2552:     if (ref($settings) eq 'HASH') {
 2553:         $notif_sender = $settings->{'sender_uname'};
 2554:     }
 2555:     my $datatable='<tr class="LC_odd_row">'.
 2556:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2557:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2558:                   '<input type="radio" name="autoenroll_run"'.
 2559:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2560:                   '<label><input type="radio" name="autoenroll_run"'.
 2561:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2562:                   '</tr><tr>'.
 2563:                   '<td>'.&mt('Notification messages - sender').
 2564:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2565:                   &mt('username').':&nbsp;'.
 2566:                   '<input type="text" name="sender_uname" value="'.
 2567:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2568:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2569:                   '<tr class="LC_odd_row">'.
 2570:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2571:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2572:                   '<input type="radio" name="autoassign_coowners"'.
 2573:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2574:                   '<label><input type="radio" name="autoassign_coowners"'.
 2575:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2576:                   '</tr><tr>'.
 2577:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2578:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2579:                   '<input type="text" name="autoenroll_failsafe"'.
 2580:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 2581:     $$rowtotal += 4;
 2582:     return $datatable;
 2583: }
 2584: 
 2585: sub print_autoupdate {
 2586:     my ($position,$dom,$settings,$rowtotal) = @_;
 2587:     my $datatable;
 2588:     if ($position eq 'top') {
 2589:         my $updateon = ' ';
 2590:         my $updateoff = ' checked="checked" ';
 2591:         my $classlistson = ' ';
 2592:         my $classlistsoff = ' checked="checked" ';
 2593:         if (ref($settings) eq 'HASH') {
 2594:             if ($settings->{'run'} eq '1') {
 2595:                 $updateon = $updateoff;
 2596:                 $updateoff = ' ';
 2597:             }
 2598:             if ($settings->{'classlists'} eq '1') {
 2599:                 $classlistson = $classlistsoff;
 2600:                 $classlistsoff = ' ';
 2601:             }
 2602:         }
 2603:         my %title = (
 2604:                    run => 'Auto-update active?',
 2605:                    classlists => 'Update information in classlists?',
 2606:                     );
 2607:         $datatable = '<tr class="LC_odd_row">'. 
 2608:                   '<td>'.&mt($title{'run'}).'</td>'.
 2609:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2610:                   '<input type="radio" name="autoupdate_run"'.
 2611:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2612:                   '<label><input type="radio" name="autoupdate_run"'.
 2613:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2614:                   '</tr><tr>'.
 2615:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2616:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2617:                   '<label><input type="radio" name="classlists"'.
 2618:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2619:                   '<label><input type="radio" name="classlists"'.
 2620:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2621:                   '</tr>';
 2622:         $$rowtotal += 2;
 2623:     } elsif ($position eq 'middle') {
 2624:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2625:         my $numinrow = 3;
 2626:         my $locknamesettings;
 2627:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2628:                                      $dom,$numinrow,$othertitle,
 2629:                                     'lockablenames',$rowtotal);
 2630:         $$rowtotal ++;
 2631:     } else {
 2632:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2633:         my @fields = ('lastname','firstname','middlename','generation',
 2634:                       'permanentemail','id');
 2635:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2636:         my $numrows = 0;
 2637:         if (ref($types) eq 'ARRAY') {
 2638:             if (@{$types} > 0) {
 2639:                 $datatable = 
 2640:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2641:                                          \@fields,$types,\$numrows);
 2642:                     $$rowtotal += @{$types}; 
 2643:             }
 2644:         }
 2645:         $datatable .= 
 2646:             &usertype_update_row($settings,{'default' => $othertitle},
 2647:                                  \%fieldtitles,\@fields,['default'],
 2648:                                  \$numrows);
 2649:         $$rowtotal ++;     
 2650:     }
 2651:     return $datatable;
 2652: }
 2653: 
 2654: sub print_autocreate {
 2655:     my ($dom,$settings,$rowtotal) = @_;
 2656:     my (%createon,%createoff,%currhash);
 2657:     my @types = ('xml','req');
 2658:     if (ref($settings) eq 'HASH') {
 2659:         foreach my $item (@types) {
 2660:             $createoff{$item} = ' checked="checked" ';
 2661:             $createon{$item} = ' ';
 2662:             if (exists($settings->{$item})) {
 2663:                 if ($settings->{$item}) {
 2664:                     $createon{$item} = ' checked="checked" ';
 2665:                     $createoff{$item} = ' ';
 2666:                 }
 2667:             }
 2668:         }
 2669:         if ($settings->{'xmldc'} ne '') {
 2670:             $currhash{$settings->{'xmldc'}} = 1;
 2671:         }
 2672:     } else {
 2673:         foreach my $item (@types) {
 2674:             $createoff{$item} = ' checked="checked" ';
 2675:             $createon{$item} = ' ';
 2676:         }
 2677:     }
 2678:     $$rowtotal += 2;
 2679:     my $numinrow = 2;
 2680:     my $datatable='<tr class="LC_odd_row">'.
 2681:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2682:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2683:                   '<input type="radio" name="autocreate_xml"'.
 2684:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2685:                   '<label><input type="radio" name="autocreate_xml"'.
 2686:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2687:                   '</td></tr><tr>'.
 2688:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2689:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2690:                   '<input type="radio" name="autocreate_req"'.
 2691:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2692:                   '<label><input type="radio" name="autocreate_req"'.
 2693:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2694:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2695:                                                    'autocreate_xmldc',%currhash);
 2696:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2697:     if ($numdc > 1) {
 2698:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2699:                       '</td><td class="LC_left_item">';
 2700:     } else {
 2701:         $datatable .= &mt('Course creation processed as:').
 2702:                       '</td><td class="LC_right_item">';
 2703:     }
 2704:     $datatable .= $dctable.'</td></tr>';
 2705:     $$rowtotal += $rows;
 2706:     return $datatable;
 2707: }
 2708: 
 2709: sub print_directorysrch {
 2710:     my ($position,$dom,$settings,$rowtotal) = @_;
 2711:     my $datatable;
 2712:     if ($position eq 'top') {
 2713:         my $instsrchon = ' ';
 2714:         my $instsrchoff = ' checked="checked" ';
 2715:         my ($exacton,$containson,$beginson);
 2716:         my $instlocalon = ' ';
 2717:         my $instlocaloff = ' checked="checked" ';
 2718:         if (ref($settings) eq 'HASH') {
 2719:             if ($settings->{'available'} eq '1') {
 2720:                 $instsrchon = $instsrchoff;
 2721:                 $instsrchoff = ' ';
 2722:             }
 2723:             if ($settings->{'localonly'} eq '1') {
 2724:                 $instlocalon = $instlocaloff;
 2725:                 $instlocaloff = ' ';
 2726:             }
 2727:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2728:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2729:                     if ($type eq 'exact') {
 2730:                         $exacton = ' checked="checked" ';
 2731:                     } elsif ($type eq 'contains') {
 2732:                         $containson = ' checked="checked" ';
 2733:                     } elsif ($type eq 'begins') {
 2734:                         $beginson = ' checked="checked" ';
 2735:                     }
 2736:                 }
 2737:             } else {
 2738:                 if ($settings->{'searchtypes'} eq 'exact') {
 2739:                     $exacton = ' checked="checked" ';
 2740:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2741:                     $containson = ' checked="checked" ';
 2742:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2743:                     $exacton = ' checked="checked" ';
 2744:                     $containson = ' checked="checked" ';
 2745:                 }
 2746:             }
 2747:         }
 2748:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2749:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2750: 
 2751:         my $numinrow = 4;
 2752:         my $cansrchrow = 0;
 2753:         $datatable='<tr class="LC_odd_row">'.
 2754:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2755:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2756:                    '<input type="radio" name="dirsrch_available"'.
 2757:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2758:                    '<label><input type="radio" name="dirsrch_available"'.
 2759:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2760:                    '</tr><tr>'.
 2761:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2762:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2763:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2764:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2765:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2766:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2767:                    '</tr>';
 2768:         $$rowtotal += 2;
 2769:         if (ref($usertypes) eq 'HASH') {
 2770:             if (keys(%{$usertypes}) > 0) {
 2771:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2772:                                              $numinrow,$othertitle,'cansearch',
 2773:                                              $rowtotal);
 2774:                 $cansrchrow = 1;
 2775:             }
 2776:         }
 2777:         if ($cansrchrow) {
 2778:             $$rowtotal ++;
 2779:             $datatable .= '<tr>';
 2780:         } else {
 2781:             $datatable .= '<tr class="LC_odd_row">';
 2782:         }
 2783:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2784:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2785:         foreach my $title (@{$titleorder}) {
 2786:             if (defined($searchtitles->{$title})) {
 2787:                 my $check = ' ';
 2788:                 if (ref($settings) eq 'HASH') {
 2789:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2790:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2791:                             $check = ' checked="checked" ';
 2792:                         }
 2793:                     }
 2794:                 }
 2795:                 $datatable .= '<td class="LC_left_item">'.
 2796:                               '<span class="LC_nobreak"><label>'.
 2797:                               '<input type="checkbox" name="searchby" '.
 2798:                               'value="'.$title.'"'.$check.'/>'.
 2799:                               $searchtitles->{$title}.'</label></span></td>';
 2800:             }
 2801:         }
 2802:         $datatable .= '</tr></table></td></tr>';
 2803:         $$rowtotal ++;
 2804:         if ($cansrchrow) {
 2805:             $datatable .= '<tr class="LC_odd_row">';
 2806:         } else {
 2807:             $datatable .= '<tr>';
 2808:         }
 2809:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2810:                       '<td class="LC_left_item" colspan="2">'.
 2811:                       '<span class="LC_nobreak"><label>'.
 2812:                       '<input type="checkbox" name="searchtypes" '.
 2813:                       $exacton.' value="exact" />'.&mt('Exact match').
 2814:                       '</label>&nbsp;'.
 2815:                       '<label><input type="checkbox" name="searchtypes" '.
 2816:                       $beginson.' value="begins" />'.&mt('Begins with').
 2817:                       '</label>&nbsp;'.
 2818:                       '<label><input type="checkbox" name="searchtypes" '.
 2819:                       $containson.' value="contains" />'.&mt('Contains').
 2820:                       '</label></span></td></tr>';
 2821:         $$rowtotal ++;
 2822:     } else {
 2823:         my $domsrchon = ' checked="checked" ';
 2824:         my $domsrchoff = ' ';
 2825:         my $domlocalon = ' ';
 2826:         my $domlocaloff = ' checked="checked" ';
 2827:         if (ref($settings) eq 'HASH') {
 2828:             if ($settings->{'lclocalonly'} eq '1') {
 2829:                 $domlocalon = $domlocaloff;
 2830:                 $domlocaloff = ' ';
 2831:             }
 2832:             if ($settings->{'lcavailable'} eq '0') {
 2833:                 $domsrchoff = $domsrchon;
 2834:                 $domsrchon = ' ';
 2835:             }
 2836:         }
 2837:         $datatable='<tr class="LC_odd_row">'.
 2838:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2839:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2840:                       '<input type="radio" name="dirsrch_domavailable"'.
 2841:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2842:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2843:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2844:                       '</tr><tr>'.
 2845:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2846:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2847:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2848:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2849:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2850:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2851:                       '</tr>';
 2852:         $$rowtotal += 2;
 2853:     }
 2854:     return $datatable;
 2855: }
 2856: 
 2857: sub print_contacts {
 2858:     my ($position,$dom,$settings,$rowtotal) = @_;
 2859:     my $datatable;
 2860:     my @contacts = ('adminemail','supportemail');
 2861:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2862:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2863:     if ($position eq 'top') {
 2864:         if (ref($settings) eq 'HASH') {
 2865:             foreach my $item (@contacts) {
 2866:                 if (exists($settings->{$item})) {
 2867:                     $to{$item} = $settings->{$item};
 2868:                 }
 2869:             }
 2870:         }
 2871:     } elsif ($position eq 'middle') {
 2872:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2873:                      'updatesmail','idconflictsmail','hostipmail');
 2874:         foreach my $type (@mailings) {
 2875:             $otheremails{$type} = '';
 2876:         }
 2877:     } else {
 2878:         @mailings = ('helpdeskmail','otherdomsmail');
 2879:         foreach my $type (@mailings) {
 2880:             $otheremails{$type} = '';
 2881:         }
 2882:         $bccemails{'helpdeskmail'} = '';
 2883:         $bccemails{'otherdomsmail'} = '';
 2884:         $includestr{'helpdeskmail'} = '';
 2885:         $includestr{'otherdomsmail'} = '';
 2886:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2887:     }
 2888:     if (ref($settings) eq 'HASH') {
 2889:         unless ($position eq 'top') {
 2890:             foreach my $type (@mailings) {
 2891:                 if (exists($settings->{$type})) {
 2892:                     if (ref($settings->{$type}) eq 'HASH') {
 2893:                         foreach my $item (@contacts) {
 2894:                             if ($settings->{$type}{$item}) {
 2895:                                 $checked{$type}{$item} = ' checked="checked" ';
 2896:                             }
 2897:                         }
 2898:                         $otheremails{$type} = $settings->{$type}{'others'};
 2899:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2900:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2901:                             if ($settings->{$type}{'include'} ne '') {
 2902:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2903:                                 $includestr{$type} = &unescape($includestr{$type});
 2904:                             }
 2905:                         }
 2906:                     }
 2907:                 } elsif ($type eq 'lonstatusmail') {
 2908:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2909:                 }
 2910:             }
 2911:         }
 2912:         if ($position eq 'bottom') {
 2913:             foreach my $type (@mailings) {
 2914:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 2915:                 if ($settings->{$type}{'include'} ne '') {
 2916:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2917:                     $includestr{$type} = &unescape($includestr{$type});
 2918:                 }
 2919:             }
 2920:             if (ref($settings->{'helpform'}) eq 'HASH') {
 2921:                 if (ref($fields) eq 'ARRAY') {
 2922:                     foreach my $field (@{$fields}) {
 2923:                         $currfield{$field} = $settings->{'helpform'}{$field};
 2924:                     }
 2925:                 }
 2926:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 2927:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 2928:                 } else {
 2929:                     $maxsize = '1.0';
 2930:                 }
 2931:             } else {
 2932:                 if (ref($fields) eq 'ARRAY') {
 2933:                     foreach my $field (@{$fields}) {
 2934:                         $currfield{$field} = 'yes';
 2935:                     }
 2936:                 }
 2937:                 $maxsize = '1.0';
 2938:             }
 2939:         }
 2940:     } else {
 2941:         if ($position eq 'top') {
 2942:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2943:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2944:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2945:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2946:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2947:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2948:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2949:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2950:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 2951:         } elsif ($position eq 'bottom') {
 2952:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2953:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 2954:             if (ref($fields) eq 'ARRAY') {
 2955:                 foreach my $field (@{$fields}) {
 2956:                     $currfield{$field} = 'yes';
 2957:                 }
 2958:             }
 2959:             $maxsize = '1.0';
 2960:         }
 2961:     }
 2962:     my ($titles,$short_titles) = &contact_titles();
 2963:     my $rownum = 0;
 2964:     my $css_class;
 2965:     if ($position eq 'top') {
 2966:         foreach my $item (@contacts) {
 2967:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2968:             $datatable .= '<tr'.$css_class.'>'. 
 2969:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 2970:                           '</span></td><td class="LC_right_item">'.
 2971:                           '<input type="text" name="'.$item.'" value="'.
 2972:                           $to{$item}.'" /></td></tr>';
 2973:             $rownum ++;
 2974:         }
 2975:     } elsif ($position eq 'bottom') {
 2976:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2977:         $datatable .= '<tr'.$css_class.'>'.
 2978:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 2979:                       &mt('(e-mail, subject, and description always shown)').
 2980:                       '</td><td class="LC_left_item">';
 2981:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 2982:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 2983:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 2984:             foreach my $field (@{$fields}) {
 2985:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 2986:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 2987:                     $datatable .= ' '.&mt('(logged-in users)');
 2988:                 }
 2989:                 $datatable .='</td><td>';
 2990:                 my $clickaction;
 2991:                 if ($field eq 'screenshot') {
 2992:                     $clickaction = ' onclick="screenshotSize(this);"';
 2993:                 }
 2994:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 2995:                     foreach my $option (@{$possoptions->{$field}}) {
 2996:                         my $checked;
 2997:                         if ($currfield{$field} eq $option) {
 2998:                             $checked = ' checked="checked"';
 2999:                         }
 3000:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3001:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3002:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3003:                                       '</label></span>'.('&nbsp;'x2);
 3004:                     }
 3005:                 }
 3006:                 if ($field eq 'screenshot') {
 3007:                     my $display;
 3008:                     if ($currfield{$field} eq 'no') {
 3009:                         $display = ' style="display:none"';
 3010:                     }
 3011:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3012:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3013:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3014:                 }
 3015:                 $datatable .= '</td></tr>';
 3016:             }
 3017:             $datatable .= '</table>';
 3018:         }
 3019:         $datatable .= '</td></tr>'."\n";
 3020:         $rownum ++;
 3021:     }
 3022:     unless ($position eq 'top') {
 3023:         foreach my $type (@mailings) {
 3024:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3025:             $datatable .= '<tr'.$css_class.'>'.
 3026:                           '<td><span class="LC_nobreak">'.
 3027:                           $titles->{$type}.': </span></td>'.
 3028:                           '<td class="LC_left_item">';
 3029:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3030:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3031:             }
 3032:             $datatable .= '<span class="LC_nobreak">';
 3033:             foreach my $item (@contacts) {
 3034:                 $datatable .= '<label>'.
 3035:                               '<input type="checkbox" name="'.$type.'"'.
 3036:                               $checked{$type}{$item}.
 3037:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3038:                               '</label>&nbsp;';
 3039:             }
 3040:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3041:                           '<input type="text" name="'.$type.'_others" '.
 3042:                           'value="'.$otheremails{$type}.'"  />';
 3043:             my %locchecked;
 3044:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3045:                 foreach my $loc ('s','b') {
 3046:                     if ($includeloc{$type} eq $loc) {
 3047:                         $locchecked{$loc} = ' checked="checked"';
 3048:                         last;
 3049:                     }
 3050:                 }
 3051:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3052:                               '<input type="text" name="'.$type.'_bcc" '.
 3053:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3054:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3055:                               &mt('Text automatically added to e-mail:').' '.
 3056:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3057:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3058:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3059:                               ('&nbsp;'x2).
 3060:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3061:                               '</span></fieldset>';
 3062:             }
 3063:             $datatable .= '</td></tr>'."\n";
 3064:             $rownum ++;
 3065:         }
 3066:     }
 3067:     if ($position eq 'middle') {
 3068:         my %choices;
 3069:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3070:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3071:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3072:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3073:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3074:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3075:         my @toggles = ('reporterrors','reportupdates');
 3076:         my %defaultchecked = ('reporterrors'  => 'on',
 3077:                               'reportupdates' => 'on');
 3078:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3079:                                                    \%choices,$rownum);
 3080:         $datatable .= $reports;
 3081:     } elsif ($position eq 'bottom') {
 3082:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3083:         my (@posstypes,%usertypeshash);
 3084:         if (ref($types) eq 'ARRAY') {
 3085:             @posstypes = @{$types};
 3086:         }
 3087:         if (@posstypes) {
 3088:             if (ref($usertypes) eq 'HASH') {
 3089:                 %usertypeshash = %{$usertypes};
 3090:             }
 3091:             my @overridden;
 3092:             my $numinrow = 4;
 3093:             if (ref($settings) eq 'HASH') {
 3094:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3095:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3096:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3097:                             push(@overridden,$key);
 3098:                             foreach my $item (@contacts) {
 3099:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3100:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3101:                                 }
 3102:                             }
 3103:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3104:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3105:                             $includeloc{'override_'.$key} = '';
 3106:                             $includestr{'override_'.$key} = '';
 3107:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3108:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 3109:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3110:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3111:                             }
 3112:                         }
 3113:                     }
 3114:                 }
 3115:             }
 3116:             my $customclass = 'LC_helpdesk_override';
 3117:             my $optionsprefix = 'LC_options_helpdesk_';
 3118: 
 3119:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3120: 
 3121:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3122:                                          $numinrow,$othertitle,'overrides',
 3123:                                          \$rownum,$onclicktypes,$customclass);
 3124:             $rownum ++;
 3125:             $usertypeshash{'default'} = $othertitle;
 3126:             foreach my $status (@posstypes) {
 3127:                 my $css_class;
 3128:                 if ($rownum%2) {
 3129:                     $css_class = 'LC_odd_row ';
 3130:                 }
 3131:                 $css_class .= $customclass;
 3132:                 my $rowid = $optionsprefix.$status;
 3133:                 my $hidden = 1;
 3134:                 my $currstyle = 'display:none';
 3135:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3136:                     $currstyle = 'display:table-row';
 3137:                     $hidden = 0;
 3138:                 }
 3139:                 my $key = 'override_'.$status;
 3140:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3141:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3142:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3143:                                                   \@contacts,$short_titles);
 3144:                 unless ($hidden) {
 3145:                     $rownum ++;
 3146:                 }
 3147:             }
 3148:         }
 3149:     }
 3150:     $$rowtotal += $rownum;
 3151:     return $datatable;
 3152: }
 3153: 
 3154: sub overridden_helpdesk {
 3155:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3156:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3157:     my $class = 'LC_left_item';
 3158:     if ($css_class) {
 3159:         $css_class = ' class="'.$css_class.'"';
 3160:     }
 3161:     if ($rowid) {
 3162:         $rowid = ' id="'.$rowid.'"';
 3163:     }
 3164:     if ($rowstyle) {
 3165:         $rowstyle = ' style="'.$rowstyle.'"';
 3166:     }
 3167:     my ($output,$description);
 3168:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3169:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3170:               "<td>$description</td>\n".
 3171:               '<td class="'.$class.'" colspan="2">'.
 3172:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3173:               '<span class="LC_nobreak">';
 3174:     if (ref($contacts) eq 'ARRAY') {
 3175:         foreach my $item (@{$contacts}) {
 3176:             my $check;
 3177:             if (ref($checked) eq 'HASH') {
 3178:                $check = $checked->{$item};
 3179:             }
 3180:             my $title;
 3181:             if (ref($short_titles) eq 'HASH') {
 3182:                 $title = $short_titles->{$item};
 3183:             }
 3184:             $output .= '<label>'.
 3185:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3186:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3187:         }
 3188:     }
 3189:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3190:                '<input type="text" name="override_'.$type.'_others" '.
 3191:                'value="'.$otheremails.'"  />';
 3192:     my %locchecked;
 3193:     foreach my $loc ('s','b') {
 3194:         if ($includeloc eq $loc) {
 3195:             $locchecked{$loc} = ' checked="checked"';
 3196:             last;
 3197:         }
 3198:     }
 3199:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3200:                '<input type="text" name="override_'.$type.'_bcc" '.
 3201:                'value="'.$bccemails.'"  /></fieldset>'.
 3202:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3203:                &mt('Text automatically added to e-mail:').' '.
 3204:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3205:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3206:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3207:                ('&nbsp;'x2).
 3208:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3209:                '</span></fieldset>'.
 3210:                '</td></tr>'."\n";
 3211:     return $output;
 3212: }
 3213: 
 3214: sub contacts_javascript {
 3215:     return <<"ENDSCRIPT";
 3216: 
 3217: <script type="text/javascript">
 3218: // <![CDATA[
 3219: 
 3220: function screenshotSize(field) {
 3221:     if (document.getElementById('help_screenshotsize')) {
 3222:         if (field.value == 'no') {
 3223:             document.getElementById('help_screenshotsize').style.display="none";
 3224:         } else {
 3225:             document.getElementById('help_screenshotsize').style.display="";
 3226:         }
 3227:     }
 3228:     return;
 3229: }
 3230: 
 3231: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3232:     if (form.elements[checkbox].length != undefined) {
 3233:         var count = 0;
 3234:         if (docount) {
 3235:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3236:                 if (form.elements[checkbox][i].checked) {
 3237:                     count ++;
 3238:                 }
 3239:             }
 3240:         }
 3241:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3242:             var type = form.elements[checkbox][i].value;
 3243:             if (document.getElementById(prefix+type)) {
 3244:                 if (form.elements[checkbox][i].checked) {
 3245:                     document.getElementById(prefix+type).style.display = 'table-row';
 3246:                     if (count % 2 == 1) {
 3247:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3248:                     } else {
 3249:                         document.getElementById(prefix+type).className = target;
 3250:                     }
 3251:                     count ++;
 3252:                 } else {
 3253:                     document.getElementById(prefix+type).style.display = 'none';
 3254:                 }
 3255:             }
 3256:         }
 3257:     }
 3258:     return;
 3259: }
 3260: 
 3261: // ]]>
 3262: </script>
 3263: 
 3264: ENDSCRIPT
 3265: }
 3266: 
 3267: sub print_helpsettings {
 3268:     my ($position,$dom,$settings,$rowtotal) = @_;
 3269:     my $confname = $dom.'-domainconfig';
 3270:     my $formname = 'display';
 3271:     my ($datatable,$itemcount);
 3272:     if ($position eq 'top') {
 3273:         $itemcount = 1;
 3274:         my (%choices,%defaultchecked,@toggles);
 3275:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3276:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3277:                                      &mt('LON-CAPA bug tracker'),600,500));
 3278:         %defaultchecked = ('submitbugs' => 'on');
 3279:         @toggles = ('submitbugs');
 3280:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3281:                                                      \%choices,$itemcount);
 3282:         $$rowtotal ++;
 3283:     } else {
 3284:         my $css_class;
 3285:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3286:         my (%customroles,%ordered,%current);
 3287:         if (ref($settings) eq 'HASH') {
 3288:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3289:                 %current = %{$settings->{'adhoc'}};
 3290:             }
 3291:         }
 3292:         my $count = 0;
 3293:         foreach my $key (sort(keys(%existing))) {
 3294:             if ($key=~/^rolesdef\_(\w+)$/) {
 3295:                 my $rolename = $1;
 3296:                 my (%privs,$order);
 3297:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3298:                 $customroles{$rolename} = \%privs;
 3299:                 if (ref($current{$rolename}) eq 'HASH') {
 3300:                     $order = $current{$rolename}{'order'};
 3301:                 }
 3302:                 if ($order eq '') {
 3303:                     $order = $count;
 3304:                 }
 3305:                 $ordered{$order} = $rolename;
 3306:                 $count++;
 3307:             }
 3308:         }
 3309:         my $maxnum = scalar(keys(%ordered));
 3310:         my @roles_by_num = ();
 3311:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3312:             push(@roles_by_num,$item);
 3313:         }
 3314:         my $context = 'domprefs';
 3315:         my $crstype = 'Course';
 3316:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3317:         my @accesstypes = ('all','dh','da','none');
 3318:         my ($numstatustypes,@jsarray);
 3319:         if (ref($types) eq 'ARRAY') {
 3320:             if (@{$types} > 0) {
 3321:                 $numstatustypes = scalar(@{$types});
 3322:                 push(@accesstypes,'status');
 3323:                 @jsarray = ('bystatus');
 3324:             }
 3325:         }
 3326:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3327:         if (keys(%domhelpdesk)) {
 3328:             push(@accesstypes,('inc','exc'));
 3329:             push(@jsarray,('notinc','notexc'));
 3330:         }
 3331:         my $hiddenstr = join("','",@jsarray);
 3332:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3333:         my $context = 'domprefs';
 3334:         my $crstype = 'Course';
 3335:         my $prefix = 'helproles_';
 3336:         my $add_class = 'LC_hidden';
 3337:         foreach my $num (@roles_by_num) {
 3338:             my $role = $ordered{$num};
 3339:             my ($desc,$access,@statuses);
 3340:             if (ref($current{$role}) eq 'HASH') {
 3341:                 $desc = $current{$role}{'desc'};
 3342:                 $access = $current{$role}{'access'};
 3343:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3344:                     @statuses = @{$current{$role}{'insttypes'}};
 3345:                 }
 3346:             }
 3347:             if ($desc eq '') {
 3348:                 $desc = $role;
 3349:             }
 3350:             my $identifier = 'custhelp'.$num;
 3351:             my %full=();
 3352:             my %levels= (
 3353:                          course => {},
 3354:                          domain => {},
 3355:                          system => {},
 3356:                         );
 3357:             my %levelscurrent=(
 3358:                                course => {},
 3359:                                domain => {},
 3360:                                system => {},
 3361:                               );
 3362:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3363:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3364:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3365:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3366:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3367:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3368:             for (my $k=0; $k<=$maxnum; $k++) {
 3369:                 my $vpos = $k+1;
 3370:                 my $selstr;
 3371:                 if ($k == $num) {
 3372:                     $selstr = ' selected="selected" ';
 3373:                 }
 3374:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3375:             }
 3376:             $datatable .= '</select>'.('&nbsp;'x2).
 3377:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3378:                           '</td>'.
 3379:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3380:                           &mt('Name shown to users:').
 3381:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3382:                           '</fieldset>'.
 3383:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3384:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3385:                           '<fieldset>'.
 3386:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3387:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3388:                                                                    \%levelscurrent,$identifier,
 3389:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3390:                           '</fieldset></td>';
 3391:             $itemcount ++;
 3392:         }
 3393:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3394:         my $newcust = 'custhelp'.$count;
 3395:         my (%privs,%levelscurrent);
 3396:         my %full=();
 3397:         my %levels= (
 3398:                      course => {},
 3399:                      domain => {},
 3400:                      system => {},
 3401:                     );
 3402:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3403:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3404:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3405:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3406:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3407:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3408:         for (my $k=0; $k<$maxnum+1; $k++) {
 3409:             my $vpos = $k+1;
 3410:             my $selstr;
 3411:             if ($k == $maxnum) {
 3412:                 $selstr = ' selected="selected" ';
 3413:             }
 3414:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3415:         }
 3416:         $datatable .= '</select>&nbsp;'."\n".
 3417:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3418:                       '</label></span></td>'.
 3419:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3420:                       '<span class="LC_nobreak">'.
 3421:                       &mt('Internal name:').
 3422:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3423:                       '</span>'.('&nbsp;'x4).
 3424:                       '<span class="LC_nobreak">'.
 3425:                       &mt('Name shown to users:').
 3426:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3427:                       '</span></fieldset>'.
 3428:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3429:                                              $usertypes,$types,\%domhelpdesk).
 3430:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3431:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3432:                                                                 \@templateroles,$newcust).
 3433:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3434:                                                                \%levelscurrent,$newcust).
 3435:                       '</fieldset>'.
 3436:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 3437:                       '</td></tr>';
 3438:         $count ++;
 3439:         $$rowtotal += $count;
 3440:     }
 3441:     return $datatable;
 3442: }
 3443: 
 3444: sub adhocbutton {
 3445:     my ($prefix,$num,$field,$visibility) = @_;
 3446:     my %lt = &Apache::lonlocal::texthash(
 3447:                                           show => 'Show details',
 3448:                                           hide => 'Hide details',
 3449:                                         );
 3450:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3451:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3452:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3453:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3454: }
 3455: 
 3456: sub helpsettings_javascript {
 3457:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3458:     return unless(ref($roles_by_num) eq 'ARRAY');
 3459:     my %html_js_lt = &Apache::lonlocal::texthash(
 3460:                                           show => 'Show details',
 3461:                                           hide => 'Hide details',
 3462:                                         );
 3463:     &html_escape(\%html_js_lt);
 3464:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3465:     return <<"ENDSCRIPT";
 3466: <script type="text/javascript">
 3467: // <![CDATA[
 3468: 
 3469: function reorderHelpRoles(form,item) {
 3470:     var changedVal;
 3471: $jstext
 3472:     var newpos = 'helproles_${total}_pos';
 3473:     var maxh = 1 + $total;
 3474:     var current = new Array();
 3475:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3476:     if (item == newpos) {
 3477:         changedVal = newitemVal;
 3478:     } else {
 3479:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3480:         current[newitemVal] = newpos;
 3481:     }
 3482:     for (var i=0; i<helproles.length; i++) {
 3483:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3484:         if (elementName != item) {
 3485:             if (form.elements[elementName]) {
 3486:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3487:                 current[currVal] = elementName;
 3488:             }
 3489:         }
 3490:     }
 3491:     var oldVal;
 3492:     for (var j=0; j<maxh; j++) {
 3493:         if (current[j] == undefined) {
 3494:             oldVal = j;
 3495:         }
 3496:     }
 3497:     if (oldVal < changedVal) {
 3498:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3499:            var elementName = current[k];
 3500:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3501:         }
 3502:     } else {
 3503:         for (var k=changedVal; k<oldVal; k++) {
 3504:             var elementName = current[k];
 3505:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3506:         }
 3507:     }
 3508:     return;
 3509: }
 3510: 
 3511: function helpdeskAccess(num) {
 3512:     var curraccess = null;
 3513:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3514:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3515:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3516:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3517:             }
 3518:         }
 3519:     }
 3520:     var shown = Array();
 3521:     var hidden = Array();
 3522:     if (curraccess == 'none') {
 3523:         hidden = Array('$hiddenstr');
 3524:     } else {
 3525:         if (curraccess == 'status') {
 3526:             shown = Array('bystatus');
 3527:             hidden = Array('notinc','notexc');
 3528:         } else {
 3529:             if (curraccess == 'exc') {
 3530:                 shown = Array('notexc');
 3531:                 hidden = Array('notinc','bystatus');
 3532:             }
 3533:             if (curraccess == 'inc') {
 3534:                 shown = Array('notinc');
 3535:                 hidden = Array('notexc','bystatus');
 3536:             }
 3537:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3538:                 hidden = Array('notinc','notexc','bystatus');
 3539:             }
 3540:         }
 3541:     }
 3542:     if (hidden.length > 0) {
 3543:         for (var i=0; i<hidden.length; i++) {
 3544:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3545:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3546:             }
 3547:         }
 3548:     }
 3549:     if (shown.length > 0) {
 3550:         for (var i=0; i<shown.length; i++) {
 3551:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3552:                 if (shown[i] == 'privs') {
 3553:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3554:                 } else {
 3555:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3556:                 }
 3557:             }
 3558:         }
 3559:     }
 3560:     return;
 3561: }
 3562: 
 3563: function toggleHelpdeskItem(num,field) {
 3564:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3565:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3566:             document.getElementById('helproles_'+num+'_'+field).className =
 3567:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3568:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3569:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3570:             }
 3571:         } else {
 3572:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3573:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3574:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3575:             }
 3576:         }
 3577:     }
 3578:     return;
 3579: }
 3580: 
 3581: // ]]>
 3582: </script>
 3583: 
 3584: ENDSCRIPT
 3585: }
 3586: 
 3587: sub helpdeskroles_access {
 3588:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3589:         $usertypes,$types,$domhelpdesk) = @_;
 3590:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3591:     my %lt = &Apache::lonlocal::texthash(
 3592:                     'rou'    => 'Role usage',
 3593:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3594:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3595:                     'dh'     => 'All with domain helpdesk role',
 3596:                     'da'     => 'All with domain helpdesk assistant role',
 3597:                     'none'   => 'None',
 3598:                     'status' => 'Determined based on institutional status',
 3599:                     'inc'    => 'Include all, but exclude specific personnel',
 3600:                     'exc'    => 'Exclude all, but include specific personnel',
 3601:                   );
 3602:     my %usecheck = (
 3603:                      all => ' checked="checked"',
 3604:                    );
 3605:     my %displaydiv = (
 3606:                       status => 'none',
 3607:                       inc    => 'none',
 3608:                       exc    => 'none',
 3609:                       priv   => 'block',
 3610:                      );
 3611:     my $output;
 3612:     if (ref($current) eq 'HASH') {
 3613:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3614:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3615:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3616:                 delete($usecheck{'all'});
 3617:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3618:                     my $access = $1;
 3619:                     $displaydiv{$access} = 'inline';
 3620:                 } elsif ($current->{access} eq 'none') {
 3621:                     $displaydiv{'priv'} = 'none';
 3622:                 }
 3623:             }
 3624:         }
 3625:     }
 3626:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3627:               '<p>'.$lt{'whi'}.'</p>';
 3628:     foreach my $access (@{$accesstypes}) {
 3629:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3630:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3631:                    $lt{$access}.'</label>';
 3632:         if ($access eq 'status') {
 3633:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3634:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3635:                                                                  $othertitle,$usertypes,$types).
 3636:                        '</div>';
 3637:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3638:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3639:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3640:                        '</div>';
 3641:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3642:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3643:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3644:                        '</div>';
 3645:         }
 3646:         $output .= '</p>';
 3647:     }
 3648:     $output .= '</fieldset>';
 3649:     return $output;
 3650: }
 3651: 
 3652: sub radiobutton_prefs {
 3653:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3654:         $additional,$align) = @_;
 3655:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3656:                    (ref($choices) eq 'HASH'));
 3657: 
 3658:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3659: 
 3660:     foreach my $item (@{$toggles}) {
 3661:         if ($defaultchecked->{$item} eq 'on') {
 3662:             $checkedon{$item} = ' checked="checked" ';
 3663:             $checkedoff{$item} = ' ';
 3664:         } elsif ($defaultchecked->{$item} eq 'off') {
 3665:             $checkedoff{$item} = ' checked="checked" ';
 3666:             $checkedon{$item} = ' ';
 3667:         }
 3668:     }
 3669:     if (ref($settings) eq 'HASH') {
 3670:         foreach my $item (@{$toggles}) {
 3671:             if ($settings->{$item} eq '1') {
 3672:                 $checkedon{$item} =  ' checked="checked" ';
 3673:                 $checkedoff{$item} = ' ';
 3674:             } elsif ($settings->{$item} eq '0') {
 3675:                 $checkedoff{$item} =  ' checked="checked" ';
 3676:                 $checkedon{$item} = ' ';
 3677:             }
 3678:         }
 3679:     }
 3680:     if ($onclick) {
 3681:         $onclick = ' onclick="'.$onclick.'"';
 3682:     }
 3683:     foreach my $item (@{$toggles}) {
 3684:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3685:         $datatable .=
 3686:             '<tr'.$css_class.'><td valign="top">'.
 3687:             '<span class="LC_nobreak">'.$choices->{$item}.
 3688:             '</span></td>';
 3689:         if ($align eq 'left') {
 3690:             $datatable .= '<td class="LC_left_item">';
 3691:         } else {
 3692:             $datatable .= '<td class="LC_right_item">';
 3693:         }
 3694:         $datatable .=
 3695:             '<span class="LC_nobreak">'.
 3696:             '<label><input type="radio" name="'.
 3697:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3698:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3699:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3700:             '</span>'.$additional.
 3701:             '</td>'.
 3702:             '</tr>';
 3703:         $itemcount ++;
 3704:     }
 3705:     return ($datatable,$itemcount);
 3706: }
 3707: 
 3708: sub print_coursedefaults {
 3709:     my ($position,$dom,$settings,$rowtotal) = @_;
 3710:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 3711:     my $itemcount = 1;
 3712:     my %choices =  &Apache::lonlocal::texthash (
 3713:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 3714:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 3715:         coursecredits        => 'Credits can be specified for courses',
 3716:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 3717:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 3718:         texengine            => 'Default method to display mathematics',
 3719:         postsubmit           => 'Disable submit button/keypress following student submission',
 3720:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 3721:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 3722:     );
 3723:     my %staticdefaults = (
 3724:                            anonsurvey_threshold => 10,
 3725:                            uploadquota          => 500,
 3726:                            postsubmit           => 60,
 3727:                            mysqltables          => 172800,
 3728:                          );
 3729:     if ($position eq 'top') {
 3730:         %defaultchecked = (
 3731:                             'uselcmath'       => 'on',
 3732:                             'usejsme'         => 'on',
 3733:                             'canclone'        => 'none',
 3734:                           );
 3735:         @toggles = ('uselcmath','usejsme');
 3736:         my $deftex = $Apache::lonnet::deftex;
 3737:         if (ref($settings) eq 'HASH') {
 3738:             if ($settings->{'texengine'}) {
 3739:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 3740:                     $deftex = $settings->{'texengine'};
 3741:                 }
 3742:             }
 3743:         }
 3744:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3745:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 3746:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 3747:                        '</span></td><td class="LC_right_item">'.
 3748:                        '<select name="texengine">'."\n";
 3749:         my %texoptions = (
 3750:                             MathJax  => 'MathJax',
 3751:                             mimetex  => &mt('Convert to Images'),
 3752:                             tth      => &mt('TeX to HTML'),
 3753:                          );
 3754:         foreach my $renderer ('MathJax','mimetex','tth') {
 3755:             my $selected = '';
 3756:             if ($renderer eq $deftex) {
 3757:                 $selected = ' selected="selected"';
 3758:             }
 3759:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 3760:         }
 3761:         $mathdisp .= '</select></td></tr>'."\n";
 3762:         $itemcount ++;
 3763:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3764:                                                      \%choices,$itemcount);
 3765:         $datatable = $mathdisp.$datatable;
 3766:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3767:         $datatable .=
 3768:             '<tr'.$css_class.'><td valign="top">'.
 3769:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 3770:             '</span></td><td class="LC_left_item">';
 3771:         my $currcanclone = 'none';
 3772:         my $onclick;
 3773:         my @cloneoptions = ('none','domain');
 3774:         my %clonetitles = (
 3775:                              none     => 'No additional course requesters',
 3776:                              domain   => "Any course requester in course's domain",
 3777:                              instcode => 'Course requests for official courses ...',
 3778:                           );
 3779:         my (%codedefaults,@code_order,@posscodes);
 3780:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 3781:                                                     \@code_order) eq 'ok') {
 3782:             if (@code_order > 0) {
 3783:                 push(@cloneoptions,'instcode');
 3784:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 3785:             }
 3786:         }
 3787:         if (ref($settings) eq 'HASH') {
 3788:             if ($settings->{'canclone'}) {
 3789:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 3790:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 3791:                         if (@code_order > 0) {
 3792:                             $currcanclone = 'instcode';
 3793:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 3794:                         }
 3795:                     }
 3796:                 } elsif ($settings->{'canclone'} eq 'domain') {
 3797:                     $currcanclone = $settings->{'canclone'};
 3798:                 }
 3799:             }
 3800:         }
 3801:         foreach my $option (@cloneoptions) {
 3802:             my ($checked,$additional);
 3803:             if ($currcanclone eq $option) {
 3804:                 $checked = ' checked="checked"';
 3805:             }
 3806:             if ($option eq 'instcode') {
 3807:                 if (@code_order) {
 3808:                     my $show = 'none';
 3809:                     if ($checked) {
 3810:                         $show = 'block';
 3811:                     }
 3812:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 3813:                                   &mt('Institutional codes for new and cloned course have identical:').
 3814:                                   '<br />';
 3815:                     foreach my $item (@code_order) {
 3816:                         my $codechk;
 3817:                         if ($checked) {
 3818:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 3819:                                 $codechk = ' checked="checked"';
 3820:                             }
 3821:                         }
 3822:                         $additional .= '<label>'.
 3823:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 3824:                                        $item.'</label>';
 3825:                     }
 3826:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 3827:                 }
 3828:             }
 3829:             $datatable .=
 3830:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 3831:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 3832:                 '</label>&nbsp;'.$additional.'</span><br />';
 3833:         }
 3834:         $datatable .= '</td>'.
 3835:                       '</tr>';
 3836:         $itemcount ++;
 3837:     } else {
 3838:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3839:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 3840:         my $currusecredits = 0;
 3841:         my $postsubmitclient = 1;
 3842:         my @types = ('official','unofficial','community','textbook');
 3843:         if (ref($settings) eq 'HASH') {
 3844:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 3845:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 3846:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3847:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3848:                 }
 3849:             }
 3850:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3851:                 foreach my $type (@types) {
 3852:                     next if ($type eq 'community');
 3853:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3854:                     if ($defcredits{$type} ne '') {
 3855:                         $currusecredits = 1;
 3856:                     }
 3857:                 }
 3858:             }
 3859:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3860:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3861:                     $postsubmitclient = 0;
 3862:                     foreach my $type (@types) {
 3863:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3864:                     }
 3865:                 } else {
 3866:                     foreach my $type (@types) {
 3867:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3868:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3869:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 3870:                             } else {
 3871:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3872:                             }
 3873:                         } else {
 3874:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3875:                         }
 3876:                     }
 3877:                 }
 3878:             } else {
 3879:                 foreach my $type (@types) {
 3880:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3881:                 }
 3882:             }
 3883:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 3884:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 3885:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 3886:                 }
 3887:             } else {
 3888:                 foreach my $type (@types) {
 3889:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 3890:                 }
 3891:             }
 3892:         } else {
 3893:             foreach my $type (@types) {
 3894:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3895:             }
 3896:         }
 3897:         if (!$currdefresponder) {
 3898:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3899:         } elsif ($currdefresponder < 1) {
 3900:             $currdefresponder = 1;
 3901:         }
 3902:         foreach my $type (@types) {
 3903:             if ($curruploadquota{$type} eq '') {
 3904:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3905:             }
 3906:         }
 3907:         $datatable .=
 3908:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3909:                 $choices{'anonsurvey_threshold'}.
 3910:                 '</span></td>'.
 3911:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3912:                 '<input type="text" name="anonsurvey_threshold"'.
 3913:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3914:                 '</td></tr>'."\n";
 3915:         $itemcount ++;
 3916:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3917:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3918:                       $choices{'uploadquota'}.
 3919:                       '</span></td>'.
 3920:                       '<td align="right" class="LC_right_item">'.
 3921:                       '<table><tr>';
 3922:         foreach my $type (@types) {
 3923:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3924:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3925:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3926:         }
 3927:         $datatable .= '</tr></table></td></tr>'."\n";
 3928:         $itemcount ++;
 3929:         my $onclick = "toggleDisplay(this.form,'credits');";
 3930:         my $display = 'none';
 3931:         if ($currusecredits) {
 3932:             $display = 'block';
 3933:         }
 3934:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3935:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3936:         foreach my $type (@types) {
 3937:             next if ($type eq 'community');
 3938:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3939:                            '<input type="text" name="'.$type.'_credits"'.
 3940:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3941:         }
 3942:         $additional .= '</tr></table></div>'."\n";
 3943:         %defaultchecked = ('coursecredits' => 'off');
 3944:         @toggles = ('coursecredits');
 3945:         my $current = {
 3946:                         'coursecredits' => $currusecredits,
 3947:                       };
 3948:         (my $table,$itemcount) =
 3949:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3950:                                \%choices,$itemcount,$onclick,$additional,'left');
 3951:         $datatable .= $table;
 3952:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3953:         my $display = 'none';
 3954:         if ($postsubmitclient) {
 3955:             $display = 'block';
 3956:         }
 3957:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3958:                       &mt('Number of seconds submit is disabled').'<br />'.
 3959:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3960:                       '<table><tr>';
 3961:         foreach my $type (@types) {
 3962:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3963:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3964:                            $deftimeout{$type}.'" size="5" /></td>';
 3965:         }
 3966:         $additional .= '</tr></table></div>'."\n";
 3967:         %defaultchecked = ('postsubmit' => 'on');
 3968:         @toggles = ('postsubmit');
 3969:         $current = {
 3970:                        'postsubmit' => $postsubmitclient,
 3971:                    };
 3972:         ($table,$itemcount) =
 3973:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3974:                                \%choices,$itemcount,$onclick,$additional,'left');
 3975:         $datatable .= $table;
 3976:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3977:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3978:                       $choices{'mysqltables'}.
 3979:                       '</span></td>'.
 3980:                       '<td align="right" class="LC_right_item">'.
 3981:                       '<table><tr>';
 3982:         foreach my $type (@types) {
 3983:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3984:                            '<input type="text" name="mysqltables_'.$type.'"'.
 3985:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 3986:         }
 3987:         $datatable .= '</tr></table></td></tr>'."\n";
 3988:         $itemcount ++;
 3989: 
 3990:     }
 3991:     $$rowtotal += $itemcount;
 3992:     return $datatable;
 3993: }
 3994: 
 3995: sub print_selfenrollment {
 3996:     my ($position,$dom,$settings,$rowtotal) = @_;
 3997:     my ($css_class,$datatable);
 3998:     my $itemcount = 1;
 3999:     my @types = ('official','unofficial','community','textbook');
 4000:     if (($position eq 'top') || ($position eq 'middle')) {
 4001:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4002:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4003:         my @rows;
 4004:         my $key;
 4005:         if ($position eq 'top') {
 4006:             $key = 'admin'; 
 4007:             if (ref($rowsref) eq 'ARRAY') {
 4008:                 @rows = @{$rowsref};
 4009:             }
 4010:         } elsif ($position eq 'middle') {
 4011:             $key = 'default';
 4012:             @rows = ('types','registered','approval','limit');
 4013:         }
 4014:         foreach my $row (@rows) {
 4015:             if (defined($titlesref->{$row})) {
 4016:                 $itemcount ++;
 4017:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4018:                 $datatable .= '<tr'.$css_class.'>'.
 4019:                               '<td>'.$titlesref->{$row}.'</td>'.
 4020:                               '<td class="LC_left_item">'.
 4021:                               '<table><tr>';
 4022:                 my (%current,%currentcap);
 4023:                 if (ref($settings) eq 'HASH') {
 4024:                     if (ref($settings->{$key}) eq 'HASH') {
 4025:                         foreach my $type (@types) {
 4026:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4027:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4028:                             }
 4029:                             if (($row eq 'limit') && ($key eq 'default')) {
 4030:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4031:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4032:                                 }
 4033:                             }
 4034:                         }
 4035:                     }
 4036:                 }
 4037:                 my %roles = (
 4038:                              '0' => &Apache::lonnet::plaintext('dc'),
 4039:                             ); 
 4040:             
 4041:                 foreach my $type (@types) {
 4042:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4043:                         $datatable .= '<th>'.&mt($type).'</th>';
 4044:                     }
 4045:                 }
 4046:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4047:                     $datatable .= '</tr><tr>';
 4048:                 }
 4049:                 foreach my $type (@types) {
 4050:                     if ($type eq 'community') {
 4051:                         $roles{'1'} = &mt('Community personnel');
 4052:                     } else {
 4053:                         $roles{'1'} = &mt('Course personnel');
 4054:                     }
 4055:                     $datatable .= '<td style="vertical-align: top">';
 4056:                     if ($position eq 'top') {
 4057:                         my %checked;
 4058:                         if ($current{$type} eq '0') {
 4059:                             $checked{'0'} = ' checked="checked"';
 4060:                         } else {
 4061:                             $checked{'1'} = ' checked="checked"';
 4062:                         }
 4063:                         foreach my $role ('1','0') {
 4064:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4065:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4066:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4067:                                           $roles{$role}.'</label></span> ';
 4068:                         }
 4069:                     } else {
 4070:                         if ($row eq 'types') {
 4071:                             my %checked;
 4072:                             if ($current{$type} =~ /^(all|dom)$/) {
 4073:                                 $checked{$1} = ' checked="checked"';
 4074:                             } else {
 4075:                                 $checked{''} = ' checked="checked"';
 4076:                             }
 4077:                             foreach my $val ('','dom','all') {
 4078:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4079:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4080:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4081:                             }
 4082:                         } elsif ($row eq 'registered') {
 4083:                             my %checked;
 4084:                             if ($current{$type} eq '1') {
 4085:                                 $checked{'1'} = ' checked="checked"';
 4086:                             } else {
 4087:                                 $checked{'0'} = ' checked="checked"';
 4088:                             }
 4089:                             foreach my $val ('0','1') {
 4090:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4091:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4092:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4093:                             }
 4094:                         } elsif ($row eq 'approval') {
 4095:                             my %checked;
 4096:                             if ($current{$type} =~ /^([12])$/) {
 4097:                                 $checked{$1} = ' checked="checked"';
 4098:                             } else {
 4099:                                 $checked{'0'} = ' checked="checked"';
 4100:                             }
 4101:                             for my $val (0..2) {
 4102:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4103:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4104:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4105:                             }
 4106:                         } elsif ($row eq 'limit') {
 4107:                             my %checked;
 4108:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4109:                                 $checked{$1} = ' checked="checked"';
 4110:                             } else {
 4111:                                 $checked{'none'} = ' checked="checked"';
 4112:                             }
 4113:                             my $cap;
 4114:                             if ($currentcap{$type} =~ /^\d+$/) {
 4115:                                 $cap = $currentcap{$type};
 4116:                             }
 4117:                             foreach my $val ('none','allstudents','selfenrolled') {
 4118:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4119:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4120:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4121:                             }
 4122:                             $datatable .= '<br />'.
 4123:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4124:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4125:                                           '</span>'; 
 4126:                         }
 4127:                     }
 4128:                     $datatable .= '</td>';
 4129:                 }
 4130:                 $datatable .= '</tr>';
 4131:             }
 4132:             $datatable .= '</table></td></tr>';
 4133:         }
 4134:     } elsif ($position eq 'bottom') {
 4135:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4136:     }
 4137:     $$rowtotal += $itemcount;
 4138:     return $datatable;
 4139: }
 4140: 
 4141: sub print_validation_rows {
 4142:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4143:     my ($itemsref,$namesref,$fieldsref);
 4144:     if ($caller eq 'selfenroll') { 
 4145:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4146:     } elsif ($caller eq 'requestcourses') {
 4147:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4148:     }
 4149:     my %currvalidation;
 4150:     if (ref($settings) eq 'HASH') {
 4151:         if (ref($settings->{'validation'}) eq 'HASH') {
 4152:             %currvalidation = %{$settings->{'validation'}};
 4153:         }
 4154:     }
 4155:     my $datatable;
 4156:     my $itemcount = 0;
 4157:     foreach my $item (@{$itemsref}) {
 4158:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4159:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4160:                       $namesref->{$item}.
 4161:                       '</span></td>'.
 4162:                       '<td class="LC_left_item">';
 4163:         if (($item eq 'url') || ($item eq 'button')) {
 4164:             $datatable .= '<span class="LC_nobreak">'.
 4165:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4166:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4167:         } elsif ($item eq 'fields') {
 4168:             my @currfields;
 4169:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4170:                 @currfields = @{$currvalidation{$item}};
 4171:             }
 4172:             foreach my $field (@{$fieldsref}) {
 4173:                 my $check = '';
 4174:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4175:                     $check = ' checked="checked"';
 4176:                 }
 4177:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4178:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4179:                               ' value="'.$field.'"'.$check.' />'.$field.
 4180:                               '</label></span> ';
 4181:             }
 4182:         } elsif ($item eq 'markup') {
 4183:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 4184:                            $currvalidation{$item}.
 4185:                               '</textarea>';
 4186:         }
 4187:         $datatable .= '</td></tr>'."\n";
 4188:         if (ref($rowtotal)) {
 4189:             $itemcount ++;
 4190:         }
 4191:     }
 4192:     if ($caller eq 'requestcourses') {
 4193:         my %currhash;
 4194:         if (ref($settings) eq 'HASH') {
 4195:             if (ref($settings->{'validation'}) eq 'HASH') {
 4196:                 if ($settings->{'validation'}{'dc'} ne '') {
 4197:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4198:                 }
 4199:             }
 4200:         }
 4201:         my $numinrow = 2;
 4202:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4203:                                                        'validationdc',%currhash);
 4204:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4205:         $datatable .= '<tr'.$css_class.'><td>';
 4206:         if ($numdc > 1) {
 4207:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4208:         } else {
 4209:             $datatable .=  &mt('Course creation processed as: ');
 4210:         }
 4211:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4212:         $itemcount ++;
 4213:     }
 4214:     if (ref($rowtotal)) {
 4215:         $$rowtotal += $itemcount;
 4216:     }
 4217:     return $datatable;
 4218: }
 4219: 
 4220: sub print_passwords {
 4221:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 4222:     my ($datatable,$css_class);
 4223:     my $itemcount = 0;
 4224:     my %titles = &Apache::lonlocal::texthash (
 4225:         captcha        => '"Forgot Password" CAPTCHA validation',
 4226:         link           => 'Reset link expiration (hours)',
 4227:         case           => 'Case-sensitive usernames/e-mail',
 4228:         prelink        => 'Information required (form 1)',
 4229:         postlink       => 'Information required (form 2)',
 4230:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 4231:         customtext     => 'Domain specific text (HTML)',
 4232:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 4233:         intauth_check  => 'Check bcrypt cost if authenticated',
 4234:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 4235:         permanent      => 'Permanent e-mail address',
 4236:         critical       => 'Critical notification address',
 4237:         notify         => 'Notification address',
 4238:         min            => 'Minimum password length',
 4239:         max            => 'Maximum password length',
 4240:         chars          => 'Required characters',
 4241:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 4242:     );
 4243:     if ($position eq 'top') {
 4244:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4245:         my $shownlinklife = 2;
 4246:         my $prelink = 'both';
 4247:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 4248:         if (ref($settings) eq 'HASH') {
 4249:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 4250:                 $shownlinklife = $settings->{resetlink};
 4251:             }
 4252:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 4253:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 4254:             }
 4255:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 4256:                 $prelink = $settings->{resetprelink};
 4257:             }
 4258:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 4259:                 %postlink = %{$settings->{resetpostlink}};
 4260:             }
 4261:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 4262:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 4263:             }
 4264:             if ($settings->{resetremove}) {
 4265:                 $nostdtext = 1;
 4266:             }
 4267:             if ($settings->{resetcustom}) {
 4268:                 $customurl = $settings->{resetcustom};
 4269:             }
 4270:         } else {
 4271:             if (ref($types) eq 'ARRAY') {
 4272:                 foreach my $item (@{$types}) {
 4273:                     $casesens{$item} = 1;
 4274:                     $postlink{$item} = ['username','email'];
 4275:                 }
 4276:             }
 4277:             $casesens{'default'} = 1;
 4278:             $postlink{'default'} = ['username','email'];
 4279:             $prelink = 'both';
 4280:             %emailsrc = (
 4281:                           permanent => 1,
 4282:                           critical  => 1,
 4283:                           notify    => 1,
 4284:             );
 4285:         }
 4286:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 4287:         $itemcount ++;
 4288:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4289:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 4290:                       '<td class="LC_left_item">'.
 4291:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 4292:                       'name="passwords_link" size="3" /></td></tr>';
 4293:         $itemcount ++;
 4294:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4295:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 4296:                       '<td class="LC_left_item">';
 4297:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4298:             foreach my $item (@{$types}) {
 4299:                 my $checkedcase;
 4300:                 if ($casesens{$item}) {
 4301:                     $checkedcase = ' checked="checked"';
 4302:                 }
 4303:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4304:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 4305:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 4306:                               '</span>&nbsp;&nbsp; ';
 4307:             }
 4308:         }
 4309:         my $checkedcase;
 4310:         if ($casesens{'default'}) {
 4311:             $checkedcase = ' checked="checked"';
 4312:         }
 4313:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 4314:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 4315:                       $othertitle.'</label></span></td>';
 4316:         $itemcount ++;
 4317:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4318:         my %checkedpre = (
 4319:                              both => ' checked="checked"',
 4320:                              either => '',
 4321:                          );
 4322:         if ($prelink eq 'either') {
 4323:             $checkedpre{either} = ' checked="checked"';
 4324:             $checkedpre{both} = '';
 4325:         }
 4326:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 4327:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4328:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 4329:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 4330:                       '<span class="LC_nobreak"><label>'.
 4331:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 4332:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 4333:         $itemcount ++;
 4334:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4335:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 4336:                       '<td class="LC_left_item">';
 4337:         my %postlinked;
 4338:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4339:             foreach my $item (@{$types}) {
 4340:                 undef(%postlinked);
 4341:                 $datatable .= '<fieldset style="display: inline-block;">'.
 4342:                               '<legend>'.$usertypes->{$item}.'</legend>';
 4343:                 if (ref($postlink{$item}) eq 'ARRAY') {
 4344:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 4345:                 }
 4346:                 foreach my $field ('email','username') {
 4347:                     my $checked;
 4348:                     if ($postlinked{$field}) {
 4349:                         $checked = ' checked="checked"';
 4350:                     }
 4351:                     $datatable .= '<span class="LC_nobreak"><label>'.
 4352:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 4353:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 4354:                                   '<span>&nbsp;&nbsp; ';
 4355:                 }
 4356:                 $datatable .= '</fieldset>';
 4357:             }
 4358:         }
 4359:         if (ref($postlink{'default'}) eq 'ARRAY') {
 4360:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 4361:         }
 4362:         $datatable .= '<fieldset style="display: inline-block;">'.
 4363:                       '<legend>'.$othertitle.'</legend>';
 4364:         foreach my $field ('email','username') {
 4365:             my $checked;
 4366:             if ($postlinked{$field}) {
 4367:                 $checked = ' checked="checked"';
 4368:             }
 4369:             $datatable .= '<span class="LC_nobreak"><label>'.
 4370:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 4371:                           $field.'"'.$checked.' />'.$field.'</label>'.
 4372:                           '<span>&nbsp;&nbsp; ';
 4373:         }
 4374:         $datatable .= '</fieldset></td></tr>';
 4375:         $itemcount ++;
 4376:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4377:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 4378:                       '<td class="LC_left_item">';
 4379:         foreach my $type ('permanent','critical','notify') {
 4380:             my $checkedemail;
 4381:             if ($emailsrc{$type}) {
 4382:                 $checkedemail = ' checked="checked"';
 4383:             }
 4384:             $datatable .= '<span class="LC_nobreak"><label>'.
 4385:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 4386:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 4387:                           '<span>&nbsp;&nbsp; ';
 4388:         }
 4389:         $datatable .= '</td></tr>';
 4390:         $itemcount ++;
 4391:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4392:         my $switchserver = &check_switchserver($dom,$confname);
 4393:         my ($showstd,$noshowstd);
 4394:         if ($nostdtext) {
 4395:             $noshowstd = ' checked="checked"';
 4396:         } else {
 4397:             $showstd = ' checked="checked"';
 4398:         }
 4399:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 4400:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4401:                       &mt('Retain standard text:').
 4402:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 4403:                       &mt('Yes').'</label>'.'&nbsp;'.
 4404:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 4405:                       &mt('No').'</label></span><br />'.
 4406:                       '<span class="LC_fontsize_small">'.
 4407:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 4408:                       &mt('Include custom text:');
 4409:         if ($customurl) {
 4410:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 4411:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4412:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 4413:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 4414:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 4415:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 4416:         }
 4417:         if ($switchserver) {
 4418:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 4419:         } else {
 4420:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4421:                          '<input type="file" name="passwords_customfile" /></span>';
 4422:         }
 4423:         $datatable .= '</td></tr>';
 4424:     } elsif ($position eq 'middle') {
 4425:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 4426:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 4427:         my %defaults;
 4428:         if (ref($domconf{'defaults'}) eq 'HASH') {
 4429:             %defaults = %{$domconf{'defaults'}};
 4430:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 4431:                 $defaults{'intauth_cost'} = 10;
 4432:             }
 4433:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 4434:                 $defaults{'intauth_check'} = 0;
 4435:             }
 4436:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 4437:                 $defaults{'intauth_switch'} = 0;
 4438:             }
 4439:         } else {
 4440:             %defaults = (
 4441:                           'intauth_cost'   => 10,
 4442:                           'intauth_check'  => 0,
 4443:                           'intauth_switch' => 0,
 4444:                         );
 4445:         }
 4446:         foreach my $item (@items) {
 4447:             if ($itemcount%2) {
 4448:                 $css_class = '';
 4449:             } else {
 4450:                 $css_class = ' class="LC_odd_row" ';
 4451:             }
 4452:             $datatable .= '<tr'.$css_class.'>'.
 4453:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 4454:                           '</span></td><td class="LC_left_item" colspan="3">';
 4455:             if ($item eq 'intauth_switch') {
 4456:                 my @options = (0,1,2);
 4457:                 my %optiondesc = &Apache::lonlocal::texthash (
 4458:                                    0 => 'No',
 4459:                                    1 => 'Yes',
 4460:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 4461:                                  );
 4462:                 $datatable .= '<table width="100%">';
 4463:                 foreach my $option (@options) {
 4464:                     my $checked = ' ';
 4465:                     if ($defaults{$item} eq $option) {
 4466:                         $checked = ' checked="checked"';
 4467:                     }
 4468:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4469:                                   '<label><input type="radio" name="'.$item.
 4470:                                   '" value="'.$option.'"'.$checked.' />'.
 4471:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4472:                 }
 4473:                 $datatable .= '</table>';
 4474:             } elsif ($item eq 'intauth_check') {
 4475:                 my @options = (0,1,2);
 4476:                 my %optiondesc = &Apache::lonlocal::texthash (
 4477:                                    0 => 'No',
 4478:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 4479:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 4480:                                  );
 4481:                 $datatable .= '<table width="100%">';
 4482:                 foreach my $option (@options) {
 4483:                     my $checked = ' ';
 4484:                     my $onclick;
 4485:                     if ($defaults{$item} eq $option) {
 4486:                         $checked = ' checked="checked"';
 4487:                     }
 4488:                     if ($option == 2) {
 4489:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 4490:                     }
 4491:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4492:                                   '<label><input type="radio" name="'.$item.
 4493:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 4494:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4495:                 }
 4496:                 $datatable .= '</table>';
 4497:             } else {
 4498:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4499:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 4500:             }
 4501:             $datatable .= '</td></tr>';
 4502:             $itemcount ++;
 4503:         }
 4504:     } elsif ($position eq 'lower') {
 4505:         my ($min,$max,%chars,$numsaved);
 4506:         $min = $Apache::lonnet::passwdmin;
 4507:         if (ref($settings) eq 'HASH') {
 4508:             if ($settings->{min}) {
 4509:                 $min = $settings->{min};
 4510:             }
 4511:             if ($settings->{max}) {
 4512:                 $max = $settings->{max};
 4513:             }
 4514:             if (ref($settings->{chars}) eq 'ARRAY') {
 4515:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 4516:             }
 4517:             if ($settings->{numsaved}) {
 4518:                 $numsaved = $settings->{numsaved};
 4519:             }
 4520:         }
 4521:         my %rulenames = &Apache::lonlocal::texthash(
 4522:                                                      uc => 'At least one upper case letter',
 4523:                                                      lc => 'At least one lower case letter',
 4524:                                                      num => 'At least one number',
 4525:                                                      spec => 'At least one non-alphanumeric',
 4526:                                                    );
 4527:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4528:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 4529:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4530:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 4531:                       'onblur="javascript:warnIntPass(this);" />'.
 4532:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 4533:                       '</span></td></tr>';
 4534:         $itemcount ++;
 4535:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4536:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 4537:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4538:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 4539:                       'onblur="javascript:warnIntPass(this);" />'.
 4540:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 4541:                       '</span></td></tr>';
 4542:         $itemcount ++;
 4543:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4544:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 4545:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 4546:                       '</span></td>';
 4547:         my $numinrow = 2;
 4548:         my @possrules = ('uc','lc','num','spec');
 4549:         $datatable .= '<td class="LC_left_item"><table>';
 4550:         for (my $i=0; $i<@possrules; $i++) {
 4551:             my ($rem,$checked);
 4552:             if ($chars{$possrules[$i]}) {
 4553:                 $checked = ' checked="checked"';
 4554:             }
 4555:             $rem = $i%($numinrow);
 4556:             if ($rem == 0) {
 4557:                 if ($i > 0) {
 4558:                     $datatable .= '</tr>';
 4559:                 }
 4560:                 $datatable .= '<tr>';
 4561:             }
 4562:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 4563:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 4564:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 4565:         }
 4566:         my $rem = @possrules%($numinrow);
 4567:         my $colsleft = $numinrow - $rem;
 4568:         if ($colsleft > 1 ) {
 4569:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4570:                           '&nbsp;</td>';
 4571:         } elsif ($colsleft == 1) {
 4572:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4573:         }
 4574:         $datatable .='</table></td></tr>';
 4575:         $itemcount ++;
 4576:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4577:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 4578:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4579:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 4580:                       'onblur="javascript:warnIntPass(this);" />'.
 4581:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 4582:                       '</span></td></tr>';
 4583:     } else {
 4584:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4585:         my %ownerchg = (
 4586:                           by  => {},
 4587:                           for => {},
 4588:                        );
 4589:         my %ownertitles = &Apache::lonlocal::texthash (
 4590:                             by  => 'Course owner status(es) allowed',
 4591:                             for => 'Student status(es) allowed',
 4592:                           );
 4593:         if (ref($settings) eq 'HASH') {
 4594:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 4595:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 4596:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 4597:                 }
 4598:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 4599:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 4600:                 }
 4601:             }
 4602:         }
 4603:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4604:         $datatable .= '<tr '.$css_class.'>'.
 4605:                       '<td>'.
 4606:                       &mt('Requirements').'<ul>'.
 4607:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 4608:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 4609:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 4610:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 4611:                       '</ul>'.
 4612:                       '</td>'.
 4613:                       '<td class="LC_left_item">';
 4614:         foreach my $item ('by','for') {
 4615:             $datatable .= '<fieldset style="display: inline-block;">'.
 4616:                           '<legend>'.$ownertitles{$item}.'</legend>';
 4617:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4618:                 foreach my $type (@{$types}) {
 4619:                     my $checked;
 4620:                     if ($ownerchg{$item}{$type}) {
 4621:                         $checked = ' checked="checked"';
 4622:                     }
 4623:                     $datatable .= '<span class="LC_nobreak"><label>'.
 4624:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 4625:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 4626:                                   '</span>&nbsp;&nbsp; ';
 4627:                 }
 4628:             }
 4629:             my $checked;
 4630:             if ($ownerchg{$item}{'default'}) {
 4631:                 $checked = ' checked="checked"';
 4632:             }
 4633:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 4634:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 4635:                           $othertitle.'</label></span></fieldset>';
 4636:         }
 4637:         $datatable .= '</td></tr>';
 4638:     }
 4639:     return $datatable;
 4640: }
 4641: 
 4642: sub print_usersessions {
 4643:     my ($position,$dom,$settings,$rowtotal) = @_;
 4644:     my ($css_class,$datatable,%checked,%choices);
 4645:     my (%by_ip,%by_location,@intdoms);
 4646:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 4647: 
 4648:     my @alldoms = &Apache::lonnet::all_domains();
 4649:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4650:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4651:     my %altids = &id_for_thisdom(%servers);
 4652:     my $itemcount = 1;
 4653:     if ($position eq 'top') {
 4654:         if (keys(%serverhomes) > 1) {
 4655:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4656:             my $curroffloadnow;
 4657:             if (ref($settings) eq 'HASH') {
 4658:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4659:                     $curroffloadnow = $settings->{'offloadnow'};
 4660:                 }
 4661:             }
 4662:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4663:         } else {
 4664:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4665:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 4666:         }
 4667:     } else {
 4668:         if (keys(%by_location) == 0) {
 4669:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4670:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 4671:         } else {
 4672:             my %lt = &usersession_titles();
 4673:             my $numinrow = 5;
 4674:             my $prefix;
 4675:             my @types;
 4676:             if ($position eq 'bottom') {
 4677:                 $prefix = 'remote';
 4678:                 @types = ('version','excludedomain','includedomain');
 4679:             } else {
 4680:                 $prefix = 'hosted';
 4681:                 @types = ('excludedomain','includedomain');
 4682:             }
 4683:             my (%current,%checkedon,%checkedoff);
 4684:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 4685:             my @locations = sort(keys(%by_location));
 4686:             foreach my $type (@types) {
 4687:                 $checkedon{$type} = '';
 4688:                 $checkedoff{$type} = ' checked="checked"';
 4689:             }
 4690:             if (ref($settings) eq 'HASH') {
 4691:                 if (ref($settings->{$prefix}) eq 'HASH') {
 4692:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 4693:                         $current{$key} = $settings->{$prefix}{$key};
 4694:                         if ($key eq 'version') {
 4695:                             if ($current{$key} ne '') {
 4696:                                 $checkedon{$key} = ' checked="checked"';
 4697:                                 $checkedoff{$key} = '';
 4698:                             }
 4699:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 4700:                             $checkedon{$key} = ' checked="checked"';
 4701:                             $checkedoff{$key} = '';
 4702:                         }
 4703:                     }
 4704:                 }
 4705:             }
 4706:             foreach my $type (@types) {
 4707:                 next if ($type ne 'version' && !@locations);
 4708:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4709:                 $datatable .= '<tr'.$css_class.'>
 4710:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 4711:                                <span class="LC_nobreak">&nbsp;
 4712:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4713:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4714:                 if ($type eq 'version') {
 4715:                     my $selector = '<select name="'.$prefix.'_version">';
 4716:                     foreach my $version (@lcversions) {
 4717:                         my $selected = '';
 4718:                         if ($current{'version'} eq $version) {
 4719:                             $selected = ' selected="selected"';
 4720:                         }
 4721:                         $selector .= ' <option value="'.$version.'"'.
 4722:                                      $selected.'>'.$version.'</option>';
 4723:                     }
 4724:                     $selector .= '</select> ';
 4725:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4726:                 } else {
 4727:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4728:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4729:                                  ' />'.('&nbsp;'x2).
 4730:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 4731:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4732:                                  "\n".
 4733:                                  '</div><div><table>';
 4734:                     my $rem;
 4735:                     for (my $i=0; $i<@locations; $i++) {
 4736:                         my ($showloc,$value,$checkedtype);
 4737:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 4738:                             my $ip = $by_location{$locations[$i]}->[0];
 4739:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 4740:                                  $value = join(':',@{$by_ip{$ip}});
 4741:                                 $showloc = join(', ',@{$by_ip{$ip}});
 4742:                                 if (ref($current{$type}) eq 'ARRAY') {
 4743:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 4744:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4745:                                             $checkedtype = ' checked="checked"';
 4746:                                             last;
 4747:                                         }
 4748:                                     }
 4749:                                 }
 4750:                             }
 4751:                         }
 4752:                         $rem = $i%($numinrow);
 4753:                         if ($rem == 0) {
 4754:                             if ($i > 0) {
 4755:                                 $datatable .= '</tr>';
 4756:                             }
 4757:                             $datatable .= '<tr>';
 4758:                         }
 4759:                         $datatable .= '<td class="LC_left_item">'.
 4760:                                       '<span class="LC_nobreak"><label>'.
 4761:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 4762:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4763:                                       '</label></span></td>';
 4764:                     }
 4765:                     $rem = @locations%($numinrow);
 4766:                     my $colsleft = $numinrow - $rem;
 4767:                     if ($colsleft > 1 ) {
 4768:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4769:                                       '&nbsp;</td>';
 4770:                     } elsif ($colsleft == 1) {
 4771:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4772:                     }
 4773:                     $datatable .= '</tr></table>';
 4774:                 }
 4775:                 $datatable .= '</td></tr>';
 4776:                 $itemcount ++;
 4777:             }
 4778:         }
 4779:     }
 4780:     $$rowtotal += $itemcount;
 4781:     return $datatable;
 4782: }
 4783: 
 4784: sub build_location_hashes {
 4785:     my ($intdoms,$by_ip,$by_location) = @_;
 4786:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4787:                   (ref($by_location) eq 'HASH')); 
 4788:     my %iphost = &Apache::lonnet::get_iphost();
 4789:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4790:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4791:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4792:         foreach my $id (@{$iphost{$primary_ip}}) {
 4793:             my $intdom = &Apache::lonnet::internet_dom($id);
 4794:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4795:                 push(@{$intdoms},$intdom);
 4796:             }
 4797:         }
 4798:     }
 4799:     foreach my $ip (keys(%iphost)) {
 4800:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4801:             foreach my $id (@{$iphost{$ip}}) {
 4802:                 my $location = &Apache::lonnet::internet_dom($id);
 4803:                 if ($location) {
 4804:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 4805:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4806:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4807:                             push(@{$by_ip->{$ip}},$location);
 4808:                         }
 4809:                     } else {
 4810:                         $by_ip->{$ip} = [$location];
 4811:                     }
 4812:                 }
 4813:             }
 4814:         }
 4815:     }
 4816:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4817:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4818:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4819:             my $first = $by_ip->{$ip}->[0];
 4820:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4821:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4822:                     push(@{$by_location->{$first}},$ip);
 4823:                 }
 4824:             } else {
 4825:                 $by_location->{$first} = [$ip];
 4826:             }
 4827:         }
 4828:     }
 4829:     return;
 4830: }
 4831: 
 4832: sub current_offloads_to {
 4833:     my ($dom,$settings,$servers) = @_;
 4834:     my (%spareid,%otherdomconfigs);
 4835:     if (ref($servers) eq 'HASH') {
 4836:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4837:             my $gotspares;
 4838:             if (ref($settings) eq 'HASH') {
 4839:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4840:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4841:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4842:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4843:                         $gotspares = 1;
 4844:                     }
 4845:                 }
 4846:             }
 4847:             unless ($gotspares) {
 4848:                 my $gotspares;
 4849:                 my $serverhomeID =
 4850:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4851:                 my $serverhomedom =
 4852:                     &Apache::lonnet::host_domain($serverhomeID);
 4853:                 if ($serverhomedom ne $dom) {
 4854:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4855:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4856:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4857:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4858:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4859:                                 $gotspares = 1;
 4860:                             }
 4861:                         }
 4862:                     } else {
 4863:                         $otherdomconfigs{$serverhomedom} =
 4864:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4865:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4866:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4867:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4868:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4869:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4870:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4871:                                         $gotspares = 1;
 4872:                                     }
 4873:                                 }
 4874:                             }
 4875:                         }
 4876:                     }
 4877:                 }
 4878:             }
 4879:             unless ($gotspares) {
 4880:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4881:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4882:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4883:                } else {
 4884:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4885:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4886:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4887:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4888:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4889:                     } else {
 4890:                         my %what = (
 4891:                              spareid => 1,
 4892:                         );
 4893:                         my ($result,$returnhash) = 
 4894:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4895:                         if ($result eq 'ok') { 
 4896:                             if (ref($returnhash) eq 'HASH') {
 4897:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4898:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4899:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4900:                                 }
 4901:                             }
 4902:                         }
 4903:                     }
 4904:                 }
 4905:             }
 4906:         }
 4907:     }
 4908:     return %spareid;
 4909: }
 4910: 
 4911: sub spares_row {
 4912:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4913:     my $css_class;
 4914:     my $numinrow = 4;
 4915:     my $itemcount = 1;
 4916:     my $datatable;
 4917:     my %typetitles = &sparestype_titles();
 4918:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4919:         foreach my $server (sort(keys(%{$servers}))) {
 4920:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4921:             my ($othercontrol,$serverdom);
 4922:             if ($serverhome ne $server) {
 4923:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4924:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4925:             } else {
 4926:                 $serverdom = &Apache::lonnet::host_domain($server);
 4927:                 if ($serverdom ne $dom) {
 4928:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4929:                 }
 4930:             }
 4931:             next unless (ref($spareid->{$server}) eq 'HASH');
 4932:             my $checkednow;
 4933:             if (ref($curroffloadnow) eq 'HASH') {
 4934:                 if ($curroffloadnow->{$server}) {
 4935:                     $checkednow = ' checked="checked"';
 4936:                 }
 4937:             }
 4938:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4939:             $datatable .= '<tr'.$css_class.'>
 4940:                            <td rowspan="2">
 4941:                             <span class="LC_nobreak">'.
 4942:                           &mt('[_1] when busy, offloads to:'
 4943:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4944:                           '<span class="LC_nobreak">'."\n".
 4945:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4946:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4947:                           "\n";
 4948:             my (%current,%canselect);
 4949:             my @choices = 
 4950:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4951:             foreach my $type ('primary','default') {
 4952:                 if (ref($spareid->{$server}) eq 'HASH') {
 4953:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4954:                         my @spares = @{$spareid->{$server}{$type}};
 4955:                         if (@spares > 0) {
 4956:                             if ($othercontrol) {
 4957:                                 $current{$type} = join(', ',@spares);
 4958:                             } else {
 4959:                                 $current{$type} .= '<table>';
 4960:                                 my $numspares = scalar(@spares);
 4961:                                 for (my $i=0;  $i<@spares; $i++) {
 4962:                                     my $rem = $i%($numinrow);
 4963:                                     if ($rem == 0) {
 4964:                                         if ($i > 0) {
 4965:                                             $current{$type} .= '</tr>';
 4966:                                         }
 4967:                                         $current{$type} .= '<tr>';
 4968:                                     }
 4969:                                     $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;'.
 4970:                                                        $spareid->{$server}{$type}[$i].
 4971:                                                        '</label></td>'."\n";
 4972:                                 }
 4973:                                 my $rem = @spares%($numinrow);
 4974:                                 my $colsleft = $numinrow - $rem;
 4975:                                 if ($colsleft > 1 ) {
 4976:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4977:                                                        '" class="LC_left_item">'.
 4978:                                                        '&nbsp;</td>';
 4979:                                 } elsif ($colsleft == 1) {
 4980:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4981:                                 }
 4982:                                 $current{$type} .= '</tr></table>';
 4983:                             }
 4984:                         }
 4985:                     }
 4986:                     if ($current{$type} eq '') {
 4987:                         $current{$type} = &mt('None specified');
 4988:                     }
 4989:                     if ($othercontrol) {
 4990:                         if ($type eq 'primary') {
 4991:                             $canselect{$type} = $othercontrol;
 4992:                         }
 4993:                     } else {
 4994:                         $canselect{$type} = 
 4995:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4996:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4997:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4998:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4999:                         if (@choices > 0) {
 5000:                             foreach my $lonhost (@choices) {
 5001:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5002:                             }
 5003:                         }
 5004:                         $canselect{$type} .= '</select>'."\n";
 5005:                     }
 5006:                 } else {
 5007:                     $current{$type} = &mt('Could not be determined');
 5008:                     if ($type eq 'primary') {
 5009:                         $canselect{$type} =  $othercontrol;
 5010:                     }
 5011:                 }
 5012:                 if ($type eq 'default') {
 5013:                     $datatable .= '<tr'.$css_class.'>';
 5014:                 }
 5015:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5016:                               '<td>'.$current{$type}.'</td>'."\n".
 5017:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5018:             }
 5019:             $itemcount ++;
 5020:         }
 5021:     }
 5022:     $$rowtotal += $itemcount;
 5023:     return $datatable;
 5024: }
 5025: 
 5026: sub possible_newspares {
 5027:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5028:     my $serverhostname = &Apache::lonnet::hostname($server);
 5029:     my %excluded;
 5030:     if ($serverhostname ne '') {
 5031:         %excluded = (
 5032:                        $serverhostname => 1,
 5033:                     );
 5034:     }
 5035:     if (ref($currspares) eq 'HASH') {
 5036:         foreach my $type (keys(%{$currspares})) {
 5037:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5038:                 if (@{$currspares->{$type}} > 0) {
 5039:                     foreach my $curr (@{$currspares->{$type}}) {
 5040:                         my $hostname = &Apache::lonnet::hostname($curr);
 5041:                         $excluded{$hostname} = 1;
 5042:                     }
 5043:                 }
 5044:             }
 5045:         }
 5046:     }
 5047:     my @choices;
 5048:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5049:         if (keys(%{$serverhomes}) > 1) {
 5050:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5051:                 unless ($excluded{$name}) {
 5052:                     if (exists($altids->{$serverhomes->{$name}})) {
 5053:                         push(@choices,$altids->{$serverhomes->{$name}});
 5054:                     } else {
 5055:                         push(@choices,$serverhomes->{$name});
 5056:                     }
 5057:                 }
 5058:             }
 5059:         }
 5060:     }
 5061:     return sort(@choices);
 5062: }
 5063: 
 5064: sub print_loadbalancing {
 5065:     my ($dom,$settings,$rowtotal) = @_;
 5066:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5067:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5068:     my $numinrow = 1;
 5069:     my $datatable;
 5070:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5071:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 5072:     if (ref($settings) eq 'HASH') {
 5073:         %existing = %{$settings};
 5074:     }
 5075:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5076:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5077:                                   \%currtargets,\%currrules,\%currcookies);
 5078:     } else {
 5079:         return;
 5080:     }
 5081:     my ($othertitle,$usertypes,$types) =
 5082:         &Apache::loncommon::sorted_inst_types($dom);
 5083:     my $rownum = 8;
 5084:     if (ref($types) eq 'ARRAY') {
 5085:         $rownum += scalar(@{$types});
 5086:     }
 5087:     my @css_class = ('LC_odd_row','LC_even_row');
 5088:     my $balnum = 0;
 5089:     my $islast;
 5090:     my (@toshow,$disabledtext);
 5091:     if (keys(%currbalancer) > 0) {
 5092:         @toshow = sort(keys(%currbalancer));
 5093:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5094:             push(@toshow,'');
 5095:         }
 5096:     } else {
 5097:         @toshow = ('');
 5098:         $disabledtext = &mt('No existing load balancer');
 5099:     }
 5100:     foreach my $lonhost (@toshow) {
 5101:         if ($balnum == scalar(@toshow)-1) {
 5102:             $islast = 1;
 5103:         } else {
 5104:             $islast = 0;
 5105:         }
 5106:         my $cssidx = $balnum%2;
 5107:         my $targets_div_style = 'display: none';
 5108:         my $disabled_div_style = 'display: block';
 5109:         my $homedom_div_style = 'display: none';
 5110:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5111:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5112:                       '<p>';
 5113:         if ($lonhost eq '') {
 5114:             $datatable .= '<span class="LC_nobreak">';
 5115:             if (keys(%currbalancer) > 0) {
 5116:                 $datatable .= &mt('Add balancer:');
 5117:             } else {
 5118:                 $datatable .= &mt('Enable balancer:');
 5119:             }
 5120:             $datatable .= '&nbsp;'.
 5121:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5122:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5123:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5124:                           '<option value="" selected="selected">'.&mt('None').
 5125:                           '</option>'."\n";
 5126:             foreach my $server (sort(keys(%servers))) {
 5127:                 next if ($currbalancer{$server});
 5128:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5129:             }
 5130:             $datatable .=
 5131:                 '</select>'."\n".
 5132:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5133:         } else {
 5134:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5135:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5136:                            &mt('Stop balancing').'</label>'.
 5137:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5138:             $targets_div_style = 'display: block';
 5139:             $disabled_div_style = 'display: none';
 5140:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5141:                 $homedom_div_style = 'display: block';
 5142:             }
 5143:         }
 5144:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5145:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5146:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5147:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5148:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5149:         my @sparestypes = ('primary','default');
 5150:         my %typetitles = &sparestype_titles();
 5151:         my %hostherechecked = (
 5152:                                   no => ' checked="checked"',
 5153:                               );
 5154:         my %balcookiechecked = (
 5155:                                   no => ' checked="checked"',
 5156:                                );
 5157:         foreach my $sparetype (@sparestypes) {
 5158:             my $targettable;
 5159:             for (my $i=0; $i<$numspares; $i++) {
 5160:                 my $checked;
 5161:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5162:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5163:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5164:                             $checked = ' checked="checked"';
 5165:                         }
 5166:                     }
 5167:                 }
 5168:                 my ($chkboxval,$disabled);
 5169:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5170:                     $chkboxval = $spares[$i];
 5171:                 }
 5172:                 if (exists($currbalancer{$spares[$i]})) {
 5173:                     $disabled = ' disabled="disabled"';
 5174:                 }
 5175:                 $targettable .=
 5176:                     '<td><span class="LC_nobreak"><label>'.
 5177:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5178:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5179:                     '</span></label></span></td>';
 5180:                 my $rem = $i%($numinrow);
 5181:                 if ($rem == 0) {
 5182:                     if (($i > 0) && ($i < $numspares-1)) {
 5183:                         $targettable .= '</tr>';
 5184:                     }
 5185:                     if ($i < $numspares-1) {
 5186:                         $targettable .= '<tr>';
 5187:                     }
 5188:                 }
 5189:             }
 5190:             if ($targettable ne '') {
 5191:                 my $rem = $numspares%($numinrow);
 5192:                 my $colsleft = $numinrow - $rem;
 5193:                 if ($colsleft > 1 ) {
 5194:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5195:                                     '&nbsp;</td>';
 5196:                 } elsif ($colsleft == 1) {
 5197:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5198:                 }
 5199:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5200:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5201:             }
 5202:             $hostherechecked{$sparetype} = '';
 5203:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5204:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5205:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5206:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5207:                         $hostherechecked{'no'} = '';
 5208:                     }
 5209:                 }
 5210:             }
 5211:         }
 5212:         if ($currcookies{$lonhost}) {
 5213:             %balcookiechecked = (
 5214:                                     yes => ' checked="checked"',
 5215:                                 );
 5216:         }
 5217:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5218:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5219:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5220:         foreach my $sparetype (@sparestypes) {
 5221:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5222:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5223:                           '</i></label><br />';
 5224:         }
 5225:         $datatable .= &mt('Use balancer cookie').'<br />'.
 5226:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 5227:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 5228:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 5229:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 5230:                       '</div></td></tr>'.
 5231:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5232:                                            $othertitle,$usertypes,$types,\%servers,
 5233:                                            \%currbalancer,$lonhost,
 5234:                                            $targets_div_style,$homedom_div_style,
 5235:                                            $css_class[$cssidx],$balnum,$islast);
 5236:         $$rowtotal += $rownum;
 5237:         $balnum ++;
 5238:     }
 5239:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5240:     return $datatable;
 5241: }
 5242: 
 5243: sub get_loadbalancers_config {
 5244:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 5245:     return unless ((ref($servers) eq 'HASH') &&
 5246:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5247:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 5248:                    (ref($currcookies) eq 'HASH'));
 5249:     if (keys(%{$existing}) > 0) {
 5250:         my $oldlonhost;
 5251:         foreach my $key (sort(keys(%{$existing}))) {
 5252:             if ($key eq 'lonhost') {
 5253:                 $oldlonhost = $existing->{'lonhost'};
 5254:                 $currbalancer->{$oldlonhost} = 1;
 5255:             } elsif ($key eq 'targets') {
 5256:                 if ($oldlonhost) {
 5257:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5258:                 }
 5259:             } elsif ($key eq 'rules') {
 5260:                 if ($oldlonhost) {
 5261:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5262:                 }
 5263:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5264:                 $currbalancer->{$key} = 1;
 5265:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5266:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5267:                 if ($existing->{$key}{'cookie'}) {
 5268:                     $currcookies->{$key} = 1;
 5269:                 }
 5270:             }
 5271:         }
 5272:     } else {
 5273:         my ($balancerref,$targetsref) =
 5274:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5275:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5276:             foreach my $server (sort(keys(%{$balancerref}))) {
 5277:                 $currbalancer->{$server} = 1;
 5278:                 $currtargets->{$server} = $targetsref->{$server};
 5279:             }
 5280:         }
 5281:     }
 5282:     return;
 5283: }
 5284: 
 5285: sub loadbalancing_rules {
 5286:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5287:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5288:         $css_class,$balnum,$islast) = @_;
 5289:     my $output;
 5290:     my $num = 0;
 5291:     my ($alltypes,$othertypes,$titles) =
 5292:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5293:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5294:         foreach my $type (@{$alltypes}) {
 5295:             $num ++;
 5296:             my $current;
 5297:             if (ref($currrules) eq 'HASH') {
 5298:                 $current = $currrules->{$type};
 5299:             }
 5300:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5301:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5302:                     $current = '';
 5303:                 }
 5304:             }
 5305:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5306:                                              $servers,$currbalancer,$lonhost,$dom,
 5307:                                              $targets_div_style,$homedom_div_style,
 5308:                                              $css_class,$balnum,$num,$islast);
 5309:         }
 5310:     }
 5311:     return $output;
 5312: }
 5313: 
 5314: sub loadbalancing_titles {
 5315:     my ($dom,$intdom,$usertypes,$types) = @_;
 5316:     my %othertypes = (
 5317:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5318:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5319:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5320:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5321:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5322:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5323:                      );
 5324:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5325:     my @available;
 5326:     if (ref($types) eq 'ARRAY') {
 5327:         @available = @{$types};
 5328:     }
 5329:     unless (grep(/^default$/,@available)) {
 5330:         push(@available,'default');
 5331:     }
 5332:     unshift(@alltypes,@available);
 5333:     my %titles;
 5334:     foreach my $type (@alltypes) {
 5335:         if ($type =~ /^_LC_/) {
 5336:             $titles{$type} = $othertypes{$type};
 5337:         } elsif ($type eq 'default') {
 5338:             $titles{$type} = &mt('All users from [_1]',$dom);
 5339:             if (ref($types) eq 'ARRAY') {
 5340:                 if (@{$types} > 0) {
 5341:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5342:                 }
 5343:             }
 5344:         } elsif (ref($usertypes) eq 'HASH') {
 5345:             $titles{$type} = $usertypes->{$type};
 5346:         }
 5347:     }
 5348:     return (\@alltypes,\%othertypes,\%titles);
 5349: }
 5350: 
 5351: sub loadbalance_rule_row {
 5352:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5353:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5354:     my @rulenames;
 5355:     my %ruletitles = &offloadtype_text();
 5356:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5357:         @rulenames = ('balancer','offloadedto','specific');
 5358:     } else {
 5359:         @rulenames = ('default','homeserver');
 5360:         if ($type eq '_LC_external') {
 5361:             push(@rulenames,'externalbalancer');
 5362:         } else {
 5363:             push(@rulenames,'specific');
 5364:         }
 5365:         push(@rulenames,'none');
 5366:     }
 5367:     my $style = $targets_div_style;
 5368:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5369:         $style = $homedom_div_style;
 5370:     }
 5371:     my $space;
 5372:     if ($islast && $num == 1) {
 5373:         $space = '<div display="inline-block">&nbsp;</div>';
 5374:     }
 5375:     my $output =
 5376:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5377:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5378:         '<td valaign="top">'.$space.
 5379:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5380:     for (my $i=0; $i<@rulenames; $i++) {
 5381:         my $rule = $rulenames[$i];
 5382:         my ($checked,$extra);
 5383:         if ($rulenames[$i] eq 'default') {
 5384:             $rule = '';
 5385:         }
 5386:         if ($rulenames[$i] eq 'specific') {
 5387:             if (ref($servers) eq 'HASH') {
 5388:                 my $default;
 5389:                 if (($current ne '') && (exists($servers->{$current}))) {
 5390:                     $checked = ' checked="checked"';
 5391:                 }
 5392:                 unless ($checked) {
 5393:                     $default = ' selected="selected"';
 5394:                 }
 5395:                 $extra =
 5396:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5397:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5398:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5399:                     '<option value=""'.$default.'></option>'."\n";
 5400:                 foreach my $server (sort(keys(%{$servers}))) {
 5401:                     if (ref($currbalancer) eq 'HASH') {
 5402:                         next if (exists($currbalancer->{$server}));
 5403:                     }
 5404:                     my $selected;
 5405:                     if ($server eq $current) {
 5406:                         $selected = ' selected="selected"';
 5407:                     }
 5408:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5409:                 }
 5410:                 $extra .= '</select>';
 5411:             }
 5412:         } elsif ($rule eq $current) {
 5413:             $checked = ' checked="checked"';
 5414:         }
 5415:         $output .= '<span class="LC_nobreak"><label>'.
 5416:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5417:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5418:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5419:                    ')"'.$checked.' />&nbsp;';
 5420:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5421:             $output .= $ruletitles{'particular'};
 5422:         } else {
 5423:             $output .= $ruletitles{$rulenames[$i]};
 5424:         }
 5425:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5426:     }
 5427:     $output .= '</div></td></tr>'."\n";
 5428:     return $output;
 5429: }
 5430: 
 5431: sub offloadtype_text {
 5432:     my %ruletitles = &Apache::lonlocal::texthash (
 5433:            'default'          => 'Offloads to default destinations',
 5434:            'homeserver'       => "Offloads to user's home server",
 5435:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5436:            'specific'         => 'Offloads to specific server',
 5437:            'none'             => 'No offload',
 5438:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5439:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5440:            'particular'       => 'Session hosted (after re-auth) on server:',
 5441:     );
 5442:     return %ruletitles;
 5443: }
 5444: 
 5445: sub sparestype_titles {
 5446:     my %typestitles = &Apache::lonlocal::texthash (
 5447:                           'primary' => 'primary',
 5448:                           'default' => 'default',
 5449:                       );
 5450:     return %typestitles;
 5451: }
 5452: 
 5453: sub contact_titles {
 5454:     my %titles = &Apache::lonlocal::texthash (
 5455:                    'supportemail'    => 'Support E-mail address',
 5456:                    'adminemail'      => 'Default Server Admin E-mail address',
 5457:                    'errormail'       => 'Error reports to be e-mailed to',
 5458:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5459:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 5460:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 5461:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5462:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5463:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5464:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5465:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 5466:                  );
 5467:     my %short_titles = &Apache::lonlocal::texthash (
 5468:                            adminemail   => 'Admin E-mail address',
 5469:                            supportemail => 'Support E-mail',
 5470:                        );   
 5471:     return (\%titles,\%short_titles);
 5472: }
 5473: 
 5474: sub helpform_fields {
 5475:     my %titles =  &Apache::lonlocal::texthash (
 5476:                        'username'   => 'Name',
 5477:                        'user'       => 'Username/domain',
 5478:                        'phone'      => 'Phone',
 5479:                        'cc'         => 'Cc e-mail',
 5480:                        'course'     => 'Course Details',
 5481:                        'section'    => 'Sections',
 5482:                        'screenshot' => 'File upload',
 5483:     );
 5484:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5485:     my %possoptions = (
 5486:                         username     => ['yes','no','req'],
 5487:                         phone        => ['yes','no','req'],
 5488:                         user         => ['yes','no'],
 5489:                         cc           => ['yes','no'],
 5490:                         course       => ['yes','no'],
 5491:                         section      => ['yes','no'],
 5492:                         screenshot   => ['yes','no'],
 5493:                       );
 5494:     my %fieldoptions = &Apache::lonlocal::texthash (
 5495:                          'yes'  => 'Optional',
 5496:                          'req'  => 'Required',
 5497:                          'no'   => "Not shown",
 5498:     );
 5499:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5500: }
 5501: 
 5502: sub tool_titles {
 5503:     my %titles = &Apache::lonlocal::texthash (
 5504:                      aboutme    => 'Personal web page',
 5505:                      blog       => 'Blog',
 5506:                      webdav     => 'WebDAV',
 5507:                      portfolio  => 'Portfolio',
 5508:                      official   => 'Official courses (with institutional codes)',
 5509:                      unofficial => 'Unofficial courses',
 5510:                      community  => 'Communities',
 5511:                      textbook   => 'Textbook courses',
 5512:                  );
 5513:     return %titles;
 5514: }
 5515: 
 5516: sub courserequest_titles {
 5517:     my %titles = &Apache::lonlocal::texthash (
 5518:                                    official   => 'Official',
 5519:                                    unofficial => 'Unofficial',
 5520:                                    community  => 'Communities',
 5521:                                    textbook   => 'Textbook',
 5522:                                    norequest  => 'Not allowed',
 5523:                                    approval   => 'Approval by Dom. Coord.',
 5524:                                    validate   => 'With validation',
 5525:                                    autolimit  => 'Numerical limit',
 5526:                                    unlimited  => '(blank for unlimited)',
 5527:                  );
 5528:     return %titles;
 5529: }
 5530: 
 5531: sub authorrequest_titles {
 5532:     my %titles = &Apache::lonlocal::texthash (
 5533:                                    norequest  => 'Not allowed',
 5534:                                    approval   => 'Approval by Dom. Coord.',
 5535:                                    automatic  => 'Automatic approval',
 5536:                  );
 5537:     return %titles;
 5538: }
 5539: 
 5540: sub courserequest_conditions {
 5541:     my %conditions = &Apache::lonlocal::texthash (
 5542:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5543:        validate   => '(Processing of request subject to institutional validation).',
 5544:                  );
 5545:     return %conditions;
 5546: }
 5547: 
 5548: 
 5549: sub print_usercreation {
 5550:     my ($position,$dom,$settings,$rowtotal) = @_;
 5551:     my $numinrow = 4;
 5552:     my $datatable;
 5553:     if ($position eq 'top') {
 5554:         $$rowtotal ++;
 5555:         my $rowcount = 0;
 5556:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5557:         if (ref($rules) eq 'HASH') {
 5558:             if (keys(%{$rules}) > 0) {
 5559:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5560:                                                 $ruleorder,$numinrow,$rowcount);
 5561:                 $$rowtotal ++;
 5562:                 $rowcount ++;
 5563:             }
 5564:         }
 5565:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5566:         if (ref($idrules) eq 'HASH') {
 5567:             if (keys(%{$idrules}) > 0) {
 5568:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5569:                                                 $idruleorder,$numinrow,$rowcount);
 5570:                 $$rowtotal ++;
 5571:                 $rowcount ++;
 5572:             }
 5573:         }
 5574:         if ($rowcount == 0) {
 5575:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5576:             $$rowtotal ++;
 5577:             $rowcount ++;
 5578:         }
 5579:     } elsif ($position eq 'middle') {
 5580:         my @creators = ('author','course','requestcrs');
 5581:         my ($rules,$ruleorder) =
 5582:             &Apache::lonnet::inst_userrules($dom,'username');
 5583:         my %lt = &usercreation_types();
 5584:         my %checked;
 5585:         if (ref($settings) eq 'HASH') {
 5586:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5587:                 foreach my $item (@creators) {
 5588:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5589:                 }
 5590:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5591:                 foreach my $item (@creators) {
 5592:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5593:                         $checked{$item} = 'none';
 5594:                     }
 5595:                 }
 5596:             }
 5597:         }
 5598:         my $rownum = 0;
 5599:         foreach my $item (@creators) {
 5600:             $rownum ++;
 5601:             if ($checked{$item} eq '') {
 5602:                 $checked{$item} = 'any';
 5603:             }
 5604:             my $css_class;
 5605:             if ($rownum%2) {
 5606:                 $css_class = '';
 5607:             } else {
 5608:                 $css_class = ' class="LC_odd_row" ';
 5609:             }
 5610:             $datatable .= '<tr'.$css_class.'>'.
 5611:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5612:                          '</span></td><td align="right">';
 5613:             my @options = ('any');
 5614:             if (ref($rules) eq 'HASH') {
 5615:                 if (keys(%{$rules}) > 0) {
 5616:                     push(@options,('official','unofficial'));
 5617:                 }
 5618:             }
 5619:             push(@options,'none');
 5620:             foreach my $option (@options) {
 5621:                 my $type = 'radio';
 5622:                 my $check = ' ';
 5623:                 if ($checked{$item} eq $option) {
 5624:                     $check = ' checked="checked" ';
 5625:                 } 
 5626:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5627:                               '<input type="'.$type.'" name="can_createuser_'.
 5628:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5629:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5630:             }
 5631:             $datatable .= '</td></tr>';
 5632:         }
 5633:     } else {
 5634:         my @contexts = ('author','course','domain');
 5635:         my @authtypes = ('int','krb4','krb5','loc');
 5636:         my %checked;
 5637:         if (ref($settings) eq 'HASH') {
 5638:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5639:                 foreach my $item (@contexts) {
 5640:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5641:                         foreach my $auth (@authtypes) {
 5642:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5643:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5644:                             }
 5645:                         }
 5646:                     }
 5647:                 }
 5648:             }
 5649:         } else {
 5650:             foreach my $item (@contexts) {
 5651:                 foreach my $auth (@authtypes) {
 5652:                     $checked{$item}{$auth} = ' checked="checked" ';
 5653:                 }
 5654:             }
 5655:         }
 5656:         my %title = &context_names();
 5657:         my %authname = &authtype_names();
 5658:         my $rownum = 0;
 5659:         my $css_class; 
 5660:         foreach my $item (@contexts) {
 5661:             if ($rownum%2) {
 5662:                 $css_class = '';
 5663:             } else {
 5664:                 $css_class = ' class="LC_odd_row" ';
 5665:             }
 5666:             $datatable .=   '<tr'.$css_class.'>'.
 5667:                             '<td>'.$title{$item}.
 5668:                             '</td><td class="LC_left_item">'.
 5669:                             '<span class="LC_nobreak">';
 5670:             foreach my $auth (@authtypes) {
 5671:                 $datatable .= '<label>'. 
 5672:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5673:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5674:                               $authname{$auth}.'</label>&nbsp;';
 5675:             }
 5676:             $datatable .= '</span></td></tr>';
 5677:             $rownum ++;
 5678:         }
 5679:         $$rowtotal += $rownum;
 5680:     }
 5681:     return $datatable;
 5682: }
 5683: 
 5684: sub print_selfcreation {
 5685:     my ($position,$dom,$settings,$rowtotal) = @_;
 5686:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 5687:         $emaildomain,$datatable);
 5688:     if (ref($settings) eq 'HASH') {
 5689:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5690:             $createsettings = $settings->{'cancreate'};
 5691:             if (ref($createsettings) eq 'HASH') {
 5692:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5693:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5694:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5695:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5696:                         @selfcreate = ('email','login','sso');
 5697:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5698:                         @selfcreate = ($createsettings->{'selfcreate'});
 5699:                     }
 5700:                 }
 5701:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5702:                     $processing = $createsettings->{'selfcreateprocessing'};
 5703:                 }
 5704:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 5705:                     $emailoptions = $createsettings->{'emailoptions'};
 5706:                 }
 5707:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 5708:                     $emailverified = $createsettings->{'emailverified'};
 5709:                 }
 5710:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 5711:                     $emaildomain = $createsettings->{'emaildomain'};
 5712:                 }
 5713:             }
 5714:         }
 5715:     }
 5716:     my %radiohash;
 5717:     my $numinrow = 4;
 5718:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5719:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5720:     if ($position eq 'top') {
 5721:         my %choices = &Apache::lonlocal::texthash (
 5722:                                                       cancreate_login      => 'Institutional Login',
 5723:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5724:                                                   );
 5725:         my @toggles = sort(keys(%choices));
 5726:         my %defaultchecked = (
 5727:                                'cancreate_login' => 'off',
 5728:                                'cancreate_sso'   => 'off',
 5729:                              );
 5730:         my ($onclick,$itemcount);
 5731:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5732:                                                      \%choices,$itemcount,$onclick);
 5733:         $$rowtotal += $itemcount;
 5734: 
 5735:         if (ref($usertypes) eq 'HASH') {
 5736:             if (keys(%{$usertypes}) > 0) {
 5737:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5738:                                              $dom,$numinrow,$othertitle,
 5739:                                              'statustocreate',$rowtotal);
 5740:                 $$rowtotal ++;
 5741:             }
 5742:         }
 5743:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5744:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5745:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5746:         my $rem;
 5747:         my $numperrow = 2;
 5748:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5749:         $datatable .= '<tr'.$css_class.'>'.
 5750:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5751:                      '<td class="LC_left_item">'."\n".
 5752:                      '<table>'."\n";
 5753:         for (my $i=0; $i<@fields; $i++) {
 5754:             $rem = $i%($numperrow);
 5755:             if ($rem == 0) {
 5756:                 if ($i > 0) {
 5757:                     $datatable .= '</tr>';
 5758:                 }
 5759:                 $datatable .= '<tr>';
 5760:             }
 5761:             my $currval;
 5762:             if (ref($createsettings) eq 'HASH') {
 5763:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5764:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5765:                 }
 5766:             }
 5767:             $datatable .= '<td class="LC_left_item">'.
 5768:                           '<span class="LC_nobreak">'.
 5769:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5770:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5771:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5772:         }
 5773:         my $colsleft = $numperrow - $rem;
 5774:         if ($colsleft > 1 ) {
 5775:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5776:                          '&nbsp;</td>';
 5777:         } elsif ($colsleft == 1) {
 5778:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5779:         }
 5780:         $datatable .= '</tr></table></td></tr>';
 5781:         $$rowtotal ++;
 5782:     } elsif ($position eq 'middle') {
 5783:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5784:         my @posstypes;
 5785:         if (ref($types) eq 'ARRAY') {
 5786:             @posstypes = @{$types};
 5787:         }
 5788:         unless (grep(/^default$/,@posstypes)) {
 5789:             push(@posstypes,'default');
 5790:         }
 5791:         my %usertypeshash;
 5792:         if (ref($usertypes) eq 'HASH') {
 5793:             %usertypeshash = %{$usertypes};
 5794:         }
 5795:         $usertypeshash{'default'} = $othertitle;
 5796:         foreach my $status (@posstypes) {
 5797:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5798:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 5799:             $$rowtotal ++;
 5800:         }
 5801:     } else {
 5802:         my %choices = &Apache::lonlocal::texthash (
 5803:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 5804:                                                   );
 5805:         my @toggles = sort(keys(%choices));
 5806:         my %defaultchecked = (
 5807:                                'cancreate_email' => 'off',
 5808:                              );
 5809:         my $customclass = 'LC_selfcreate_email';
 5810:         my $classprefix = 'LC_canmodify_emailusername_';
 5811:         my $optionsprefix = 'LC_options_emailusername_';
 5812:         my $display = 'none';
 5813:         my $rowstyle = 'display:none';
 5814:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5815:             $display = 'block';
 5816:             $rowstyle = 'display:table-row';
 5817:         }
 5818:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 5819:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5820:                                                      \%choices,$$rowtotal,$onclick);
 5821:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 5822:                                          $rowstyle);
 5823:         $$rowtotal ++;
 5824:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 5825:                                       $rowstyle);
 5826:         $$rowtotal ++;
 5827:         my (@ordered,@posstypes,%usertypeshash);
 5828:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5829:         my ($emailrules,$emailruleorder) =
 5830:             &Apache::lonnet::inst_userrules($dom,'email');
 5831:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5832:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5833:         if (ref($types) eq 'ARRAY') {
 5834:             @posstypes = @{$types};
 5835:         }
 5836:         if (@posstypes) {
 5837:             unless (grep(/^default$/,@posstypes)) {
 5838:                 push(@posstypes,'default');
 5839:             }
 5840:             if (ref($usertypes) eq 'HASH') {
 5841:                 %usertypeshash = %{$usertypes};
 5842:             }
 5843:             my $currassign;
 5844:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 5845:                 $currassign = {
 5846:                                   selfassign => $domdefaults{'inststatusguest'},
 5847:                               };
 5848:                 @ordered = @{$domdefaults{'inststatusguest'}};
 5849:             } else {
 5850:                 $currassign = { selfassign => [] };
 5851:             }
 5852:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 5853:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 5854:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 5855:                                          $numinrow,$othertitle,'selfassign',
 5856:                                          $rowtotal,$onclicktypes,$customclass,
 5857:                                          $rowstyle);
 5858:             $$rowtotal ++;
 5859:             $usertypeshash{'default'} = $othertitle;
 5860:             foreach my $status (@posstypes) {
 5861:                 my $css_class;
 5862:                 if ($$rowtotal%2) {
 5863:                     $css_class = 'LC_odd_row ';
 5864:                 }
 5865:                 $css_class .= $customclass;
 5866:                 my $rowid = $optionsprefix.$status;
 5867:                 my $hidden = 1;
 5868:                 my $currstyle = 'display:none';
 5869:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5870:                     $currstyle = $rowstyle;
 5871:                     $hidden = 0;
 5872:                 }
 5873:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5874:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 5875:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 5876:                 unless ($hidden) {
 5877:                     $$rowtotal ++;
 5878:                 }
 5879:             }
 5880:         } else {
 5881:             my $css_class;
 5882:             if ($$rowtotal%2) {
 5883:                 $css_class = 'LC_odd_row ';
 5884:             }
 5885:             $css_class .= $customclass;
 5886:             $usertypeshash{'default'} = $othertitle;
 5887:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5888:                                          $emailrules,$emailruleorder,$settings,'default','',
 5889:                                          $othertitle,$css_class,$rowstyle,$intdom);
 5890:             $$rowtotal ++;
 5891:         }
 5892:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5893:         $numinrow = 1;
 5894:         if (@posstypes) {
 5895:             foreach my $status (@posstypes) {
 5896:                 my $rowid = $classprefix.$status;
 5897:                 my $datarowstyle = 'display:none';
 5898:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5899:                     $datarowstyle = $rowstyle;
 5900:                 }
 5901:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5902:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5903:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 5904:                 unless ($datarowstyle eq 'display:none') {
 5905:                     $$rowtotal ++;
 5906:                 }
 5907:             }
 5908:         } else {
 5909:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 5910:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5911:                                                    $infotitles,'',$customclass,$rowstyle);
 5912:         }
 5913:     }
 5914:     return $datatable;
 5915: }
 5916: 
 5917: sub selfcreate_javascript {
 5918:     return <<"ENDSCRIPT";
 5919: 
 5920: <script type="text/javascript">
 5921: // <![CDATA[
 5922: 
 5923: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 5924:     var x = document.getElementsByClassName(target);
 5925:     var insttypes = 0;
 5926:     var insttypeRegExp = new RegExp(prefix);
 5927:     if ((x.length != undefined) && (x.length > 0)) {
 5928:         if (form.elements[radio].length != undefined) {
 5929:             for (var i=0; i<form.elements[radio].length; i++) {
 5930:                 if (form.elements[radio][i].checked) {
 5931:                     if (form.elements[radio][i].value == 1) {
 5932:                         for (var j=0; j<x.length; j++) {
 5933:                             if (x[j].id == 'undefined') {
 5934:                                 x[j].style.display = 'table-row';
 5935:                             } else if (insttypeRegExp.test(x[j].id)) {
 5936:                                 insttypes ++;
 5937:                             } else {
 5938:                                 x[j].style.display = 'table-row';
 5939:                             }
 5940:                         }
 5941:                     } else {
 5942:                         for (var j=0; j<x.length; j++) {
 5943:                             x[j].style.display = 'none';
 5944:                         }
 5945:                     }
 5946:                     break;
 5947:                 }
 5948:             }
 5949:             if (insttypes > 0) {
 5950:                 toggleDataRow(form,checkbox,target,altprefix);
 5951:                 toggleDataRow(form,checkbox,target,prefix,1);
 5952:             }
 5953:         }
 5954:     }
 5955:     return;
 5956: }
 5957: 
 5958: function toggleDataRow(form,checkbox,target,prefix,docount) {
 5959:     if (form.elements[checkbox].length != undefined) {
 5960:         var count = 0;
 5961:         if (docount) {
 5962:             for (var i=0; i<form.elements[checkbox].length; i++) {
 5963:                 if (form.elements[checkbox][i].checked) {
 5964:                     count ++;
 5965:                 }
 5966:             }
 5967:         }
 5968:         for (var i=0; i<form.elements[checkbox].length; i++) {
 5969:             var type = form.elements[checkbox][i].value;
 5970:             if (document.getElementById(prefix+type)) {
 5971:                 if (form.elements[checkbox][i].checked) {
 5972:                     document.getElementById(prefix+type).style.display = 'table-row';
 5973:                     if (count % 2 == 1) {
 5974:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 5975:                     } else {
 5976:                         document.getElementById(prefix+type).className = target;
 5977:                     }
 5978:                     count ++;
 5979:                 } else {
 5980:                     document.getElementById(prefix+type).style.display = 'none';
 5981:                 }
 5982:             }
 5983:         }
 5984:     }
 5985:     return;
 5986: }
 5987: 
 5988: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 5989:     var caller = radio+'_'+status;
 5990:     if (form.elements[caller].length != undefined) {
 5991:         for (var i=0; i<form.elements[caller].length; i++) {
 5992:             if (form.elements[caller][i].checked) {
 5993:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 5994:                     var curr = form.elements[caller][i].value;
 5995:                     if (prefix) {
 5996:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 5997:                     }
 5998:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 5999:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 6000:                     if (curr == 'custom') {
 6001:                         if (prefix) {
 6002:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 6003:                         }
 6004:                     } else if (curr == 'inst') {
 6005:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 6006:                     } else if (curr == 'noninst') {
 6007:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 6008:                     }
 6009:                     break;
 6010:                 }
 6011:             }
 6012:         }
 6013:     }
 6014: }
 6015: 
 6016: // ]]>
 6017: </script>
 6018: 
 6019: ENDSCRIPT
 6020: }
 6021: 
 6022: sub noninst_users {
 6023:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 6024:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 6025:     my $class = 'LC_left_item';
 6026:     if ($css_class) {
 6027:         $css_class = ' class="'.$css_class.'"';
 6028:     }
 6029:     if ($rowid) {
 6030:         $rowid = ' id="'.$rowid.'"';
 6031:     }
 6032:     if ($rowstyle) {
 6033:         $rowstyle = ' style="'.$rowstyle.'"';
 6034:     }
 6035:     my ($output,$description);
 6036:     if ($type eq 'default') {
 6037:         $description = &mt('Requests for: [_1]',$typetitle);
 6038:     } else {
 6039:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 6040:     }
 6041:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 6042:               "<td>$description</td>\n".
 6043:               '<td class="'.$class.'" colspan="2">'.
 6044:               '<table><tr>';
 6045:     my %headers = &Apache::lonlocal::texthash(
 6046:               approve  => 'Processing',
 6047:               email    => 'E-mail',
 6048:               username => 'Username',
 6049:     );
 6050:     foreach my $item ('approve','email','username') {
 6051:         $output .= '<th>'.$headers{$item}.'</th>';
 6052:     }
 6053:     $output .= '</tr><tr>';
 6054:     foreach my $item ('approve','email','username') {
 6055:         $output .= '<td valign="top">';
 6056:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 6057:         if ($item eq 'approve') {
 6058:             %choices = &Apache::lonlocal::texthash (
 6059:                                                      automatic => 'Automatically approved',
 6060:                                                      approval  => 'Queued for approval',
 6061:                                                    );
 6062:             @options = ('automatic','approval');
 6063:             $hashref = $processing;
 6064:             $defoption = 'automatic';
 6065:             $name = 'cancreate_emailprocess_'.$type;
 6066:         } elsif ($item eq 'email') {
 6067:             %choices = &Apache::lonlocal::texthash (
 6068:                                                      any     => 'Any e-mail',
 6069:                                                      inst    => 'Institutional only',
 6070:                                                      noninst => 'Non-institutional only',
 6071:                                                      custom  => 'Custom restrictions',
 6072:                                                    );
 6073:             @options = ('any','inst','noninst');
 6074:             my $showcustom;
 6075:             if (ref($emailrules) eq 'HASH') {
 6076:                 if (keys(%{$emailrules}) > 0) {
 6077:                     push(@options,'custom');
 6078:                     $showcustom = 'cancreate_emailrule';
 6079:                     if (ref($settings) eq 'HASH') {
 6080:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 6081:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 6082:                                 if (exists($emailrules->{$rule})) {
 6083:                                     $hascustom ++;
 6084:                                 }
 6085:                             }
 6086:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 6087:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 6088:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 6089:                                     if (exists($emailrules->{$rule})) {
 6090:                                         $hascustom ++;
 6091:                                     }
 6092:                                 }
 6093:                             }
 6094:                         }
 6095:                     }
 6096:                 }
 6097:             }
 6098:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 6099:                                                      "'cancreate_emaildomain','$type'".');"';
 6100:             $hashref = $emailoptions;
 6101:             $defoption = 'any';
 6102:             $name = 'cancreate_emailoptions_'.$type;
 6103:         } elsif ($item eq 'username') {
 6104:             %choices = &Apache::lonlocal::texthash (
 6105:                                                      all    => 'Same as e-mail',
 6106:                                                      first  => 'Omit @domain',
 6107:                                                      free   => 'Free to choose',
 6108:                                                    );
 6109:             @options = ('all','first','free');
 6110:             $hashref = $emailverified;
 6111:             $defoption = 'all';
 6112:             $name = 'cancreate_usernameoptions_'.$type;
 6113:         }
 6114:         foreach my $option (@options) {
 6115:             my $checked;
 6116:             if (ref($hashref) eq 'HASH') {
 6117:                 if ($type eq '') {
 6118:                     if (!exists($hashref->{'default'})) {
 6119:                         if ($option eq $defoption) {
 6120:                             $checked = ' checked="checked"';
 6121:                         }
 6122:                     } else {
 6123:                         if ($hashref->{'default'} eq $option) {
 6124:                             $checked = ' checked="checked"';
 6125:                         }
 6126:                     }
 6127:                 } else {
 6128:                     if (!exists($hashref->{$type})) {
 6129:                         if ($option eq $defoption) {
 6130:                             $checked = ' checked="checked"';
 6131:                         }
 6132:                     } else {
 6133:                         if ($hashref->{$type} eq $option) {
 6134:                             $checked = ' checked="checked"';
 6135:                         }
 6136:                     }
 6137:                 }
 6138:             } elsif (($item eq 'email') && ($hascustom)) {
 6139:                 if ($option eq 'custom') {
 6140:                     $checked = ' checked="checked"';
 6141:                 }
 6142:             } elsif ($option eq $defoption) {
 6143:                 $checked = ' checked="checked"';
 6144:             }
 6145:             $output .= '<span class="LC_nobreak"><label>'.
 6146:                        '<input type="radio" name="'.$name.'"'.
 6147:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 6148:                        $choices{$option}.'</label></span><br />';
 6149:             if ($item eq 'email') {
 6150:                 if ($option eq 'custom') {
 6151:                     my $id = 'cancreate_emailrule_'.$type;
 6152:                     my $display = 'none';
 6153:                     if ($checked) {
 6154:                         $display = 'inline';
 6155:                     }
 6156:                     my $numinrow = 2;
 6157:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 6158:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 6159:                                &user_formats_row('email',$settings,$emailrules,
 6160:                                                  $emailruleorder,$numinrow,'',$type);
 6161:                               '</table></fieldset>';
 6162:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 6163:                     my %text = &Apache::lonlocal::texthash (
 6164:                                                              inst    => 'must end:',
 6165:                                                              noninst => 'cannot end:',
 6166:                                                            );
 6167:                     my $value;
 6168:                     if (ref($emaildomain) eq 'HASH') {
 6169:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 6170:                             $value = $emaildomain->{$type}->{$option};
 6171:                         }
 6172:                     }
 6173:                     if ($value eq '') {
 6174:                         $value = '@'.$intdom;
 6175:                     }
 6176:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 6177:                     my $display = 'none';
 6178:                     if ($checked) {
 6179:                         $display = 'inline';
 6180:                     }
 6181:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 6182:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 6183:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 6184:                                '</div>';
 6185:                 }
 6186:             }
 6187:         }
 6188:         $output .= '</td>'."\n";
 6189:     }
 6190:     $output .= "</tr></table></td></tr>\n";
 6191:     return $output;
 6192: }
 6193: 
 6194: sub captcha_choice {
 6195:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 6196:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 6197:         $vertext,$currver); 
 6198:     my %lt = &captcha_phrases();
 6199:     $keyentry = 'hidden';
 6200:     my $colspan=2;
 6201:     if ($context eq 'cancreate') {
 6202:         $rowname = &mt('CAPTCHA validation');
 6203:     } elsif ($context eq 'login') {
 6204:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 6205:     } elsif ($context eq 'passwords') {
 6206:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 6207:         $colspan=1;
 6208:     }
 6209:     if (ref($settings) eq 'HASH') {
 6210:         if ($settings->{'captcha'}) {
 6211:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6212:         } else {
 6213:             $checked{'original'} = ' checked="checked"';
 6214:         }
 6215:         if ($settings->{'captcha'} eq 'recaptcha') {
 6216:             $pubtext = $lt{'pub'};
 6217:             $privtext = $lt{'priv'};
 6218:             $keyentry = 'text';
 6219:             $vertext = $lt{'ver'};
 6220:             $currver = $settings->{'recaptchaversion'};
 6221:             if ($currver ne '2') {
 6222:                 $currver = 1;
 6223:             }
 6224:         }
 6225:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6226:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6227:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6228:         }
 6229:     } else {
 6230:         $checked{'original'} = ' checked="checked"';
 6231:     }
 6232:     my $css_class;
 6233:     if ($itemcount%2) {
 6234:         $css_class = 'LC_odd_row';
 6235:     }
 6236:     if ($customcss) {
 6237:         $css_class .= " $customcss";
 6238:     }
 6239:     $css_class =~ s/^\s+//;
 6240:     if ($css_class) {
 6241:         $css_class = ' class="'.$css_class.'"';
 6242:     }
 6243:     if ($rowstyle) {
 6244:         $css_class .= ' style="'.$rowstyle.'"';
 6245:     }
 6246:     my $output = '<tr'.$css_class.'>'.
 6247:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 6248:                  '<table><tr><td>'."\n";
 6249:     foreach my $option ('original','recaptcha','notused') {
 6250:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6251:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6252:                    $lt{$option}.'</label></span>';
 6253:         unless ($option eq 'notused') {
 6254:             $output .= ('&nbsp;'x2)."\n";
 6255:         }
 6256:     }
 6257: #
 6258: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6259: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6260: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6261: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6262: #
 6263:     $output .= '</td></tr>'."\n".
 6264:                '<tr><td class="LC_zero_height">'."\n".
 6265:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6266:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6267:                $currpub.'" size="40" /></span><br />'."\n".
 6268:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6269:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6270:                $currpriv.'" size="40" /></span><br />'.
 6271:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6272:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6273:                $currver.'" size="3" /></span><br />'.
 6274:                '</td></tr></table>'."\n".
 6275:                '</td></tr>';
 6276:     return $output;
 6277: }
 6278: 
 6279: sub user_formats_row {
 6280:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 6281:     my $output;
 6282:     my %text = (
 6283:                    'username' => 'new usernames',
 6284:                    'id'       => 'IDs',
 6285:                );
 6286:     unless ($type eq 'email') {
 6287:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6288:         $output = '<tr '.$css_class.'>'.
 6289:                   '<td><span class="LC_nobreak">'.
 6290:                   &mt("Format rules to check for $text{$type}: ").
 6291:                   '</td><td class="LC_left_item" colspan="2"><table>';
 6292:     }
 6293:     my $rem;
 6294:     if (ref($ruleorder) eq 'ARRAY') {
 6295:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6296:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6297:                 my $rem = $i%($numinrow);
 6298:                 if ($rem == 0) {
 6299:                     if ($i > 0) {
 6300:                         $output .= '</tr>';
 6301:                     }
 6302:                     $output .= '<tr>';
 6303:                 }
 6304:                 my $check = ' ';
 6305:                 if (ref($settings) eq 'HASH') {
 6306:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6307:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6308:                             $check = ' checked="checked" ';
 6309:                         }
 6310:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 6311:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 6312:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 6313:                                 $check = ' checked="checked" ';
 6314:                             }
 6315:                         }
 6316:                     }
 6317:                 }
 6318:                 my $name = $type.'_rule';
 6319:                 if ($type eq 'email') {
 6320:                     $name .= '_'.$status;
 6321:                 }
 6322:                 $output .= '<td class="LC_left_item">'.
 6323:                            '<span class="LC_nobreak"><label>'.
 6324:                            '<input type="checkbox" name="'.$name.'" '.
 6325:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6326:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6327:             }
 6328:         }
 6329:         $rem = @{$ruleorder}%($numinrow);
 6330:     }
 6331:     my $colsleft;
 6332:     if ($rem) {
 6333:         $colsleft = $numinrow - $rem;
 6334:     }
 6335:     if ($colsleft > 1 ) {
 6336:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6337:                    '&nbsp;</td>';
 6338:     } elsif ($colsleft == 1) {
 6339:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6340:     }
 6341:     $output .= '</tr></table>';
 6342:     unless ($type eq 'email') {
 6343:         $output .= '</td></tr>';
 6344:     }
 6345:     return $output;
 6346: }
 6347: 
 6348: sub usercreation_types {
 6349:     my %lt = &Apache::lonlocal::texthash (
 6350:                     author     => 'When adding a co-author',
 6351:                     course     => 'When adding a user to a course',
 6352:                     requestcrs => 'When requesting a course',
 6353:                     any        => 'Any',
 6354:                     official   => 'Institutional only ',
 6355:                     unofficial => 'Non-institutional only',
 6356:                     none       => 'None',
 6357:     );
 6358:     return %lt;
 6359: }
 6360: 
 6361: sub selfcreation_types {
 6362:     my %lt = &Apache::lonlocal::texthash (
 6363:                     selfcreate => 'User creates own account',
 6364:                     any        => 'Any',
 6365:                     official   => 'Institutional only ',
 6366:                     unofficial => 'Non-institutional only',
 6367:                     email      => 'E-mail address',
 6368:                     login      => 'Institutional Login',
 6369:                     sso        => 'SSO',
 6370:              );
 6371: }
 6372: 
 6373: sub authtype_names {
 6374:     my %lt = &Apache::lonlocal::texthash(
 6375:                       int    => 'Internal',
 6376:                       krb4   => 'Kerberos 4',
 6377:                       krb5   => 'Kerberos 5',
 6378:                       loc    => 'Local',
 6379:                   );
 6380:     return %lt;
 6381: }
 6382: 
 6383: sub context_names {
 6384:     my %context_title = &Apache::lonlocal::texthash(
 6385:        author => 'Creating users when an Author',
 6386:        course => 'Creating users when in a course',
 6387:        domain => 'Creating users when a Domain Coordinator',
 6388:     );
 6389:     return %context_title;
 6390: }
 6391: 
 6392: sub print_usermodification {
 6393:     my ($position,$dom,$settings,$rowtotal) = @_;
 6394:     my $numinrow = 4;
 6395:     my ($context,$datatable,$rowcount);
 6396:     if ($position eq 'top') {
 6397:         $rowcount = 0;
 6398:         $context = 'author'; 
 6399:         foreach my $role ('ca','aa') {
 6400:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6401:                                                    $numinrow,$rowcount);
 6402:             $$rowtotal ++;
 6403:             $rowcount ++;
 6404:         }
 6405:     } elsif ($position eq 'bottom') {
 6406:         $context = 'course';
 6407:         $rowcount = 0;
 6408:         foreach my $role ('st','ep','ta','in','cr') {
 6409:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6410:                                                    $numinrow,$rowcount);
 6411:             $$rowtotal ++;
 6412:             $rowcount ++;
 6413:         }
 6414:     }
 6415:     return $datatable;
 6416: }
 6417: 
 6418: sub print_defaults {
 6419:     my ($position,$dom,$settings,$rowtotal) = @_;
 6420:     my $rownum = 0;
 6421:     my ($datatable,$css_class,$titles);
 6422:     unless ($position eq 'bottom') {
 6423:         $titles = &defaults_titles($dom);
 6424:     }
 6425:     if ($position eq 'top') {
 6426:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6427:                      'datelocale_def','portal_def');
 6428:         my %defaults;
 6429:         if (ref($settings) eq 'HASH') {
 6430:             %defaults = %{$settings};
 6431:         } else {
 6432:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6433:             foreach my $item (@items) {
 6434:                 $defaults{$item} = $domdefaults{$item};
 6435:             }
 6436:         }
 6437:         foreach my $item (@items) {
 6438:             if ($rownum%2) {
 6439:                 $css_class = '';
 6440:             } else {
 6441:                 $css_class = ' class="LC_odd_row" ';
 6442:             }
 6443:             $datatable .= '<tr'.$css_class.'>'.
 6444:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6445:                           '</span></td><td class="LC_right_item" colspan="3">';
 6446:             if ($item eq 'auth_def') {
 6447:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6448:                 my %shortauth = (
 6449:                                  internal => 'int',
 6450:                                  krb4 => 'krb4',
 6451:                                  krb5 => 'krb5',
 6452:                                  localauth  => 'loc'
 6453:                                 );
 6454:                 my %authnames = &authtype_names();
 6455:                 foreach my $auth (@authtypes) {
 6456:                     my $checked = ' ';
 6457:                     if ($defaults{$item} eq $auth) {
 6458:                         $checked = ' checked="checked" ';
 6459:                     }
 6460:                     $datatable .= '<label><input type="radio" name="'.$item.
 6461:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6462:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6463:                 }
 6464:             } elsif ($item eq 'timezone_def') {
 6465:                 my $includeempty = 1;
 6466:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6467:             } elsif ($item eq 'datelocale_def') {
 6468:                 my $includeempty = 1;
 6469:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6470:             } elsif ($item eq 'lang_def') {
 6471:                 my $includeempty = 1;
 6472:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6473:             } else {
 6474:                 my $size;
 6475:                 if ($item eq 'portal_def') {
 6476:                     $size = ' size="25"';
 6477:                 }
 6478:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6479:                               $defaults{$item}.'"'.$size.' />';
 6480:             }
 6481:             $datatable .= '</td></tr>';
 6482:             $rownum ++;
 6483:         }
 6484:     } else {
 6485:         my %defaults;
 6486:         if (ref($settings) eq 'HASH') {
 6487:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6488:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6489:                 for (my $i=0; $i<$maxnum; $i++) {
 6490:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6491:                     my $item = $settings->{'inststatusorder'}->[$i];
 6492:                     my $title = $settings->{'inststatustypes'}->{$item};
 6493:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6494:                     $datatable .= '<tr'.$css_class.'>'.
 6495:                                   '<td><span class="LC_nobreak">'.
 6496:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6497:                     for (my $k=0; $k<=$maxnum; $k++) {
 6498:                         my $vpos = $k+1;
 6499:                         my $selstr;
 6500:                         if ($k == $i) {
 6501:                             $selstr = ' selected="selected" ';
 6502:                         }
 6503:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6504:                     }
 6505:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6506:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6507:                                   &mt('delete').'</span></td>'.
 6508:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6509:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6510:                                   '</span></td></tr>';
 6511:                 }
 6512:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6513:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6514:                 $datatable .= '<tr '.$css_class.'>'.
 6515:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6516:                 for (my $k=0; $k<=$maxnum; $k++) {
 6517:                     my $vpos = $k+1;
 6518:                     my $selstr;
 6519:                     if ($k == $maxnum) {
 6520:                         $selstr = ' selected="selected" ';
 6521:                     }
 6522:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6523:                 }
 6524:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6525:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6526:                               '&nbsp;'.&mt('(new)').
 6527:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6528:                               &mt('Name displayed:').
 6529:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6530:                               '</tr>'."\n";
 6531:                 $rownum ++;
 6532:             }
 6533:         }
 6534:     }
 6535:     $$rowtotal += $rownum;
 6536:     return $datatable;
 6537: }
 6538: 
 6539: sub get_languages_hash {
 6540:     my %langchoices;
 6541:     foreach my $id (&Apache::loncommon::languageids()) {
 6542:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6543:         if ($code ne '') {
 6544:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6545:         }
 6546:     }
 6547:     return %langchoices;
 6548: }
 6549: 
 6550: sub defaults_titles {
 6551:     my ($dom) = @_;
 6552:     my %titles = &Apache::lonlocal::texthash (
 6553:                    'auth_def'      => 'Default authentication type',
 6554:                    'auth_arg_def'  => 'Default authentication argument',
 6555:                    'lang_def'      => 'Default language',
 6556:                    'timezone_def'  => 'Default timezone',
 6557:                    'datelocale_def' => 'Default locale for dates',
 6558:                    'portal_def'     => 'Portal/Default URL',
 6559:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6560:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6561:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6562:                  );
 6563:     if ($dom) {
 6564:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6565:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6566:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6567:         $protocol = 'http' if ($protocol ne 'https');
 6568:         if ($uint_dom) {
 6569:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6570:                                          $uint_dom);
 6571:         }
 6572:     }
 6573:     return (\%titles);
 6574: }
 6575: 
 6576: sub print_scantron {
 6577:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 6578:     if ($position eq 'top') {
 6579:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 6580:     } else {
 6581:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 6582:     }
 6583: }
 6584: 
 6585: sub scantron_javascript {
 6586:     return <<"ENDSCRIPT";
 6587: 
 6588: <script type="text/javascript">
 6589: // <![CDATA[
 6590: 
 6591: function toggleScantron(form) {
 6592:     var csvfieldset = new Array();
 6593:     if (document.getElementById('scantroncsv_cols')) {
 6594:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 6595:     }
 6596:     if (document.getElementById('scantroncsv_options')) {
 6597:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 6598:     }
 6599:     if (csvfieldset.length) {
 6600:         if (document.getElementById('scantronconfcsv')) {
 6601:             var scantroncsv = document.getElementById('scantronconfcsv');
 6602:             if (scantroncsv.checked) {
 6603:                 for (var i=0; i<csvfieldset.length; i++) {
 6604:                     csvfieldset[i].style.display = 'block';
 6605:                 }
 6606:             } else {
 6607:                 for (var i=0; i<csvfieldset.length; i++) {
 6608:                     csvfieldset[i].style.display = 'none';
 6609:                 }
 6610:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 6611:                 if (csvselects.length) {
 6612:                     for (var j=0; j<csvselects.length; j++) {
 6613:                         csvselects[j].selectedIndex = 0;
 6614:                     }
 6615:                 }
 6616:             }
 6617:         }
 6618:     }
 6619:     return;
 6620: }
 6621: // ]]>
 6622: </script>
 6623: 
 6624: ENDSCRIPT
 6625: 
 6626: }
 6627: 
 6628: sub print_scantronformat {
 6629:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6630:     my $itemcount = 1;
 6631:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6632:         %confhash);
 6633:     my $switchserver = &check_switchserver($dom,$confname);
 6634:     my %lt = &Apache::lonlocal::texthash (
 6635:                 default => 'Default bubblesheet format file error',
 6636:                 custom  => 'Custom bubblesheet format file error',
 6637:              );
 6638:     my %scantronfiles = (
 6639:         default => 'default.tab',
 6640:         custom => 'custom.tab',
 6641:     );
 6642:     foreach my $key (keys(%scantronfiles)) {
 6643:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6644:                               .$scantronfiles{$key};
 6645:     }
 6646:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6647:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6648:         if (!$switchserver) {
 6649:             my $servadm = $r->dir_config('lonAdmEMail');
 6650:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6651:             if ($configuserok eq 'ok') {
 6652:                 if ($author_ok eq 'ok') {
 6653:                     my %legacyfile = (
 6654:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 6655:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 6656:                     );
 6657:                     my %md5chk;
 6658:                     foreach my $type (keys(%legacyfile)) {
 6659:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6660:                         chomp($md5chk{$type});
 6661:                     }
 6662:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6663:                         foreach my $type (keys(%legacyfile)) {
 6664:                             ($scantronurls{$type},my $error) =
 6665:                                 &legacy_scantronformat($r,$dom,$confname,
 6666:                                                  $type,$legacyfile{$type},
 6667:                                                  $scantronurls{$type},
 6668:                                                  $scantronfiles{$type});
 6669:                             if ($error ne '') {
 6670:                                 $error{$type} = $error;
 6671:                             }
 6672:                         }
 6673:                         if (keys(%error) == 0) {
 6674:                             $is_custom = 1;
 6675:                             $confhash{'scantron'}{'scantronformat'} =
 6676:                                 $scantronurls{'custom'};
 6677:                             my $putresult =
 6678:                                 &Apache::lonnet::put_dom('configuration',
 6679:                                                          \%confhash,$dom);
 6680:                             if ($putresult ne 'ok') {
 6681:                                 $error{'custom'} =
 6682:                                     '<span class="LC_error">'.
 6683:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6684:                             }
 6685:                         }
 6686:                     } else {
 6687:                         ($scantronurls{'default'},my $error) =
 6688:                             &legacy_scantronformat($r,$dom,$confname,
 6689:                                           'default',$legacyfile{'default'},
 6690:                                           $scantronurls{'default'},
 6691:                                           $scantronfiles{'default'});
 6692:                         if ($error eq '') {
 6693:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6694:                             my $putresult =
 6695:                                 &Apache::lonnet::put_dom('configuration',
 6696:                                                          \%confhash,$dom);
 6697:                             if ($putresult ne 'ok') {
 6698:                                 $error{'default'} =
 6699:                                     '<span class="LC_error">'.
 6700:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6701:                             }
 6702:                         } else {
 6703:                             $error{'default'} = $error;
 6704:                         }
 6705:                     }
 6706:                 }
 6707:             }
 6708:         } else {
 6709:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6710:         }
 6711:     }
 6712:     if (ref($settings) eq 'HASH') {
 6713:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6714:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6715:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6716:                 $scantronurl = '';
 6717:             } else {
 6718:                 $scantronurl = $settings->{'scantronformat'};
 6719:             }
 6720:             $is_custom = 1;
 6721:         } else {
 6722:             $scantronurl = $scantronurls{'default'};
 6723:         }
 6724:     } else {
 6725:         if ($is_custom) {
 6726:             $scantronurl = $scantronurls{'custom'};
 6727:         } else {
 6728:             $scantronurl = $scantronurls{'default'};
 6729:         }
 6730:     }
 6731:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6732:     $datatable .= '<tr'.$css_class.'>';
 6733:     if (!$is_custom) {
 6734:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6735:                       '<span class="LC_nobreak">';
 6736:         if ($scantronurl) {
 6737:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6738:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6739:         } else {
 6740:             $datatable = &mt('File unavailable for display');
 6741:         }
 6742:         $datatable .= '</span></td>';
 6743:         if (keys(%error) == 0) { 
 6744:             $datatable .= '<td valign="bottom">';
 6745:             if (!$switchserver) {
 6746:                 $datatable .= &mt('Upload:').'<br />';
 6747:             }
 6748:         } else {
 6749:             my $errorstr;
 6750:             foreach my $key (sort(keys(%error))) {
 6751:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6752:             }
 6753:             $datatable .= '<td>'.$errorstr;
 6754:         }
 6755:     } else {
 6756:         if (keys(%error) > 0) {
 6757:             my $errorstr;
 6758:             foreach my $key (sort(keys(%error))) {
 6759:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6760:             } 
 6761:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6762:         } elsif ($scantronurl) {
 6763:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6764:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6765:             $datatable .= '<td><span class="LC_nobreak">'.
 6766:                           $link.
 6767:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6768:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6769:                           '<td><span class="LC_nobreak">&nbsp;'.
 6770:                           &mt('Replace:').'</span><br />';
 6771:         }
 6772:     }
 6773:     if (keys(%error) == 0) {
 6774:         if ($switchserver) {
 6775:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6776:         } else {
 6777:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6778:                          '<input type="file" name="scantronformat" /></span>';
 6779:         }
 6780:     }
 6781:     $datatable .= '</td></tr>';
 6782:     $$rowtotal ++;
 6783:     return $datatable;
 6784: }
 6785: 
 6786: sub legacy_scantronformat {
 6787:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6788:     my ($url,$error);
 6789:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6790:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6791:         (my $result,$url) =
 6792:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6793:                          '','',$newfile);
 6794:         if ($result ne 'ok') {
 6795:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6796:         }
 6797:     }
 6798:     return ($url,$error);
 6799: }
 6800: 
 6801: sub print_scantronconfig {
 6802:     my ($dom,$settings,$rowtotal) = @_;
 6803:     my $itemcount = 2;
 6804:     my $is_checked = ' checked="checked"';
 6805:     my %optionson = (
 6806:                      hdr => ' checked="checked"',
 6807:                      pad => ' checked="checked"',
 6808:                      rem => ' checked="checked"',
 6809:                     );
 6810:     my %optionsoff = (
 6811:                       hdr => '',
 6812:                       pad => '',
 6813:                       rem => '',
 6814:                      );
 6815:     my $currcsvsty = 'none';
 6816:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 6817:     my @fields = &scantroncsv_fields();
 6818:     my %titles = &scantronconfig_titles();
 6819:     if (ref($settings) eq 'HASH') {
 6820:         if (ref($settings->{config}) eq 'HASH') {
 6821:             if ($settings->{config}->{dat}) {
 6822:                 $checked{'dat'} = $is_checked;
 6823:             }
 6824:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 6825:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 6826:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 6827:                     if (keys(%csvfields) > 0) {
 6828:                         $checked{'csv'} = $is_checked;
 6829:                         $currcsvsty = 'block';
 6830:                     }
 6831:                 }
 6832:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 6833:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 6834:                     foreach my $option (keys(%optionson)) {
 6835:                         unless ($csvoptions{$option}) {
 6836:                             $optionsoff{$option} = $optionson{$option};
 6837:                             $optionson{$option} = '';
 6838:                         }
 6839:                     }
 6840:                 }
 6841:             }
 6842:         } else {
 6843:             $checked{'dat'} = $is_checked;
 6844:         }
 6845:     } else {
 6846:         $checked{'dat'} = $is_checked;
 6847:     }
 6848:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 6849:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6850:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 6851:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 6852:     foreach my $item ('dat','csv') {
 6853:         my $id;
 6854:         if ($item eq 'csv') {
 6855:             $id = 'id="scantronconfcsv" ';
 6856:         }
 6857:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 6858:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 6859:         if ($item eq 'csv') {
 6860:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 6861:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 6862:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 6863:             foreach my $col (@fields) {
 6864:                 my $selnone;
 6865:                 if ($csvfields{$col} eq '') {
 6866:                     $selnone = ' selected="selected"';
 6867:                 }
 6868:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 6869:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 6870:                               '<option value=""'.$selnone.'></option>';
 6871:                 for (my $i=0; $i<20; $i++) {
 6872:                     my $shown = $i+1;
 6873:                     my $sel;
 6874:                     unless ($selnone) {
 6875:                         if (exists($csvfields{$col})) {
 6876:                             if ($csvfields{$col} == $i) {
 6877:                                 $sel = ' selected="selected"';
 6878:                             }
 6879:                         }
 6880:                     }
 6881:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 6882:                 }
 6883:                 $datatable .= '</select></td></tr>';
 6884:            }
 6885:            $datatable .= '</table></fieldset>'.
 6886:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 6887:                          '<legend>'.&mt('CSV Options').'</legend>';
 6888:            foreach my $option ('hdr','pad','rem') {
 6889:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 6890:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 6891:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 6892:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 6893:            }
 6894:            $datatable .= '</fieldset>';
 6895:            $itemcount ++;
 6896:         }
 6897:     }
 6898:     $datatable .= '</td></tr>';
 6899:     $$rowtotal ++;
 6900:     return $datatable;
 6901: }
 6902: 
 6903: sub scantronconfig_titles {
 6904:     return &Apache::lonlocal::texthash(
 6905:                                           dat => 'Standard format (.dat)',
 6906:                                           csv => 'Comma separated values (.csv)',
 6907:                                           hdr => 'Remove first line in file (contains column titles)',
 6908:                                           pad => 'Prepend 0s to PaperID',
 6909:                                           rem => 'Remove leading spaces (except Question Response columns)',
 6910:                                           CODE => 'CODE',
 6911:                                           ID   => 'Student ID',
 6912:                                           PaperID => 'Paper ID',
 6913:                                           FirstName => 'First Name',
 6914:                                           LastName => 'Last Name',
 6915:                                           FirstQuestion => 'First Question Response',
 6916:                                           Section => 'Section',
 6917:     );
 6918: }
 6919: 
 6920: sub scantroncsv_fields {
 6921:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 6922: }
 6923: 
 6924: sub print_coursecategories {
 6925:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6926:     my $datatable;
 6927:     if ($position eq 'top') {
 6928:         my (%checked);
 6929:         my @catitems = ('unauth','auth');
 6930:         my @cattypes = ('std','domonly','codesrch','none');
 6931:         $checked{'unauth'} = 'std';
 6932:         $checked{'auth'} = 'std';
 6933:         if (ref($settings) eq 'HASH') {
 6934:             foreach my $type (@cattypes) {
 6935:                 if ($type eq $settings->{'unauth'}) {
 6936:                     $checked{'unauth'} = $type;
 6937:                 }
 6938:                 if ($type eq $settings->{'auth'}) {
 6939:                     $checked{'auth'} = $type;
 6940:                 }
 6941:             }
 6942:         }
 6943:         my %lt = &Apache::lonlocal::texthash (
 6944:                                                unauth   => 'Catalog type for unauthenticated users',
 6945:                                                auth     => 'Catalog type for authenticated users',
 6946:                                                none     => 'No catalog',
 6947:                                                std      => 'Standard catalog',
 6948:                                                domonly  => 'Domain-only catalog',
 6949:                                                codesrch => "Code search form",
 6950:                                              );
 6951:        my $itemcount = 0;
 6952:        foreach my $item (@catitems) {
 6953:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6954:            $datatable .= '<tr '.$css_class.'>'.
 6955:                          '<td>'.$lt{$item}.'</td>'.
 6956:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6957:            foreach my $type (@cattypes) {
 6958:                my $ischecked;
 6959:                if ($checked{$item} eq $type) {
 6960:                    $ischecked=' checked="checked"';
 6961:                }
 6962:                $datatable .= '<label>'.
 6963:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6964:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6965:            }
 6966:            $datatable .= '</span></td></tr>';
 6967:            $itemcount ++;
 6968:         }
 6969:         $$rowtotal += $itemcount;
 6970:     } elsif ($position eq 'middle') {
 6971:         my $toggle_cats_crs = ' ';
 6972:         my $toggle_cats_dom = ' checked="checked" ';
 6973:         my $can_cat_crs = ' ';
 6974:         my $can_cat_dom = ' checked="checked" ';
 6975:         my $toggle_catscomm_comm = ' ';
 6976:         my $toggle_catscomm_dom = ' checked="checked" ';
 6977:         my $can_catcomm_comm = ' ';
 6978:         my $can_catcomm_dom = ' checked="checked" ';
 6979: 
 6980:         if (ref($settings) eq 'HASH') {
 6981:             if ($settings->{'togglecats'} eq 'crs') {
 6982:                 $toggle_cats_crs = $toggle_cats_dom;
 6983:                 $toggle_cats_dom = ' ';
 6984:             }
 6985:             if ($settings->{'categorize'} eq 'crs') {
 6986:                 $can_cat_crs = $can_cat_dom;
 6987:                 $can_cat_dom = ' ';
 6988:             }
 6989:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6990:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6991:                 $toggle_catscomm_dom = ' ';
 6992:             }
 6993:             if ($settings->{'categorizecomm'} eq 'comm') {
 6994:                 $can_catcomm_comm = $can_catcomm_dom;
 6995:                 $can_catcomm_dom = ' ';
 6996:             }
 6997:         }
 6998:         my %title = &Apache::lonlocal::texthash (
 6999:                      togglecats     => 'Show/Hide a course in catalog',
 7000:                      togglecatscomm => 'Show/Hide a community in catalog',
 7001:                      categorize     => 'Assign a category to a course',
 7002:                      categorizecomm => 'Assign a category to a community',
 7003:                     );
 7004:         my %level = &Apache::lonlocal::texthash (
 7005:                      dom  => 'Set in Domain',
 7006:                      crs  => 'Set in Course',
 7007:                      comm => 'Set in Community',
 7008:                     );
 7009:         $datatable = '<tr class="LC_odd_row">'.
 7010:                   '<td>'.$title{'togglecats'}.'</td>'.
 7011:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7012:                   '<input type="radio" name="togglecats"'.
 7013:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7014:                   '<label><input type="radio" name="togglecats"'.
 7015:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7016:                   '</tr><tr>'.
 7017:                   '<td>'.$title{'categorize'}.'</td>'.
 7018:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7019:                   '<label><input type="radio" name="categorize"'.
 7020:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7021:                   '<label><input type="radio" name="categorize"'.
 7022:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7023:                   '</tr><tr class="LC_odd_row">'.
 7024:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 7025:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7026:                   '<input type="radio" name="togglecatscomm"'.
 7027:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7028:                   '<label><input type="radio" name="togglecatscomm"'.
 7029:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7030:                   '</tr><tr>'.
 7031:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 7032:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7033:                   '<label><input type="radio" name="categorizecomm"'.
 7034:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7035:                   '<label><input type="radio" name="categorizecomm"'.
 7036:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7037:                   '</tr>';
 7038:         $$rowtotal += 4;
 7039:     } else {
 7040:         my $css_class;
 7041:         my $itemcount = 1;
 7042:         my $cathash; 
 7043:         if (ref($settings) eq 'HASH') {
 7044:             $cathash = $settings->{'cats'};
 7045:         }
 7046:         if (ref($cathash) eq 'HASH') {
 7047:             my (@cats,@trails,%allitems,%idx,@jsarray);
 7048:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 7049:                                                    \%allitems,\%idx,\@jsarray);
 7050:             my $maxdepth = scalar(@cats);
 7051:             my $colattrib = '';
 7052:             if ($maxdepth > 2) {
 7053:                 $colattrib = ' colspan="2" ';
 7054:             }
 7055:             my @path;
 7056:             if (@cats > 0) {
 7057:                 if (ref($cats[0]) eq 'ARRAY') {
 7058:                     my $numtop = @{$cats[0]};
 7059:                     my $maxnum = $numtop;
 7060:                     my %default_names = (
 7061:                           instcode    => &mt('Official courses'),
 7062:                           communities => &mt('Communities'),
 7063:                     );
 7064: 
 7065:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 7066:                         ($cathash->{'instcode::0'} eq '') ||
 7067:                         (!grep(/^communities$/,@{$cats[0]})) || 
 7068:                         ($cathash->{'communities::0'} eq '')) {
 7069:                         $maxnum ++;
 7070:                     }
 7071:                     my $lastidx;
 7072:                     for (my $i=0; $i<$numtop; $i++) {
 7073:                         my $parent = $cats[0][$i];
 7074:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7075:                         my $item = &escape($parent).'::0';
 7076:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 7077:                         $lastidx = $idx{$item};
 7078:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7079:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 7080:                         for (my $k=0; $k<=$maxnum; $k++) {
 7081:                             my $vpos = $k+1;
 7082:                             my $selstr;
 7083:                             if ($k == $i) {
 7084:                                 $selstr = ' selected="selected" ';
 7085:                             }
 7086:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7087:                         }
 7088:                         $datatable .= '</select></span></td><td>';
 7089:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 7090:                             $datatable .=  '<span class="LC_nobreak">'
 7091:                                            .$default_names{$parent}.'</span>';
 7092:                             if ($parent eq 'instcode') {
 7093:                                 $datatable .= '<br /><span class="LC_nobreak">('
 7094:                                               .&mt('with institutional codes')
 7095:                                               .')</span></td><td'.$colattrib.'>';
 7096:                             } else {
 7097:                                 $datatable .= '<table><tr><td>';
 7098:                             }
 7099:                             $datatable .= '<span class="LC_nobreak">'
 7100:                                           .'<label><input type="radio" name="'
 7101:                                           .$parent.'" value="1" checked="checked" />'
 7102:                                           .&mt('Display').'</label>';
 7103:                             if ($parent eq 'instcode') {
 7104:                                 $datatable .= '&nbsp;';
 7105:                             } else {
 7106:                                 $datatable .= '</span></td></tr><tr><td>'
 7107:                                               .'<span class="LC_nobreak">';
 7108:                             }
 7109:                             $datatable .= '<label><input type="radio" name="'
 7110:                                           .$parent.'" value="0" />'
 7111:                                           .&mt('Do not display').'</label></span>';
 7112:                             if ($parent eq 'communities') {
 7113:                                 $datatable .= '</td></tr></table>';
 7114:                             }
 7115:                             $datatable .= '</td>';
 7116:                         } else {
 7117:                             $datatable .= $parent
 7118:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 7119:                                           .'<input type="checkbox" name="deletecategory" '
 7120:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 7121:                         }
 7122:                         my $depth = 1;
 7123:                         push(@path,$parent);
 7124:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 7125:                         pop(@path);
 7126:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 7127:                         $itemcount ++;
 7128:                     }
 7129:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7130:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 7131:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 7132:                     for (my $k=0; $k<=$maxnum; $k++) {
 7133:                         my $vpos = $k+1;
 7134:                         my $selstr;
 7135:                         if ($k == $numtop) {
 7136:                             $selstr = ' selected="selected" ';
 7137:                         }
 7138:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7139:                     }
 7140:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 7141:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 7142:                                   .'</tr>'."\n";
 7143:                     $itemcount ++;
 7144:                     foreach my $default ('instcode','communities') {
 7145:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 7146:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7147:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 7148:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 7149:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 7150:                             for (my $k=0; $k<=$maxnum; $k++) {
 7151:                                 my $vpos = $k+1;
 7152:                                 my $selstr;
 7153:                                 if ($k == $maxnum) {
 7154:                                     $selstr = ' selected="selected" ';
 7155:                                 }
 7156:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7157:                             }
 7158:                             $datatable .= '</select></span></td>'.
 7159:                                           '<td><span class="LC_nobreak">'.
 7160:                                           $default_names{$default}.'</span>';
 7161:                             if ($default eq 'instcode') {
 7162:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 7163:                                               .&mt('with institutional codes').')</span>';
 7164:                             }
 7165:                             $datatable .= '</td>'
 7166:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 7167:                                           .&mt('Display').'</label>&nbsp;'
 7168:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 7169:                                           .&mt('Do not display').'</label></span></td></tr>';
 7170:                         }
 7171:                     }
 7172:                 }
 7173:             } else {
 7174:                 $datatable .= &initialize_categories($itemcount);
 7175:             }
 7176:         } else {
 7177:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 7178:                           .&initialize_categories($itemcount);
 7179:         }
 7180:         $$rowtotal += $itemcount;
 7181:     }
 7182:     return $datatable;
 7183: }
 7184: 
 7185: sub print_serverstatuses {
 7186:     my ($dom,$settings,$rowtotal) = @_;
 7187:     my $datatable;
 7188:     my @pages = &serverstatus_pages();
 7189:     my (%namedaccess,%machineaccess);
 7190:     foreach my $type (@pages) {
 7191:         $namedaccess{$type} = '';
 7192:         $machineaccess{$type}= '';
 7193:     }
 7194:     if (ref($settings) eq 'HASH') {
 7195:         foreach my $type (@pages) {
 7196:             if (exists($settings->{$type})) {
 7197:                 if (ref($settings->{$type}) eq 'HASH') {
 7198:                     foreach my $key (keys(%{$settings->{$type}})) {
 7199:                         if ($key eq 'namedusers') {
 7200:                             $namedaccess{$type} = $settings->{$type}->{$key};
 7201:                         } elsif ($key eq 'machines') {
 7202:                             $machineaccess{$type} = $settings->{$type}->{$key};
 7203:                         }
 7204:                     }
 7205:                 }
 7206:             }
 7207:         }
 7208:     }
 7209:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7210:     my $rownum = 0;
 7211:     my $css_class;
 7212:     foreach my $type (@pages) {
 7213:         $rownum ++;
 7214:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 7215:         $datatable .= '<tr'.$css_class.'>'.
 7216:                       '<td><span class="LC_nobreak">'.
 7217:                       $titles->{$type}.'</span></td>'.
 7218:                       '<td class="LC_left_item">'.
 7219:                       '<input type="text" name="'.$type.'_namedusers" '.
 7220:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 7221:                       '<td class="LC_right_item">'.
 7222:                       '<span class="LC_nobreak">'.
 7223:                       '<input type="text" name="'.$type.'_machines" '.
 7224:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 7225:                       '</span></td></tr>'."\n";
 7226:     }
 7227:     $$rowtotal += $rownum;
 7228:     return $datatable;
 7229: }
 7230: 
 7231: sub serverstatus_pages {
 7232:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 7233:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 7234:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 7235:             'uniquecodes','diskusage','coursecatalog');
 7236: }
 7237: 
 7238: sub defaults_javascript {
 7239:     my ($settings) = @_;
 7240:     return unless (ref($settings) eq 'HASH');
 7241:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7242:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 7243:         if ($maxnum eq '') {
 7244:             $maxnum = 0;
 7245:         }
 7246:         $maxnum ++;
 7247:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 7248:         return <<"ENDSCRIPT";
 7249: <script type="text/javascript">
 7250: // <![CDATA[
 7251: function reorderTypes(form,caller) {
 7252:     var changedVal;
 7253: $jstext 
 7254:     var newpos = 'addinststatus_pos';
 7255:     var current = new Array;
 7256:     var maxh = $maxnum;
 7257:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7258:     var oldVal;
 7259:     if (caller == newpos) {
 7260:         changedVal = newitemVal;
 7261:     } else {
 7262:         var curritem = 'inststatus_pos_'+caller;
 7263:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7264:         current[newitemVal] = newpos;
 7265:     }
 7266:     for (var i=0; i<inststatuses.length; i++) {
 7267:         if (inststatuses[i] != caller) {
 7268:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7269:             if (form.elements[elementName]) {
 7270:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7271:                 current[currVal] = elementName;
 7272:             }
 7273:         }
 7274:     }
 7275:     for (var j=0; j<maxh; j++) {
 7276:         if (current[j] == undefined) {
 7277:             oldVal = j;
 7278:         }
 7279:     }
 7280:     if (oldVal < changedVal) {
 7281:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7282:            var elementName = current[k];
 7283:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7284:         }
 7285:     } else {
 7286:         for (var k=changedVal; k<oldVal; k++) {
 7287:             var elementName = current[k];
 7288:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7289:         }
 7290:     }
 7291:     return;
 7292: }
 7293: 
 7294: // ]]>
 7295: </script>
 7296: 
 7297: ENDSCRIPT
 7298:     }
 7299: }
 7300: 
 7301: sub passwords_javascript {
 7302:     my %intalert = &Apache::lonlocal::texthash (
 7303:         authcheck => 'Warning: disallowing login for an authenticated user if the stored cost is less than the default will require a password reset by/for the user.',
 7304:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
 7305:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
 7306:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
 7307:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
 7308:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
 7309:     );
 7310:     &js_escape(\%intalert);
 7311:     my $defmin = $Apache::lonnet::passwdmin;
 7312:     my $intauthjs = <<"ENDSCRIPT";
 7313: 
 7314: function warnIntAuth(field) {
 7315:     if (field.name == 'intauth_check') {
 7316:         if (field.value == '2') {
 7317:             alert('$intalert{authcheck}');
 7318:         }
 7319:     }
 7320:     if (field.name == 'intauth_cost') {
 7321:         field.value.replace(/\s/g,'');
 7322:         if (field.value != '') {
 7323:             var regexdigit=/^\\d+\$/;
 7324:             if (!regexdigit.test(field.value)) {
 7325:                 alert('$intalert{authcost}');
 7326:             }
 7327:         }
 7328:     }
 7329:     return;
 7330: }
 7331: 
 7332: function warnIntPass(field) {
 7333:     field.value.replace(/^\s+/,'');
 7334:     field.value.replace(/\s+\$/,'');
 7335:     var regexdigit=/^\\d+\$/;
 7336:     if (field.name == 'passwords_min') {
 7337:         if (field.value == '') {
 7338:             alert('$intalert{passmin}');
 7339:             field.value = '$defmin';
 7340:         } else {
 7341:             if (!regexdigit.test(field.value)) {
 7342:                 alert('$intalert{passmin}');
 7343:                 field.value = '$defmin';
 7344:             }
 7345:             var minval = parseInt(field.value,10);
 7346:             if (minval < $defmin) {
 7347:                 alert('$intalert{passmin}');
 7348:                 field.value = '$defmin';
 7349:             }
 7350:         }
 7351:     } else {
 7352:         if (field.value == '0') {
 7353:             field.value = '';
 7354:         }
 7355:         if (field.value != '') {
 7356:             if (field.name == 'passwords_expire') {
 7357:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/;
 7358:                 if (!regexpposnum.test(field.value)) {
 7359:                     alert('$intalert{passexp}');
 7360:                     field.value = '';
 7361:                 } else {
 7362:                     var expval = parseFloat(field.value);
 7363:                     if (expval == 0) {
 7364:                         alert('$intalert{passexp}');
 7365:                         field.value = '';
 7366:                     }
 7367:                 }
 7368:             } else {
 7369:                 if (!regexdigit.test(field.value)) {
 7370:                     if (field.name == 'passwords_max') {
 7371:                         alert('$intalert{passmax}');
 7372:                     } else {
 7373:                         if (field.name == 'passwords_numsaved') {
 7374:                             alert('$intalert{passnum}');
 7375:                         }
 7376:                     }
 7377:                     field.value = '';
 7378:                 }
 7379:             }
 7380:         }
 7381:     }
 7382:     return;
 7383: }
 7384: 
 7385: ENDSCRIPT
 7386:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7387: }
 7388: 
 7389: sub coursecategories_javascript {
 7390:     my ($settings) = @_;
 7391:     my ($output,$jstext,$cathash);
 7392:     if (ref($settings) eq 'HASH') {
 7393:         $cathash = $settings->{'cats'};
 7394:     }
 7395:     if (ref($cathash) eq 'HASH') {
 7396:         my (@cats,@jsarray,%idx);
 7397:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7398:         if (@jsarray > 0) {
 7399:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7400:             for (my $i=0; $i<@jsarray; $i++) {
 7401:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7402:                     my $catstr = join('","',@{$jsarray[$i]});
 7403:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7404:                 }
 7405:             }
 7406:         }
 7407:     } else {
 7408:         $jstext  = '    var categories = Array(1);'."\n".
 7409:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7410:     }
 7411:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7412:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7413:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7414:     &js_escape(\$instcode_reserved);
 7415:     &js_escape(\$communities_reserved);
 7416:     &js_escape(\$choose_again);
 7417:     $output = <<"ENDSCRIPT";
 7418: <script type="text/javascript">
 7419: // <![CDATA[
 7420: function reorderCats(form,parent,item,idx) {
 7421:     var changedVal;
 7422: $jstext
 7423:     var newpos = 'addcategory_pos';
 7424:     if (parent == '') {
 7425:         var has_instcode = 0;
 7426:         var maxtop = categories[idx].length;
 7427:         for (var j=0; j<maxtop; j++) {
 7428:             if (categories[idx][j] == 'instcode::0') {
 7429:                 has_instcode == 1;
 7430:             }
 7431:         }
 7432:         if (has_instcode == 0) {
 7433:             categories[idx][maxtop] = 'instcode_pos';
 7434:         }
 7435:     } else {
 7436:         newpos += '_'+parent;
 7437:     }
 7438:     var maxh = 1 + categories[idx].length;
 7439:     var current = new Array;
 7440:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7441:     if (item == newpos) {
 7442:         changedVal = newitemVal;
 7443:     } else {
 7444:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7445:         current[newitemVal] = newpos;
 7446:     }
 7447:     for (var i=0; i<categories[idx].length; i++) {
 7448:         var elementName = categories[idx][i];
 7449:         if (elementName != item) {
 7450:             if (form.elements[elementName]) {
 7451:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7452:                 current[currVal] = elementName;
 7453:             }
 7454:         }
 7455:     }
 7456:     var oldVal;
 7457:     for (var j=0; j<maxh; j++) {
 7458:         if (current[j] == undefined) {
 7459:             oldVal = j;
 7460:         }
 7461:     }
 7462:     if (oldVal < changedVal) {
 7463:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7464:            var elementName = current[k];
 7465:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7466:         }
 7467:     } else {
 7468:         for (var k=changedVal; k<oldVal; k++) {
 7469:             var elementName = current[k];
 7470:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7471:         }
 7472:     }
 7473:     return;
 7474: }
 7475: 
 7476: function categoryCheck(form) {
 7477:     if (form.elements['addcategory_name'].value == 'instcode') {
 7478:         alert('$instcode_reserved\\n$choose_again');
 7479:         return false;
 7480:     }
 7481:     if (form.elements['addcategory_name'].value == 'communities') {
 7482:         alert('$communities_reserved\\n$choose_again');
 7483:         return false;
 7484:     }
 7485:     return true;
 7486: }
 7487: 
 7488: // ]]>
 7489: </script>
 7490: 
 7491: ENDSCRIPT
 7492:     return $output;
 7493: }
 7494: 
 7495: sub initialize_categories {
 7496:     my ($itemcount) = @_;
 7497:     my ($datatable,$css_class,$chgstr);
 7498:     my %default_names = (
 7499:                       instcode    => 'Official courses (with institutional codes)',
 7500:                       communities => 'Communities',
 7501:                         );
 7502:     my $select0 = ' selected="selected"';
 7503:     my $select1 = '';
 7504:     foreach my $default ('instcode','communities') {
 7505:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7506:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 7507:         if ($default eq 'communities') {
 7508:             $select1 = $select0;
 7509:             $select0 = '';
 7510:         }
 7511:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7512:                      .'<select name="'.$default.'_pos">'
 7513:                      .'<option value="0"'.$select0.'>1</option>'
 7514:                      .'<option value="1"'.$select1.'>2</option>'
 7515:                      .'<option value="2">3</option></select>&nbsp;'
 7516:                      .$default_names{$default}
 7517:                      .'</span></td><td><span class="LC_nobreak">'
 7518:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7519:                      .&mt('Display').'</label>&nbsp;<label>'
 7520:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7521:                  .'</label></span></td></tr>';
 7522:         $itemcount ++;
 7523:     }
 7524:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7525:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7526:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7527:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7528:                   .'<option value="0">1</option>'
 7529:                   .'<option value="1">2</option>'
 7530:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7531:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 7532:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 7533:                   .'</td></tr>';
 7534:     return $datatable;
 7535: }
 7536: 
 7537: sub build_category_rows {
 7538:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7539:     my ($text,$name,$item,$chgstr);
 7540:     if (ref($cats) eq 'ARRAY') {
 7541:         my $maxdepth = scalar(@{$cats});
 7542:         if (ref($cats->[$depth]) eq 'HASH') {
 7543:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7544:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7545:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7546:                 $text .= '<td><table class="LC_data_table">';
 7547:                 my ($idxnum,$parent_name,$parent_item);
 7548:                 my $higher = $depth - 1;
 7549:                 if ($higher == 0) {
 7550:                     $parent_name = &escape($parent).'::'.$higher;
 7551:                 } else {
 7552:                     if (ref($path) eq 'ARRAY') {
 7553:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7554:                     }
 7555:                 }
 7556:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7557:                 for (my $j=0; $j<=$numchildren; $j++) {
 7558:                     if ($j < $numchildren) {
 7559:                         $name = $cats->[$depth]{$parent}[$j];
 7560:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7561:                         $idxnum = $idx->{$item};
 7562:                     } else {
 7563:                         $name = $parent_name;
 7564:                         $item = $parent_item;
 7565:                     }
 7566:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7567:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7568:                     for (my $i=0; $i<=$numchildren; $i++) {
 7569:                         my $vpos = $i+1;
 7570:                         my $selstr;
 7571:                         if ($j == $i) {
 7572:                             $selstr = ' selected="selected" ';
 7573:                         }
 7574:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7575:                     }
 7576:                     $text .= '</select>&nbsp;';
 7577:                     if ($j < $numchildren) {
 7578:                         my $deeper = $depth+1;
 7579:                         $text .= $name.'&nbsp;'
 7580:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7581:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7582:                         if(ref($path) eq 'ARRAY') {
 7583:                             push(@{$path},$name);
 7584:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7585:                             pop(@{$path});
 7586:                         }
 7587:                     } else {
 7588:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 7589:                         if ($j == $numchildren) {
 7590:                             $text .= $name;
 7591:                         } else {
 7592:                             $text .= $item;
 7593:                         }
 7594:                         $text .= '" value="" />';
 7595:                     }
 7596:                     $text .= '</td></tr>';
 7597:                 }
 7598:                 $text .= '</table></td>';
 7599:             } else {
 7600:                 my $higher = $depth-1;
 7601:                 if ($higher == 0) {
 7602:                     $name = &escape($parent).'::'.$higher;
 7603:                 } else {
 7604:                     if (ref($path) eq 'ARRAY') {
 7605:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7606:                     }
 7607:                 }
 7608:                 my $colspan;
 7609:                 if ($parent ne 'instcode') {
 7610:                     $colspan = $maxdepth - $depth - 1;
 7611:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 7612:                 }
 7613:             }
 7614:         }
 7615:     }
 7616:     return $text;
 7617: }
 7618: 
 7619: sub modifiable_userdata_row {
 7620:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 7621:         $rowid,$customcss,$rowstyle) = @_;
 7622:     my ($role,$rolename,$statustype);
 7623:     $role = $item;
 7624:     if ($context eq 'cancreate') {
 7625:         if ($item =~ /^(emailusername)_(.+)$/) {
 7626:             $role = $1;
 7627:             $statustype = $2;
 7628:             if (ref($usertypes) eq 'HASH') {
 7629:                 if ($usertypes->{$statustype}) {
 7630:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7631:                 } else {
 7632:                     $rolename = &mt('Data provided by user');
 7633:                 }
 7634:             }
 7635:         }
 7636:     } elsif ($context eq 'selfcreate') {
 7637:         if (ref($usertypes) eq 'HASH') {
 7638:             $rolename = $usertypes->{$role};
 7639:         } else {
 7640:             $rolename = $role;
 7641:         }
 7642:     } else {
 7643:         if ($role eq 'cr') {
 7644:             $rolename = &mt('Custom role');
 7645:         } else {
 7646:             $rolename = &Apache::lonnet::plaintext($role);
 7647:         }
 7648:     }
 7649:     my (@fields,%fieldtitles);
 7650:     if (ref($fieldsref) eq 'ARRAY') {
 7651:         @fields = @{$fieldsref};
 7652:     } else {
 7653:         @fields = ('lastname','firstname','middlename','generation',
 7654:                    'permanentemail','id');
 7655:     }
 7656:     if ((ref($titlesref) eq 'HASH')) {
 7657:         %fieldtitles = %{$titlesref};
 7658:     } else {
 7659:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7660:     }
 7661:     my $output;
 7662:     my $css_class;
 7663:     if ($rowcount%2) {
 7664:         $css_class = 'LC_odd_row';
 7665:     }
 7666:     if ($customcss) {
 7667:         $css_class .= " $customcss";
 7668:     }
 7669:     $css_class =~ s/^\s+//;
 7670:     if ($css_class) {
 7671:         $css_class = ' class="'.$css_class.'"';
 7672:     }
 7673:     if ($rowstyle) {
 7674:         $css_class .= ' style="'.$rowstyle.'"';
 7675:     }
 7676:     if ($rowid) {
 7677:         $rowid = ' id="'.$rowid.'"';
 7678:     }
 7679: 
 7680:     $output = '<tr '.$css_class.$rowid.'>'.
 7681:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7682:               '<td class="LC_left_item" colspan="2"><table>';
 7683:     my $rem;
 7684:     my %checks;
 7685:     if (ref($settings) eq 'HASH') {
 7686:         if (ref($settings->{$context}) eq 'HASH') {
 7687:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7688:                 my $hashref = $settings->{$context}->{$role};
 7689:                 if ($role eq 'emailusername') {
 7690:                     if ($statustype) {
 7691:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7692:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7693:                             if (ref($hashref) eq 'HASH') { 
 7694:                                 foreach my $field (@fields) {
 7695:                                     if ($hashref->{$field}) {
 7696:                                         $checks{$field} = $hashref->{$field};
 7697:                                     }
 7698:                                 }
 7699:                             }
 7700:                         }
 7701:                     }
 7702:                 } else {
 7703:                     if (ref($hashref) eq 'HASH') {
 7704:                         foreach my $field (@fields) {
 7705:                             if ($hashref->{$field}) {
 7706:                                 $checks{$field} = ' checked="checked" ';
 7707:                             }
 7708:                         }
 7709:                     }
 7710:                 }
 7711:             }
 7712:         }
 7713:     }
 7714: 
 7715:     my $total = scalar(@fields);
 7716:     for (my $i=0; $i<$total; $i++) {
 7717:         $rem = $i%($numinrow);
 7718:         if ($rem == 0) {
 7719:             if ($i > 0) {
 7720:                 $output .= '</tr>';
 7721:             }
 7722:             $output .= '<tr>';
 7723:         }
 7724:         my $check = ' ';
 7725:         unless ($role eq 'emailusername') {
 7726:             if (exists($checks{$fields[$i]})) {
 7727:                 $check = $checks{$fields[$i]};
 7728:             } else {
 7729:                 if ($role eq 'st') {
 7730:                     if (ref($settings) ne 'HASH') {
 7731:                         $check = ' checked="checked" '; 
 7732:                     }
 7733:                 }
 7734:             }
 7735:         }
 7736:         $output .= '<td class="LC_left_item">'.
 7737:                    '<span class="LC_nobreak">';
 7738:         if ($role eq 'emailusername') {
 7739:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7740:                 $checks{$fields[$i]} = 'omit';
 7741:             }
 7742:             foreach my $option ('required','optional','omit') {
 7743:                 my $checked='';
 7744:                 if ($checks{$fields[$i]} eq $option) {
 7745:                     $checked='checked="checked" ';
 7746:                 }
 7747:                 $output .= '<label>'.
 7748:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7749:                            &mt($option).'</label>'.('&nbsp;' x2);
 7750:             }
 7751:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7752:         } else {
 7753:             $output .= '<label>'.
 7754:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7755:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7756:                        '</label>';
 7757:         }
 7758:         $output .= '</span></td>';
 7759:     }
 7760:     $rem = $total%$numinrow;
 7761:     my $colsleft;
 7762:     if ($rem) {
 7763:         $colsleft = $numinrow - $rem;
 7764:     }
 7765:     if ($colsleft > 1) {
 7766:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7767:                    '&nbsp;</td>';
 7768:     } elsif ($colsleft == 1) {
 7769:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7770:     }
 7771:     $output .= '</tr></table></td></tr>';
 7772:     return $output;
 7773: }
 7774: 
 7775: sub insttypes_row {
 7776:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 7777:         $customcss,$rowstyle) = @_;
 7778:     my %lt = &Apache::lonlocal::texthash (
 7779:                       cansearch => 'Users allowed to search',
 7780:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7781:                       lockablenames => 'User preference to lock name',
 7782:                       selfassign    => 'Self-reportable affiliations',
 7783:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 7784:              );
 7785:     my $showdom;
 7786:     if ($context eq 'cansearch') {
 7787:         $showdom = ' ('.$dom.')';
 7788:     }
 7789:     my $class = 'LC_left_item';
 7790:     if ($context eq 'statustocreate') {
 7791:         $class = 'LC_right_item';
 7792:     }
 7793:     my $css_class;
 7794:     if ($$rowtotal%2) {
 7795:         $css_class = 'LC_odd_row';
 7796:     }
 7797:     if ($customcss) {
 7798:         $css_class .= ' '.$customcss;
 7799:     }
 7800:     $css_class =~ s/^\s+//;
 7801:     if ($css_class) {
 7802:         $css_class = ' class="'.$css_class.'"';
 7803:     }
 7804:     if ($rowstyle) {
 7805:         $css_class .= ' style="'.$rowstyle.'"';
 7806:     }
 7807:     if ($onclick) {
 7808:         $onclick = 'onclick="'.$onclick.'" ';
 7809:     }
 7810:     my $output = '<tr'.$css_class.'>'.
 7811:                  '<td>'.$lt{$context}.$showdom.
 7812:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7813:     my $rem;
 7814:     if (ref($types) eq 'ARRAY') {
 7815:         for (my $i=0; $i<@{$types}; $i++) {
 7816:             if (defined($usertypes->{$types->[$i]})) {
 7817:                 my $rem = $i%($numinrow);
 7818:                 if ($rem == 0) {
 7819:                     if ($i > 0) {
 7820:                         $output .= '</tr>';
 7821:                     }
 7822:                     $output .= '<tr>';
 7823:                 }
 7824:                 my $check = ' ';
 7825:                 if (ref($settings) eq 'HASH') {
 7826:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7827:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7828:                             $check = ' checked="checked" ';
 7829:                         }
 7830:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 7831:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 7832:                             $check = ' checked="checked" ';
 7833:                         }
 7834:                     } elsif ($context eq 'statustocreate') {
 7835:                         $check = ' checked="checked" ';
 7836:                     }
 7837:                 }
 7838:                 $output .= '<td class="LC_left_item">'.
 7839:                            '<span class="LC_nobreak"><label>'.
 7840:                            '<input type="checkbox" name="'.$context.'" '.
 7841:                            'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
 7842:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7843:             }
 7844:         }
 7845:         $rem = @{$types}%($numinrow);
 7846:     }
 7847:     my $colsleft = $numinrow - $rem;
 7848:     if ($context eq 'overrides') {
 7849:         if ($colsleft > 1) {
 7850:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7851:         } else {
 7852:             $output .= '<td class="LC_left_item">';
 7853:         }
 7854:         $output .= '&nbsp;';
 7855:     } else {
 7856:         if ($rem == 0) {
 7857:             $output .= '<tr>';
 7858:         }
 7859:         if ($colsleft > 1) {
 7860:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7861:         } else {
 7862:             $output .= '<td class="LC_left_item">';
 7863:         }
 7864:         my $defcheck = ' ';
 7865:         if (ref($settings) eq 'HASH') {  
 7866:             if (ref($settings->{$context}) eq 'ARRAY') {
 7867:                 if (grep(/^default$/,@{$settings->{$context}})) {
 7868:                     $defcheck = ' checked="checked" ';
 7869:                 }
 7870:             } elsif ($context eq 'statustocreate') {
 7871:                 $defcheck = ' checked="checked" ';
 7872:             }
 7873:         }
 7874:         $output .= '<span class="LC_nobreak"><label>'.
 7875:                    '<input type="checkbox" name="'.$context.'" '.
 7876:                    'value="default"'.$defcheck.$onclick.' />'.
 7877:                    $othertitle.'</label></span>';
 7878:     }
 7879:     $output .= '</td></tr></table></td></tr>';
 7880:     return $output;
 7881: }
 7882: 
 7883: sub sorted_searchtitles {
 7884:     my %searchtitles = &Apache::lonlocal::texthash(
 7885:                          'uname' => 'username',
 7886:                          'lastname' => 'last name',
 7887:                          'lastfirst' => 'last name, first name',
 7888:                      );
 7889:     my @titleorder = ('uname','lastname','lastfirst');
 7890:     return (\%searchtitles,\@titleorder);
 7891: }
 7892: 
 7893: sub sorted_searchtypes {
 7894:     my %srchtypes_desc = (
 7895:                            exact    => 'is exact match',
 7896:                            contains => 'contains ..',
 7897:                            begins   => 'begins with ..',
 7898:                          );
 7899:     my @srchtypeorder = ('exact','begins','contains');
 7900:     return (\%srchtypes_desc,\@srchtypeorder);
 7901: }
 7902: 
 7903: sub usertype_update_row {
 7904:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7905:     my $datatable;
 7906:     my $numinrow = 4;
 7907:     foreach my $type (@{$types}) {
 7908:         if (defined($usertypes->{$type})) {
 7909:             $$rownums ++;
 7910:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7911:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7912:                           '</td><td class="LC_left_item"><table>';
 7913:             for (my $i=0; $i<@{$fields}; $i++) {
 7914:                 my $rem = $i%($numinrow);
 7915:                 if ($rem == 0) {
 7916:                     if ($i > 0) {
 7917:                         $datatable .= '</tr>';
 7918:                     }
 7919:                     $datatable .= '<tr>';
 7920:                 }
 7921:                 my $check = ' ';
 7922:                 if (ref($settings) eq 'HASH') {
 7923:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7924:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7925:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7926:                                 $check = ' checked="checked" ';
 7927:                             }
 7928:                         }
 7929:                     }
 7930:                 }
 7931: 
 7932:                 if ($i == @{$fields}-1) {
 7933:                     my $colsleft = $numinrow - $rem;
 7934:                     if ($colsleft > 1) {
 7935:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7936:                     } else {
 7937:                         $datatable .= '<td>';
 7938:                     }
 7939:                 } else {
 7940:                     $datatable .= '<td>';
 7941:                 }
 7942:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7943:                               '<input type="checkbox" name="updateable_'.$type.
 7944:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7945:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7946:             }
 7947:             $datatable .= '</tr></table></td></tr>';
 7948:         }
 7949:     }
 7950:     return $datatable;
 7951: }
 7952: 
 7953: sub modify_login {
 7954:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7955:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7956:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7957:     %title = ( coursecatalog => 'Display course catalog',
 7958:                adminmail => 'Display administrator E-mail address',
 7959:                helpdesk  => 'Display "Contact Helpdesk" link',
 7960:                newuser => 'Link for visitors to create a user account',
 7961:                loginheader => 'Log-in box header');
 7962:     @offon = ('off','on');
 7963:     if (ref($domconfig{login}) eq 'HASH') {
 7964:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7965:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7966:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7967:             }
 7968:         }
 7969:     }
 7970:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7971:                                            \%domconfig,\%loginhash);
 7972:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7973:     foreach my $item (@toggles) {
 7974:         $loginhash{login}{$item} = $env{'form.'.$item};
 7975:     }
 7976:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7977:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7978:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7979:                                          \%loginhash);
 7980:     }
 7981: 
 7982:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7983:     my %domservers = &Apache::lonnet::get_servers($dom);
 7984:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7985:     if (keys(%servers) > 1) {
 7986:         foreach my $lonhost (keys(%servers)) {
 7987:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7988:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7989:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7990:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7991:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7992:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7993:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7994:                         $changes{'loginvia'}{$lonhost} = 1;
 7995:                     } else {
 7996:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7997:                         $changes{'loginvia'}{$lonhost} = 1;
 7998:                     }
 7999:                 } else {
 8000:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8001:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8002:                         $changes{'loginvia'}{$lonhost} = 1;
 8003:                     }
 8004:                 }
 8005:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 8006:                     foreach my $item (@loginvia_attribs) {
 8007:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 8008:                     }
 8009:                 } else {
 8010:                     foreach my $item (@loginvia_attribs) {
 8011:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8012:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8013:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 8014:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8015:                                 $new = '/';
 8016:                             }
 8017:                         }
 8018:                         if (($item eq 'custompath') && 
 8019:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8020:                             $new = '';
 8021:                         }
 8022:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 8023:                             $changes{'loginvia'}{$lonhost} = 1;
 8024:                         }
 8025:                         if ($item eq 'exempt') {
 8026:                             $new = &check_exempt_addresses($new);
 8027:                         }
 8028:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8029:                     }
 8030:                 }
 8031:             } else {
 8032:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8033:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8034:                     $changes{'loginvia'}{$lonhost} = 1;
 8035:                     foreach my $item (@loginvia_attribs) {
 8036:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8037:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8038:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8039:                                 $new = '/';
 8040:                             }
 8041:                         }
 8042:                         if (($item eq 'custompath') && 
 8043:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8044:                             $new = '';
 8045:                         }
 8046:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8047:                     }
 8048:                 }
 8049:             }
 8050:         }
 8051:     }
 8052: 
 8053:     my $servadm = $r->dir_config('lonAdmEMail');
 8054:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 8055:     if (ref($domconfig{'login'}) eq 'HASH') {
 8056:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 8057:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 8058:                 if ($lang eq 'nolang') {
 8059:                     push(@currlangs,$lang);
 8060:                 } elsif (defined($langchoices{$lang})) {
 8061:                     push(@currlangs,$lang);
 8062:                 } else {
 8063:                     next;
 8064:                 }
 8065:             }
 8066:         }
 8067:     }
 8068:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 8069:     if (@currlangs > 0) {
 8070:         foreach my $lang (@currlangs) {
 8071:             if (grep(/^\Q$lang\E$/,@delurls)) {
 8072:                 $changes{'helpurl'}{$lang} = 1;
 8073:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 8074:                 $changes{'helpurl'}{$lang} = 1;
 8075:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 8076:                 push(@newlangs,$lang);
 8077:             } else {
 8078:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8079:             }
 8080:         }
 8081:     }
 8082:     unless (grep(/^nolang$/,@currlangs)) {
 8083:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 8084:             $changes{'helpurl'}{'nolang'} = 1;
 8085:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 8086:             push(@newlangs,'nolang');
 8087:         }
 8088:     }
 8089:     if ($env{'form.loginhelpurl_add_lang'}) {
 8090:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 8091:             ($env{'form.loginhelpurl_add_file.filename'})) {
 8092:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 8093:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 8094:         }
 8095:     }
 8096:     if ((@newlangs > 0) || ($addedfile)) {
 8097:         my $error;
 8098:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8099:         if ($configuserok eq 'ok') {
 8100:             if ($switchserver) {
 8101:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 8102:             } elsif ($author_ok eq 'ok') {
 8103:                 my @allnew = @newlangs;
 8104:                 if ($addedfile ne '') {
 8105:                     push(@allnew,$addedfile);
 8106:                 }
 8107:                 foreach my $lang (@allnew) {
 8108:                     my $formelem = 'loginhelpurl_'.$lang;
 8109:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 8110:                         $formelem = 'loginhelpurl_add_file';
 8111:                     }
 8112:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8113:                                                                "help/$lang",'','',$newfile{$lang});
 8114:                     if ($result eq 'ok') {
 8115:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 8116:                         $changes{'helpurl'}{$lang} = 1;
 8117:                     } else {
 8118:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 8119:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8120:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 8121:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 8122:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8123:                         }
 8124:                     }
 8125:                 }
 8126:             } else {
 8127:                 $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);
 8128:             }
 8129:         } else {
 8130:             $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);
 8131:         }
 8132:         if ($error) {
 8133:             &Apache::lonnet::logthis($error);
 8134:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8135:         }
 8136:     }
 8137: 
 8138:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 8139:     if (ref($domconfig{'login'}) eq 'HASH') {
 8140:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 8141:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 8142:                 if ($domservers{$lonhost}) {
 8143:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8144:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 8145:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 8146:                     }
 8147:                 }
 8148:             }
 8149:         }
 8150:     }
 8151:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 8152:     foreach my $lonhost (sort(keys(%domservers))) {
 8153:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8154:             $changes{'headtag'}{$lonhost} = 1;
 8155:         } else {
 8156:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 8157:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 8158:             }
 8159:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 8160:                 push(@newhosts,$lonhost);
 8161:             } elsif ($currheadtagurls{$lonhost}) {
 8162:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 8163:                 if ($currexempt{$lonhost}) {
 8164:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 8165:                         $changes{'headtag'}{$lonhost} = 1;
 8166:                     }
 8167:                 } elsif ($possexempt{$lonhost}) {
 8168:                     $changes{'headtag'}{$lonhost} = 1;
 8169:                 }
 8170:                 if ($possexempt{$lonhost}) {
 8171:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8172:                 }
 8173:             }
 8174:         }
 8175:     }
 8176:     if (@newhosts) {
 8177:         my $error;
 8178:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8179:         if ($configuserok eq 'ok') {
 8180:             if ($switchserver) {
 8181:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8182:             } elsif ($author_ok eq 'ok') {
 8183:                 foreach my $lonhost (@newhosts) {
 8184:                     my $formelem = 'loginheadtag_'.$lonhost;
 8185:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8186:                                                                           "login/headtag/$lonhost",'','',
 8187:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8188:                     if ($result eq 'ok') {
 8189:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8190:                         $changes{'headtag'}{$lonhost} = 1;
 8191:                         if ($possexempt{$lonhost}) {
 8192:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8193:                         }
 8194:                     } else {
 8195:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 8196:                                            $newheadtagurls{$lonhost},$result);
 8197:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8198:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 8199:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 8200:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 8201:                         }
 8202:                     }
 8203:                 }
 8204:             } else {
 8205:                 $error = &mt("Upload of custom markup file(s) failed because an author role could not be assigned to a Domain Configuration user ([_1]) in domain: [_2].  Error was: [_3].",$confname,$dom,$author_ok);
 8206:             }
 8207:         } else {
 8208:             $error = &mt("Upload of custom markup file(s) failed because a Domain Configuration user ([_1]) could not be created in domain: [_2].  Error was: [_3].",$confname,$dom,$configuserok);
 8209:         }
 8210:         if ($error) {
 8211:             &Apache::lonnet::logthis($error);
 8212:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8213:         }
 8214:     }
 8215:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 8216: 
 8217:     my $defaulthelpfile = '/adm/loginproblems.html';
 8218:     my $defaulttext = &mt('Default in use');
 8219: 
 8220:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 8221:                                              $dom);
 8222:     if ($putresult eq 'ok') {
 8223:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8224:         my %defaultchecked = (
 8225:                     'coursecatalog' => 'on',
 8226:                     'helpdesk'      => 'on',
 8227:                     'adminmail'     => 'off',
 8228:                     'newuser'       => 'off',
 8229:         );
 8230:         if (ref($domconfig{'login'}) eq 'HASH') {
 8231:             foreach my $item (@toggles) {
 8232:                 if ($defaultchecked{$item} eq 'on') { 
 8233:                     if (($domconfig{'login'}{$item} eq '0') &&
 8234:                         ($env{'form.'.$item} eq '1')) {
 8235:                         $changes{$item} = 1;
 8236:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8237:                               $domconfig{'login'}{$item} eq '1') &&
 8238:                              ($env{'form.'.$item} eq '0')) {
 8239:                         $changes{$item} = 1;
 8240:                     }
 8241:                 } elsif ($defaultchecked{$item} eq 'off') {
 8242:                     if (($domconfig{'login'}{$item} eq '1') &&
 8243:                         ($env{'form.'.$item} eq '0')) {
 8244:                         $changes{$item} = 1;
 8245:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8246:                               $domconfig{'login'}{$item} eq '0') &&
 8247:                              ($env{'form.'.$item} eq '1')) {
 8248:                         $changes{$item} = 1;
 8249:                     }
 8250:                 }
 8251:             }
 8252:         }
 8253:         if (keys(%changes) > 0 || $colchgtext) {
 8254:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8255:             if (ref($lastactref) eq 'HASH') {
 8256:                 $lastactref->{'domainconfig'} = 1;
 8257:             }
 8258:             $resulttext = &mt('Changes made:').'<ul>';
 8259:             foreach my $item (sort(keys(%changes))) {
 8260:                 if ($item eq 'loginvia') {
 8261:                     if (ref($changes{$item}) eq 'HASH') {
 8262:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 8263:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8264:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 8265:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 8266:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 8267:                                     $protocol = 'http' if ($protocol ne 'https');
 8268:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 8269: 
 8270:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 8271:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 8272:                                     } else {
 8273:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 8274:                                     }
 8275:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 8276:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 8277:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 8278:                                     }
 8279:                                     $resulttext .= '</li>';
 8280:                                 } else {
 8281:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 8282:                                 }
 8283:                             } else {
 8284:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 8285:                             }
 8286:                         }
 8287:                         $resulttext .= '</ul></li>';
 8288:                     }
 8289:                 } elsif ($item eq 'helpurl') {
 8290:                     if (ref($changes{$item}) eq 'HASH') {
 8291:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 8292:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 8293:                                 my ($chg,$link);
 8294:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 8295:                                 if ($lang eq 'nolang') {
 8296:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 8297:                                 } else {
 8298:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 8299:                                 }
 8300:                                 $resulttext .= '<li>'.$chg.'</li>';
 8301:                             } else {
 8302:                                 my $chg;
 8303:                                 if ($lang eq 'nolang') {
 8304:                                     $chg = &mt('custom log-in help file for no preferred language');
 8305:                                 } else {
 8306:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 8307:                                 }
 8308:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 8309:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 8310:                                                       '?inhibitmenu=yes',$chg,600,500).
 8311:                                                '</li>';
 8312:                             }
 8313:                         }
 8314:                     }
 8315:                 } elsif ($item eq 'headtag') {
 8316:                     if (ref($changes{$item}) eq 'HASH') {
 8317:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8318:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8319:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 8320:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8321:                                 $resulttext .= '<li><a href="'.
 8322:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 8323:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 8324:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 8325:                                 if ($possexempt{$lonhost}) {
 8326:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 8327:                                 } else {
 8328:                                     $resulttext .= &mt('included for any client IP');
 8329:                                 }
 8330:                                 $resulttext .= '</li>';
 8331:                             }
 8332:                         }
 8333:                     }
 8334:                 } elsif ($item eq 'captcha') {
 8335:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8336:                         my $chgtxt;
 8337:                         if ($loginhash{'login'}{$item} eq 'notused') {
 8338:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 8339:                         } else {
 8340:                             my %captchas = &captcha_phrases();
 8341:                             if ($captchas{$loginhash{'login'}{$item}}) {
 8342:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 8343:                             } else {
 8344:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 8345:                             }
 8346:                         }
 8347:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8348:                     }
 8349:                 } elsif ($item eq 'recaptchakeys') {
 8350:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8351:                         my ($privkey,$pubkey);
 8352:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 8353:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 8354:                             $privkey = $loginhash{'login'}{$item}{'private'};
 8355:                         }
 8356:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 8357:                         if (!$pubkey) {
 8358:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 8359:                         } else {
 8360:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8361:                         }
 8362:                         if (!$privkey) {
 8363:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 8364:                         } else {
 8365:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 8366:                         }
 8367:                         $chgtxt .= '</ul>';
 8368:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8369:                     }
 8370:                 } elsif ($item eq 'recaptchaversion') {
 8371:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8372:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 8373:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 8374:                                            '</li>';
 8375:                         }
 8376:                     }
 8377:                 } else {
 8378:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 8379:                 }
 8380:             }
 8381:             $resulttext .= $colchgtext.'</ul>';
 8382:         } else {
 8383:             $resulttext = &mt('No changes made to log-in page settings');
 8384:         }
 8385:     } else {
 8386:         $resulttext = '<span class="LC_error">'.
 8387: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8388:     }
 8389:     if ($errors) {
 8390:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8391:                        $errors.'</ul>';
 8392:     }
 8393:     return $resulttext;
 8394: }
 8395: 
 8396: sub check_exempt_addresses {
 8397:     my ($iplist) = @_;
 8398:     $iplist =~ s/^\s+//;
 8399:     $iplist =~ s/\s+$//;
 8400:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8401:     my (@okips,$new);
 8402:     foreach my $ip (@poss_ips) {
 8403:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8404:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8405:                 push(@okips,$ip);
 8406:             }
 8407:         }
 8408:     }
 8409:     if (@okips > 0) {
 8410:         $new = join(',',@okips);
 8411:     } else {
 8412:         $new = '';
 8413:     }
 8414:     return $new;
 8415: }
 8416: 
 8417: sub color_font_choices {
 8418:     my %choices =
 8419:         &Apache::lonlocal::texthash (
 8420:             img => "Header",
 8421:             bgs => "Background colors",
 8422:             links => "Link colors",
 8423:             images => "Images",
 8424:             font => "Font color",
 8425:             fontmenu => "Font menu",
 8426:             pgbg => "Page",
 8427:             tabbg => "Header",
 8428:             sidebg => "Border",
 8429:             link => "Link",
 8430:             alink => "Active link",
 8431:             vlink => "Visited link",
 8432:         );
 8433:     return %choices;
 8434: }
 8435: 
 8436: sub modify_rolecolors {
 8437:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8438:     my ($resulttext,%rolehash);
 8439:     $rolehash{'rolecolors'} = {};
 8440:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8441:         if ($domconfig{'rolecolors'} eq '') {
 8442:             $domconfig{'rolecolors'} = {};
 8443:         }
 8444:     }
 8445:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8446:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8447:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8448:                                              $dom);
 8449:     if ($putresult eq 'ok') {
 8450:         if (keys(%changes) > 0) {
 8451:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8452:             if (ref($lastactref) eq 'HASH') {
 8453:                 $lastactref->{'domainconfig'} = 1;
 8454:             }
 8455:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8456:                                              $rolehash{'rolecolors'});
 8457:         } else {
 8458:             $resulttext = &mt('No changes made to default color schemes');
 8459:         }
 8460:     } else {
 8461:         $resulttext = '<span class="LC_error">'.
 8462: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8463:     }
 8464:     if ($errors) {
 8465:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8466:                        $errors.'</ul>';
 8467:     }
 8468:     return $resulttext;
 8469: }
 8470: 
 8471: sub modify_colors {
 8472:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8473:     my (%changes,%choices);
 8474:     my @bgs;
 8475:     my @links = ('link','alink','vlink');
 8476:     my @logintext;
 8477:     my @images;
 8478:     my $servadm = $r->dir_config('lonAdmEMail');
 8479:     my $errors;
 8480:     my %defaults;
 8481:     foreach my $role (@{$roles}) {
 8482:         if ($role eq 'login') {
 8483:             %choices = &login_choices();
 8484:             @logintext = ('textcol','bgcol');
 8485:         } else {
 8486:             %choices = &color_font_choices();
 8487:         }
 8488:         if ($role eq 'login') {
 8489:             @images = ('img','logo','domlogo','login');
 8490:             @bgs = ('pgbg','mainbg','sidebg');
 8491:         } else {
 8492:             @images = ('img');
 8493:             @bgs = ('pgbg','tabbg','sidebg');
 8494:         }
 8495:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8496:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8497:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8498:         }
 8499:         if ($role eq 'login') {
 8500:             foreach my $item (@logintext) {
 8501:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8502:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8503:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8504:                 }
 8505:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8506:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8507:                 }
 8508:             }
 8509:         } else {
 8510:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8511:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8512:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8513:             }
 8514:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8515:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8516:             }
 8517:         }
 8518:         foreach my $item (@bgs) {
 8519:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8520:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8521:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8522:             }
 8523:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8524:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8525:             }
 8526:         }
 8527:         foreach my $item (@links) {
 8528:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8529:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8530:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8531:             }
 8532:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8533:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8534:             }
 8535:         }
 8536:         my ($configuserok,$author_ok,$switchserver) = 
 8537:             &config_check($dom,$confname,$servadm);
 8538:         my ($width,$height) = &thumb_dimensions();
 8539:         if (ref($domconfig->{$role}) ne 'HASH') {
 8540:             $domconfig->{$role} = {};
 8541:         }
 8542:         foreach my $img (@images) {
 8543:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8544:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8545:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8546:                 } else { 
 8547:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8548:                 }
 8549:             } 
 8550: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8551: 		 && !defined($domconfig->{$role}{$img})
 8552: 		 && !$env{'form.'.$role.'_del_'.$img}
 8553: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8554: 		# import the old configured image from the .tab setting
 8555: 		# if they haven't provided a new one 
 8556: 		$domconfig->{$role}{$img} = 
 8557: 		    $env{'form.'.$role.'_import_'.$img};
 8558: 	    }
 8559:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8560:                 my $error;
 8561:                 if ($configuserok eq 'ok') {
 8562:                     if ($switchserver) {
 8563:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8564:                     } else {
 8565:                         if ($author_ok eq 'ok') {
 8566:                             my ($result,$logourl) = 
 8567:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8568:                                            $dom,$confname,$img,$width,$height);
 8569:                             if ($result eq 'ok') {
 8570:                                 $confhash->{$role}{$img} = $logourl;
 8571:                                 $changes{$role}{'images'}{$img} = 1;
 8572:                             } else {
 8573:                                 $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);
 8574:                             }
 8575:                         } else {
 8576:                             $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);
 8577:                         }
 8578:                     }
 8579:                 } else {
 8580:                     $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);
 8581:                 }
 8582:                 if ($error) {
 8583:                     &Apache::lonnet::logthis($error);
 8584:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8585:                 }
 8586:             } elsif ($domconfig->{$role}{$img} ne '') {
 8587:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8588:                     my $error;
 8589:                     if ($configuserok eq 'ok') {
 8590: # is confname an author?
 8591:                         if ($switchserver eq '') {
 8592:                             if ($author_ok eq 'ok') {
 8593:                                 my ($result,$logourl) = 
 8594:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8595:                                             $dom,$confname,$img,$width,$height);
 8596:                                 if ($result eq 'ok') {
 8597:                                     $confhash->{$role}{$img} = $logourl;
 8598: 				    $changes{$role}{'images'}{$img} = 1;
 8599:                                 }
 8600:                             }
 8601:                         }
 8602:                     }
 8603:                 }
 8604:             }
 8605:         }
 8606:         if (ref($domconfig) eq 'HASH') {
 8607:             if (ref($domconfig->{$role}) eq 'HASH') {
 8608:                 foreach my $img (@images) {
 8609:                     if ($domconfig->{$role}{$img} ne '') {
 8610:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8611:                             $confhash->{$role}{$img} = '';
 8612:                             $changes{$role}{'images'}{$img} = 1;
 8613:                         } else {
 8614:                             if ($confhash->{$role}{$img} eq '') {
 8615:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8616:                             }
 8617:                         }
 8618:                     } else {
 8619:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8620:                             $confhash->{$role}{$img} = '';
 8621:                             $changes{$role}{'images'}{$img} = 1;
 8622:                         } 
 8623:                     }
 8624:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8625:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8626:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8627:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8628:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8629:                             }
 8630:                         } else {
 8631:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8632:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8633:                             }
 8634:                         }
 8635:                     }
 8636:                 }
 8637:                 if ($domconfig->{$role}{'font'} ne '') {
 8638:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8639:                         $changes{$role}{'font'} = 1;
 8640:                     }
 8641:                 } else {
 8642:                     if ($confhash->{$role}{'font'}) {
 8643:                         $changes{$role}{'font'} = 1;
 8644:                     }
 8645:                 }
 8646:                 if ($role ne 'login') {
 8647:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8648:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8649:                             $changes{$role}{'fontmenu'} = 1;
 8650:                         }
 8651:                     } else {
 8652:                         if ($confhash->{$role}{'fontmenu'}) {
 8653:                             $changes{$role}{'fontmenu'} = 1;
 8654:                         }
 8655:                     }
 8656:                 }
 8657:                 foreach my $item (@bgs) {
 8658:                     if ($domconfig->{$role}{$item} ne '') {
 8659:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8660:                             $changes{$role}{'bgs'}{$item} = 1;
 8661:                         } 
 8662:                     } else {
 8663:                         if ($confhash->{$role}{$item}) {
 8664:                             $changes{$role}{'bgs'}{$item} = 1;
 8665:                         }
 8666:                     }
 8667:                 }
 8668:                 foreach my $item (@links) {
 8669:                     if ($domconfig->{$role}{$item} ne '') {
 8670:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8671:                             $changes{$role}{'links'}{$item} = 1;
 8672:                         }
 8673:                     } else {
 8674:                         if ($confhash->{$role}{$item}) {
 8675:                             $changes{$role}{'links'}{$item} = 1;
 8676:                         }
 8677:                     }
 8678:                 }
 8679:                 foreach my $item (@logintext) {
 8680:                     if ($domconfig->{$role}{$item} ne '') {
 8681:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8682:                             $changes{$role}{'logintext'}{$item} = 1;
 8683:                         }
 8684:                     } else {
 8685:                         if ($confhash->{$role}{$item}) {
 8686:                             $changes{$role}{'logintext'}{$item} = 1;
 8687:                         }
 8688:                     }
 8689:                 }
 8690:             } else {
 8691:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8692:                                         \@logintext,$confhash,\%changes); 
 8693:             }
 8694:         } else {
 8695:             &default_change_checker($role,\@images,\@links,\@bgs,
 8696:                                     \@logintext,$confhash,\%changes); 
 8697:         }
 8698:     }
 8699:     return ($errors,%changes);
 8700: }
 8701: 
 8702: sub config_check {
 8703:     my ($dom,$confname,$servadm) = @_;
 8704:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8705:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8706:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8707:                                                    $confname,$servadm);
 8708:     if ($configuserok eq 'ok') {
 8709:         $switchserver = &check_switchserver($dom,$confname);
 8710:         if ($switchserver eq '') {
 8711:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8712:         }
 8713:     }
 8714:     return ($configuserok,$author_ok,$switchserver);
 8715: }
 8716: 
 8717: sub default_change_checker {
 8718:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8719:     foreach my $item (@{$links}) {
 8720:         if ($confhash->{$role}{$item}) {
 8721:             $changes->{$role}{'links'}{$item} = 1;
 8722:         }
 8723:     }
 8724:     foreach my $item (@{$bgs}) {
 8725:         if ($confhash->{$role}{$item}) {
 8726:             $changes->{$role}{'bgs'}{$item} = 1;
 8727:         }
 8728:     }
 8729:     foreach my $item (@{$logintext}) {
 8730:         if ($confhash->{$role}{$item}) {
 8731:             $changes->{$role}{'logintext'}{$item} = 1;
 8732:         }
 8733:     }
 8734:     foreach my $img (@{$images}) {
 8735:         if ($env{'form.'.$role.'_del_'.$img}) {
 8736:             $confhash->{$role}{$img} = '';
 8737:             $changes->{$role}{'images'}{$img} = 1;
 8738:         }
 8739:         if ($role eq 'login') {
 8740:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8741:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8742:             }
 8743:         }
 8744:     }
 8745:     if ($confhash->{$role}{'font'}) {
 8746:         $changes->{$role}{'font'} = 1;
 8747:     }
 8748: }
 8749: 
 8750: sub display_colorchgs {
 8751:     my ($dom,$changes,$roles,$confhash) = @_;
 8752:     my (%choices,$resulttext);
 8753:     if (!grep(/^login$/,@{$roles})) {
 8754:         $resulttext = &mt('Changes made:').'<br />';
 8755:     }
 8756:     foreach my $role (@{$roles}) {
 8757:         if ($role eq 'login') {
 8758:             %choices = &login_choices();
 8759:         } else {
 8760:             %choices = &color_font_choices();
 8761:         }
 8762:         if (ref($changes->{$role}) eq 'HASH') {
 8763:             if ($role ne 'login') {
 8764:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8765:             }
 8766:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8767:                 if ($role ne 'login') {
 8768:                     $resulttext .= '<ul>';
 8769:                 }
 8770:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8771:                     if ($role ne 'login') {
 8772:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8773:                     }
 8774:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8775:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8776:                             if ($confhash->{$role}{$key}{$item}) {
 8777:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8778:                             } else {
 8779:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8780:                             }
 8781:                         } elsif ($confhash->{$role}{$item} eq '') {
 8782:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8783:                         } else {
 8784:                             my $newitem = $confhash->{$role}{$item};
 8785:                             if ($key eq 'images') {
 8786:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8787:                             }
 8788:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8789:                         }
 8790:                     }
 8791:                     if ($role ne 'login') {
 8792:                         $resulttext .= '</ul></li>';
 8793:                     }
 8794:                 } else {
 8795:                     if ($confhash->{$role}{$key} eq '') {
 8796:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8797:                     } else {
 8798:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8799:                     }
 8800:                 }
 8801:                 if ($role ne 'login') {
 8802:                     $resulttext .= '</ul>';
 8803:                 }
 8804:             }
 8805:         }
 8806:     }
 8807:     return $resulttext;
 8808: }
 8809: 
 8810: sub thumb_dimensions {
 8811:     return ('200','50');
 8812: }
 8813: 
 8814: sub check_dimensions {
 8815:     my ($inputfile) = @_;
 8816:     my ($fullwidth,$fullheight);
 8817:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8818:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8819:             my $imageinfo = <PIPE>;
 8820:             if (!close(PIPE)) {
 8821:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8822:             }
 8823:             chomp($imageinfo);
 8824:             my ($fullsize) = 
 8825:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8826:             if ($fullsize) {
 8827:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8828:             }
 8829:         }
 8830:     }
 8831:     return ($fullwidth,$fullheight);
 8832: }
 8833: 
 8834: sub check_configuser {
 8835:     my ($uhome,$dom,$confname,$servadm) = @_;
 8836:     my ($configuserok,%currroles);
 8837:     if ($uhome eq 'no_host') {
 8838:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8839:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
 8840:         $configuserok = 
 8841:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8842:                              $configpass,'','','','','',undef,$servadm);
 8843:     } else {
 8844:         $configuserok = 'ok';
 8845:         %currroles = 
 8846:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8847:     }
 8848:     return ($configuserok,%currroles);
 8849: }
 8850: 
 8851: sub check_authorstatus {
 8852:     my ($dom,$confname,%currroles) = @_;
 8853:     my $author_ok;
 8854:     if (!$currroles{':'.$dom.':au'}) {
 8855:         my $start = time;
 8856:         my $end = 0;
 8857:         $author_ok = 
 8858:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8859:                                         'au',$end,$start,'','','domconfig');
 8860:     } else {
 8861:         $author_ok = 'ok';
 8862:     }
 8863:     return $author_ok;
 8864: }
 8865: 
 8866: sub publishlogo {
 8867:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8868:     my ($output,$fname,$logourl);
 8869:     if ($action eq 'upload') {
 8870:         $fname=$env{'form.'.$formname.'.filename'};
 8871:         chop($env{'form.'.$formname});
 8872:     } else {
 8873:         ($fname) = ($formname =~ /([^\/]+)$/);
 8874:     }
 8875:     if ($savefileas ne '') {
 8876:         $fname = $savefileas;
 8877:     }
 8878:     $fname=&Apache::lonnet::clean_filename($fname);
 8879: # See if there is anything left
 8880:     unless ($fname) { return ('error: no uploaded file'); }
 8881:     $fname="$subdir/$fname";
 8882:     my $docroot=$r->dir_config('lonDocRoot');
 8883:     my $filepath="$docroot/priv";
 8884:     my $relpath = "$dom/$confname";
 8885:     my ($fnamepath,$file,$fetchthumb);
 8886:     $file=$fname;
 8887:     if ($fname=~m|/|) {
 8888:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8889:     }
 8890:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8891:     my $count;
 8892:     for ($count=5;$count<=$#parts;$count++) {
 8893:         $filepath.="/$parts[$count]";
 8894:         if ((-e $filepath)!=1) {
 8895:             mkdir($filepath,02770);
 8896:         }
 8897:     }
 8898:     # Check for bad extension and disallow upload
 8899:     if ($file=~/\.(\w+)$/ &&
 8900:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8901:         $output = 
 8902:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8903:     } elsif ($file=~/\.(\w+)$/ &&
 8904:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8905:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8906:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8907:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8908:     } elsif (-d "$filepath/$file") {
 8909:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8910:     } else {
 8911:         my $source = $filepath.'/'.$file;
 8912:         my $logfile;
 8913:         if (!open($logfile,">>",$source.'.log')) {
 8914:             return (&mt('No write permission to Authoring Space'));
 8915:         }
 8916:         print $logfile
 8917: "\n================= Publish ".localtime()." ================\n".
 8918: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8919: # Save the file
 8920:         if (!open(FH,">",$source)) {
 8921:             &Apache::lonnet::logthis('Failed to create '.$source);
 8922:             return (&mt('Failed to create file'));
 8923:         }
 8924:         if ($action eq 'upload') {
 8925:             if (!print FH ($env{'form.'.$formname})) {
 8926:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8927:                 return (&mt('Failed to write file'));
 8928:             }
 8929:         } else {
 8930:             my $original = &Apache::lonnet::filelocation('',$formname);
 8931:             if(!copy($original,$source)) {
 8932:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8933:                 return (&mt('Failed to write file'));
 8934:             }
 8935:         }
 8936:         close(FH);
 8937:         chmod(0660, $source); # Permissions to rw-rw---.
 8938: 
 8939:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8940:         my $copyfile=$targetdir.'/'.$file;
 8941: 
 8942:         my @parts=split(/\//,$targetdir);
 8943:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8944:         for (my $count=5;$count<=$#parts;$count++) {
 8945:             $path.="/$parts[$count]";
 8946:             if (!-e $path) {
 8947:                 print $logfile "\nCreating directory ".$path;
 8948:                 mkdir($path,02770);
 8949:             }
 8950:         }
 8951:         my $versionresult;
 8952:         if (-e $copyfile) {
 8953:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8954:         } else {
 8955:             $versionresult = 'ok';
 8956:         }
 8957:         if ($versionresult eq 'ok') {
 8958:             if (copy($source,$copyfile)) {
 8959:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8960:                 $output = 'ok';
 8961:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8962:                 push(@{$modified_urls},[$copyfile,$source]);
 8963:                 my $metaoutput = 
 8964:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8965:                 unless ($registered_cleanup) {
 8966:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8967:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8968:                     $registered_cleanup=1;
 8969:                 }
 8970:             } else {
 8971:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8972:                 $output = &mt('Failed to copy file to RES space').", $!";
 8973:             }
 8974:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8975:                 my $inputfile = $filepath.'/'.$file;
 8976:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8977:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8978:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8979:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8980:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8981:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 8982:                         system({$args[0]} @args);
 8983:                         chmod(0660, $filepath.'/tn-'.$file);
 8984:                         if (-e $outfile) {
 8985:                             my $copyfile=$targetdir.'/tn-'.$file;
 8986:                             if (copy($outfile,$copyfile)) {
 8987:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8988:                                 my $thumb_metaoutput = 
 8989:                                     &write_metadata($dom,$confname,$formname,
 8990:                                                     $targetdir,'tn-'.$file,$logfile);
 8991:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8992:                                 unless ($registered_cleanup) {
 8993:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8994:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8995:                                     $registered_cleanup=1;
 8996:                                 }
 8997:                             } else {
 8998:                                 print $logfile "\nUnable to write ".$copyfile.
 8999:                                                ':'.$!."\n";
 9000:                             }
 9001:                         }
 9002:                     }
 9003:                 }
 9004:             }
 9005:         } else {
 9006:             $output = $versionresult;
 9007:         }
 9008:     }
 9009:     return ($output,$logourl);
 9010: }
 9011: 
 9012: sub logo_versioning {
 9013:     my ($targetdir,$file,$logfile) = @_;
 9014:     my $target = $targetdir.'/'.$file;
 9015:     my ($maxversion,$fn,$extn,$output);
 9016:     $maxversion = 0;
 9017:     if ($file =~ /^(.+)\.(\w+)$/) {
 9018:         $fn=$1;
 9019:         $extn=$2;
 9020:     }
 9021:     opendir(DIR,$targetdir);
 9022:     while (my $filename=readdir(DIR)) {
 9023:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 9024:             $maxversion=($1>$maxversion)?$1:$maxversion;
 9025:         }
 9026:     }
 9027:     $maxversion++;
 9028:     print $logfile "\nCreating old version ".$maxversion."\n";
 9029:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 9030:     if (copy($target,$copyfile)) {
 9031:         print $logfile "Copied old target to ".$copyfile."\n";
 9032:         $copyfile=$copyfile.'.meta';
 9033:         if (copy($target.'.meta',$copyfile)) {
 9034:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 9035:             $output = 'ok';
 9036:         } else {
 9037:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 9038:             $output = &mt('Failed to copy old meta').", $!, ";
 9039:         }
 9040:     } else {
 9041:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 9042:         $output = &mt('Failed to copy old target').", $!, ";
 9043:     }
 9044:     return $output;
 9045: }
 9046: 
 9047: sub write_metadata {
 9048:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 9049:     my (%metadatafields,%metadatakeys,$output);
 9050:     $metadatafields{'title'}=$formname;
 9051:     $metadatafields{'creationdate'}=time;
 9052:     $metadatafields{'lastrevisiondate'}=time;
 9053:     $metadatafields{'copyright'}='public';
 9054:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 9055:                                          $env{'user.domain'};
 9056:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 9057:     $metadatafields{'domain'}=$dom;
 9058:     {
 9059:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 9060:         my $mfh;
 9061:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 9062:             foreach (sort(keys(%metadatafields))) {
 9063:                 unless ($_=~/\./) {
 9064:                     my $unikey=$_;
 9065:                     $unikey=~/^([A-Za-z]+)/;
 9066:                     my $tag=$1;
 9067:                     $tag=~tr/A-Z/a-z/;
 9068:                     print $mfh "\n\<$tag";
 9069:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 9070:                         my $value=$metadatafields{$unikey.'.'.$_};
 9071:                         $value=~s/\"/\'\'/g;
 9072:                         print $mfh ' '.$_.'="'.$value.'"';
 9073:                     }
 9074:                     print $mfh '>'.
 9075:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 9076:                             .'</'.$tag.'>';
 9077:                 }
 9078:             }
 9079:             $output = 'ok';
 9080:             print $logfile "\nWrote metadata";
 9081:             close($mfh);
 9082:         } else {
 9083:             print $logfile "\nFailed to open metadata file";
 9084:             $output = &mt('Could not write metadata');
 9085:         }
 9086:     }
 9087:     return $output;
 9088: }
 9089: 
 9090: sub notifysubscribed {
 9091:     foreach my $targetsource (@{$modified_urls}){
 9092:         next unless (ref($targetsource) eq 'ARRAY');
 9093:         my ($target,$source)=@{$targetsource};
 9094:         if ($source ne '') {
 9095:             if (open(my $logfh,">>",$source.'.log')) {
 9096:                 print $logfh "\nCleanup phase: Notifications\n";
 9097:                 my @subscribed=&subscribed_hosts($target);
 9098:                 foreach my $subhost (@subscribed) {
 9099:                     print $logfh "\nNotifying host ".$subhost.':';
 9100:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 9101:                     print $logfh $reply;
 9102:                 }
 9103:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 9104:                 foreach my $subhost (@subscribedmeta) {
 9105:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 9106:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 9107:                                                         $subhost);
 9108:                     print $logfh $reply;
 9109:                 }
 9110:                 print $logfh "\n============ Done ============\n";
 9111:                 close($logfh);
 9112:             }
 9113:         }
 9114:     }
 9115:     return OK;
 9116: }
 9117: 
 9118: sub subscribed_hosts {
 9119:     my ($target) = @_;
 9120:     my @subscribed;
 9121:     if (open(my $fh,"<","$target.subscription")) {
 9122:         while (my $subline=<$fh>) {
 9123:             if ($subline =~ /^($match_lonid):/) {
 9124:                 my $host = $1;
 9125:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 9126:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 9127:                         push(@subscribed,$host);
 9128:                     }
 9129:                 }
 9130:             }
 9131:         }
 9132:     }
 9133:     return @subscribed;
 9134: }
 9135: 
 9136: sub check_switchserver {
 9137:     my ($dom,$confname) = @_;
 9138:     my ($allowed,$switchserver);
 9139:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 9140:     if ($home eq 'no_host') {
 9141:         $home = &Apache::lonnet::domain($dom,'primary');
 9142:     }
 9143:     my @ids=&Apache::lonnet::current_machine_ids();
 9144:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 9145:     if (!$allowed) {
 9146: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 9147:     }
 9148:     return $switchserver;
 9149: }
 9150: 
 9151: sub modify_quotas {
 9152:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9153:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 9154:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 9155:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 9156:         $validationfieldsref);
 9157:     if ($action eq 'quotas') {
 9158:         $context = 'tools'; 
 9159:     } else {
 9160:         $context = $action;
 9161:     }
 9162:     if ($context eq 'requestcourses') {
 9163:         @usertools = ('official','unofficial','community','textbook');
 9164:         @options =('norequest','approval','validate','autolimit');
 9165:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 9166:         %titles = &courserequest_titles();
 9167:         $toolregexp = join('|',@usertools);
 9168:         %conditions = &courserequest_conditions();
 9169:         $confname = $dom.'-domainconfig';
 9170:         my $servadm = $r->dir_config('lonAdmEMail');
 9171:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9172:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 9173:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 9174:     } elsif ($context eq 'requestauthor') {
 9175:         @usertools = ('author');
 9176:         %titles = &authorrequest_titles();
 9177:     } else {
 9178:         @usertools = ('aboutme','blog','webdav','portfolio');
 9179:         %titles = &tool_titles();
 9180:     }
 9181:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9182:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9183:     foreach my $key (keys(%env)) {
 9184:         if ($context eq 'requestcourses') {
 9185:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9186:                 my $item = $1;
 9187:                 my $type = $2;
 9188:                 if ($type =~ /^limit_(.+)/) {
 9189:                     $limithash{$item}{$1} = $env{$key};
 9190:                 } else {
 9191:                     $confhash{$item}{$type} = $env{$key};
 9192:                 }
 9193:             }
 9194:         } elsif ($context eq 'requestauthor') {
 9195:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 9196:                 $confhash{$1} = $env{$key};
 9197:             }
 9198:         } else {
 9199:             if ($key =~ /^form\.quota_(.+)$/) {
 9200:                 $confhash{'defaultquota'}{$1} = $env{$key};
 9201:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 9202:                 $confhash{'authorquota'}{$1} = $env{$key};
 9203:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 9204:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 9205:             }
 9206:         }
 9207:     }
 9208:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9209:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 9210:         @approvalnotify = sort(@approvalnotify);
 9211:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 9212:         my @crstypes = ('official','unofficial','community','textbook');
 9213:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 9214:         foreach my $type (@hasuniquecode) {
 9215:             if (grep(/^\Q$type\E$/,@crstypes)) {
 9216:                 $confhash{'uniquecode'}{$type} = 1;
 9217:             }
 9218:         }
 9219:         my (%newbook,%allpos);
 9220:         if ($context eq 'requestcourses') {
 9221:             foreach my $type ('textbooks','templates') {
 9222:                 @{$allpos{$type}} = (); 
 9223:                 my $invalid;
 9224:                 if ($type eq 'textbooks') {
 9225:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 9226:                 } else {
 9227:                     $invalid = &mt('Invalid LON-CAPA course for template');
 9228:                 }
 9229:                 if ($env{'form.'.$type.'_addbook'}) {
 9230:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 9231:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 9232:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 9233:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 9234:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9235:                         } else {
 9236:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 9237:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 9238:                             $position =~ s/\D+//g;
 9239:                             if ($position ne '') {
 9240:                                 $allpos{$type}[$position] = $newbook{$type};
 9241:                             }
 9242:                         }
 9243:                     } else {
 9244:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9245:                     }
 9246:                 }
 9247:             } 
 9248:         }
 9249:         if (ref($domconfig{$action}) eq 'HASH') {
 9250:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 9251:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 9252:                     $changes{'notify'}{'approval'} = 1;
 9253:                 }
 9254:             } else {
 9255:                 if ($confhash{'notify'}{'approval'}) {
 9256:                     $changes{'notify'}{'approval'} = 1;
 9257:                 }
 9258:             }
 9259:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 9260:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9261:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 9262:                         unless ($confhash{'uniquecode'}{$crstype}) {
 9263:                             $changes{'uniquecode'} = 1;
 9264:                         }
 9265:                     }
 9266:                     unless ($changes{'uniquecode'}) {
 9267:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 9268:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 9269:                                 $changes{'uniquecode'} = 1;
 9270:                             }
 9271:                         }
 9272:                     }
 9273:                } else {
 9274:                    $changes{'uniquecode'} = 1;
 9275:                }
 9276:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 9277:                 $changes{'uniquecode'} = 1;
 9278:             }
 9279:             if ($context eq 'requestcourses') {
 9280:                 foreach my $type ('textbooks','templates') {
 9281:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9282:                         my %deletions;
 9283:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 9284:                         if (@todelete) {
 9285:                             map { $deletions{$_} = 1; } @todelete;
 9286:                         }
 9287:                         my %imgdeletions;
 9288:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 9289:                         if (@todeleteimages) {
 9290:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9291:                         }
 9292:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 9293:                         for (my $i=0; $i<=$maxnum; $i++) {
 9294:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 9295:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 9296:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 9297:                                 if ($deletions{$key}) {
 9298:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 9299:                                         #FIXME need to obsolete item in RES space
 9300:                                     }
 9301:                                     next;
 9302:                                 } else {
 9303:                                     my $newpos = $env{'form.'.$itemid};
 9304:                                     $newpos =~ s/\D+//g;
 9305:                                     foreach my $item ('subject','title','publisher','author') {
 9306:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9307:                                                  ($type eq 'templates'));
 9308:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 9309:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 9310:                                             $changes{$type}{$key} = 1;
 9311:                                         }
 9312:                                     }
 9313:                                     $allpos{$type}[$newpos] = $key;
 9314:                                 }
 9315:                                 if ($imgdeletions{$key}) {
 9316:                                     $changes{$type}{$key} = 1;
 9317:                                     #FIXME need to obsolete item in RES space
 9318:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 9319:                                     my ($cdom,$cnum) = split(/_/,$key);
 9320:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9321:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9322:                                     } else {
 9323:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 9324:                                                                                       $cdom,$cnum,$type,$configuserok,
 9325:                                                                                       $switchserver,$author_ok);
 9326:                                         if ($imgurl) {
 9327:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 9328:                                             $changes{$type}{$key} = 1; 
 9329:                                         }
 9330:                                         if ($error) {
 9331:                                             &Apache::lonnet::logthis($error);
 9332:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9333:                                         }
 9334:                                     } 
 9335:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 9336:                                     $confhash{$type}{$key}{'image'} = 
 9337:                                         $domconfig{$action}{$type}{$key}{'image'};
 9338:                                 }
 9339:                             }
 9340:                         }
 9341:                     }
 9342:                 }
 9343:             }
 9344:         } else {
 9345:             if ($confhash{'notify'}{'approval'}) {
 9346:                 $changes{'notify'}{'approval'} = 1;
 9347:             }
 9348:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 9349:                 $changes{'uniquecode'} = 1;
 9350:             }
 9351:         }
 9352:         if ($context eq 'requestcourses') {
 9353:             foreach my $type ('textbooks','templates') {
 9354:                 if ($newbook{$type}) {
 9355:                     $changes{$type}{$newbook{$type}} = 1;
 9356:                     foreach my $item ('subject','title','publisher','author') {
 9357:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9358:                                  ($type eq 'template'));
 9359:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 9360:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 9361:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 9362:                         }
 9363:                     }
 9364:                     if ($type eq 'textbooks') {
 9365:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 9366:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 9367:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9368:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9369:                             } else {
 9370:                                 my ($imageurl,$error) =
 9371:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 9372:                                                             $configuserok,$switchserver,$author_ok);
 9373:                                 if ($imageurl) {
 9374:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 9375:                                 }
 9376:                                 if ($error) {
 9377:                                     &Apache::lonnet::logthis($error);
 9378:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9379:                                 }
 9380:                             }
 9381:                         }
 9382:                     }
 9383:                 }
 9384:                 if (@{$allpos{$type}} > 0) {
 9385:                     my $idx = 0;
 9386:                     foreach my $item (@{$allpos{$type}}) {
 9387:                         if ($item ne '') {
 9388:                             $confhash{$type}{$item}{'order'} = $idx;
 9389:                             if (ref($domconfig{$action}) eq 'HASH') {
 9390:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9391:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 9392:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 9393:                                             $changes{$type}{$item} = 1;
 9394:                                         }
 9395:                                     }
 9396:                                 }
 9397:                             }
 9398:                             $idx ++;
 9399:                         }
 9400:                     }
 9401:                 }
 9402:             }
 9403:             if (ref($validationitemsref) eq 'ARRAY') {
 9404:                 foreach my $item (@{$validationitemsref}) {
 9405:                     if ($item eq 'fields') {
 9406:                         my @changed;
 9407:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9408:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9409:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9410:                         }
 9411:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9412:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9413:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9414:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9415:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9416:                                 } else {
 9417:                                     @changed = @{$confhash{'validation'}{$item}};
 9418:                                 }
 9419:                             } else {
 9420:                                 @changed = @{$confhash{'validation'}{$item}};
 9421:                             }
 9422:                         } else {
 9423:                             @changed = @{$confhash{'validation'}{$item}};
 9424:                         }
 9425:                         if (@changed) {
 9426:                             if ($confhash{'validation'}{$item}) {
 9427:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9428:                             } else {
 9429:                                 $changes{'validation'}{$item} = &mt('None');
 9430:                             }
 9431:                         }
 9432:                     } else {
 9433:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9434:                         if ($item eq 'markup') {
 9435:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9436:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9437:                             }
 9438:                         }
 9439:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9440:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9441:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9442:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9443:                                 }
 9444:                             } else {
 9445:                                 if ($confhash{'validation'}{$item} ne '') {
 9446:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9447:                                 }
 9448:                             }
 9449:                         } else {
 9450:                             if ($confhash{'validation'}{$item} ne '') {
 9451:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9452:                             }
 9453:                         }
 9454:                     }
 9455:                 }
 9456:             }
 9457:             if ($env{'form.validationdc'}) {
 9458:                 my $newval = $env{'form.validationdc'};
 9459:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9460:                 if (exists($domcoords{$newval})) {
 9461:                     $confhash{'validation'}{'dc'} = $newval;
 9462:                 }
 9463:             }
 9464:             if (ref($confhash{'validation'}) eq 'HASH') {
 9465:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9466:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9467:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9468:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9469:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9470:                                     $changes{'validation'}{'dc'} = &mt('None');
 9471:                                 } else {
 9472:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9473:                                 }
 9474:                             }
 9475:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9476:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9477:                         }
 9478:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9479:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9480:                     }
 9481:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9482:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9483:                 }
 9484:             } else {
 9485:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9486:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9487:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9488:                             $changes{'validation'}{'dc'} = &mt('None');
 9489:                         }
 9490:                     }
 9491:                 }
 9492:             }
 9493:         }
 9494:     } else {
 9495:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9496:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9497:     }
 9498:     foreach my $item (@usertools) {
 9499:         foreach my $type (@{$types},'default','_LC_adv') {
 9500:             my $unset; 
 9501:             if ($context eq 'requestcourses') {
 9502:                 $unset = '0';
 9503:                 if ($type eq '_LC_adv') {
 9504:                     $unset = '';
 9505:                 }
 9506:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9507:                     $confhash{$item}{$type} .= '=';
 9508:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9509:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9510:                     }
 9511:                 }
 9512:             } elsif ($context eq 'requestauthor') {
 9513:                 $unset = '0';
 9514:                 if ($type eq '_LC_adv') {
 9515:                     $unset = '';
 9516:                 }
 9517:             } else {
 9518:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9519:                     $confhash{$item}{$type} = 1;
 9520:                 } else {
 9521:                     $confhash{$item}{$type} = 0;
 9522:                 }
 9523:             }
 9524:             if (ref($domconfig{$action}) eq 'HASH') {
 9525:                 if ($action eq 'requestauthor') {
 9526:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9527:                         $changes{$type} = 1;
 9528:                     }
 9529:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9530:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9531:                         $changes{$item}{$type} = 1;
 9532:                     }
 9533:                 } else {
 9534:                     if ($context eq 'requestcourses') {
 9535:                         if ($confhash{$item}{$type} ne $unset) {
 9536:                             $changes{$item}{$type} = 1;
 9537:                         }
 9538:                     } else {
 9539:                         if (!$confhash{$item}{$type}) {
 9540:                             $changes{$item}{$type} = 1;
 9541:                         }
 9542:                     }
 9543:                 }
 9544:             } else {
 9545:                 if ($context eq 'requestcourses') {
 9546:                     if ($confhash{$item}{$type} ne $unset) {
 9547:                         $changes{$item}{$type} = 1;
 9548:                     }
 9549:                 } elsif ($context eq 'requestauthor') {
 9550:                     if ($confhash{$type} ne $unset) {
 9551:                         $changes{$type} = 1;
 9552:                     }
 9553:                 } else {
 9554:                     if (!$confhash{$item}{$type}) {
 9555:                         $changes{$item}{$type} = 1;
 9556:                     }
 9557:                 }
 9558:             }
 9559:         }
 9560:     }
 9561:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9562:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9563:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9564:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9565:                     if (exists($confhash{'defaultquota'}{$key})) {
 9566:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9567:                             $changes{'defaultquota'}{$key} = 1;
 9568:                         }
 9569:                     } else {
 9570:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9571:                     }
 9572:                 }
 9573:             } else {
 9574:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9575:                     if (exists($confhash{'defaultquota'}{$key})) {
 9576:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9577:                             $changes{'defaultquota'}{$key} = 1;
 9578:                         }
 9579:                     } else {
 9580:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9581:                     }
 9582:                 }
 9583:             }
 9584:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9585:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9586:                     if (exists($confhash{'authorquota'}{$key})) {
 9587:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9588:                             $changes{'authorquota'}{$key} = 1;
 9589:                         }
 9590:                     } else {
 9591:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9592:                     }
 9593:                 }
 9594:             }
 9595:         }
 9596:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9597:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9598:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9599:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9600:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9601:                             $changes{'defaultquota'}{$key} = 1;
 9602:                         }
 9603:                     } else {
 9604:                         if (!exists($domconfig{'quotas'}{$key})) {
 9605:                             $changes{'defaultquota'}{$key} = 1;
 9606:                         }
 9607:                     }
 9608:                 } else {
 9609:                     $changes{'defaultquota'}{$key} = 1;
 9610:                 }
 9611:             }
 9612:         }
 9613:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9614:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9615:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9616:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9617:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9618:                             $changes{'authorquota'}{$key} = 1;
 9619:                         }
 9620:                     } else {
 9621:                         $changes{'authorquota'}{$key} = 1;
 9622:                     }
 9623:                 } else {
 9624:                     $changes{'authorquota'}{$key} = 1;
 9625:                 }
 9626:             }
 9627:         }
 9628:     }
 9629: 
 9630:     if ($context eq 'requestauthor') {
 9631:         $domdefaults{'requestauthor'} = \%confhash;
 9632:     } else {
 9633:         foreach my $key (keys(%confhash)) {
 9634:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9635:                 $domdefaults{$key} = $confhash{$key};
 9636:             }
 9637:         }
 9638:     }
 9639: 
 9640:     my %quotahash = (
 9641:                       $action => { %confhash }
 9642:                     );
 9643:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9644:                                              $dom);
 9645:     if ($putresult eq 'ok') {
 9646:         if (keys(%changes) > 0) {
 9647:             my $cachetime = 24*60*60;
 9648:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9649:             if (ref($lastactref) eq 'HASH') {
 9650:                 $lastactref->{'domdefaults'} = 1;
 9651:             }
 9652:             $resulttext = &mt('Changes made:').'<ul>';
 9653:             unless (($context eq 'requestcourses') ||
 9654:                     ($context eq 'requestauthor')) {
 9655:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9656:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9657:                     foreach my $type (@{$types},'default') {
 9658:                         if (defined($changes{'defaultquota'}{$type})) {
 9659:                             my $typetitle = $usertypes->{$type};
 9660:                             if ($type eq 'default') {
 9661:                                 $typetitle = $othertitle;
 9662:                             }
 9663:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9664:                         }
 9665:                     }
 9666:                     $resulttext .= '</ul></li>';
 9667:                 }
 9668:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9669:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9670:                     foreach my $type (@{$types},'default') {
 9671:                         if (defined($changes{'authorquota'}{$type})) {
 9672:                             my $typetitle = $usertypes->{$type};
 9673:                             if ($type eq 'default') {
 9674:                                 $typetitle = $othertitle;
 9675:                             }
 9676:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9677:                         }
 9678:                     }
 9679:                     $resulttext .= '</ul></li>';
 9680:                 }
 9681:             }
 9682:             my %newenv;
 9683:             foreach my $item (@usertools) {
 9684:                 my (%haschgs,%inconf);
 9685:                 if ($context eq 'requestauthor') {
 9686:                     %haschgs = %changes;
 9687:                     %inconf = %confhash;
 9688:                 } else {
 9689:                     if (ref($changes{$item}) eq 'HASH') {
 9690:                         %haschgs = %{$changes{$item}};
 9691:                     }
 9692:                     if (ref($confhash{$item}) eq 'HASH') {
 9693:                         %inconf = %{$confhash{$item}};
 9694:                     }
 9695:                 }
 9696:                 if (keys(%haschgs) > 0) {
 9697:                     my $newacc = 
 9698:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9699:                                                           $env{'user.domain'},
 9700:                                                           $item,'reload',$context);
 9701:                     if (($context eq 'requestcourses') ||
 9702:                         ($context eq 'requestauthor')) {
 9703:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9704:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9705:                         }
 9706:                     } else {
 9707:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9708:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9709:                         }
 9710:                     }
 9711:                     unless ($context eq 'requestauthor') {
 9712:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9713:                     }
 9714:                     foreach my $type (@{$types},'default','_LC_adv') {
 9715:                         if ($haschgs{$type}) {
 9716:                             my $typetitle = $usertypes->{$type};
 9717:                             if ($type eq 'default') {
 9718:                                 $typetitle = $othertitle;
 9719:                             } elsif ($type eq '_LC_adv') {
 9720:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9721:                             }
 9722:                             if ($inconf{$type}) {
 9723:                                 if ($context eq 'requestcourses') {
 9724:                                     my $cond;
 9725:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9726:                                         if ($1 eq '') {
 9727:                                             $cond = &mt('(Automatic processing of any request).');
 9728:                                         } else {
 9729:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9730:                                         }
 9731:                                     } else { 
 9732:                                         $cond = $conditions{$inconf{$type}};
 9733:                                     }
 9734:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9735:                                 } elsif ($context eq 'requestauthor') {
 9736:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9737:                                                              $titles{$inconf{$type}},$typetitle);
 9738: 
 9739:                                 } else {
 9740:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9741:                                 }
 9742:                             } else {
 9743:                                 if ($type eq '_LC_adv') {
 9744:                                     if ($inconf{$type} eq '0') {
 9745:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9746:                                     } else { 
 9747:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9748:                                     }
 9749:                                 } else {
 9750:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9751:                                 }
 9752:                             }
 9753:                         }
 9754:                     }
 9755:                     unless ($context eq 'requestauthor') {
 9756:                         $resulttext .= '</ul></li>';
 9757:                     }
 9758:                 }
 9759:             }
 9760:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9761:                 if (ref($changes{'notify'}) eq 'HASH') {
 9762:                     if ($changes{'notify'}{'approval'}) {
 9763:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9764:                             if ($confhash{'notify'}{'approval'}) {
 9765:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9766:                             } else {
 9767:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9768:                             }
 9769:                         }
 9770:                     }
 9771:                 }
 9772:             }
 9773:             if ($action eq 'requestcourses') {
 9774:                 my @offon = ('off','on');
 9775:                 if ($changes{'uniquecode'}) {
 9776:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9777:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9778:                         $resulttext .= '<li>'.
 9779:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9780:                                        '</li>';
 9781:                     } else {
 9782:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9783:                                        '</li>';
 9784:                     }
 9785:                 }
 9786:                 foreach my $type ('textbooks','templates') {
 9787:                     if (ref($changes{$type}) eq 'HASH') {
 9788:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9789:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9790:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9791:                             my $coursetitle = $coursehash{'description'};
 9792:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9793:                             $resulttext .= '<li>';
 9794:                             foreach my $item ('subject','title','publisher','author') {
 9795:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9796:                                          ($type eq 'templates'));
 9797:                                 my $name = $item.':';
 9798:                                 $name =~ s/^(\w)/\U$1/;
 9799:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9800:                             }
 9801:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9802:                             if ($type eq 'textbooks') {
 9803:                                 if ($confhash{$type}{$key}{'image'}) {
 9804:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9805:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9806:                                                    ' alt="Textbook cover" />').'<br />';
 9807:                                 }
 9808:                             }
 9809:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9810:                         }
 9811:                         $resulttext .= '</ul></li>';
 9812:                     }
 9813:                 }
 9814:                 if (ref($changes{'validation'}) eq 'HASH') {
 9815:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9816:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9817:                         foreach my $item (@{$validationitemsref}) {
 9818:                             if (exists($changes{'validation'}{$item})) {
 9819:                                 if ($item eq 'markup') {
 9820:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9821:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9822:                                 } else {
 9823:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9824:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9825:                                 }
 9826:                             }
 9827:                         }
 9828:                         if (exists($changes{'validation'}{'dc'})) {
 9829:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9830:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9831:                         }
 9832:                     }
 9833:                 }
 9834:             }
 9835:             $resulttext .= '</ul>';
 9836:             if (keys(%newenv)) {
 9837:                 &Apache::lonnet::appenv(\%newenv);
 9838:             }
 9839:         } else {
 9840:             if ($context eq 'requestcourses') {
 9841:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9842:             } elsif ($context eq 'requestauthor') {
 9843:                 $resulttext = &mt('No changes made to rights to request author space.');
 9844:             } else {
 9845:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9846:             }
 9847:         }
 9848:     } else {
 9849:         $resulttext = '<span class="LC_error">'.
 9850: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9851:     }
 9852:     if ($errors) {
 9853:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9854:                        '<ul>'.$errors.'</ul></p>';
 9855:     }
 9856:     return $resulttext;
 9857: }
 9858: 
 9859: sub process_textbook_image {
 9860:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9861:     my $filename = $env{'form.'.$caller.'.filename'};
 9862:     my ($error,$url);
 9863:     my ($width,$height) = (50,50);
 9864:     if ($configuserok eq 'ok') {
 9865:         if ($switchserver) {
 9866:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9867:                          $switchserver);
 9868:         } elsif ($author_ok eq 'ok') {
 9869:             my ($result,$imageurl) =
 9870:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9871:                              "$type/$cdom/$cnum/cover",$width,$height);
 9872:             if ($result eq 'ok') {
 9873:                 $url = $imageurl;
 9874:             } else {
 9875:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9876:             }
 9877:         } else {
 9878:             $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);
 9879:         }
 9880:     } else {
 9881:         $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);
 9882:     }
 9883:     return ($url,$error);
 9884: }
 9885: 
 9886: sub modify_autoenroll {
 9887:     my ($dom,$lastactref,%domconfig) = @_;
 9888:     my ($resulttext,%changes);
 9889:     my %currautoenroll;
 9890:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9891:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9892:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9893:         }
 9894:     }
 9895:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9896:     my %title = ( run => 'Auto-enrollment active',
 9897:                   sender => 'Sender for notification messages',
 9898:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9899:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9900:     my @offon = ('off','on');
 9901:     my $sender_uname = $env{'form.sender_uname'};
 9902:     my $sender_domain = $env{'form.sender_domain'};
 9903:     if ($sender_domain eq '') {
 9904:         $sender_uname = '';
 9905:     } elsif ($sender_uname eq '') {
 9906:         $sender_domain = '';
 9907:     }
 9908:     my $coowners = $env{'form.autoassign_coowners'};
 9909:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9910:     $failsafe =~ s{^\s+|\s+$}{}g;
 9911:     if ($failsafe =~ /\D/) {
 9912:         undef($failsafe);
 9913:     }
 9914:     my %autoenrollhash =  (
 9915:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9916:                                        'sender_uname' => $sender_uname,
 9917:                                        'sender_domain' => $sender_domain,
 9918:                                        'co-owners' => $coowners,
 9919:                                        'autofailsafe' => $failsafe,
 9920:                                 }
 9921:                      );
 9922:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9923:                                              $dom);
 9924:     if ($putresult eq 'ok') {
 9925:         if (exists($currautoenroll{'run'})) {
 9926:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9927:                  $changes{'run'} = 1;
 9928:              }
 9929:         } elsif ($autorun) {
 9930:             if ($env{'form.autoenroll_run'} ne '1') {
 9931:                  $changes{'run'} = 1;
 9932:             }
 9933:         }
 9934:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9935:             $changes{'sender'} = 1;
 9936:         }
 9937:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9938:             $changes{'sender'} = 1;
 9939:         }
 9940:         if ($currautoenroll{'co-owners'} ne '') {
 9941:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9942:                 $changes{'coowners'} = 1;
 9943:             }
 9944:         } elsif ($coowners) {
 9945:             $changes{'coowners'} = 1;
 9946:         }
 9947:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9948:             $changes{'autofailsafe'} = 1;
 9949:         }
 9950:         if (keys(%changes) > 0) {
 9951:             $resulttext = &mt('Changes made:').'<ul>';
 9952:             if ($changes{'run'}) {
 9953:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9954:             }
 9955:             if ($changes{'sender'}) {
 9956:                 if ($sender_uname eq '' || $sender_domain eq '') {
 9957:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 9958:                 } else {
 9959:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 9960:                 }
 9961:             }
 9962:             if ($changes{'coowners'}) {
 9963:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 9964:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9965:                 if (ref($lastactref) eq 'HASH') {
 9966:                     $lastactref->{'domainconfig'} = 1;
 9967:                 }
 9968:             }
 9969:             if ($changes{'autofailsafe'}) {
 9970:                 if ($failsafe ne '') {
 9971:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
 9972:                 } else {
 9973:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
 9974:                 }
 9975:                 &Apache::lonnet::get_domain_defaults($dom,1);
 9976:                 if (ref($lastactref) eq 'HASH') {
 9977:                     $lastactref->{'domdefaults'} = 1;
 9978:                 }
 9979:             }
 9980:             $resulttext .= '</ul>';
 9981:         } else {
 9982:             $resulttext = &mt('No changes made to auto-enrollment settings');
 9983:         }
 9984:     } else {
 9985:         $resulttext = '<span class="LC_error">'.
 9986: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9987:     }
 9988:     return $resulttext;
 9989: }
 9990: 
 9991: sub modify_autoupdate {
 9992:     my ($dom,%domconfig) = @_;
 9993:     my ($resulttext,%currautoupdate,%fields,%changes);
 9994:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 9995:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 9996:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 9997:         }
 9998:     }
 9999:     my @offon = ('off','on');
10000:     my %title = &Apache::lonlocal::texthash (
10001:                    run => 'Auto-update:',
10002:                    classlists => 'Updates to user information in classlists?'
10003:                 );
10004:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10005:     my %fieldtitles = &Apache::lonlocal::texthash (
10006:                         id => 'Student/Employee ID',
10007:                         permanentemail => 'E-mail address',
10008:                         lastname => 'Last Name',
10009:                         firstname => 'First Name',
10010:                         middlename => 'Middle Name',
10011:                         generation => 'Generation',
10012:                       );
10013:     $othertitle = &mt('All users');
10014:     if (keys(%{$usertypes}) >  0) {
10015:         $othertitle = &mt('Other users');
10016:     }
10017:     foreach my $key (keys(%env)) {
10018:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10019:             my ($usertype,$item) = ($1,$2);
10020:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10021:                 if ($usertype eq 'default') {   
10022:                     push(@{$fields{$1}},$2);
10023:                 } elsif (ref($types) eq 'ARRAY') {
10024:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10025:                         push(@{$fields{$1}},$2);
10026:                     }
10027:                 }
10028:             }
10029:         }
10030:     }
10031:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10032:     @lockablenames = sort(@lockablenames);
10033:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10034:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10035:         if (@changed) {
10036:             $changes{'lockablenames'} = 1;
10037:         }
10038:     } else {
10039:         if (@lockablenames) {
10040:             $changes{'lockablenames'} = 1;
10041:         }
10042:     }
10043:     my %updatehash = (
10044:                       autoupdate => { run => $env{'form.autoupdate_run'},
10045:                                       classlists => $env{'form.classlists'},
10046:                                       fields => {%fields},
10047:                                       lockablenames => \@lockablenames,
10048:                                     }
10049:                      );
10050:     foreach my $key (keys(%currautoupdate)) {
10051:         if (($key eq 'run') || ($key eq 'classlists')) {
10052:             if (exists($updatehash{autoupdate}{$key})) {
10053:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10054:                     $changes{$key} = 1;
10055:                 }
10056:             }
10057:         } elsif ($key eq 'fields') {
10058:             if (ref($currautoupdate{$key}) eq 'HASH') {
10059:                 foreach my $item (@{$types},'default') {
10060:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10061:                         my $change = 0;
10062:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10063:                             if (!exists($fields{$item})) {
10064:                                 $change = 1;
10065:                                 last;
10066:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10067:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10068:                                     $change = 1;
10069:                                     last;
10070:                                 }
10071:                             }
10072:                         }
10073:                         if ($change) {
10074:                             push(@{$changes{$key}},$item);
10075:                         }
10076:                     } 
10077:                 }
10078:             }
10079:         } elsif ($key eq 'lockablenames') {
10080:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10081:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10082:                 if (@changed) {
10083:                     $changes{'lockablenames'} = 1;
10084:                 }
10085:             } else {
10086:                 if (@lockablenames) {
10087:                     $changes{'lockablenames'} = 1;
10088:                 }
10089:             }
10090:         }
10091:     }
10092:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10093:         if (@lockablenames) {
10094:             $changes{'lockablenames'} = 1;
10095:         }
10096:     }
10097:     foreach my $item (@{$types},'default') {
10098:         if (defined($fields{$item})) {
10099:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10100:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10101:                     my $change = 0;
10102:                     if (ref($fields{$item}) eq 'ARRAY') {
10103:                         foreach my $type (@{$fields{$item}}) {
10104:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10105:                                 $change = 1;
10106:                                 last;
10107:                             }
10108:                         }
10109:                     }
10110:                     if ($change) {
10111:                         push(@{$changes{'fields'}},$item);
10112:                     }
10113:                 } else {
10114:                     push(@{$changes{'fields'}},$item);
10115:                 }
10116:             } else {
10117:                 push(@{$changes{'fields'}},$item);
10118:             }
10119:         }
10120:     }
10121:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10122:                                              $dom);
10123:     if ($putresult eq 'ok') {
10124:         if (keys(%changes) > 0) {
10125:             $resulttext = &mt('Changes made:').'<ul>';
10126:             foreach my $key (sort(keys(%changes))) {
10127:                 if ($key eq 'lockablenames') {
10128:                     $resulttext .= '<li>';
10129:                     if (@lockablenames) {
10130:                         $usertypes->{'default'} = $othertitle;
10131:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10132:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10133:                     } else {
10134:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10135:                     }
10136:                     $resulttext .= '</li>';
10137:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10138:                     foreach my $item (@{$changes{$key}}) {
10139:                         my @newvalues;
10140:                         foreach my $type (@{$fields{$item}}) {
10141:                             push(@newvalues,$fieldtitles{$type});
10142:                         }
10143:                         my $newvaluestr;
10144:                         if (@newvalues > 0) {
10145:                             $newvaluestr = join(', ',@newvalues);
10146:                         } else {
10147:                             $newvaluestr = &mt('none');
10148:                         }
10149:                         if ($item eq 'default') {
10150:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10151:                         } else {
10152:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10153:                         }
10154:                     }
10155:                 } else {
10156:                     my $newvalue;
10157:                     if ($key eq 'run') {
10158:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10159:                     } else {
10160:                         $newvalue = $offon[$env{'form.'.$key}];
10161:                     }
10162:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10163:                 }
10164:             }
10165:             $resulttext .= '</ul>';
10166:         } else {
10167:             $resulttext = &mt('No changes made to autoupdates');
10168:         }
10169:     } else {
10170:         $resulttext = '<span class="LC_error">'.
10171: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10172:     }
10173:     return $resulttext;
10174: }
10175: 
10176: sub modify_autocreate {
10177:     my ($dom,%domconfig) = @_;
10178:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10179:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10180:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10181:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10182:         }
10183:     }
10184:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10185:                  req => 'Auto-creation of validated requests for official courses',
10186:                  xmldc => 'Identity of course creator of courses from XML files',
10187:                );
10188:     my @types = ('xml','req');
10189:     foreach my $item (@types) {
10190:         $newvals{$item} = $env{'form.autocreate_'.$item};
10191:         $newvals{$item} =~ s/\D//g;
10192:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10193:     }
10194:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10195:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10196:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10197:         $newvals{'xmldc'} = '';
10198:     } 
10199:     %autocreatehash =  (
10200:                         autocreate => { xml => $newvals{'xml'},
10201:                                         req => $newvals{'req'},
10202:                                       }
10203:                        );
10204:     if ($newvals{'xmldc'} ne '') {
10205:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10206:     }
10207:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10208:                                              $dom);
10209:     if ($putresult eq 'ok') {
10210:         my @items = @types;
10211:         if ($newvals{'xml'}) {
10212:             push(@items,'xmldc');
10213:         }
10214:         foreach my $item (@items) {
10215:             if (exists($currautocreate{$item})) {
10216:                 if ($currautocreate{$item} ne $newvals{$item}) {
10217:                     $changes{$item} = 1;
10218:                 }
10219:             } elsif ($newvals{$item}) {
10220:                 $changes{$item} = 1;
10221:             }
10222:         }
10223:         if (keys(%changes) > 0) {
10224:             my @offon = ('off','on'); 
10225:             $resulttext = &mt('Changes made:').'<ul>';
10226:             foreach my $item (@types) {
10227:                 if ($changes{$item}) {
10228:                     my $newtxt = $offon[$newvals{$item}];
10229:                     $resulttext .= '<li>'.
10230:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10231:                                        '<b>','</b>').
10232:                                    '</li>';
10233:                 }
10234:             }
10235:             if ($changes{'xmldc'}) {
10236:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10237:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10238:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10239:             }
10240:             $resulttext .= '</ul>';
10241:         } else {
10242:             $resulttext = &mt('No changes made to auto-creation settings');
10243:         }
10244:     } else {
10245:         $resulttext = '<span class="LC_error">'.
10246:             &mt('An error occurred: [_1]',$putresult).'</span>';
10247:     }
10248:     return $resulttext;
10249: }
10250: 
10251: sub modify_directorysrch {
10252:     my ($dom,$lastactref,%domconfig) = @_;
10253:     my ($resulttext,%changes);
10254:     my %currdirsrch;
10255:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10256:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10257:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10258:         }
10259:     }
10260:     my %title = ( available => 'Institutional directory search available',
10261:                   localonly => 'Other domains can search institution',
10262:                   lcavailable => 'LON-CAPA directory search available',
10263:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10264:                   searchby => 'Search types',
10265:                   searchtypes => 'Search latitude');
10266:     my @offon = ('off','on');
10267:     my @otherdoms = ('Yes','No');
10268: 
10269:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10270:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10271:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10272: 
10273:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10274:     if (keys(%{$usertypes}) == 0) {
10275:         @cansearch = ('default');
10276:     } else {
10277:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10278:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10279:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10280:                     push(@{$changes{'cansearch'}},$type);
10281:                 }
10282:             }
10283:             foreach my $type (@cansearch) {
10284:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10285:                     push(@{$changes{'cansearch'}},$type);
10286:                 }
10287:             }
10288:         } else {
10289:             push(@{$changes{'cansearch'}},@cansearch);
10290:         }
10291:     }
10292: 
10293:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10294:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10295:             if (!grep(/^\Q$by\E$/,@searchby)) {
10296:                 push(@{$changes{'searchby'}},$by);
10297:             }
10298:         }
10299:         foreach my $by (@searchby) {
10300:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10301:                 push(@{$changes{'searchby'}},$by);
10302:             }
10303:         }
10304:     } else {
10305:         push(@{$changes{'searchby'}},@searchby);
10306:     }
10307: 
10308:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10309:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10310:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10311:                 push(@{$changes{'searchtypes'}},$type);
10312:             }
10313:         }
10314:         foreach my $type (@searchtypes) {
10315:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10316:                 push(@{$changes{'searchtypes'}},$type);
10317:             }
10318:         }
10319:     } else {
10320:         if (exists($currdirsrch{'searchtypes'})) {
10321:             foreach my $type (@searchtypes) {  
10322:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10323:                     push(@{$changes{'searchtypes'}},$type);
10324:                 }
10325:             }
10326:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10327:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10328:             }   
10329:         } else {
10330:             push(@{$changes{'searchtypes'}},@searchtypes); 
10331:         }
10332:     }
10333: 
10334:     my %dirsrch_hash =  (
10335:             directorysrch => { available => $env{'form.dirsrch_available'},
10336:                                cansearch => \@cansearch,
10337:                                localonly => $env{'form.dirsrch_instlocalonly'},
10338:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10339:                                lcavailable => $env{'form.dirsrch_domavailable'},
10340:                                searchby => \@searchby,
10341:                                searchtypes => \@searchtypes,
10342:                              }
10343:             );
10344:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10345:                                              $dom);
10346:     if ($putresult eq 'ok') {
10347:         if (exists($currdirsrch{'available'})) {
10348:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10349:                  $changes{'available'} = 1;
10350:              }
10351:         } else {
10352:             if ($env{'form.dirsrch_available'} eq '1') {
10353:                 $changes{'available'} = 1;
10354:             }
10355:         }
10356:         if (exists($currdirsrch{'lcavailable'})) {
10357:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10358:                 $changes{'lcavailable'} = 1;
10359:             }
10360:         } else {
10361:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10362:                 $changes{'lcavailable'} = 1;
10363:             }
10364:         }
10365:         if (exists($currdirsrch{'localonly'})) {
10366:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10367:                 $changes{'localonly'} = 1;
10368:             }
10369:         } else {
10370:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10371:                 $changes{'localonly'} = 1;
10372:             }
10373:         }
10374:         if (exists($currdirsrch{'lclocalonly'})) {
10375:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10376:                 $changes{'lclocalonly'} = 1;
10377:             }
10378:         } else {
10379:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10380:                 $changes{'lclocalonly'} = 1;
10381:             }
10382:         }
10383:         if (keys(%changes) > 0) {
10384:             $resulttext = &mt('Changes made:').'<ul>';
10385:             if ($changes{'available'}) {
10386:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10387:             }
10388:             if ($changes{'lcavailable'}) {
10389:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10390:             }
10391:             if ($changes{'localonly'}) {
10392:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10393:             }
10394:             if ($changes{'lclocalonly'}) {
10395:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10396:             }
10397:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10398:                 my $chgtext;
10399:                 if (ref($usertypes) eq 'HASH') {
10400:                     if (keys(%{$usertypes}) > 0) {
10401:                         foreach my $type (@{$types}) {
10402:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10403:                                 $chgtext .= $usertypes->{$type}.'; ';
10404:                             }
10405:                         }
10406:                         if (grep(/^default$/,@cansearch)) {
10407:                             $chgtext .= $othertitle;
10408:                         } else {
10409:                             $chgtext =~ s/\; $//;
10410:                         }
10411:                         $resulttext .=
10412:                             '<li>'.
10413:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10414:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10415:                             '</li>';
10416:                     }
10417:                 }
10418:             }
10419:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10420:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10421:                 my $chgtext;
10422:                 foreach my $type (@{$titleorder}) {
10423:                     if (grep(/^\Q$type\E$/,@searchby)) {
10424:                         if (defined($searchtitles->{$type})) {
10425:                             $chgtext .= $searchtitles->{$type}.'; ';
10426:                         }
10427:                     }
10428:                 }
10429:                 $chgtext =~ s/\; $//;
10430:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10431:             }
10432:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10433:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10434:                 my $chgtext;
10435:                 foreach my $type (@{$srchtypeorder}) {
10436:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10437:                         if (defined($srchtypes_desc->{$type})) {
10438:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10439:                         }
10440:                     }
10441:                 }
10442:                 $chgtext =~ s/\; $//;
10443:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10444:             }
10445:             $resulttext .= '</ul>';
10446:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10447:             if (ref($lastactref) eq 'HASH') {
10448:                 $lastactref->{'directorysrch'} = 1;
10449:             }
10450:         } else {
10451:             $resulttext = &mt('No changes made to directory search settings');
10452:         }
10453:     } else {
10454:         $resulttext = '<span class="LC_error">'.
10455:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10456:     }
10457:     return $resulttext;
10458: }
10459: 
10460: sub modify_contacts {
10461:     my ($dom,$lastactref,%domconfig) = @_;
10462:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10463:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10464:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10465:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10466:         }
10467:     }
10468:     my (%others,%to,%bcc,%includestr,%includeloc);
10469:     my @contacts = ('supportemail','adminemail');
10470:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10471:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
10472:     my @toggles = ('reporterrors','reportupdates');
10473:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10474:     foreach my $type (@mailings) {
10475:         @{$newsetting{$type}} = 
10476:             &Apache::loncommon::get_env_multiple('form.'.$type);
10477:         foreach my $item (@contacts) {
10478:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10479:                 $contacts_hash{contacts}{$type}{$item} = 1;
10480:             } else {
10481:                 $contacts_hash{contacts}{$type}{$item} = 0;
10482:             }
10483:         }
10484:         $others{$type} = $env{'form.'.$type.'_others'};
10485:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10486:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10487:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10488:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10489:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10490:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10491:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10492:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10493:             }
10494:         }
10495:     }
10496:     foreach my $item (@contacts) {
10497:         $to{$item} = $env{'form.'.$item};
10498:         $contacts_hash{'contacts'}{$item} = $to{$item};
10499:     }
10500:     foreach my $item (@toggles) {
10501:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10502:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10503:         }
10504:     }
10505:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10506:         foreach my $field (@{$fields}) {
10507:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10508:                 my $value = $env{'form.helpform_'.$field};
10509:                 $value =~ s/^\s+|\s+$//g;
10510:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10511:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
10512:                     if ($field eq 'screenshot') {
10513:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10514:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10515:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10516:                         }
10517:                     }
10518:                 }
10519:             }
10520:         }
10521:     }
10522:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10523:     my (@statuses,%usertypeshash,@overrides);
10524:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
10525:         @statuses = @{$types};
10526:         if (ref($usertypes) eq 'HASH') {
10527:             %usertypeshash = %{$usertypes};
10528:         }
10529:     }
10530:     if (@statuses) {
10531:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
10532:         foreach my $type (@possoverrides) {
10533:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
10534:                 push(@overrides,$type);
10535:             }
10536:         }
10537:         if (@overrides) {
10538:             foreach my $type (@overrides) {
10539:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
10540:                 foreach my $item (@contacts) {
10541:                     if (grep(/^\Q$item\E$/,@standard)) {
10542:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
10543:                         $newsetting{'override_'.$type}{$item} = 1;
10544:                     } else {
10545:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
10546:                         $newsetting{'override_'.$type}{$item} = 0;
10547:                     }
10548:                 }
10549:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
10550:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10551:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
10552:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10553:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
10554:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
10555:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
10556:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10557:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
10558:                 }
10559:             }    
10560:         }
10561:     }
10562:     if (keys(%currsetting) > 0) {
10563:         foreach my $item (@contacts) {
10564:             if ($to{$item} ne $currsetting{$item}) {
10565:                 $changes{$item} = 1;
10566:             }
10567:         }
10568:         foreach my $type (@mailings) {
10569:             foreach my $item (@contacts) {
10570:                 if (ref($currsetting{$type}) eq 'HASH') {
10571:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10572:                         push(@{$changes{$type}},$item);
10573:                     }
10574:                 } else {
10575:                     push(@{$changes{$type}},@{$newsetting{$type}});
10576:                 }
10577:             }
10578:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10579:                 push(@{$changes{$type}},'others');
10580:             }
10581:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10582:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10583:                     push(@{$changes{$type}},'bcc'); 
10584:                 }
10585:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10586:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10587:                     push(@{$changes{$type}},'include');
10588:                 }
10589:             }
10590:         }
10591:         if (ref($fields) eq 'ARRAY') {
10592:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10593:                 foreach my $field (@{$fields}) {
10594:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10595:                         push(@{$changes{'helpform'}},$field);
10596:                     }
10597:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10598:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10599:                             push(@{$changes{'helpform'}},'maxsize');
10600:                         }
10601:                     }
10602:                 }
10603:             } else {
10604:                 foreach my $field (@{$fields}) {
10605:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10606:                         push(@{$changes{'helpform'}},$field);
10607:                     }
10608:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10609:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10610:                             push(@{$changes{'helpform'}},'maxsize');
10611:                         }
10612:                     }
10613:                 }
10614:             }
10615:         }
10616:         if (@statuses) {
10617:             if (ref($currsetting{'overrides'}) eq 'HASH') {
10618:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
10619:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
10620:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
10621:                             foreach my $item (@contacts,'bcc','others','include') {
10622:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
10623:                                     push(@{$changes{'overrides'}},$key);
10624:                                     last;
10625:                                 }
10626:                             }
10627:                         } else {
10628:                             push(@{$changes{'overrides'}},$key);
10629:                         }
10630:                     }
10631:                 }
10632:                 foreach my $key (@overrides) {
10633:                     unless (exists($currsetting{'overrides'}{$key})) {
10634:                         push(@{$changes{'overrides'}},$key);
10635:                     }
10636:                 }
10637:             } else {
10638:                 foreach my $key (@overrides) {
10639:                     push(@{$changes{'overrides'}},$key);
10640:                 }
10641:             }
10642:         }
10643:     } else {
10644:         my %default;
10645:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10646:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10647:         $default{'errormail'} = 'adminemail';
10648:         $default{'packagesmail'} = 'adminemail';
10649:         $default{'helpdeskmail'} = 'supportemail';
10650:         $default{'otherdomsmail'} = 'supportemail';
10651:         $default{'lonstatusmail'} = 'adminemail';
10652:         $default{'requestsmail'} = 'adminemail';
10653:         $default{'updatesmail'} = 'adminemail';
10654:         $default{'hostipmail'} = 'adminemail';
10655:         foreach my $item (@contacts) {
10656:            if ($to{$item} ne $default{$item}) {
10657:                $changes{$item} = 1;
10658:            }
10659:         }
10660:         foreach my $type (@mailings) {
10661:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10662:                 push(@{$changes{$type}},@{$newsetting{$type}});
10663:             }
10664:             if ($others{$type} ne '') {
10665:                 push(@{$changes{$type}},'others');
10666:             }
10667:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10668:                 if ($bcc{$type} ne '') {
10669:                     push(@{$changes{$type}},'bcc');
10670:                 }
10671:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10672:                     push(@{$changes{$type}},'include');
10673:                 }
10674:             }
10675:         }
10676:         if (ref($fields) eq 'ARRAY') {
10677:             foreach my $field (@{$fields}) {
10678:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10679:                     push(@{$changes{'helpform'}},$field);
10680:                 }
10681:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10682:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10683:                         push(@{$changes{'helpform'}},'maxsize');
10684:                     }
10685:                 }
10686:             }
10687:         }
10688:     }
10689:     foreach my $item (@toggles) {
10690:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10691:             $changes{$item} = 1;
10692:         } elsif ((!$env{'form.'.$item}) &&
10693:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10694:             $changes{$item} = 1;
10695:         }
10696:     }
10697:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10698:                                              $dom);
10699:     if ($putresult eq 'ok') {
10700:         if (keys(%changes) > 0) {
10701:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10702:             if (ref($lastactref) eq 'HASH') {
10703:                 $lastactref->{'domainconfig'} = 1;
10704:             }
10705:             my ($titles,$short_titles)  = &contact_titles();
10706:             $resulttext = &mt('Changes made:').'<ul>';
10707:             foreach my $item (@contacts) {
10708:                 if ($changes{$item}) {
10709:                     $resulttext .= '<li>'.$titles->{$item}.
10710:                                     &mt(' set to: ').
10711:                                     '<span class="LC_cusr_emph">'.
10712:                                     $to{$item}.'</span></li>';
10713:                 }
10714:             }
10715:             foreach my $type (@mailings) {
10716:                 if (ref($changes{$type}) eq 'ARRAY') {
10717:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10718:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10719:                     } else {
10720:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10721:                     }
10722:                     my @text;
10723:                     foreach my $item (@{$newsetting{$type}}) {
10724:                         push(@text,$short_titles->{$item});
10725:                     }
10726:                     if ($others{$type} ne '') {
10727:                         push(@text,$others{$type});
10728:                     }
10729:                     if (@text) {
10730:                         $resulttext .= '<span class="LC_cusr_emph">'.
10731:                                        join(', ',@text).'</span>';
10732:                     }
10733:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10734:                         if ($bcc{$type} ne '') {
10735:                             my $bcctext;
10736:                             if (@text) {
10737:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10738:                             } else {
10739:                                 $bcctext = '(Bcc)';
10740:                             }
10741:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10742:                         } elsif (!@text) {
10743:                             $resulttext .= &mt('No one');
10744:                         }
10745:                         if ($includestr{$type} ne '') {
10746:                             if ($includeloc{$type} eq 'b') {
10747:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10748:                             } elsif ($includeloc{$type} eq 's') {
10749:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10750:                             }
10751:                         }
10752:                     } elsif (!@text) {
10753:                         $resulttext .= &mt('No recipients');
10754:                     }
10755:                     $resulttext .= '</li>';
10756:                 }
10757:             }
10758:             if (ref($changes{'overrides'}) eq 'ARRAY') {
10759:                 my @deletions;
10760:                 foreach my $type (@{$changes{'overrides'}}) {
10761:                     if ($usertypeshash{$type}) {
10762:                         if (grep(/^\Q$type\E/,@overrides)) {
10763:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
10764:                                                       $usertypeshash{$type}).'<ul><li>';
10765:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
10766:                                 my @text;
10767:                                 foreach my $item (@contacts) {
10768:                                     if ($newsetting{'override_'.$type}{$item}) {
10769:                                         push(@text,$short_titles->{$item});
10770:                                     }
10771:                                 }
10772:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
10773:                                     push(@text,$newsetting{'override_'.$type}{'others'});
10774:                                 }
10775: 
10776:                                 if (@text) {
10777:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
10778:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
10779:                                 }
10780:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
10781:                                     my $bcctext;
10782:                                     if (@text) {
10783:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
10784:                                     } else {
10785:                                         $bcctext = '(Bcc)';
10786:                                     }
10787:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
10788:                                 } elsif (!@text) {
10789:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
10790:                                 }
10791:                                 $resulttext .= '</li>';
10792:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
10793:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
10794:                                     if ($loc eq 'b') {
10795:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
10796:                                     } elsif ($loc eq 's') {
10797:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
10798:                                     }
10799:                                 }
10800:                             }
10801:                             $resulttext .= '</li></ul></li>';
10802:                         } else {
10803:                             push(@deletions,$usertypeshash{$type});
10804:                         }
10805:                     }
10806:                 }
10807:                 if (@deletions) {
10808:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
10809:                                               join(', ',@deletions)).'</li>';
10810:                 }
10811:             }
10812:             my @offon = ('off','on');
10813:             if ($changes{'reporterrors'}) {
10814:                 $resulttext .= '<li>'.
10815:                                &mt('E-mail error reports to [_1] set to "'.
10816:                                    $offon[$env{'form.reporterrors'}].'".',
10817:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10818:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10819:                                '</li>';
10820:             }
10821:             if ($changes{'reportupdates'}) {
10822:                 $resulttext .= '<li>'.
10823:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10824:                                     $offon[$env{'form.reportupdates'}].'".',
10825:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10826:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10827:                                 '</li>';
10828:             }
10829:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10830:                 my (@optional,@required,@unused,$maxsizechg);
10831:                 foreach my $field (@{$changes{'helpform'}}) {
10832:                     if ($field eq 'maxsize') {
10833:                         $maxsizechg = 1;
10834:                         next;
10835:                     }
10836:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10837:                         push(@optional,$field);
10838:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10839:                         push(@unused,$field);
10840:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10841:                         push(@required,$field);
10842:                     }
10843:                 }
10844:                 if (@optional) {
10845:                     $resulttext .= '<li>'.
10846:                                    &mt('Help form fields changed to "Optional": [_1].',
10847:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10848:                                    '</li>';
10849:                 }
10850:                 if (@required) {
10851:                     $resulttext .= '<li>'.
10852:                                    &mt('Help form fields changed to "Required": [_1].',
10853:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10854:                                    '</li>';
10855:                 }
10856:                 if (@unused) {
10857:                     $resulttext .= '<li>'.
10858:                                    &mt('Help form fields changed to "Not shown": [_1].',
10859:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10860:                                    '</li>';
10861:                 }
10862:                 if ($maxsizechg) {
10863:                     $resulttext .= '<li>'.
10864:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10865:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10866:                                    '</li>';
10867:                 }
10868:             }
10869:             $resulttext .= '</ul>';
10870:         } else {
10871:             $resulttext = &mt('No changes made to contacts and form settings');
10872:         }
10873:     } else {
10874:         $resulttext = '<span class="LC_error">'.
10875:             &mt('An error occurred: [_1].',$putresult).'</span>';
10876:     }
10877:     return $resulttext;
10878: }
10879: 
10880: sub modify_passwords {
10881:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10882:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
10883:         $updatedefaults,$updateconf);
10884:     my $customfn = 'resetpw.html';
10885:     if (ref($domconfig{'passwords'}) eq 'HASH') {
10886:         %current = %{$domconfig{'passwords'}};
10887:     }
10888:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10889:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10890:     if (ref($types) eq 'ARRAY') {
10891:         @oktypes = @{$types};
10892:     }
10893:     push(@oktypes,'default');
10894: 
10895:     my %titles = &Apache::lonlocal::texthash (
10896:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
10897:         intauth_check  => 'Check bcrypt cost if authenticated',
10898:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
10899:         permanent      => 'Permanent e-mail address',
10900:         critical       => 'Critical notification address',
10901:         notify         => 'Notification address',
10902:         min            => 'Minimum password length',
10903:         max            => 'Maximum password length',
10904:         chars          => 'Required characters',
10905:         numsaved       => 'Number of previous passwords to save',
10906:         reset          => 'Resetting Forgotten Password',
10907:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
10908:         rules          => 'Rules for LON-CAPA Passwords',
10909:         crsownerchg    => 'Course Owner Changing Student Passwords',
10910:         username       => 'Username',
10911:         email          => 'E-mail address',
10912:     );
10913: 
10914: #
10915: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
10916: #
10917:     my (%curr_defaults,%save_defaults);
10918:     if (ref($domconfig{'defaults'}) eq 'HASH') {
10919:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
10920:             if ($key =~ /^intauth_(cost|check|switch)$/) {
10921:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
10922:             } else {
10923:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
10924:             }
10925:         }
10926:     }
10927:     my %staticdefaults = (
10928:         'resetlink'      => 2,
10929:         'resetcase'      => \@oktypes,
10930:         'resetprelink'   => 'both',
10931:         'resetemail'     => ['critical','notify','permanent'],
10932:         'intauth_cost'   => 10,
10933:         'intauth_check'  => 0,
10934:         'intauth_switch' => 0,
10935:     );
10936:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
10937:     foreach my $type (@oktypes) {
10938:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
10939:     }
10940:     my $linklife = $env{'form.passwords_link'};
10941:     $linklife =~ s/^\s+|\s+$//g;
10942:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
10943:         $newvalues{'resetlink'} = $linklife;
10944:         if ($current{'resetlink'}) {
10945:             if ($current{'resetlink'} ne $linklife) {
10946:                 $changes{'reset'} = 1;
10947:             }
10948:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
10949:             if ($staticdefaults{'resetlink'} ne $linklife) {
10950:                 $changes{'reset'} = 1;
10951:             }
10952:         }
10953:     } elsif ($current{'resetlink'}) {
10954:         $changes{'reset'} = 1;
10955:     }
10956:     my @casesens;
10957:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
10958:     foreach my $case (sort(@posscase)) {
10959:         if (grep(/^\Q$case\E$/,@oktypes)) {
10960:             push(@casesens,$case);
10961:         }
10962:     }
10963:     $newvalues{'resetcase'} = \@casesens;
10964:     if (ref($current{'resetcase'}) eq 'ARRAY') {
10965:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
10966:         if (@diffs > 0) {
10967:             $changes{'reset'} = 1;
10968:         }
10969:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
10970:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
10971:         if (@diffs > 0) {
10972:             $changes{'reset'} = 1;
10973:         }
10974:     }
10975:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
10976:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
10977:         if (exists($current{'resetprelink'})) {
10978:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
10979:                 $changes{'reset'} = 1;
10980:             }
10981:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
10982:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
10983:                 $changes{'reset'} = 1;
10984:             }
10985:         }
10986:     } elsif ($current{'resetprelink'}) {
10987:         $changes{'reset'} = 1;
10988:     }
10989:     foreach my $type (@oktypes) {
10990:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
10991:         my @postlink;
10992:         foreach my $item (sort(@possplink)) {
10993:             if ($item =~ /^(email|username)$/) {
10994:                 push(@postlink,$item);
10995:             }
10996:         }
10997:         $newvalues{'resetpostlink'}{$type} = \@postlink;
10998:         unless ($changes{'reset'}) {
10999:             if (ref($current{'resetpostlink'}) eq 'HASH') {
11000:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
11001:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
11002:                     if (@diffs > 0) {
11003:                         $changes{'reset'} = 1;
11004:                     }
11005:                 } else {
11006:                     $changes{'reset'} = 1;
11007:                 }
11008:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
11009:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
11010:                 if (@diffs > 0) {
11011:                     $changes{'reset'} = 1;
11012:                 }
11013:             }
11014:         }
11015:     }
11016:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
11017:     my @resetemail;
11018:     foreach my $item (sort(@possemailsrc)) {
11019:         if ($item =~ /^(permanent|critical|notify)$/) {
11020:             push(@resetemail,$item);
11021:         }
11022:     }
11023:     $newvalues{'resetemail'} = \@resetemail;
11024:     unless ($changes{'reset'}) {
11025:         if (ref($current{'resetemail'}) eq 'ARRAY') {
11026:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
11027:             if (@diffs > 0) {
11028:                 $changes{'reset'} = 1;
11029:             }
11030:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
11031:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
11032:             if (@diffs > 0) {
11033:                 $changes{'reset'} = 1;
11034:             }
11035:         }
11036:     }
11037:     if ($env{'form.passwords_stdtext'} == 0) {
11038:         $newvalues{'resetremove'} = 1;
11039:         unless ($current{'resetremove'}) {
11040:             $changes{'reset'} = 1;
11041:         }
11042:     } elsif ($current{'resetremove'}) {
11043:         $changes{'reset'} = 1;
11044:     }
11045:     if ($env{'form.passwords_customfile.filename'} ne '') {
11046:         my $servadm = $r->dir_config('lonAdmEMail');
11047:         my $servadm = $r->dir_config('lonAdmEMail');
11048:         my ($configuserok,$author_ok,$switchserver) =
11049:             &config_check($dom,$confname,$servadm);
11050:         my $error;
11051:         if ($configuserok eq 'ok') {
11052:             if ($switchserver) {
11053:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
11054:             } else {
11055:                 if ($author_ok eq 'ok') {
11056:                     my ($result,$customurl) =
11057:                         &publishlogo($r,'upload','passwords_customfile',$dom,
11058:                                      $confname,'customtext/resetpw','','',$customfn);
11059:                     if ($result eq 'ok') {
11060:                         $newvalues{'resetcustom'} = $customurl;
11061:                         $changes{'reset'} = 1;
11062:                     } else {
11063:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
11064:                     }
11065:                 } else {
11066:                     $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].",$customfn,$confname,$dom,$author_ok);
11067:                 }
11068:             }
11069:         } else {
11070:             $error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$customfn,$confname,$dom,$configuserok);
11071:         }
11072:         if ($error) {
11073:             &Apache::lonnet::logthis($error);
11074:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11075:         }
11076:     } elsif ($current{'resetcustom'}) {
11077:         if ($env{'form.passwords_custom_del'}) {
11078:             $changes{'reset'} = 1;
11079:         } else {
11080:             $newvalues{'resetcustom'} = $current{'resetcustom'};
11081:         }
11082:     }
11083:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
11084:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
11085:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
11086:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
11087:             $changes{'intauth'} = 1;
11088:         }
11089:     } else {
11090:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
11091:     }
11092:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
11093:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
11094:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
11095:             $changes{'intauth'} = 1;
11096:         }
11097:     } else {
11098:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
11099:     }
11100:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
11101:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
11102:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
11103:             $changes{'intauth'} = 1;
11104:         }
11105:     } else {
11106:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
11107:     }
11108:     foreach my $item ('cost','check','switch') {
11109:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
11110:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
11111:             $updatedefaults = 1;
11112:         }
11113:     }
11114:     foreach my $rule ('min','max','numsaved') {
11115:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
11116:         my $ruleok;
11117:         if ($rule eq 'min') {
11118:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
11119:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
11120:                     $ruleok = 1;
11121:                 }
11122:             }
11123:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
11124:                  ($env{'form.passwords_'.$rule} ne '0')) {
11125:             $ruleok = 1;
11126:         }
11127:         if ($ruleok) {
11128:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
11129:             if (exists($current{$rule})) {
11130:                 if ($newvalues{$rule} ne $current{$rule}) {
11131:                     $changes{'rules'} = 1;
11132:                 }
11133:             } elsif ($rule eq 'min') {
11134:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
11135:                     $changes{'rules'} = 1;
11136:                 }
11137:             } else {
11138:                 $changes{'rules'} = 1;
11139:             }
11140:         } elsif (exists($current{$rule})) {
11141:             $changes{'rules'} = 1;
11142:         }
11143:     }
11144:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
11145:     my @chars;
11146:     foreach my $item (sort(@posschars)) {
11147:         if ($item =~ /^(uc|lc|num|spec)$/) {
11148:             push(@chars,$item);
11149:         }
11150:     }
11151:     $newvalues{'chars'} = \@chars;
11152:     unless ($changes{'rules'}) {
11153:         if (ref($current{'chars'}) eq 'ARRAY') {
11154:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
11155:             if (@diffs > 0) {
11156:                 $changes{'rules'} = 1;
11157:             }
11158:         } else {
11159:             if (@chars > 0) {
11160:                 $changes{'rules'} = 1;
11161:             }
11162:         }
11163:     }
11164:     my %crsownerchg = (
11165:                         by => [],
11166:                         for => [],
11167:                       );
11168:     foreach my $item ('by','for') {
11169:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
11170:         foreach my $type (sort(@posstypes)) {
11171:             if (grep(/^\Q$type\E$/,@oktypes)) {
11172:                 push(@{$crsownerchg{$item}},$type);
11173:             }
11174:         }
11175:     }
11176:     $newvalues{'crsownerchg'} = \%crsownerchg;
11177:     if (ref($current{'crsownerchg'}) eq 'HASH') {
11178:         foreach my $item ('by','for') {
11179:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
11180:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
11181:                 if (@diffs > 0) {
11182:                     $changes{'crsownerchg'} = 1;
11183:                     last;
11184:                 }
11185:             }
11186:         }
11187:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
11188:         foreach my $item ('by','for') {
11189:             if (@{$crsownerchg{$item}} > 0) {
11190:                 $changes{'crsownerchg'} = 1;
11191:                 last;
11192:             }
11193:         }
11194:     }
11195: 
11196:     my %confighash = (
11197:                         defaults  => \%save_defaults,
11198:                         passwords => \%newvalues,
11199:                      );
11200:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
11201: 
11202:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
11203:     if ($putresult eq 'ok') {
11204:         if (keys(%changes) > 0) {
11205:             $resulttext = &mt('Changes made: ').'<ul>';
11206:             foreach my $key ('reset','intauth','rules','crsownerchg') {
11207:                 if ($changes{$key}) {
11208:                     unless ($key eq 'intauth') {
11209:                         $updateconf = 1;
11210:                     }
11211:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
11212:                     if ($key eq 'reset') {
11213:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
11214:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
11215:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
11216:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
11217:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
11218:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
11219:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
11220:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
11221:                             }
11222:                         } else {
11223:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
11224:                         }
11225:                         if ($confighash{'passwords'}{'resetlink'}) {
11226:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
11227:                         } else {
11228:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
11229:                                                   &mt('Will default to 2 hours').'</li>';
11230:                         }
11231:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
11232:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
11233:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
11234:                             } else {
11235:                                 my $casesens;
11236:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
11237:                                     if ($type eq 'default') {
11238:                                         $casesens .= $othertitle.', ';
11239:                                     } elsif ($usertypes->{$type} ne '') {
11240:                                         $casesens .= $usertypes->{$type}.', ';
11241:                                     }
11242:                                 }
11243:                                 $casesens =~ s/\Q, \E$//;
11244:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
11245:                             }
11246:                         } else {
11247:                             $resulttext .= '<li>'.&mt('Case-sensitivity not set for "Forgot Password" web form').' '.&mt('Will default to case-sensitive for username and/or e-mail address for all').'</li>';
11248:                         }
11249:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
11250:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
11251:                         } else {
11252:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
11253:                         }
11254:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
11255:                             my $output;
11256:                             if (ref($types) eq 'ARRAY') {
11257:                                 foreach my $type (@{$types}) {
11258:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
11259:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
11260:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
11261:                                         } else {
11262:                                             $output .= $usertypes->{$type}.' -- '.
11263:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
11264:                                         }
11265:                                     }
11266:                                 }
11267:                             }
11268:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
11269:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
11270:                                     $output .= $othertitle.' -- '.&mt('none');
11271:                                 } else {
11272:                                     $output .= $othertitle.' -- '.
11273:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
11274:                                 }
11275:                             }
11276:                             if ($output) {
11277:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
11278:                             } else {
11279:                                 $resulttext .= '<li>'.&mt('Information required for new password form not set.').' '.&mt('Will default to requiring both the username and an e-mail address').'</li>';
11280:                             }
11281:                         } else {
11282:                             $resulttext .= '<li>'.&mt('Information required for new password form not set.').' '.&mt('Will default to requiring both the username and an e-mail address').'</li>';
11283:                         }
11284:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
11285:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
11286:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
11287:                             } else {
11288:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
11289:                             }
11290:                         } else {
11291:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA usedfor verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
11292:                         }
11293:                         if ($confighash{'passwords'}{'resetremove'}) {
11294:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
11295:                         } else {
11296:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
11297:                         }
11298:                         if ($confighash{'passwords'}{'resetcustom'}) {
11299:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
11300:                                                                             &mt('custom text'),600,500,undef,undef,
11301:                                                                             undef,undef,'background-color:#ffffff');
11302:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
11303:                         } else {
11304:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
11305:                         }
11306:                     } elsif ($key eq 'intauth') {
11307:                         foreach my $item ('cost','switch','check') {
11308:                             my $value = $save_defaults{$key.'_'.$item};
11309:                             if ($item eq 'switch') {
11310:                                 my %optiondesc = &Apache::lonlocal::texthash (
11311:                                                      0 => 'No',
11312:                                                      1 => 'Yes',
11313:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
11314:                                                  );
11315:                                 if ($value =~ /^(0|1|2)$/) {
11316:                                     $value = $optiondesc{$value};
11317:                                 } else {
11318:                                     $value = &mt('none -- defaults to No');
11319:                                 }
11320:                             } elsif ($item eq 'check') {
11321:                                 my %optiondesc = &Apache::lonlocal::texthash (
11322:                                                      0 => 'No',
11323:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
11324:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
11325:                                                  );
11326:                                 if ($value =~ /^(0|1|2)$/) {
11327:                                     $value = $optiondesc{$value};
11328:                                 } else {
11329:                                     $value = &mt('none -- defaults to No');
11330:                                 }
11331:                             }
11332:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
11333:                         }
11334:                     } elsif ($key eq 'rules') {
11335:                         foreach my $rule ('min','max','numsaved') {
11336:                             if ($confighash{'passwords'}{$rule} eq '') {
11337:                                 if ($rule eq 'min') {
11338:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
11339:                                                    ' '.&mt('Default of [_1] will be used',
11340:                                                            $Apache::lonnet::passwdmin).'</li>';
11341:                                 } else {
11342:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
11343:                                 }
11344:                             } else {
11345:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
11346:                             }
11347:                         }
11348:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
11349:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
11350:                                 my %rulenames = &Apache::lonlocal::texthash(
11351:                                                      uc => 'At least one upper case letter',
11352:                                                      lc => 'At least one lower case letter',
11353:                                                      num => 'At least one number',
11354:                                                      spec => 'At least one non-alphanumeric',
11355:                                                    );
11356:                                 my $needed = '<ul><li>'.
11357:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
11358:                                              '</li></ul>';
11359:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
11360:                             } else {
11361:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
11362:                             }
11363:                         } else {
11364:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
11365:                         }
11366:                     } elsif ($key eq 'crsownerchg') {
11367:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
11368:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
11369:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
11370:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
11371:                             } else {
11372:                                 my %crsownerstr;
11373:                                 foreach my $item ('by','for') {
11374:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
11375:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
11376:                                             if ($type eq 'default') {
11377:                                                 $crsownerstr{$item} .= $othertitle.', ';
11378:                                             } elsif ($usertypes->{$type} ne '') {
11379:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
11380:                                             }
11381:                                         }
11382:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
11383:                                     }
11384:                                 }
11385:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
11386:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
11387:                             }
11388:                         } else {
11389:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
11390:                         }
11391:                     }
11392:                     $resulttext .= '</ul></li>';
11393:                 }
11394:             }
11395:             $resulttext .= '</ul>';
11396:         } else {
11397:             $resulttext = &mt('No changes made to password settings');
11398:         }
11399:         my $cachetime = 24*60*60;
11400:         if ($updatedefaults) {
11401:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11402:             if (ref($lastactref) eq 'HASH') {
11403:                 $lastactref->{'domdefaults'} = 1;
11404:             }
11405:         }
11406:         if ($updateconf) {
11407:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
11408:             if (ref($lastactref) eq 'HASH') {
11409:                 $lastactref->{'passwdconf'} = 1;
11410:             }
11411:         }
11412:     } else {
11413:         $resulttext = '<span class="LC_error">'.
11414:             &mt('An error occurred: [_1]',$putresult).'</span>';
11415:     }
11416:     if ($errors) {
11417:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
11418:                        $errors.'</ul></p>';
11419:     }
11420:     return $resulttext;
11421: }
11422: 
11423: sub modify_usercreation {
11424:     my ($dom,%domconfig) = @_;
11425:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
11426:     my $warningmsg;
11427:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11428:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11429:             if ($key eq 'cancreate') {
11430:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11431:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11432:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
11433:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11434:                         } else {
11435:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11436:                         }
11437:                     }
11438:                 }
11439:             } elsif ($key eq 'email_rule') {
11440:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11441:             } else {
11442:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11443:             }
11444:         }
11445:     }
11446:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
11447:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
11448:     my @contexts = ('author','course','requestcrs');
11449:     foreach my $item(@contexts) {
11450:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
11451:     }
11452:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11453:         foreach my $item (@contexts) {
11454:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
11455:                 push(@{$changes{'cancreate'}},$item);
11456:             }
11457:         }
11458:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
11459:         foreach my $item (@contexts) {
11460:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
11461:                 if ($cancreate{$item} ne 'any') {
11462:                     push(@{$changes{'cancreate'}},$item);
11463:                 }
11464:             } else {
11465:                 if ($cancreate{$item} ne 'none') {
11466:                     push(@{$changes{'cancreate'}},$item);
11467:                 }
11468:             }
11469:         }
11470:     } else {
11471:         foreach my $item (@contexts)  {
11472:             push(@{$changes{'cancreate'}},$item);
11473:         }
11474:     }
11475: 
11476:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
11477:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
11478:             if (!grep(/^\Q$type\E$/,@username_rule)) {
11479:                 push(@{$changes{'username_rule'}},$type);
11480:             }
11481:         }
11482:         foreach my $type (@username_rule) {
11483:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
11484:                 push(@{$changes{'username_rule'}},$type);
11485:             }
11486:         }
11487:     } else {
11488:         push(@{$changes{'username_rule'}},@username_rule);
11489:     }
11490: 
11491:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
11492:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
11493:             if (!grep(/^\Q$type\E$/,@id_rule)) {
11494:                 push(@{$changes{'id_rule'}},$type);
11495:             }
11496:         }
11497:         foreach my $type (@id_rule) {
11498:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11499:                 push(@{$changes{'id_rule'}},$type);
11500:             }
11501:         }
11502:     } else {
11503:         push(@{$changes{'id_rule'}},@id_rule);
11504:     }
11505: 
11506:     my @authen_contexts = ('author','course','domain');
11507:     my @authtypes = ('int','krb4','krb5','loc');
11508:     my %authhash;
11509:     foreach my $item (@authen_contexts) {
11510:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11511:         foreach my $auth (@authtypes) {
11512:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11513:                 $authhash{$item}{$auth} = 1;
11514:             } else {
11515:                 $authhash{$item}{$auth} = 0;
11516:             }
11517:         }
11518:     }
11519:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11520:         foreach my $item (@authen_contexts) {
11521:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11522:                 foreach my $auth (@authtypes) {
11523:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11524:                         push(@{$changes{'authtypes'}},$item);
11525:                         last;
11526:                     }
11527:                 }
11528:             }
11529:         }
11530:     } else {
11531:         foreach my $item (@authen_contexts) {
11532:             push(@{$changes{'authtypes'}},$item);
11533:         }
11534:     }
11535: 
11536:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11537:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11538:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11539:     $save_usercreate{'id_rule'} = \@id_rule;
11540:     $save_usercreate{'username_rule'} = \@username_rule,
11541:     $save_usercreate{'authtypes'} = \%authhash;
11542: 
11543:     my %usercreation_hash =  (
11544:         usercreation     => \%save_usercreate,
11545:     );
11546: 
11547:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11548:                                              $dom);
11549: 
11550:     if ($putresult eq 'ok') {
11551:         if (keys(%changes) > 0) {
11552:             $resulttext = &mt('Changes made:').'<ul>';
11553:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11554:                 my %lt = &usercreation_types();
11555:                 foreach my $type (@{$changes{'cancreate'}}) {
11556:                     my $chgtext = $lt{$type}.', ';
11557:                     if ($cancreate{$type} eq 'none') {
11558:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11559:                     } elsif ($cancreate{$type} eq 'any') {
11560:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11561:                     } elsif ($cancreate{$type} eq 'official') {
11562:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11563:                     } elsif ($cancreate{$type} eq 'unofficial') {
11564:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11565:                     }
11566:                     $resulttext .= '<li>'.$chgtext.'</li>';
11567:                 }
11568:             }
11569:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11570:                 my ($rules,$ruleorder) = 
11571:                     &Apache::lonnet::inst_userrules($dom,'username');
11572:                 my $chgtext = '<ul>';
11573:                 foreach my $type (@username_rule) {
11574:                     if (ref($rules->{$type}) eq 'HASH') {
11575:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11576:                     }
11577:                 }
11578:                 $chgtext .= '</ul>';
11579:                 if (@username_rule > 0) {
11580:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11581:                 } else {
11582:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11583:                 }
11584:             }
11585:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11586:                 my ($idrules,$idruleorder) = 
11587:                     &Apache::lonnet::inst_userrules($dom,'id');
11588:                 my $chgtext = '<ul>';
11589:                 foreach my $type (@id_rule) {
11590:                     if (ref($idrules->{$type}) eq 'HASH') {
11591:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11592:                     }
11593:                 }
11594:                 $chgtext .= '</ul>';
11595:                 if (@id_rule > 0) {
11596:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11597:                 } else {
11598:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11599:                 }
11600:             }
11601:             my %authname = &authtype_names();
11602:             my %context_title = &context_names();
11603:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11604:                 my $chgtext = '<ul>';
11605:                 foreach my $type (@{$changes{'authtypes'}}) {
11606:                     my @allowed;
11607:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11608:                     foreach my $auth (@authtypes) {
11609:                         if ($authhash{$type}{$auth}) {
11610:                             push(@allowed,$authname{$auth});
11611:                         }
11612:                     }
11613:                     if (@allowed > 0) {
11614:                         $chgtext .= join(', ',@allowed).'</li>';
11615:                     } else {
11616:                         $chgtext .= &mt('none').'</li>';
11617:                     }
11618:                 }
11619:                 $chgtext .= '</ul>';
11620:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11621:                 $resulttext .= '</li>';
11622:             }
11623:             $resulttext .= '</ul>';
11624:         } else {
11625:             $resulttext = &mt('No changes made to user creation settings');
11626:         }
11627:     } else {
11628:         $resulttext = '<span class="LC_error">'.
11629:             &mt('An error occurred: [_1]',$putresult).'</span>';
11630:     }
11631:     if ($warningmsg ne '') {
11632:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11633:     }
11634:     return $resulttext;
11635: }
11636: 
11637: sub modify_selfcreation {
11638:     my ($dom,$lastactref,%domconfig) = @_;
11639:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
11640:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
11641:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11642:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
11643:     if (ref($typesref) eq 'ARRAY') {
11644:         @types = @{$typesref};
11645:     }
11646:     if (ref($usertypesref) eq 'HASH') {
11647:         %usertypes = %{$usertypesref};
11648:     }
11649:     $usertypes{'default'} = $othertitle;
11650: #
11651: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11652: #
11653:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11654:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11655:             if ($key eq 'cancreate') {
11656:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11657:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11658:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11659:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
11660:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
11661:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
11662:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
11663:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
11664:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11665:                         } else {
11666:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11667:                         }
11668:                     }
11669:                 }
11670:             } elsif ($key eq 'email_rule') {
11671:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11672:             } else {
11673:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11674:             }
11675:         }
11676:     }
11677: #
11678: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11679: #
11680:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11681:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11682:             if ($key eq 'selfcreate') {
11683:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11684:             } else {
11685:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11686:             }
11687:         }
11688:     }
11689: #
11690: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
11691: #
11692:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11693:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
11694:             if ($key eq 'inststatusguest') {
11695:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
11696:             } else {
11697:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
11698:             }
11699:         }
11700:     }
11701: 
11702:     my @contexts = ('selfcreate');
11703:     @{$cancreate{'selfcreate'}} = ();
11704:     %{$cancreate{'emailusername'}} = ();
11705:     if (@types) {
11706:         @{$cancreate{'statustocreate'}} = ();
11707:     }
11708:     %{$cancreate{'selfcreateprocessing'}} = ();
11709:     %{$cancreate{'shibenv'}} = ();
11710:     %{$cancreate{'emailverified'}} = ();
11711:     %{$cancreate{'emailoptions'}} = ();
11712:     %{$cancreate{'emaildomain'}} = ();
11713:     my %selfcreatetypes = (
11714:                              sso   => 'users authenticated by institutional single sign on',
11715:                              login => 'users authenticated by institutional log-in',
11716:                              email => 'users verified by e-mail',
11717:                           );
11718: #
11719: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11720: # is permitted.
11721: #
11722: 
11723:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
11724: 
11725:     my (@statuses,%email_rule);
11726:     foreach my $item ('login','sso','email') {
11727:         if ($item eq 'email') {
11728:             if ($env{'form.cancreate_email'}) {
11729:                 if (@types) {
11730:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
11731:                     foreach my $status (@poss_statuses) {
11732:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
11733:                             push(@statuses,$status);
11734:                         }
11735:                     }
11736:                     $save_inststatus{'inststatusguest'} = \@statuses;
11737:                 } else {
11738:                     push(@statuses,'default');
11739:                 }
11740:                 if (@statuses) {
11741:                     my %curr_rule;
11742:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11743:                         foreach my $type (@statuses) {
11744:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
11745:                         }
11746:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
11747:                         foreach my $type (@statuses) {
11748:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
11749:                         }
11750:                     }
11751:                     push(@{$cancreate{'selfcreate'}},'email');
11752:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
11753:                     my %curremaildom;
11754:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
11755:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
11756:                     }
11757:                     foreach my $type (@statuses) {
11758:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
11759:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11760:                         }
11761:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
11762:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
11763:                         }
11764:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
11765: #
11766: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
11767: #
11768:                             my $chosen = $1;
11769:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
11770:                                 my $emaildom;
11771:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
11772:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
11773:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
11774:                                     if (ref($curremaildom{$type}) eq 'HASH') {
11775:                                         if (exists($curremaildom{$type}{$chosen})) {
11776:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
11777:                                                 push(@{$changes{'cancreate'}},'emaildomain');
11778:                                             }
11779:                                         } elsif ($emaildom ne '') {
11780:                                             push(@{$changes{'cancreate'}},'emaildomain');
11781:                                         }
11782:                                     } elsif ($emaildom ne '') {
11783:                                         push(@{$changes{'cancreate'}},'emaildomain');
11784:                                     }
11785:                                 }
11786:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11787:                             } elsif ($chosen eq 'custom') {
11788:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
11789:                                 $email_rule{$type} = [];
11790:                                 if (ref($emailrules) eq 'HASH') {
11791:                                     foreach my $rule (@possemail_rules) {
11792:                                         if (exists($emailrules->{$rule})) {
11793:                                             push(@{$email_rule{$type}},$rule);
11794:                                         }
11795:                                     }
11796:                                 }
11797:                                 if (@{$email_rule{$type}}) {
11798:                                     $cancreate{'emailoptions'}{$type} = 'custom';
11799:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
11800:                                         if (@{$curr_rule{$type}} > 0) {
11801:                                             foreach my $rule (@{$curr_rule{$type}}) {
11802:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
11803:                                                     push(@{$changes{'email_rule'}},$type);
11804:                                                 }
11805:                                             }
11806:                                         }
11807:                                         foreach my $type (@{$email_rule{$type}}) {
11808:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
11809:                                                 push(@{$changes{'email_rule'}},$type);
11810:                                             }
11811:                                         }
11812:                                     } else {
11813:                                         push(@{$changes{'email_rule'}},$type);
11814:                                     }
11815:                                 }
11816:                             } else {
11817:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11818:                             }
11819:                         }
11820:                     }
11821:                     if (@types) {
11822:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11823:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
11824:                             if (@changed) {
11825:                                 push(@{$changes{'inststatus'}},'inststatusguest');
11826:                             }
11827:                         } else {
11828:                             push(@{$changes{'inststatus'}},'inststatusguest');
11829:                         }
11830:                     }
11831:                 } else {
11832:                     delete($env{'form.cancreate_email'});
11833:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11834:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11835:                             push(@{$changes{'inststatus'}},'inststatusguest');
11836:                         }
11837:                     }
11838:                 }
11839:             } else {
11840:                 $save_inststatus{'inststatusguest'} = [];
11841:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11842:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11843:                         push(@{$changes{'inststatus'}},'inststatusguest');
11844:                     }
11845:                 }
11846:             }
11847:         } else {
11848:             if ($env{'form.cancreate_'.$item}) {
11849:                 push(@{$cancreate{'selfcreate'}},$item);
11850:             }
11851:         }
11852:     }
11853:     my (%userinfo,%savecaptcha);
11854:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11855: #
11856: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11857: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11858: #
11859: 
11860:     if ($env{'form.cancreate_email'}) {
11861:         push(@contexts,'emailusername');
11862:         if (@statuses) {
11863:             foreach my $type (@statuses) {
11864:                 if (ref($infofields) eq 'ARRAY') {
11865:                     foreach my $field (@{$infofields}) {
11866:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11867:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11868:                         }
11869:                     }
11870:                 }
11871:             }
11872:         }
11873: #
11874: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11875: # queued requests for self-creation of account verified by e-mail.
11876: #
11877: 
11878:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11879:         @approvalnotify = sort(@approvalnotify);
11880:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11881:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11882:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11883:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11884:                     push(@{$changes{'cancreate'}},'notify');
11885:                 }
11886:             } else {
11887:                 if ($cancreate{'notify'}{'approval'}) {
11888:                     push(@{$changes{'cancreate'}},'notify');
11889:                 }
11890:             }
11891:         } elsif ($cancreate{'notify'}{'approval'}) {
11892:             push(@{$changes{'cancreate'}},'notify');
11893:         }
11894: 
11895:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11896:     }
11897: #  
11898: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11899: # institutional log-in.
11900: #
11901:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11902:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11903:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11904:             $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.').' '.
11905:                           &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.');
11906:         }
11907:     }
11908:     my @fields = ('lastname','firstname','middlename','generation',
11909:                   'permanentemail','id');
11910:     my @shibfields = (@fields,'inststatus');
11911:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11912: #
11913: # Where usernames may created for institutional log-in and/or institutional single sign on:
11914: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11915: # may self-create accounts 
11916: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11917: # which the user may supply, if institutional data is unavailable.
11918: #
11919:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11920:         if (@types) {
11921:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11922:             push(@contexts,'statustocreate');
11923:             foreach my $type (@types) {
11924:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11925:                 foreach my $field (@fields) {
11926:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11927:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11928:                     } else {
11929:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11930:                     }
11931:                 }
11932:             }
11933:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11934:                 foreach my $type (@types) {
11935:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11936:                         foreach my $field (@fields) {
11937:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11938:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11939:                                 push(@{$changes{'selfcreate'}},$type);
11940:                                 last;
11941:                             }
11942:                         }
11943:                     }
11944:                 }
11945:             } else {
11946:                 foreach my $type (@types) {
11947:                     push(@{$changes{'selfcreate'}},$type);
11948:                 }
11949:             }
11950:         }
11951:         foreach my $field (@shibfields) {
11952:             if ($env{'form.shibenv_'.$field} ne '') {
11953:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11954:             }
11955:         }
11956:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11957:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11958:                 foreach my $field (@shibfields) {
11959:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11960:                         push(@{$changes{'cancreate'}},'shibenv');
11961:                     }
11962:                 }
11963:             } else {
11964:                 foreach my $field (@shibfields) {
11965:                     if ($env{'form.shibenv_'.$field}) {
11966:                         push(@{$changes{'cancreate'}},'shibenv');
11967:                         last;
11968:                     }
11969:                 }
11970:             }
11971:         }
11972:     }
11973:     foreach my $item (@contexts) {
11974:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11975:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11976:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11977:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11978:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11979:                             push(@{$changes{'cancreate'}},$item);
11980:                         }
11981:                     }
11982:                 }
11983:             }
11984:             if (ref($cancreate{$item}) eq 'ARRAY') {
11985:                 foreach my $type (@{$cancreate{$item}}) {
11986:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11987:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11988:                             push(@{$changes{'cancreate'}},$item);
11989:                         }
11990:                     }
11991:                 }
11992:             }
11993:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11994:             if (ref($cancreate{$item}) eq 'HASH') {
11995:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11996:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
11997:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
11998:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
11999:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12000:                                     push(@{$changes{'cancreate'}},$item);
12001:                                 }
12002:                             }
12003:                         }
12004:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
12005:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
12006:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12007:                                 push(@{$changes{'cancreate'}},$item);
12008:                             }
12009:                         }
12010:                     }
12011:                 }
12012:                 foreach my $type (keys(%{$cancreate{$item}})) {
12013:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
12014:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
12015:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
12016:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
12017:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12018:                                         push(@{$changes{'cancreate'}},$item);
12019:                                     }
12020:                                 }
12021:                             } else {
12022:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12023:                                     push(@{$changes{'cancreate'}},$item);
12024:                                 }
12025:                             }
12026:                         }
12027:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
12028:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
12029:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12030:                                 push(@{$changes{'cancreate'}},$item);
12031:                             }
12032:                         }
12033:                     }
12034:                 }
12035:             }
12036:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
12037:             if (ref($cancreate{$item}) eq 'ARRAY') {
12038:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
12039:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12040:                         push(@{$changes{'cancreate'}},$item);
12041:                     }
12042:                 }
12043:             }
12044:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
12045:             if (ref($cancreate{$item}) eq 'HASH') {
12046:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12047:                     push(@{$changes{'cancreate'}},$item);
12048:                 }
12049:             }
12050:         } elsif ($item eq 'emailusername') {
12051:             if (ref($cancreate{$item}) eq 'HASH') {
12052:                 foreach my $type (keys(%{$cancreate{$item}})) {
12053:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
12054:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
12055:                             if ($cancreate{$item}{$type}{$field}) {
12056:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12057:                                     push(@{$changes{'cancreate'}},$item);
12058:                                 }
12059:                                 last;
12060:                             }
12061:                         }
12062:                     }
12063:                 }
12064:             }
12065:         }
12066:     }
12067: #
12068: # Populate %save_usercreate hash with updates to self-creation configuration.
12069: #
12070:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
12071:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
12072:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
12073:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
12074:     if (ref($cancreate{'notify'}) eq 'HASH') {
12075:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
12076:     }
12077:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
12078:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
12079:     }
12080:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
12081:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
12082:     }
12083:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
12084:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
12085:     }
12086:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
12087:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
12088:     }
12089:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
12090:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
12091:     }
12092:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
12093:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
12094:     }
12095:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
12096:     $save_usercreate{'email_rule'} = \%email_rule;
12097: 
12098:     my %userconfig_hash = (
12099:             usercreation     => \%save_usercreate,
12100:             usermodification => \%save_usermodify,
12101:             inststatus       => \%save_inststatus,
12102:     );
12103: 
12104:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
12105:                                              $dom);
12106: #
12107: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
12108: #
12109:     if ($putresult eq 'ok') {
12110:         if (keys(%changes) > 0) {
12111:             $resulttext = &mt('Changes made:').'<ul>';
12112:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
12113:                 my %lt = &selfcreation_types();
12114:                 foreach my $type (@{$changes{'cancreate'}}) {
12115:                     my $chgtext = '';
12116:                     if ($type eq 'selfcreate') {
12117:                         if (@{$cancreate{$type}} == 0) {
12118:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
12119:                         } else {
12120:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
12121:                                         '<ul>';
12122:                             foreach my $case (@{$cancreate{$type}}) {
12123:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
12124:                             }
12125:                             $chgtext .= '</ul>';
12126:                             if (ref($cancreate{$type}) eq 'ARRAY') {
12127:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
12128:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
12129:                                         if (@{$cancreate{'statustocreate'}} == 0) {
12130:                                             $chgtext .= '<span class="LC_warning">'.
12131:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
12132:                                                         '</span><br />';
12133:                                         }
12134:                                     }
12135:                                 }
12136:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
12137:                                     if (!@statuses) {
12138:                                         $chgtext .= '<span class="LC_warning">'.
12139:                                                     &mt("However, e-mail verification is currently set to 'unavailable' for all user types (including 'other'), so self-creation of accounts is not possible for non-institutional log-in.").
12140:                                                     '</span><br />';
12141: 
12142:                                     }
12143:                                 }
12144:                             }
12145:                         }
12146:                     } elsif ($type eq 'shibenv') {
12147:                         if (keys(%{$cancreate{$type}}) == 0) {
12148:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
12149:                         } else {
12150:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
12151:                                         '<ul>';
12152:                             foreach my $field (@shibfields) {
12153:                                 next if ($cancreate{$type}{$field} eq '');
12154:                                 if ($field eq 'inststatus') {
12155:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
12156:                                 } else {
12157:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
12158:                                 }
12159:                             }
12160:                             $chgtext .= '</ul>';
12161:                         }
12162:                     } elsif ($type eq 'statustocreate') {
12163:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
12164:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
12165:                             if (@{$cancreate{'selfcreate'}} > 0) {
12166:                                 if (@{$cancreate{'statustocreate'}} == 0) {
12167:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
12168:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
12169:                                         $chgtext .= '<br />'.
12170:                                                     '<span class="LC_warning">'.
12171:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
12172:                                                     '</span>';
12173:                                     }
12174:                                 } elsif (keys(%usertypes) > 0) {
12175:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12176:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
12177:                                     } else {
12178:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
12179:                                     }
12180:                                     $chgtext .= '<ul>';
12181:                                     foreach my $case (@{$cancreate{$type}}) {
12182:                                         if ($case eq 'default') {
12183:                                             $chgtext .= '<li>'.$othertitle.'</li>';
12184:                                         } else {
12185:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
12186:                                         }
12187:                                     }
12188:                                     $chgtext .= '</ul>';
12189:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12190:                                         $chgtext .= '<span class="LC_warning">'.
12191:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
12192:                                                     '</span>';
12193:                                     }
12194:                                 }
12195:                             } else {
12196:                                 if (@{$cancreate{$type}} == 0) {
12197:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
12198:                                 } else {
12199:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
12200:                                 }
12201:                             }
12202:                             $chgtext .= '<br />';
12203:                         }
12204:                     } elsif ($type eq 'selfcreateprocessing') {
12205:                         my %choices = &Apache::lonlocal::texthash (
12206:                                                                     automatic => 'Automatic approval',
12207:                                                                     approval  => 'Queued for approval',
12208:                                                                   );
12209:                         if (@types) {
12210:                             if (@statuses) {
12211:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
12212:                                             '<ul>';
12213:                                 foreach my $status (@statuses) {
12214:                                     if ($status eq 'default') {
12215:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12216:                                     } else {
12217:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12218:                                     }
12219:                                 }
12220:                                 $chgtext .= '</ul>';
12221:                             }
12222:                         } else {
12223:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
12224:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
12225:                         }
12226:                     } elsif ($type eq 'emailverified') {
12227:                         my %options = &Apache::lonlocal::texthash (
12228:                                                                     all   => 'Same as e-mail',
12229:                                                                     first => 'Omit @domain',
12230:                                                                     free  => 'Free to choose',
12231:                                                                   );
12232:                         if (@types) {
12233:                             if (@statuses) {
12234:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
12235:                                             '<ul>';
12236:                                 foreach my $status (@statuses) {
12237:                                     if ($status eq 'default') {
12238:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12239:                                     } else {
12240:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12241:                                     }
12242:                                 }
12243:                                 $chgtext .= '</ul>';
12244:                             }
12245:                         } else {
12246:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
12247:                                             $options{$cancreate{'emailverified'}{'default'}});
12248:                         }
12249:                     } elsif ($type eq 'emailoptions') {
12250:                         my %options = &Apache::lonlocal::texthash (
12251:                                                                     any     => 'Any e-mail',
12252:                                                                     inst    => 'Institutional only',
12253:                                                                     noninst => 'Non-institutional only',
12254:                                                                     custom  => 'Custom restrictions',
12255:                                                                   );
12256:                         if (@types) {
12257:                             if (@statuses) {
12258:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
12259:                                             '<ul>';
12260:                                 foreach my $status (@statuses) {
12261:                                     if ($type eq 'default') {
12262:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12263:                                     } else {
12264:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12265:                                     }
12266:                                 }
12267:                                 $chgtext .= '</ul>';
12268:                             }
12269:                         } else {
12270:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
12271:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
12272:                             } else {
12273:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
12274:                                                 $options{$cancreate{'emailoptions'}{'default'}});
12275:                             }
12276:                         }
12277:                     } elsif ($type eq 'emaildomain') {
12278:                         my $output;
12279:                         if (@statuses) {
12280:                             foreach my $type (@statuses) {
12281:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
12282:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
12283:                                         if ($type eq 'default') {
12284:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12285:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12286:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12287:                                             } else {
12288:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
12289:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
12290:                                             }
12291:                                         } else {
12292:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12293:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12294:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12295:                                             } else {
12296:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
12297:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
12298:                                             }
12299:                                         }
12300:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
12301:                                         if ($type eq 'default') {
12302:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12303:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12304:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12305:                                             } else {
12306:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
12307:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
12308:                                             }
12309:                                         } else {
12310:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12311:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12312:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12313:                                             } else {
12314:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
12315:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
12316:                                             }
12317:                                         }
12318:                                     }
12319:                                 }
12320:                             }
12321:                         }
12322:                         if ($output ne '') {
12323:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
12324:                                         '<ul>'.$output.'</ul>';
12325:                         }
12326:                     } elsif ($type eq 'captcha') {
12327:                         if ($savecaptcha{$type} eq 'notused') {
12328:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
12329:                         } else {
12330:                             my %captchas = &captcha_phrases();
12331:                             if ($captchas{$savecaptcha{$type}}) {
12332:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
12333:                             } else {
12334:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
12335:                             }
12336:                         }
12337:                     } elsif ($type eq 'recaptchakeys') {
12338:                         my ($privkey,$pubkey);
12339:                         if (ref($savecaptcha{$type}) eq 'HASH') {
12340:                             $pubkey = $savecaptcha{$type}{'public'};
12341:                             $privkey = $savecaptcha{$type}{'private'};
12342:                         }
12343:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
12344:                         if (!$pubkey) {
12345:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
12346:                         } else {
12347:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12348:                         }
12349:                         if (!$privkey) {
12350:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
12351:                         } else {
12352:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
12353:                         }
12354:                         $chgtext .= '</ul>';
12355:                     } elsif ($type eq 'recaptchaversion') {
12356:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
12357:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
12358:                         }
12359:                     } elsif ($type eq 'emailusername') {
12360:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
12361:                             if (@statuses) {
12362:                                 foreach my $type (@statuses) {
12363:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
12364:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
12365:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
12366:                                                     '<ul>';
12367:                                             foreach my $field (@{$infofields}) {
12368:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
12369:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
12370:                                                 }
12371:                                             }
12372:                                             $chgtext .= '</ul>';
12373:                                         } else {
12374:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12375:                                         }
12376:                                     } else {
12377:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12378:                                     }
12379:                                 }
12380:                             }
12381:                         }
12382:                     } elsif ($type eq 'notify') {
12383:                         my $numapprove = 0;
12384:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
12385:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
12386:                                 if ($cancreate{'notify'}{'approval'}) {
12387:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
12388:                                     $numapprove ++;
12389:                                 }
12390:                             }
12391:                         }
12392:                         unless ($numapprove) {
12393:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
12394:                         }
12395:                     }
12396:                     if ($chgtext) {
12397:                         $resulttext .= '<li>'.$chgtext.'</li>';
12398:                     }
12399:                 }
12400:             }
12401:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
12402:                 my ($emailrules,$emailruleorder) =
12403:                     &Apache::lonnet::inst_userrules($dom,'email');
12404:                 foreach my $type (@{$changes{'email_rule'}}) {
12405:                     if (ref($email_rule{$type}) eq 'ARRAY') {
12406:                         my $chgtext = '<ul>';
12407:                         foreach my $rule (@{$email_rule{$type}}) {
12408:                             if (ref($emailrules->{$rule}) eq 'HASH') {
12409:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
12410:                             }
12411:                         }
12412:                         $chgtext .= '</ul>';
12413:                         my $typename;
12414:                         if (@types) {
12415:                             if ($type eq 'default') {
12416:                                 $typename = $othertitle;
12417:                             } else {
12418:                                 $typename = $usertypes{$type};
12419:                             }
12420:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
12421:                         }
12422:                         if (@{$email_rule{$type}} > 0) {
12423:                             $resulttext .= '<li>'.
12424:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
12425:                                                $usertypes{$type}).
12426:                                            $chgtext.
12427:                                            '</li>';
12428:                         } else {
12429:                             $resulttext .= '<li>'.
12430:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
12431:                                            '</li>'.
12432:                                            &mt('(Affiliation: [_1])',$typename);
12433:                         }
12434:                     }
12435:                 }
12436:             }
12437:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
12438:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
12439:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
12440:                         my $chgtext = '<ul>';
12441:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
12442:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
12443:                         }
12444:                         $chgtext .= '</ul>';
12445:                         $resulttext .= '<li>'.
12446:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
12447:                                           $chgtext.
12448:                                        '</li>';
12449:                     } else {
12450:                         $resulttext .= '<li>'.
12451:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
12452:                                        '</li>';
12453:                     }
12454:                 }
12455:             }
12456:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
12457:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
12458:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12459:                 foreach my $type (@{$changes{'selfcreate'}}) {
12460:                     my $typename = $type;
12461:                     if (keys(%usertypes) > 0) {
12462:                         if ($usertypes{$type} ne '') {
12463:                             $typename = $usertypes{$type};
12464:                         }
12465:                     }
12466:                     my @modifiable;
12467:                     $resulttext .= '<li>'.
12468:                                     &mt('Self-creation of account by users with status: [_1]',
12469:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
12470:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
12471:                     foreach my $field (@fields) {
12472:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
12473:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
12474:                         }
12475:                     }
12476:                     if (@modifiable > 0) {
12477:                         $resulttext .= join(', ',@modifiable);
12478:                     } else {
12479:                         $resulttext .= &mt('none');
12480:                     }
12481:                     $resulttext .= '</li>';
12482:                 }
12483:                 $resulttext .= '</ul></li>';
12484:             }
12485:             $resulttext .= '</ul>';
12486:             my $cachetime = 24*60*60;
12487:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
12488:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12489:             if (ref($lastactref) eq 'HASH') {
12490:                 $lastactref->{'domdefaults'} = 1;
12491:             }
12492:         } else {
12493:             $resulttext = &mt('No changes made to self-creation settings');
12494:         }
12495:     } else {
12496:         $resulttext = '<span class="LC_error">'.
12497:             &mt('An error occurred: [_1]',$putresult).'</span>';
12498:     }
12499:     if ($warningmsg ne '') {
12500:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
12501:     }
12502:     return $resulttext;
12503: }
12504: 
12505: sub process_captcha {
12506:     my ($container,$changes,$newsettings,$currsettings) = @_;
12507:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
12508:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
12509:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
12510:         $newsettings->{'captcha'} = 'original';
12511:     }
12512:     my %current;
12513:     if (ref($currsettings) eq 'HASH') {
12514:         %current = %{$currsettings};
12515:     }
12516:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
12517:         if ($container eq 'cancreate') {
12518:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12519:                 push(@{$changes->{'cancreate'}},'captcha');
12520:             } elsif (!defined($changes->{'cancreate'})) {
12521:                 $changes->{'cancreate'} = ['captcha'];
12522:             }
12523:         } elsif ($container eq 'passwords') {
12524:             $changes->{'reset'} = 1;
12525:         } else {
12526:             $changes->{'captcha'} = 1;
12527:         }
12528:     }
12529:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
12530:     if ($newsettings->{'captcha'} eq 'recaptcha') {
12531:         $newpub = $env{'form.'.$container.'_recaptchapub'};
12532:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
12533:         $newpub =~ s/[^\w\-]//g;
12534:         $newpriv =~ s/[^\w\-]//g;
12535:         $newsettings->{'recaptchakeys'} = {
12536:                                              public  => $newpub,
12537:                                              private => $newpriv,
12538:                                           };
12539:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
12540:         $newversion =~ s/\D//g;
12541:         if ($newversion ne '2') {
12542:             $newversion = 1;
12543:         }
12544:         $newsettings->{'recaptchaversion'} = $newversion;
12545:     }
12546:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
12547:         $currpub = $current{'recaptchakeys'}{'public'};
12548:         $currpriv = $current{'recaptchakeys'}{'private'};
12549:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
12550:             $newsettings->{'recaptchakeys'} = {
12551:                                                  public  => '',
12552:                                                  private => '',
12553:                                               }
12554:         }
12555:     }
12556:     if ($current{'captcha'} eq 'recaptcha') {
12557:         $currversion = $current{'recaptchaversion'};
12558:         if ($currversion ne '2') {
12559:             $currversion = 1;
12560:         }
12561:     }
12562:     if ($currversion ne $newversion) {
12563:         if ($container eq 'cancreate') {
12564:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12565:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
12566:             } elsif (!defined($changes->{'cancreate'})) {
12567:                 $changes->{'cancreate'} = ['recaptchaversion'];
12568:             }
12569:         } elsif ($container eq 'passwords') {
12570:             $changes->{'reset'} = 1;
12571:         } else {
12572:             $changes->{'recaptchaversion'} = 1;
12573:         }
12574:     }
12575:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
12576:         if ($container eq 'cancreate') {
12577:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12578:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
12579:             } elsif (!defined($changes->{'cancreate'})) {
12580:                 $changes->{'cancreate'} = ['recaptchakeys'];
12581:             }
12582:         } elsif ($container eq 'passwords') {
12583:             $changes->{'reset'} = 1;
12584:         } else {
12585:             $changes->{'recaptchakeys'} = 1;
12586:         }
12587:     }
12588:     return;
12589: }
12590: 
12591: sub modify_usermodification {
12592:     my ($dom,%domconfig) = @_;
12593:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
12594:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
12595:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
12596:             if ($key eq 'selfcreate') {
12597:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
12598:             } else {  
12599:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
12600:             }
12601:         }
12602:     }
12603:     my @contexts = ('author','course');
12604:     my %context_title = (
12605:                            author => 'In author context',
12606:                            course => 'In course context',
12607:                         );
12608:     my @fields = ('lastname','firstname','middlename','generation',
12609:                   'permanentemail','id');
12610:     my %roles = (
12611:                   author => ['ca','aa'],
12612:                   course => ['st','ep','ta','in','cr'],
12613:                 );
12614:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12615:     foreach my $context (@contexts) {
12616:         foreach my $role (@{$roles{$context}}) {
12617:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
12618:             foreach my $item (@fields) {
12619:                 if (grep(/^\Q$item\E$/,@modifiable)) {
12620:                     $modifyhash{$context}{$role}{$item} = 1;
12621:                 } else {
12622:                     $modifyhash{$context}{$role}{$item} = 0;
12623:                 }
12624:             }
12625:         }
12626:         if (ref($curr_usermodification{$context}) eq 'HASH') {
12627:             foreach my $role (@{$roles{$context}}) {
12628:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
12629:                     foreach my $field (@fields) {
12630:                         if ($modifyhash{$context}{$role}{$field} ne 
12631:                                 $curr_usermodification{$context}{$role}{$field}) {
12632:                             push(@{$changes{$context}},$role);
12633:                             last;
12634:                         }
12635:                     }
12636:                 }
12637:             }
12638:         } else {
12639:             foreach my $context (@contexts) {
12640:                 foreach my $role (@{$roles{$context}}) {
12641:                     push(@{$changes{$context}},$role);
12642:                 }
12643:             }
12644:         }
12645:     }
12646:     my %usermodification_hash =  (
12647:                                    usermodification => \%modifyhash,
12648:                                  );
12649:     my $putresult = &Apache::lonnet::put_dom('configuration',
12650:                                              \%usermodification_hash,$dom);
12651:     if ($putresult eq 'ok') {
12652:         if (keys(%changes) > 0) {
12653:             $resulttext = &mt('Changes made: ').'<ul>';
12654:             foreach my $context (@contexts) {
12655:                 if (ref($changes{$context}) eq 'ARRAY') {
12656:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
12657:                     if (ref($changes{$context}) eq 'ARRAY') {
12658:                         foreach my $role (@{$changes{$context}}) {
12659:                             my $rolename;
12660:                             if ($role eq 'cr') {
12661:                                 $rolename = &mt('Custom');
12662:                             } else {
12663:                                 $rolename = &Apache::lonnet::plaintext($role);
12664:                             }
12665:                             my @modifiable;
12666:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
12667:                             foreach my $field (@fields) {
12668:                                 if ($modifyhash{$context}{$role}{$field}) {
12669:                                     push(@modifiable,$fieldtitles{$field});
12670:                                 }
12671:                             }
12672:                             if (@modifiable > 0) {
12673:                                 $resulttext .= join(', ',@modifiable);
12674:                             } else {
12675:                                 $resulttext .= &mt('none'); 
12676:                             }
12677:                             $resulttext .= '</li>';
12678:                         }
12679:                         $resulttext .= '</ul></li>';
12680:                     }
12681:                 }
12682:             }
12683:             $resulttext .= '</ul>';
12684:         } else {
12685:             $resulttext = &mt('No changes made to user modification settings');
12686:         }
12687:     } else {
12688:         $resulttext = '<span class="LC_error">'.
12689:             &mt('An error occurred: [_1]',$putresult).'</span>';
12690:     }
12691:     return $resulttext;
12692: }
12693: 
12694: sub modify_defaults {
12695:     my ($dom,$lastactref,%domconfig) = @_;
12696:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
12697:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12698:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
12699:                  'portal_def');
12700:     my @authtypes = ('internal','krb4','krb5','localauth');
12701:     foreach my $item (@items) {
12702:         $newvalues{$item} = $env{'form.'.$item};
12703:         if ($item eq 'auth_def') {
12704:             if ($newvalues{$item} ne '') {
12705:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
12706:                     push(@errors,$item);
12707:                 }
12708:             }
12709:         } elsif ($item eq 'lang_def') {
12710:             if ($newvalues{$item} ne '') {
12711:                 if ($newvalues{$item} =~ /^(\w+)/) {
12712:                     my $langcode = $1;
12713:                     if ($langcode ne 'x_chef') {
12714:                         if (code2language($langcode) eq '') {
12715:                             push(@errors,$item);
12716:                         }
12717:                     }
12718:                 } else {
12719:                     push(@errors,$item);
12720:                 }
12721:             }
12722:         } elsif ($item eq 'timezone_def') {
12723:             if ($newvalues{$item} ne '') {
12724:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
12725:                     push(@errors,$item);   
12726:                 }
12727:             }
12728:         } elsif ($item eq 'datelocale_def') {
12729:             if ($newvalues{$item} ne '') {
12730:                 my @datelocale_ids = DateTime::Locale->ids();
12731:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
12732:                     push(@errors,$item);
12733:                 }
12734:             }
12735:         } elsif ($item eq 'portal_def') {
12736:             if ($newvalues{$item} ne '') {
12737:                 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])\/?$/) {
12738:                     push(@errors,$item);
12739:                 }
12740:             }
12741:         }
12742:         if (grep(/^\Q$item\E$/,@errors)) {
12743:             $newvalues{$item} = $domdefaults{$item};
12744:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12745:             $changes{$item} = 1;
12746:         }
12747:         $domdefaults{$item} = $newvalues{$item};
12748:     }
12749:     my %staticdefaults = (
12750:                            'intauth_cost'   => 10,
12751:                            'intauth_check'  => 0,
12752:                            'intauth_switch' => 0,
12753:                          );
12754:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
12755:         if (exists($domdefaults{$item})) {
12756:             $newvalues{$item} = $domdefaults{$item};
12757:         } else {
12758:             $newvalues{$item} = $staticdefaults{$item};
12759:         }
12760:     }
12761:     my %defaults_hash = (
12762:                          defaults => \%newvalues,
12763:                         );
12764:     my $title = &defaults_titles();
12765: 
12766:     my $currinststatus;
12767:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12768:         $currinststatus = $domconfig{'inststatus'};
12769:     } else {
12770:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12771:         $currinststatus = {
12772:                              inststatustypes => $usertypes,
12773:                              inststatusorder => $types,
12774:                              inststatusguest => [],
12775:                           };
12776:     }
12777:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12778:     my @allpos;
12779:     my %alltypes;
12780:     my @inststatusguest;
12781:     if (ref($currinststatus) eq 'HASH') {
12782:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12783:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
12784:                 unless (grep(/^\Q$type\E$/,@todelete)) {
12785:                     push(@inststatusguest,$type);
12786:                 }
12787:             }
12788:         }
12789:     }
12790:     my ($currtitles,$currorder);
12791:     if (ref($currinststatus) eq 'HASH') {
12792:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12793:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12794:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12795:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12796:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12797:                     }
12798:                 }
12799:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12800:                     my $position = $env{'form.inststatus_pos_'.$type};
12801:                     $position =~ s/\D+//g;
12802:                     $allpos[$position] = $type;
12803:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12804:                     $alltypes{$type} =~ s/`//g;
12805:                 }
12806:             }
12807:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12808:             $currtitles =~ s/,$//;
12809:         }
12810:     }
12811:     if ($env{'form.addinststatus'}) {
12812:         my $newtype = $env{'form.addinststatus'};
12813:         $newtype =~ s/\W//g;
12814:         unless (exists($alltypes{$newtype})) {
12815:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12816:             $alltypes{$newtype} =~ s/`//g; 
12817:             my $position = $env{'form.addinststatus_pos'};
12818:             $position =~ s/\D+//g;
12819:             if ($position ne '') {
12820:                 $allpos[$position] = $newtype;
12821:             }
12822:         }
12823:     }
12824:     my @orderedstatus;
12825:     foreach my $type (@allpos) {
12826:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12827:             push(@orderedstatus,$type);
12828:         }
12829:     }
12830:     foreach my $type (keys(%alltypes)) {
12831:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12832:             delete($alltypes{$type});
12833:         }
12834:     }
12835:     $defaults_hash{'inststatus'} = {
12836:                                      inststatustypes => \%alltypes,
12837:                                      inststatusorder => \@orderedstatus,
12838:                                      inststatusguest => \@inststatusguest,
12839:                                    };
12840:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12841:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12842:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12843:         }
12844:     }
12845:     if ($currorder ne join(',',@orderedstatus)) {
12846:         $changes{'inststatus'}{'inststatusorder'} = 1;
12847:     }
12848:     my $newtitles;
12849:     foreach my $item (@orderedstatus) {
12850:         $newtitles .= $alltypes{$item}.',';
12851:     }
12852:     $newtitles =~ s/,$//;
12853:     if ($currtitles ne $newtitles) {
12854:         $changes{'inststatus'}{'inststatustypes'} = 1;
12855:     }
12856:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12857:                                              $dom);
12858:     if ($putresult eq 'ok') {
12859:         if (keys(%changes) > 0) {
12860:             $resulttext = &mt('Changes made:').'<ul>';
12861:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12862:             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";
12863:             foreach my $item (sort(keys(%changes))) {
12864:                 if ($item eq 'inststatus') {
12865:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12866:                         if (@orderedstatus) {
12867:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12868:                             foreach my $type (@orderedstatus) { 
12869:                                 $resulttext .= $alltypes{$type}.', ';
12870:                             }
12871:                             $resulttext =~ s/, $//;
12872:                             $resulttext .= '</li>';
12873:                         } else {
12874:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
12875:                         }
12876:                     }
12877:                 } else {
12878:                     my $value = $env{'form.'.$item};
12879:                     if ($value eq '') {
12880:                         $value = &mt('none');
12881:                     } elsif ($item eq 'auth_def') {
12882:                         my %authnames = &authtype_names();
12883:                         my %shortauth = (
12884:                                           internal   => 'int',
12885:                                           krb4       => 'krb4',
12886:                                           krb5       => 'krb5',
12887:                                           localauth  => 'loc',
12888:                         );
12889:                         $value = $authnames{$shortauth{$value}};
12890:                     }
12891:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12892:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12893:                 }
12894:             }
12895:             $resulttext .= '</ul>';
12896:             $mailmsgtext .= "\n";
12897:             my $cachetime = 24*60*60;
12898:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12899:             if (ref($lastactref) eq 'HASH') {
12900:                 $lastactref->{'domdefaults'} = 1;
12901:             }
12902:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12903:                 my $notify = 1;
12904:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12905:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12906:                         $notify = 0;
12907:                     }
12908:                 }
12909:                 if ($notify) {
12910:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12911:                                                "LON-CAPA Domain Settings Change - $dom",
12912:                                                $mailmsgtext);
12913:                 }
12914:             }
12915:         } else {
12916:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12917:         }
12918:     } else {
12919:         $resulttext = '<span class="LC_error">'.
12920:             &mt('An error occurred: [_1]',$putresult).'</span>';
12921:     }
12922:     if (@errors > 0) {
12923:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12924:         foreach my $item (@errors) {
12925:             $resulttext .= ' "'.$title->{$item}.'",';
12926:         }
12927:         $resulttext =~ s/,$//;
12928:     }
12929:     return $resulttext;
12930: }
12931: 
12932: sub modify_scantron {
12933:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12934:     my ($resulttext,%confhash,%changes,$errors);
12935:     my $custom = 'custom.tab';
12936:     my $default = 'default.tab';
12937:     my $servadm = $r->dir_config('lonAdmEMail');
12938:     my ($configuserok,$author_ok,$switchserver) =
12939:         &config_check($dom,$confname,$servadm);
12940:     if ($env{'form.scantronformat.filename'} ne '') {
12941:         my $error;
12942:         if ($configuserok eq 'ok') {
12943:             if ($switchserver) {
12944:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12945:             } else {
12946:                 if ($author_ok eq 'ok') {
12947:                     my ($result,$scantronurl) =
12948:                         &publishlogo($r,'upload','scantronformat',$dom,
12949:                                      $confname,'scantron','','',$custom);
12950:                     if ($result eq 'ok') {
12951:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12952:                         $changes{'scantronformat'} = 1;
12953:                     } else {
12954:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12955:                     }
12956:                 } else {
12957:                     $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);
12958:                 }
12959:             }
12960:         } else {
12961:             $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);
12962:         }
12963:         if ($error) {
12964:             &Apache::lonnet::logthis($error);
12965:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12966:         }
12967:     }
12968:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12969:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12970:             if ($env{'form.scantronformat_del'}) {
12971:                 $confhash{'scantron'}{'scantronformat'} = '';
12972:                 $changes{'scantronformat'} = 1;
12973:             } else {
12974:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
12975:             }
12976:         }
12977:     }
12978:     my @options = ('hdr','pad','rem');
12979:     my @fields = &scantroncsv_fields();
12980:     my %titles = &scantronconfig_titles();
12981:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
12982:     my ($newdat,$currdat,%newcol,%currcol);
12983:     if (grep(/^dat$/,@formats)) {
12984:         $confhash{'scantron'}{config}{dat} = 1;
12985:         $newdat = 1;
12986:     } else {
12987:         $newdat = 0;
12988:     }
12989:     if (grep(/^csv$/,@formats)) {
12990:         my %bynum;
12991:         foreach my $field (@fields) {
12992:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
12993:                 my $posscol = $1;
12994:                 if (($posscol < 20) && (!$bynum{$posscol})) {
12995:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
12996:                     $bynum{$posscol} = $field;
12997:                     $newcol{$field} = $posscol;
12998:                 }
12999:             }
13000:         }
13001:         if (keys(%newcol)) {
13002:             foreach my $option (@options) {
13003:                 if ($env{'form.scantroncsv_'.$option}) {
13004:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
13005:                 }
13006:             }
13007:         }
13008:     }
13009:     $currdat = 1;
13010:     if (ref($domconfig{'scantron'}) eq 'HASH') {
13011:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
13012:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
13013:                 $currdat = 0;
13014:             }
13015:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
13016:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
13017:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
13018:                 }
13019:             }
13020:         }
13021:     }
13022:     if ($currdat != $newdat) {
13023:         $changes{'config'} = 1;
13024:     } else {
13025:         foreach my $field (@fields) {
13026:             if ($currcol{$field} ne '') {
13027:                 if ($currcol{$field} ne $newcol{$field}) {
13028:                     $changes{'config'} = 1;
13029:                     last;
13030:                 }
13031:             } elsif ($newcol{$field} ne '') {
13032:                 $changes{'config'} = 1;
13033:                 last;
13034:             }
13035:         }
13036:     }
13037:     if (keys(%confhash) > 0) {
13038:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
13039:                                                  $dom);
13040:         if ($putresult eq 'ok') {
13041:             if (keys(%changes) > 0) {
13042:                 if (ref($confhash{'scantron'}) eq 'HASH') {
13043:                     $resulttext = &mt('Changes made:').'<ul>';
13044:                     if ($changes{'scantronformat'}) {
13045:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
13046:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
13047:                         } else {
13048:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
13049:                         }
13050:                     }
13051:                     if ($changes{'config'}) {
13052:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
13053:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
13054:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
13055:                             }
13056:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
13057:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
13058:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
13059:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
13060:                                         foreach my $field (@fields) {
13061:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
13062:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
13063:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
13064:                                             }
13065:                                         }
13066:                                         $resulttext .= '</ul></li>';
13067:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
13068:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
13069:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
13070:                                                 foreach my $option (@options) {
13071:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
13072:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
13073:                                                     }
13074:                                                 }
13075:                                                 $resulttext .= '</ul></li>';
13076:                                             }
13077:                                         }
13078:                                     }
13079:                                 }
13080:                             }
13081:                         } else {
13082:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
13083:                         }
13084:                     }
13085:                     $resulttext .= '</ul>';
13086:                 } else {
13087:                     $resulttext = &mt('Changes made to bubblesheet format file.');
13088:                 }
13089:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
13090:                 if (ref($lastactref) eq 'HASH') {
13091:                     $lastactref->{'domainconfig'} = 1;
13092:                 }
13093:             } else {
13094:                 $resulttext = &mt('No changes made to bubblesheet format settings');
13095:             }
13096:         } else {
13097:             $resulttext = '<span class="LC_error">'.
13098:                 &mt('An error occurred: [_1]',$putresult).'</span>';
13099:         }
13100:     } else {
13101:         $resulttext = &mt('No changes made to bubblesheet format settings');
13102:     }
13103:     if ($errors) {
13104:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13105:                        $errors.'</ul>';
13106:     }
13107:     return $resulttext;
13108: }
13109: 
13110: sub modify_coursecategories {
13111:     my ($dom,$lastactref,%domconfig) = @_;
13112:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
13113:         $cathash);
13114:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
13115:     my @catitems = ('unauth','auth');
13116:     my @cattypes = ('std','domonly','codesrch','none');
13117:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
13118:         $cathash = $domconfig{'coursecategories'}{'cats'};
13119:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
13120:             $changes{'togglecats'} = 1;
13121:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
13122:         }
13123:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
13124:             $changes{'categorize'} = 1;
13125:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
13126:         }
13127:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
13128:             $changes{'togglecatscomm'} = 1;
13129:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
13130:         }
13131:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
13132:             $changes{'categorizecomm'} = 1;
13133:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
13134:         }
13135:         foreach my $item (@catitems) {
13136:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
13137:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
13138:                     $changes{$item} = 1;
13139:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
13140:                 }
13141:             }
13142:         }
13143:     } else {
13144:         $changes{'togglecats'} = 1;
13145:         $changes{'categorize'} = 1;
13146:         $changes{'togglecatscomm'} = 1;
13147:         $changes{'categorizecomm'} = 1;
13148:         $domconfig{'coursecategories'} = {
13149:                                              togglecats => $env{'form.togglecats'},
13150:                                              categorize => $env{'form.categorize'},
13151:                                              togglecatscomm => $env{'form.togglecatscomm'},
13152:                                              categorizecomm => $env{'form.categorizecomm'},
13153:                                          };
13154:         foreach my $item (@catitems) {
13155:             if ($env{'form.coursecat_'.$item} ne 'std') {
13156:                 $changes{$item} = 1;
13157:             }
13158:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
13159:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
13160:             }
13161:         }
13162:     }
13163:     if (ref($cathash) eq 'HASH') {
13164:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
13165:             push (@deletecategory,'instcode::0');
13166:         }
13167:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
13168:             push(@deletecategory,'communities::0');
13169:         }
13170:     }
13171:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
13172:     if (ref($cathash) eq 'HASH') {
13173:         if (@deletecategory > 0) {
13174:             #FIXME Need to remove category from all courses using a deleted category 
13175:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
13176:             foreach my $item (@deletecategory) {
13177:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
13178:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
13179:                     $deletions{$item} = 1;
13180:                     &recurse_cat_deletes($item,$cathash,\%deletions);
13181:                 }
13182:             }
13183:         }
13184:         foreach my $item (keys(%{$cathash})) {
13185:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
13186:             if ($cathash->{$item} ne $env{'form.'.$item}) {
13187:                 $reorderings{$item} = 1;
13188:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
13189:             }
13190:             if ($env{'form.addcategory_name_'.$item} ne '') {
13191:                 my $newcat = $env{'form.addcategory_name_'.$item};
13192:                 my $newdepth = $depth+1;
13193:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
13194:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
13195:                 $adds{$newitem} = 1; 
13196:             }
13197:             if ($env{'form.subcat_'.$item} ne '') {
13198:                 my $newcat = $env{'form.subcat_'.$item};
13199:                 my $newdepth = $depth+1;
13200:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
13201:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
13202:                 $adds{$newitem} = 1;
13203:             }
13204:         }
13205:     }
13206:     if ($env{'form.instcode'} eq '1') {
13207:         if (ref($cathash) eq 'HASH') {
13208:             my $newitem = 'instcode::0';
13209:             if ($cathash->{$newitem} eq '') {  
13210:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13211:                 $adds{$newitem} = 1;
13212:             }
13213:         } else {
13214:             my $newitem = 'instcode::0';
13215:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13216:             $adds{$newitem} = 1;
13217:         }
13218:     }
13219:     if ($env{'form.communities'} eq '1') {
13220:         if (ref($cathash) eq 'HASH') {
13221:             my $newitem = 'communities::0';
13222:             if ($cathash->{$newitem} eq '') {
13223:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13224:                 $adds{$newitem} = 1;
13225:             }
13226:         } else {
13227:             my $newitem = 'communities::0';
13228:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13229:             $adds{$newitem} = 1;
13230:         }
13231:     }
13232:     if ($env{'form.addcategory_name'} ne '') {
13233:         if (($env{'form.addcategory_name'} ne 'instcode') &&
13234:             ($env{'form.addcategory_name'} ne 'communities')) {
13235:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
13236:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
13237:             $adds{$newitem} = 1;
13238:         }
13239:     }
13240:     my $putresult;
13241:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13242:         if (keys(%deletions) > 0) {
13243:             foreach my $key (keys(%deletions)) {
13244:                 if ($predelallitems{$key} ne '') {
13245:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
13246:                 }
13247:             }
13248:         }
13249:         my (@chkcats,@chktrails,%chkallitems);
13250:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
13251:         if (ref($chkcats[0]) eq 'ARRAY') {
13252:             my $depth = 0;
13253:             my $chg = 0;
13254:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
13255:                 my $name = $chkcats[0][$i];
13256:                 my $item;
13257:                 if ($name eq '') {
13258:                     $chg ++;
13259:                 } else {
13260:                     $item = &escape($name).'::0';
13261:                     if ($chg) {
13262:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
13263:                     }
13264:                     $depth ++; 
13265:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
13266:                     $depth --;
13267:                 }
13268:             }
13269:         }
13270:     }
13271:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13272:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
13273:         if ($putresult eq 'ok') {
13274:             my %title = (
13275:                          togglecats     => 'Show/Hide a course in catalog',
13276:                          categorize     => 'Assign a category to a course',
13277:                          togglecatscomm => 'Show/Hide a community in catalog',
13278:                          categorizecomm => 'Assign a category to a community',
13279:                         );
13280:             my %level = (
13281:                          dom  => 'set in Domain ("Modify Course/Community")',
13282:                          crs  => 'set in Course ("Course Configuration")',
13283:                          comm => 'set in Community ("Community Configuration")',
13284:                          none     => 'No catalog',
13285:                          std      => 'Standard catalog',
13286:                          domonly  => 'Domain-only catalog',
13287:                          codesrch => 'Code search form',
13288:                         );
13289:             $resulttext = &mt('Changes made:').'<ul>';
13290:             if ($changes{'togglecats'}) {
13291:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
13292:             }
13293:             if ($changes{'categorize'}) {
13294:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
13295:             }
13296:             if ($changes{'togglecatscomm'}) {
13297:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
13298:             }
13299:             if ($changes{'categorizecomm'}) {
13300:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
13301:             }
13302:             if ($changes{'unauth'}) {
13303:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
13304:             }
13305:             if ($changes{'auth'}) {
13306:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
13307:             }
13308:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13309:                 my $cathash;
13310:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
13311:                     $cathash = $domconfig{'coursecategories'}{'cats'};
13312:                 } else {
13313:                     $cathash = {};
13314:                 } 
13315:                 my (@cats,@trails,%allitems);
13316:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
13317:                 if (keys(%deletions) > 0) {
13318:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
13319:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
13320:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
13321:                     }
13322:                     $resulttext .= '</ul></li>';
13323:                 }
13324:                 if (keys(%reorderings) > 0) {
13325:                     my %sort_by_trail;
13326:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
13327:                     foreach my $key (keys(%reorderings)) {
13328:                         if ($allitems{$key} ne '') {
13329:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13330:                         }
13331:                     }
13332:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13333:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13334:                     }
13335:                     $resulttext .= '</ul></li>';
13336:                 }
13337:                 if (keys(%adds) > 0) {
13338:                     my %sort_by_trail;
13339:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
13340:                     foreach my $key (keys(%adds)) {
13341:                         if ($allitems{$key} ne '') {
13342:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13343:                         }
13344:                     }
13345:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13346:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13347:                     }
13348:                     $resulttext .= '</ul></li>';
13349:                 }
13350:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
13351:                 if (ref($lastactref) eq 'HASH') {
13352:                     $lastactref->{'cats'} = 1;
13353:                 }
13354:             }
13355:             $resulttext .= '</ul>';
13356:             if ($changes{'unauth'} || $changes{'auth'}) {
13357:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13358:                 if ($changes{'auth'}) {
13359:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
13360:                 }
13361:                 if ($changes{'unauth'}) {
13362:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
13363:                 }
13364:                 my $cachetime = 24*60*60;
13365:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13366:                 if (ref($lastactref) eq 'HASH') {
13367:                     $lastactref->{'domdefaults'} = 1;
13368:                 }
13369:             }
13370:         } else {
13371:             $resulttext = '<span class="LC_error">'.
13372:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13373:         }
13374:     } else {
13375:         $resulttext = &mt('No changes made to course and community categories');
13376:     }
13377:     return $resulttext;
13378: }
13379: 
13380: sub modify_serverstatuses {
13381:     my ($dom,%domconfig) = @_;
13382:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
13383:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
13384:         %currserverstatus = %{$domconfig{'serverstatuses'}};
13385:     }
13386:     my @pages = &serverstatus_pages();
13387:     foreach my $type (@pages) {
13388:         $newserverstatus{$type}{'namedusers'} = '';
13389:         $newserverstatus{$type}{'machines'} = '';
13390:         if (defined($env{'form.'.$type.'_namedusers'})) {
13391:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
13392:             my @okusers;
13393:             foreach my $user (@users) {
13394:                 my ($uname,$udom) = split(/:/,$user);
13395:                 if (($udom =~ /^$match_domain$/) &&   
13396:                     (&Apache::lonnet::domain($udom)) &&
13397:                     ($uname =~ /^$match_username$/)) {
13398:                     if (!grep(/^\Q$user\E/,@okusers)) {
13399:                         push(@okusers,$user);
13400:                     }
13401:                 }
13402:             }
13403:             if (@okusers > 0) {
13404:                  @okusers = sort(@okusers);
13405:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
13406:             }
13407:         }
13408:         if (defined($env{'form.'.$type.'_machines'})) {
13409:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
13410:             my @okmachines;
13411:             foreach my $ip (@machines) {
13412:                 my @parts = split(/\./,$ip);
13413:                 next if (@parts < 4);
13414:                 my $badip = 0;
13415:                 for (my $i=0; $i<4; $i++) {
13416:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
13417:                         $badip = 1;
13418:                         last;
13419:                     }
13420:                 }
13421:                 if (!$badip) {
13422:                     push(@okmachines,$ip);     
13423:                 }
13424:             }
13425:             @okmachines = sort(@okmachines);
13426:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
13427:         }
13428:     }
13429:     my %serverstatushash =  (
13430:                                 serverstatuses => \%newserverstatus,
13431:                             );
13432:     foreach my $type (@pages) {
13433:         foreach my $setting ('namedusers','machines') {
13434:             my (@current,@new);
13435:             if (ref($currserverstatus{$type}) eq 'HASH') {
13436:                 if ($currserverstatus{$type}{$setting} ne '') { 
13437:                     @current = split(/,/,$currserverstatus{$type}{$setting});
13438:                 }
13439:             }
13440:             if ($newserverstatus{$type}{$setting} ne '') {
13441:                 @new = split(/,/,$newserverstatus{$type}{$setting});
13442:             }
13443:             if (@current > 0) {
13444:                 if (@new > 0) {
13445:                     foreach my $item (@current) {
13446:                         if (!grep(/^\Q$item\E$/,@new)) {
13447:                             $changes{$type}{$setting} = 1;
13448:                             last;
13449:                         }
13450:                     }
13451:                     foreach my $item (@new) {
13452:                         if (!grep(/^\Q$item\E$/,@current)) {
13453:                             $changes{$type}{$setting} = 1;
13454:                             last;
13455:                         }
13456:                     }
13457:                 } else {
13458:                     $changes{$type}{$setting} = 1;
13459:                 }
13460:             } elsif (@new > 0) {
13461:                 $changes{$type}{$setting} = 1;
13462:             }
13463:         }
13464:     }
13465:     if (keys(%changes) > 0) {
13466:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
13467:         my $putresult = &Apache::lonnet::put_dom('configuration',
13468:                                                  \%serverstatushash,$dom);
13469:         if ($putresult eq 'ok') {
13470:             $resulttext .= &mt('Changes made:').'<ul>';
13471:             foreach my $type (@pages) {
13472:                 if (ref($changes{$type}) eq 'HASH') {
13473:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
13474:                     if ($changes{$type}{'namedusers'}) {
13475:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
13476:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
13477:                         } else {
13478:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
13479:                         }
13480:                     }
13481:                     if ($changes{$type}{'machines'}) {
13482:                         if ($newserverstatus{$type}{'machines'} eq '') {
13483:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
13484:                         } else {
13485:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
13486:                         }
13487: 
13488:                     }
13489:                     $resulttext .= '</ul></li>';
13490:                 }
13491:             }
13492:             $resulttext .= '</ul>';
13493:         } else {
13494:             $resulttext = '<span class="LC_error">'.
13495:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
13496: 
13497:         }
13498:     } else {
13499:         $resulttext = &mt('No changes made to access to server status pages');
13500:     }
13501:     return $resulttext;
13502: }
13503: 
13504: sub modify_helpsettings {
13505:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13506:     my ($resulttext,$errors,%changes,%helphash);
13507:     my %defaultchecked = ('submitbugs' => 'on');
13508:     my @offon = ('off','on');
13509:     my @toggles = ('submitbugs');
13510:     my %current = ('submitbugs' => '',
13511:                    'adhoc'      => {},
13512:                   );
13513:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
13514:         %current = %{$domconfig{'helpsettings'}};
13515:     }
13516:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13517:     foreach my $item (@toggles) {
13518:         if ($defaultchecked{$item} eq 'on') { 
13519:             if ($current{$item} eq '') {
13520:                 if ($env{'form.'.$item} eq '0') {
13521:                     $changes{$item} = 1;
13522:                 }
13523:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13524:                 $changes{$item} = 1;
13525:             }
13526:         } elsif ($defaultchecked{$item} eq 'off') {
13527:             if ($current{$item} eq '') {
13528:                 if ($env{'form.'.$item} eq '1') {
13529:                     $changes{$item} = 1;
13530:                 }
13531:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13532:                 $changes{$item} = 1;
13533:             }
13534:         }
13535:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
13536:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
13537:         }
13538:     }
13539:     my $maxnum = $env{'form.helproles_maxnum'};
13540:     my $confname = $dom.'-domainconfig';
13541:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
13542:     my (@allpos,%newsettings,%changedprivs,$newrole);
13543:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13544:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
13545:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
13546:     my %lt = &Apache::lonlocal::texthash(
13547:                     s      => 'system',
13548:                     d      => 'domain',
13549:                     order  => 'Display order',
13550:                     access => 'Role usage',
13551:                     all    => 'All with domain helpdesk or helpdesk assistant role',
13552:                     dh     => 'All with domain helpdesk role',
13553:                     da     => 'All with domain helpdesk assistant role',
13554:                     none   => 'None',
13555:                     status => 'Determined based on institutional status',
13556:                     inc    => 'Include all, but exclude specific personnel',
13557:                     exc    => 'Exclude all, but include specific personnel',
13558:     );
13559:     for (my $num=0; $num<=$maxnum; $num++) {
13560:         my ($prefix,$identifier,$rolename,%curr);
13561:         if ($num == $maxnum) {
13562:             next unless ($env{'form.newcusthelp'} == $maxnum);
13563:             $identifier = 'custhelp'.$num;
13564:             $prefix = 'helproles_'.$num;
13565:             $rolename = $env{'form.custhelpname'.$num};
13566:             $rolename=~s/[^A-Za-z0-9]//gs;
13567:             next if ($rolename eq '');
13568:             next if (exists($existing{'rolesdef_'.$rolename}));
13569:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13570:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13571:                                                      $newprivs{'c'},$confname,$dom);
13572:             if ($result ne 'ok') {
13573:                 $errors .= '<li><span class="LC_error">'.
13574:                            &mt('An error occurred storing the new custom role: [_1]',
13575:                            $result).'</span></li>';
13576:                 next;
13577:             } else {
13578:                 $changedprivs{$rolename} = \%newprivs;
13579:                 $newrole = $rolename;
13580:             }
13581:         } else {
13582:             $prefix = 'helproles_'.$num;
13583:             $rolename = $env{'form.'.$prefix};
13584:             next if ($rolename eq '');
13585:             next unless (exists($existing{'rolesdef_'.$rolename}));
13586:             $identifier = 'custhelp'.$num;
13587:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13588:             my %currprivs;
13589:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
13590:                 split(/\_/,$existing{'rolesdef_'.$rolename});
13591:             foreach my $level ('c','d','s') {
13592:                 if ($newprivs{$level} ne $currprivs{$level}) {
13593:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13594:                                                              $newprivs{'c'},$confname,$dom);
13595:                     if ($result ne 'ok') {
13596:                         $errors .= '<li><span class="LC_error">'.
13597:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
13598:                                        $rolename,$result).'</span></li>';
13599:                     } else {
13600:                         $changedprivs{$rolename} = \%newprivs;
13601:                     }
13602:                     last;
13603:                 }
13604:             }
13605:             if (ref($current{'adhoc'}) eq 'HASH') {
13606:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13607:                     %curr = %{$current{'adhoc'}{$rolename}};
13608:                 }
13609:             }
13610:         }
13611:         my $newpos = $env{'form.'.$prefix.'_pos'};
13612:         $newpos =~ s/\D+//g;
13613:         $allpos[$newpos] = $rolename;
13614:         my $newdesc = $env{'form.'.$prefix.'_desc'};
13615:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
13616:         if ($curr{'desc'}) {
13617:             if ($curr{'desc'} ne $newdesc) {
13618:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
13619:                 $newsettings{$rolename}{'desc'} = $newdesc;
13620:             }
13621:         } elsif ($newdesc ne '') {
13622:             $changes{'customrole'}{$rolename}{'desc'} = 1;
13623:             $newsettings{$rolename}{'desc'} = $newdesc;
13624:         }
13625:         my $access = $env{'form.'.$prefix.'_access'};
13626:         if (grep(/^\Q$access\E$/,@accesstypes)) {
13627:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
13628:             if ($access eq 'status') {
13629:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
13630:                 if (scalar(@statuses) == 0) {
13631:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
13632:                 } else {
13633:                     my (@shownstatus,$numtypes);
13634:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13635:                     if (ref($types) eq 'ARRAY') {
13636:                         $numtypes = scalar(@{$types});
13637:                         foreach my $type (sort(@statuses)) {
13638:                             if ($type eq 'default') {
13639:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13640:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
13641:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13642:                                 push(@shownstatus,$usertypes->{$type});
13643:                             }
13644:                         }
13645:                     }
13646:                     if (grep(/^default$/,@statuses)) {
13647:                         push(@shownstatus,$othertitle);
13648:                     }
13649:                     if (scalar(@shownstatus) == 1+$numtypes) {
13650:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
13651:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
13652:                     } else {
13653:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
13654:                         if (ref($curr{'status'}) eq 'ARRAY') {
13655:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13656:                             if (@diffs) {
13657:                                 $changes{'customrole'}{$rolename}{$access} = 1;
13658:                             }
13659:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13660:                             $changes{'customrole'}{$rolename}{$access} = 1;
13661:                         }
13662:                     }
13663:                 }
13664:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
13665:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
13666:                 my @newspecstaff;
13667:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13668:                 foreach my $person (sort(@personnel)) {
13669:                     if ($domhelpdesk{$person}) {
13670:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
13671:                     }
13672:                 }
13673:                 if (ref($curr{$access}) eq 'ARRAY') {
13674:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13675:                     if (@diffs) {
13676:                         $changes{'customrole'}{$rolename}{$access} = 1;
13677:                     }
13678:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13679:                     $changes{'customrole'}{$rolename}{$access} = 1;
13680:                 }
13681:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13682:                     my ($uname,$udom) = split(/:/,$person);
13683:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
13684:                 }
13685:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
13686:             }
13687:         } else {
13688:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
13689:         }
13690:         unless ($curr{'access'} eq $access) {
13691:             $changes{'customrole'}{$rolename}{'access'} = 1;
13692:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
13693:         }
13694:     }
13695:     if (@allpos > 0) {
13696:         my $idx = 0;
13697:         foreach my $rolename (@allpos) {
13698:             if ($rolename ne '') {
13699:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
13700:                 if (ref($current{'adhoc'}) eq 'HASH') {
13701:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13702:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
13703:                             $changes{'customrole'}{$rolename}{'order'} = 1;
13704:                             $newsettings{$rolename}{'order'} = $idx+1;
13705:                         }
13706:                     }
13707:                 }
13708:                 $idx ++;
13709:             }
13710:         }
13711:     }
13712:     my $putresult;
13713:     if (keys(%changes) > 0) {
13714:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
13715:         if ($putresult eq 'ok') {
13716:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
13717:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
13718:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
13719:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
13720:                 }
13721:             }
13722:             my $cachetime = 24*60*60;
13723:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13724:             if (ref($lastactref) eq 'HASH') {
13725:                 $lastactref->{'domdefaults'} = 1;
13726:             }
13727:         } else {
13728:             $errors .= '<li><span class="LC_error">'.
13729:                        &mt('An error occurred storing the settings: [_1]',
13730:                            $putresult).'</span></li>';
13731:         }
13732:     }
13733:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
13734:         $resulttext = &mt('Changes made:').'<ul>';
13735:         my (%shownprivs,@levelorder);
13736:         @levelorder = ('c','d','s');
13737:         if ((keys(%changes)) && ($putresult eq 'ok')) {
13738:             foreach my $item (sort(keys(%changes))) {
13739:                 if ($item eq 'submitbugs') {
13740:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
13741:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
13742:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
13743:                 } elsif ($item eq 'customrole') {
13744:                     if (ref($changes{'customrole'}) eq 'HASH') {
13745:                         my @keyorder = ('order','desc','access','status','exc','inc');
13746:                         my %keytext = &Apache::lonlocal::texthash(
13747:                                                                    order  => 'Order',
13748:                                                                    desc   => 'Role description',
13749:                                                                    access => 'Role usage',
13750:                                                                    status => 'Allowed institutional types',
13751:                                                                    exc    => 'Allowed personnel',
13752:                                                                    inc    => 'Disallowed personnel',
13753:                         );
13754:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
13755:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
13756:                                 if ($role eq $newrole) {
13757:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
13758:                                                               $role).'<ul>';
13759:                                 } else {
13760:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13761:                                                               $role).'<ul>';
13762:                                 }
13763:                                 foreach my $key (@keyorder) {
13764:                                     if ($changes{'customrole'}{$role}{$key}) {
13765:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
13766:                                                                   $keytext{$key},$newsettings{$role}{$key}).
13767:                                                        '</li>';
13768:                                     }
13769:                                 }
13770:                                 if (ref($changedprivs{$role}) eq 'HASH') {
13771:                                     $shownprivs{$role} = 1;
13772:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
13773:                                     foreach my $level (@levelorder) {
13774:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13775:                                             next if ($item eq '');
13776:                                             my ($priv) = split(/\&/,$item,2);
13777:                                             if (&Apache::lonnet::plaintext($priv)) {
13778:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13779:                                                 unless ($level eq 'c') {
13780:                                                     $resulttext .= ' ('.$lt{$level}.')';
13781:                                                 }
13782:                                                 $resulttext .= '</li>';
13783:                                             }
13784:                                         }
13785:                                     }
13786:                                     $resulttext .= '</ul>';
13787:                                 }
13788:                                 $resulttext .= '</ul></li>';
13789:                             }
13790:                         }
13791:                     }
13792:                 }
13793:             }
13794:         }
13795:         if (keys(%changedprivs)) {
13796:             foreach my $role (sort(keys(%changedprivs))) {
13797:                 unless ($shownprivs{$role}) {
13798:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13799:                                               $role).'<ul>'.
13800:                                    '<li>'.&mt('Privileges set to :').'<ul>';
13801:                     foreach my $level (@levelorder) {
13802:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13803:                             next if ($item eq '');
13804:                             my ($priv) = split(/\&/,$item,2);
13805:                             if (&Apache::lonnet::plaintext($priv)) {
13806:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13807:                                 unless ($level eq 'c') {
13808:                                     $resulttext .= ' ('.$lt{$level}.')';
13809:                                 }
13810:                                 $resulttext .= '</li>';
13811:                             }
13812:                         }
13813:                     }
13814:                     $resulttext .= '</ul></li></ul></li>';
13815:                 }
13816:             }
13817:         }
13818:         $resulttext .= '</ul>';
13819:     } else {
13820:         $resulttext = &mt('No changes made to help settings');
13821:     }
13822:     if ($errors) {
13823:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13824:                                     $errors.'</ul>';
13825:     }
13826:     return $resulttext;
13827: }
13828: 
13829: sub modify_coursedefaults {
13830:     my ($dom,$lastactref,%domconfig) = @_;
13831:     my ($resulttext,$errors,%changes,%defaultshash);
13832:     my %defaultchecked = (
13833:                            'uselcmath'       => 'on',
13834:                            'usejsme'         => 'on'
13835:                          );
13836:     my @toggles = ('uselcmath','usejsme');
13837:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13838:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
13839:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
13840:     my @types = ('official','unofficial','community','textbook');
13841:     my %staticdefaults = (
13842:                            anonsurvey_threshold => 10,
13843:                            uploadquota          => 500,
13844:                            postsubmit           => 60,
13845:                            mysqltables          => 172800,
13846:                          );
13847:     my %texoptions = (
13848:                         MathJax  => 'MathJax',
13849:                         mimetex  => &mt('Convert to Images'),
13850:                         tth      => &mt('TeX to HTML'),
13851:                      );
13852:     $defaultshash{'coursedefaults'} = {};
13853: 
13854:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13855:         if ($domconfig{'coursedefaults'} eq '') {
13856:             $domconfig{'coursedefaults'} = {};
13857:         }
13858:     }
13859: 
13860:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13861:         foreach my $item (@toggles) {
13862:             if ($defaultchecked{$item} eq 'on') {
13863:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13864:                     ($env{'form.'.$item} eq '0')) {
13865:                     $changes{$item} = 1;
13866:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13867:                     $changes{$item} = 1;
13868:                 }
13869:             } elsif ($defaultchecked{$item} eq 'off') {
13870:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13871:                     ($env{'form.'.$item} eq '1')) {
13872:                     $changes{$item} = 1;
13873:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13874:                     $changes{$item} = 1;
13875:                 }
13876:             }
13877:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13878:         }
13879:         foreach my $item (@numbers) {
13880:             my ($currdef,$newdef);
13881:             $newdef = $env{'form.'.$item};
13882:             if ($item eq 'anonsurvey_threshold') {
13883:                 $currdef = $domconfig{'coursedefaults'}{$item};
13884:                 $newdef =~ s/\D//g;
13885:                 if ($newdef eq '' || $newdef < 1) {
13886:                     $newdef = 1;
13887:                 }
13888:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13889:             } else {
13890:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13891:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13892:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13893:                 }
13894:                 $newdef =~ s/[^\w.\-]//g;
13895:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13896:             }
13897:             if ($currdef ne $newdef) {
13898:                 if ($item eq 'anonsurvey_threshold') {
13899:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13900:                         $changes{$item} = 1;
13901:                     }
13902:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13903:                     my $setting = $1;
13904:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13905:                         $changes{$setting} = 1;
13906:                     }
13907:                 }
13908:             }
13909:         }
13910:         my $texengine;
13911:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
13912:             $texengine = $env{'form.texengine'};
13913:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
13914:             if ($currdef eq '') {
13915:                 unless ($texengine eq $Apache::lonnet::deftex) {
13916:                     $changes{'texengine'} = 1;
13917:                 }
13918:             } elsif ($currdef ne $texengine) {
13919:                 $changes{'texengine'} = 1;
13920:             }
13921:         }
13922:         if ($texengine ne '') {
13923:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
13924:         }
13925:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13926:         my @currclonecode;
13927:         if (ref($currclone) eq 'HASH') {
13928:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13929:                 @currclonecode = @{$currclone->{'instcode'}};
13930:             }
13931:         }
13932:         my $newclone;
13933:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13934:             $newclone = $env{'form.canclone'};
13935:         }
13936:         if ($newclone eq 'instcode') {
13937:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13938:             my (%codedefaults,@code_order,@clonecode);
13939:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13940:                                                     \@code_order);
13941:             foreach my $item (@code_order) {
13942:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13943:                     push(@clonecode,$item);
13944:                 }
13945:             }
13946:             if (@clonecode) {
13947:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13948:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13949:                 if (@diffs) {
13950:                     $changes{'canclone'} = 1;
13951:                 }
13952:             } else {
13953:                 $newclone eq '';
13954:             }
13955:         } elsif ($newclone ne '') {
13956:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13957:         }
13958:         if ($newclone ne $currclone) {
13959:             $changes{'canclone'} = 1;
13960:         }
13961:         my %credits;
13962:         foreach my $type (@types) {
13963:             unless ($type eq 'community') {
13964:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13965:                 $credits{$type} =~ s/[^\d.]+//g;
13966:             }
13967:         }
13968:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13969:             ($env{'form.coursecredits'} eq '1')) {
13970:             $changes{'coursecredits'} = 1;
13971:             foreach my $type (keys(%credits)) {
13972:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13973:             }
13974:         } else {
13975:             if ($env{'form.coursecredits'} eq '1') {
13976:                 foreach my $type (@types) {
13977:                     unless ($type eq 'community') {
13978:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13979:                             $changes{'coursecredits'} = 1;
13980:                         }
13981:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13982:                     }
13983:                 }
13984:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13985:                 foreach my $type (@types) {
13986:                     unless ($type eq 'community') {
13987:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13988:                             $changes{'coursecredits'} = 1;
13989:                             last;
13990:                         }
13991:                     }
13992:                 }
13993:             }
13994:         }
13995:         if ($env{'form.postsubmit'} eq '1') {
13996:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13997:             my %currtimeout;
13998:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13999:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
14000:                     $changes{'postsubmit'} = 1;
14001:                 }
14002:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
14003:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
14004:                 }
14005:             } else {
14006:                 $changes{'postsubmit'} = 1;
14007:             }
14008:             foreach my $type (@types) {
14009:                 my $timeout = $env{'form.'.$type.'_timeout'};
14010:                 $timeout =~ s/\D//g;
14011:                 if ($timeout == $staticdefaults{'postsubmit'}) {
14012:                     $timeout = '';
14013:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
14014:                     $timeout = '0';
14015:                 }
14016:                 unless ($timeout eq '') {
14017:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
14018:                 }
14019:                 if (exists($currtimeout{$type})) {
14020:                     if ($timeout ne $currtimeout{$type}) {
14021:                         $changes{'postsubmit'} = 1;
14022:                     }
14023:                 } elsif ($timeout ne '') {
14024:                     $changes{'postsubmit'} = 1;
14025:                 }
14026:             }
14027:         } else {
14028:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
14029:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14030:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
14031:                     $changes{'postsubmit'} = 1;
14032:                 }
14033:             } else {
14034:                 $changes{'postsubmit'} = 1;
14035:             }
14036:         }
14037:     }
14038:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14039:                                              $dom);
14040:     if ($putresult eq 'ok') {
14041:         if (keys(%changes) > 0) {
14042:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14043:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
14044:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
14045:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
14046:                 foreach my $item ('uselcmath','usejsme','texengine') {
14047:                     if ($changes{$item}) {
14048:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
14049:                     }
14050:                 }
14051:                 if ($changes{'coursecredits'}) {
14052:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
14053:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
14054:                             $domdefaults{$type.'credits'} =
14055:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
14056:                         }
14057:                     }
14058:                 }
14059:                 if ($changes{'postsubmit'}) {
14060:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14061:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
14062:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
14063:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
14064:                                 $domdefaults{$type.'postsubtimeout'} =
14065:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
14066:                             }
14067:                         }
14068:                     }
14069:                 }
14070:                 if ($changes{'uploadquota'}) {
14071:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
14072:                         foreach my $type (@types) {
14073:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
14074:                         }
14075:                     }
14076:                 }
14077:                 if ($changes{'canclone'}) {
14078:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
14079:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
14080:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
14081:                             if (@clonecodes) {
14082:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
14083:                             }
14084:                         }
14085:                     } else {
14086:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
14087:                     }
14088:                 }
14089:                 my $cachetime = 24*60*60;
14090:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14091:                 if (ref($lastactref) eq 'HASH') {
14092:                     $lastactref->{'domdefaults'} = 1;
14093:                 }
14094:             }
14095:             $resulttext = &mt('Changes made:').'<ul>';
14096:             foreach my $item (sort(keys(%changes))) {
14097:                 if ($item eq 'uselcmath') {
14098:                     if ($env{'form.'.$item} eq '1') {
14099:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
14100:                     } else {
14101:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
14102:                     }
14103:                 } elsif ($item eq 'usejsme') {
14104:                     if ($env{'form.'.$item} eq '1') {
14105:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
14106:                     } else {
14107:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
14108:                     }
14109:                 } elsif ($item eq 'texengine') {
14110:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
14111:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
14112:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
14113:                     }
14114:                 } elsif ($item eq 'anonsurvey_threshold') {
14115:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
14116:                 } elsif ($item eq 'uploadquota') {
14117:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
14118:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
14119:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
14120:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
14121:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
14122: 
14123:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
14124:                                        '</ul>'.
14125:                                        '</li>';
14126:                     } else {
14127:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
14128:                     }
14129:                 } elsif ($item eq 'mysqltables') {
14130:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
14131:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
14132:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
14133:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
14134:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
14135:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
14136:                                        '</ul>'.
14137:                                        '</li>';
14138:                     } else {
14139:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
14140:                     }
14141:                 } elsif ($item eq 'postsubmit') {
14142:                     if ($domdefaults{'postsubmit'} eq 'off') {
14143:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
14144:                     } else {
14145:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
14146:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14147:                             $resulttext .= &mt('durations:').'<ul>';
14148:                             foreach my $type (@types) {
14149:                                 $resulttext .= '<li>';
14150:                                 my $timeout;
14151:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
14152:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
14153:                                 }
14154:                                 my $display;
14155:                                 if ($timeout eq '0') {
14156:                                     $display = &mt('unlimited');
14157:                                 } elsif ($timeout eq '') {
14158:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
14159:                                 } else {
14160:                                     $display = &mt('[quant,_1,second]',$timeout);
14161:                                 }
14162:                                 if ($type eq 'community') {
14163:                                     $resulttext .= &mt('Communities');
14164:                                 } elsif ($type eq 'official') {
14165:                                     $resulttext .= &mt('Official courses');
14166:                                 } elsif ($type eq 'unofficial') {
14167:                                     $resulttext .= &mt('Unofficial courses');
14168:                                 } elsif ($type eq 'textbook') {
14169:                                     $resulttext .= &mt('Textbook courses');
14170:                                 }
14171:                                 $resulttext .= ' -- '.$display.'</li>';
14172:                             }
14173:                             $resulttext .= '</ul>';
14174:                         }
14175:                         $resulttext .= '</li>';
14176:                     }
14177:                 } elsif ($item eq 'coursecredits') {
14178:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
14179:                         if (($domdefaults{'officialcredits'} eq '') &&
14180:                             ($domdefaults{'unofficialcredits'} eq '') &&
14181:                             ($domdefaults{'textbookcredits'} eq '')) {
14182:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
14183:                         } else {
14184:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
14185:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
14186:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
14187:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
14188:                                            '</ul>'.
14189:                                            '</li>';
14190:                         }
14191:                     } else {
14192:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
14193:                     }
14194:                 } elsif ($item eq 'canclone') {
14195:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
14196:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
14197:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
14198:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
14199:                         }
14200:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
14201:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
14202:                     } else {
14203:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
14204:                     }
14205:                 }
14206:             }
14207:             $resulttext .= '</ul>';
14208:         } else {
14209:             $resulttext = &mt('No changes made to course defaults');
14210:         }
14211:     } else {
14212:         $resulttext = '<span class="LC_error">'.
14213:             &mt('An error occurred: [_1]',$putresult).'</span>';
14214:     }
14215:     return $resulttext;
14216: }
14217: 
14218: sub modify_selfenrollment {
14219:     my ($dom,$lastactref,%domconfig) = @_;
14220:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
14221:     my @types = ('official','unofficial','community','textbook');
14222:     my %titles = &tool_titles();
14223:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
14224:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
14225:     $ordered{'default'} = ['types','registered','approval','limit'];
14226: 
14227:     my (%roles,%shown,%toplevel);
14228:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
14229: 
14230:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
14231:         if ($domconfig{'selfenrollment'} eq '') {
14232:             $domconfig{'selfenrollment'} = {};
14233:         }
14234:     }
14235:     %toplevel = (
14236:                   admin      => 'Configuration Rights',
14237:                   default    => 'Default settings',
14238:                   validation => 'Validation of self-enrollment requests',
14239:                 );
14240:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
14241: 
14242:     if (ref($ordered{'admin'}) eq 'ARRAY') {
14243:         foreach my $item (@{$ordered{'admin'}}) {
14244:             foreach my $type (@types) {
14245:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
14246:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
14247:                 } else {
14248:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
14249:                 }
14250:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
14251:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
14252:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
14253:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
14254:                             push(@{$changes{'admin'}{$type}},$item);
14255:                         }
14256:                     } else {
14257:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
14258:                             push(@{$changes{'admin'}{$type}},$item);
14259:                         }
14260:                     }
14261:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
14262:                     push(@{$changes{'admin'}{$type}},$item);
14263:                 }
14264:             }
14265:         }
14266:     }
14267: 
14268:     foreach my $item (@{$ordered{'default'}}) {
14269:         foreach my $type (@types) {
14270:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
14271:             if ($item eq 'types') {
14272:                 unless (($value eq 'all') || ($value eq 'dom')) {
14273:                     $value = '';
14274:                 }
14275:             } elsif ($item eq 'registered') {
14276:                 unless ($value eq '1') {
14277:                     $value = 0;
14278:                 }
14279:             } elsif ($item eq 'approval') {
14280:                 unless ($value =~ /^[012]$/) {
14281:                     $value = 0;
14282:                 }
14283:             } else {
14284:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14285:                     $value = 'none';
14286:                 }
14287:             }
14288:             $selfenrollhash{'default'}{$type}{$item} = $value;
14289:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
14290:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14291:                     if ($selfenrollhash{'default'}{$type}{$item} ne
14292:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
14293:                          push(@{$changes{'default'}{$type}},$item);
14294:                     }
14295:                 } else {
14296:                     push(@{$changes{'default'}{$type}},$item);
14297:                 }
14298:             } else {
14299:                 push(@{$changes{'default'}{$type}},$item);
14300:             }
14301:             if ($item eq 'limit') {
14302:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14303:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
14304:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
14305:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
14306:                     }
14307:                 } else {
14308:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
14309:                 }
14310:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14311:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
14312:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
14313:                          push(@{$changes{'default'}{$type}},'cap');
14314:                     }
14315:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
14316:                     push(@{$changes{'default'}{$type}},'cap');
14317:                 }
14318:             }
14319:         }
14320:     }
14321: 
14322:     foreach my $item (@{$itemsref}) {
14323:         if ($item eq 'fields') {
14324:             my @changed;
14325:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
14326:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
14327:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
14328:             }
14329:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14330:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
14331:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
14332:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
14333:                 } else {
14334:                     @changed = @{$selfenrollhash{'validation'}{$item}};
14335:                 }
14336:             } else {
14337:                 @changed = @{$selfenrollhash{'validation'}{$item}};
14338:             }
14339:             if (@changed) {
14340:                 if ($selfenrollhash{'validation'}{$item}) { 
14341:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
14342:                 } else {
14343:                     $changes{'validation'}{$item} = &mt('None');
14344:                 }
14345:             }
14346:         } else {
14347:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
14348:             if ($item eq 'markup') {
14349:                if ($env{'form.selfenroll_validation_'.$item}) {
14350:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14351:                }
14352:             }
14353:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14354:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
14355:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
14356:                 }
14357:             }
14358:         }
14359:     }
14360: 
14361:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
14362:                                              $dom);
14363:     if ($putresult eq 'ok') {
14364:         if (keys(%changes) > 0) {
14365:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14366:             $resulttext = &mt('Changes made:').'<ul>';
14367:             foreach my $key ('admin','default','validation') {
14368:                 if (ref($changes{$key}) eq 'HASH') {
14369:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
14370:                     if ($key eq 'validation') {
14371:                         foreach my $item (@{$itemsref}) {
14372:                             if (exists($changes{$key}{$item})) {
14373:                                 if ($item eq 'markup') {
14374:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14375:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
14376:                                 } else {  
14377:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14378:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
14379:                                 }
14380:                             }
14381:                         }
14382:                     } else {
14383:                         foreach my $type (@types) {
14384:                             if ($type eq 'community') {
14385:                                 $roles{'1'} = &mt('Community personnel');
14386:                             } else {
14387:                                 $roles{'1'} = &mt('Course personnel');
14388:                             }
14389:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
14390:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
14391:                                     if ($key eq 'admin') {
14392:                                         my @mgrdc = ();
14393:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14394:                                             foreach my $item (@{$ordered{'admin'}}) {
14395:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
14396:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
14397:                                                         push(@mgrdc,$item);
14398:                                                     }
14399:                                                 }
14400:                                             }
14401:                                             if (@mgrdc) {
14402:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
14403:                                             } else {
14404:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
14405:                                             }
14406:                                         }
14407:                                     } else {
14408:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14409:                                             foreach my $item (@{$ordered{$key}}) {
14410:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14411:                                                     $domdefaults{$type.'selfenroll'.$item} =
14412:                                                         $selfenrollhash{$key}{$type}{$item};
14413:                                                 }
14414:                                             }
14415:                                         }
14416:                                     }
14417:                                 }
14418:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
14419:                                 foreach my $item (@{$ordered{$key}}) {
14420:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14421:                                         $resulttext .= '<li>';
14422:                                         if ($key eq 'admin') {
14423:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
14424:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
14425:                                         } else {
14426:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
14427:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
14428:                                         }
14429:                                         $resulttext .= '</li>';
14430:                                     }
14431:                                 }
14432:                                 $resulttext .= '</ul></li>';
14433:                             }
14434:                         }
14435:                         $resulttext .= '</ul></li>'; 
14436:                     }
14437:                 }
14438:             }
14439:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
14440:                 my $cachetime = 24*60*60;
14441:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14442:                 if (ref($lastactref) eq 'HASH') {
14443:                     $lastactref->{'domdefaults'} = 1;
14444:                 }
14445:             }
14446:             $resulttext .= '</ul>';
14447:         } else {
14448:             $resulttext = &mt('No changes made to self-enrollment settings');
14449:         }
14450:     } else {
14451:         $resulttext = '<span class="LC_error">'.
14452:             &mt('An error occurred: [_1]',$putresult).'</span>';
14453:     }
14454:     return $resulttext;
14455: }
14456: 
14457: sub modify_usersessions {
14458:     my ($dom,$lastactref,%domconfig) = @_;
14459:     my @hostingtypes = ('version','excludedomain','includedomain');
14460:     my @offloadtypes = ('primary','default');
14461:     my %types = (
14462:                   remote => \@hostingtypes,
14463:                   hosted => \@hostingtypes,
14464:                   spares => \@offloadtypes,
14465:                 );
14466:     my @prefixes = ('remote','hosted','spares');
14467:     my @lcversions = &Apache::lonnet::all_loncaparevs();
14468:     my (%by_ip,%by_location,@intdoms);
14469:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
14470:     my @locations = sort(keys(%by_location));
14471:     my (%defaultshash,%changes);
14472:     foreach my $prefix (@prefixes) {
14473:         $defaultshash{'usersessions'}{$prefix} = {};
14474:     }
14475:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14476:     my $resulttext;
14477:     my %iphost = &Apache::lonnet::get_iphost();
14478:     foreach my $prefix (@prefixes) {
14479:         next if ($prefix eq 'spares');
14480:         foreach my $type (@{$types{$prefix}}) {
14481:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14482:             if ($type eq 'version') {
14483:                 my $value = $env{'form.'.$prefix.'_'.$type};
14484:                 my $okvalue;
14485:                 if ($value ne '') {
14486:                     if (grep(/^\Q$value\E$/,@lcversions)) {
14487:                         $okvalue = $value;
14488:                     }
14489:                 }
14490:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14491:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14492:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
14493:                             if ($inuse == 0) {
14494:                                 $changes{$prefix}{$type} = 1;
14495:                             } else {
14496:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
14497:                                     $changes{$prefix}{$type} = 1;
14498:                                 }
14499:                                 if ($okvalue ne '') {
14500:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14501:                                 } 
14502:                             }
14503:                         } else {
14504:                             if (($inuse == 1) && ($okvalue ne '')) {
14505:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14506:                                 $changes{$prefix}{$type} = 1;
14507:                             }
14508:                         }
14509:                     } else {
14510:                         if (($inuse == 1) && ($okvalue ne '')) {
14511:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14512:                             $changes{$prefix}{$type} = 1;
14513:                         }
14514:                     }
14515:                 } else {
14516:                     if (($inuse == 1) && ($okvalue ne '')) {
14517:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14518:                         $changes{$prefix}{$type} = 1;
14519:                     }
14520:                 }
14521:             } else {
14522:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14523:                 my @okvals;
14524:                 foreach my $val (@vals) {
14525:                     if ($val =~ /:/) {
14526:                         my @items = split(/:/,$val);
14527:                         foreach my $item (@items) {
14528:                             if (ref($by_location{$item}) eq 'ARRAY') {
14529:                                 push(@okvals,$item);
14530:                             }
14531:                         }
14532:                     } else {
14533:                         if (ref($by_location{$val}) eq 'ARRAY') {
14534:                             push(@okvals,$val);
14535:                         }
14536:                     }
14537:                 }
14538:                 @okvals = sort(@okvals);
14539:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14540:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14541:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14542:                             if ($inuse == 0) {
14543:                                 $changes{$prefix}{$type} = 1; 
14544:                             } else {
14545:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14546:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
14547:                                 if (@changed > 0) {
14548:                                     $changes{$prefix}{$type} = 1;
14549:                                 }
14550:                             }
14551:                         } else {
14552:                             if ($inuse == 1) {
14553:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14554:                                 $changes{$prefix}{$type} = 1;
14555:                             }
14556:                         } 
14557:                     } else {
14558:                         if ($inuse == 1) {
14559:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14560:                             $changes{$prefix}{$type} = 1;
14561:                         }
14562:                     }
14563:                 } else {
14564:                     if ($inuse == 1) {
14565:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14566:                         $changes{$prefix}{$type} = 1;
14567:                     }
14568:                 }
14569:             }
14570:         }
14571:     }
14572: 
14573:     my @alldoms = &Apache::lonnet::all_domains();
14574:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14575:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
14576:     my $savespares;
14577: 
14578:     foreach my $lonhost (sort(keys(%servers))) {
14579:         my $serverhomeID =
14580:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
14581:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
14582:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
14583:         my %spareschg;
14584:         foreach my $type (@{$types{'spares'}}) {
14585:             my @okspares;
14586:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
14587:             foreach my $server (@checked) {
14588:                 if (&Apache::lonnet::hostname($server) ne '') {
14589:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
14590:                         unless (grep(/^\Q$server\E$/,@okspares)) {
14591:                             push(@okspares,$server);
14592:                         }
14593:                     }
14594:                 }
14595:             }
14596:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
14597:             my $newspare;
14598:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
14599:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
14600:                     $newspare = $new;
14601:                 }
14602:             }
14603:             my @spares;
14604:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
14605:                 @spares = sort(@okspares,$newspare);
14606:             } else {
14607:                 @spares = sort(@okspares);
14608:             }
14609:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
14610:             if (ref($spareid{$lonhost}) eq 'HASH') {
14611:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
14612:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
14613:                     if (@diffs > 0) {
14614:                         $spareschg{$type} = 1;
14615:                     }
14616:                 }
14617:             }
14618:         }
14619:         if (keys(%spareschg) > 0) {
14620:             $changes{'spares'}{$lonhost} = \%spareschg;
14621:         }
14622:     }
14623:     $defaultshash{'usersessions'}{'offloadnow'} = {};
14624:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
14625:     my @okoffload;
14626:     if (@offloadnow) {
14627:         foreach my $server (@offloadnow) {
14628:             if (&Apache::lonnet::hostname($server) ne '') {
14629:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
14630:                     push(@okoffload,$server);
14631:                 }
14632:             }
14633:         }
14634:         if (@okoffload) {
14635:             foreach my $lonhost (@okoffload) {
14636:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
14637:             }
14638:         }
14639:     }
14640:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
14641:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
14642:             if (ref($changes{'spares'}) eq 'HASH') {
14643:                 if (keys(%{$changes{'spares'}}) > 0) {
14644:                     $savespares = 1;
14645:                 }
14646:             }
14647:         } else {
14648:             $savespares = 1;
14649:         }
14650:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
14651:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
14652:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
14653:                     $changes{'offloadnow'} = 1;
14654:                     last;
14655:                 }
14656:             }
14657:             unless ($changes{'offloadnow'}) {
14658:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
14659:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
14660:                         $changes{'offloadnow'} = 1;
14661:                         last;
14662:                     }
14663:                 }
14664:             }
14665:         } elsif (@okoffload) {
14666:             $changes{'offloadnow'} = 1;
14667:         }
14668:     } elsif (@okoffload) {
14669:         $changes{'offloadnow'} = 1;
14670:     }
14671:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
14672:     if ((keys(%changes) > 0) || ($savespares)) {
14673:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14674:                                                  $dom);
14675:         if ($putresult eq 'ok') {
14676:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14677:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
14678:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
14679:                 }
14680:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
14681:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
14682:                 }
14683:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14684:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
14685:                 }
14686:             }
14687:             my $cachetime = 24*60*60;
14688:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14689:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
14690:             if (ref($lastactref) eq 'HASH') {
14691:                 $lastactref->{'domdefaults'} = 1;
14692:                 $lastactref->{'usersessions'} = 1;
14693:             }
14694:             if (keys(%changes) > 0) {
14695:                 my %lt = &usersession_titles();
14696:                 $resulttext = &mt('Changes made:').'<ul>';
14697:                 foreach my $prefix (@prefixes) {
14698:                     if (ref($changes{$prefix}) eq 'HASH') {
14699:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14700:                         if ($prefix eq 'spares') {
14701:                             if (ref($changes{$prefix}) eq 'HASH') {
14702:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
14703:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
14704:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
14705:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
14706:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14707:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
14708:                                         foreach my $type (@{$types{$prefix}}) {
14709:                                             if ($changes{$prefix}{$lonhost}{$type}) {
14710:                                                 my $offloadto = &mt('None');
14711:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
14712:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
14713:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
14714:                                                     }
14715:                                                 }
14716:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
14717:                                             }
14718:                                         }
14719:                                     }
14720:                                     $resulttext .= '</li>';
14721:                                 }
14722:                             }
14723:                         } else {
14724:                             foreach my $type (@{$types{$prefix}}) {
14725:                                 if (defined($changes{$prefix}{$type})) {
14726:                                     my $newvalue;
14727:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14728:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
14729:                                             if ($type eq 'version') {
14730:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
14731:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14732:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
14733:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
14734:                                                 }
14735:                                             }
14736:                                         }
14737:                                     }
14738:                                     if ($newvalue eq '') {
14739:                                         if ($type eq 'version') {
14740:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
14741:                                         } else {
14742:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14743:                                         }
14744:                                     } else {
14745:                                         if ($type eq 'version') {
14746:                                             $newvalue .= ' '.&mt('(or later)'); 
14747:                                         }
14748:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14749:                                     }
14750:                                 }
14751:                             }
14752:                         }
14753:                         $resulttext .= '</ul>';
14754:                     }
14755:                 }
14756:                 if ($changes{'offloadnow'}) {
14757:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14758:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
14759:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
14760:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
14761:                                 $resulttext .= '<li>'.$lonhost.'</li>';
14762:                             }
14763:                             $resulttext .= '</ul>';
14764:                         } else {
14765:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
14766:                         }
14767:                     } else {
14768:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
14769:                     }
14770:                 }
14771:                 $resulttext .= '</ul>';
14772:             } else {
14773:                 $resulttext = $nochgmsg;
14774:             }
14775:         } else {
14776:             $resulttext = '<span class="LC_error">'.
14777:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14778:         }
14779:     } else {
14780:         $resulttext = $nochgmsg;
14781:     }
14782:     return $resulttext;
14783: }
14784: 
14785: sub modify_loadbalancing {
14786:     my ($dom,%domconfig) = @_;
14787:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
14788:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
14789:     my ($othertitle,$usertypes,$types) =
14790:         &Apache::loncommon::sorted_inst_types($dom);
14791:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14792:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
14793:     my @sparestypes = ('primary','default');
14794:     my %typetitles = &sparestype_titles();
14795:     my $resulttext;
14796:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
14797:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14798:         %existing = %{$domconfig{'loadbalancing'}};
14799:     }
14800:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
14801:                               \%currtargets,\%currrules,\%currcookies);
14802:     my ($saveloadbalancing,%defaultshash,%changes);
14803:     my ($alltypes,$othertypes,$titles) =
14804:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
14805:     my %ruletitles = &offloadtype_text();
14806:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
14807:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
14808:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
14809:         if ($balancer eq '') {
14810:             next;
14811:         }
14812:         if (!exists($servers{$balancer})) {
14813:             if (exists($currbalancer{$balancer})) {
14814:                 push(@{$changes{'delete'}},$balancer);
14815:             }
14816:             next;
14817:         }
14818:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14819:             push(@{$changes{'delete'}},$balancer);
14820:             next;
14821:         }
14822:         if (!exists($currbalancer{$balancer})) {
14823:             push(@{$changes{'add'}},$balancer);
14824:         }
14825:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14826:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14827:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14828:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14829:             $saveloadbalancing = 1;
14830:         }
14831:         foreach my $sparetype (@sparestypes) {
14832:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14833:             my @offloadto;
14834:             foreach my $target (@targets) {
14835:                 if (($servers{$target}) && ($target ne $balancer)) {
14836:                     if ($sparetype eq 'default') {
14837:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14838:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14839:                         }
14840:                     }
14841:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14842:                         push(@offloadto,$target);
14843:                     }
14844:                 }
14845:             }
14846:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14847:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14848:                     push(@offloadto,$balancer);
14849:                 }
14850:             }
14851:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14852:         }
14853:         if ($env{'form.loadbalancing_cookie_'.$i}) {
14854:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
14855:             if (exists($currbalancer{$balancer})) {
14856:                 unless ($currcookies{$balancer}) {
14857:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
14858:                 }
14859:             }
14860:         } elsif (exists($currbalancer{$balancer})) {
14861:             if ($currcookies{$balancer}) {
14862:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
14863:             }
14864:         }
14865:         if (ref($currtargets{$balancer}) eq 'HASH') {
14866:             foreach my $sparetype (@sparestypes) {
14867:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14868:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14869:                     if (@targetdiffs > 0) {
14870:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14871:                     }
14872:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14873:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14874:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14875:                     }
14876:                 }
14877:             }
14878:         } else {
14879:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14880:                 foreach my $sparetype (@sparestypes) {
14881:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14882:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14883:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14884:                         }
14885:                     }
14886:                 }
14887:             }
14888:         }
14889:         my $ishomedom;
14890:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14891:             $ishomedom = 1;
14892:         }
14893:         if (ref($alltypes) eq 'ARRAY') {
14894:             foreach my $type (@{$alltypes}) {
14895:                 my $rule;
14896:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14897:                          (!$ishomedom)) {
14898:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14899:                 }
14900:                 if ($rule eq 'specific') {
14901:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14902:                     if (exists($servers{$specifiedhost})) {
14903:                         $rule = $specifiedhost;
14904:                     }
14905:                 }
14906:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14907:                 if (ref($currrules{$balancer}) eq 'HASH') {
14908:                     if ($rule ne $currrules{$balancer}{$type}) {
14909:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14910:                     }
14911:                 } elsif ($rule ne '') {
14912:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14913:                 }
14914:             }
14915:         }
14916:     }
14917:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14918:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14919:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14920:             $defaultshash{'loadbalancing'} = {};
14921:         }
14922:         my $putresult = &Apache::lonnet::put_dom('configuration',
14923:                                                  \%defaultshash,$dom);
14924:         if ($putresult eq 'ok') {
14925:             if (keys(%changes) > 0) {
14926:                 my %toupdate;
14927:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14928:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14929:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14930:                         $toupdate{$balancer} = 1;
14931:                     }
14932:                 }
14933:                 if (ref($changes{'add'}) eq 'ARRAY') {
14934:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14935:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14936:                         $toupdate{$balancer} = 1;
14937:                     }
14938:                 }
14939:                 if (ref($changes{'curr'}) eq 'HASH') {
14940:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14941:                         $toupdate{$balancer} = 1;
14942:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14943:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14944:                                 my %offloadstr;
14945:                                 foreach my $sparetype (@sparestypes) {
14946:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14947:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14948:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14949:                                         }
14950:                                     }
14951:                                 }
14952:                                 if (keys(%offloadstr) == 0) {
14953:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14954:                                 } else {
14955:                                     my $showoffload;
14956:                                     foreach my $sparetype (@sparestypes) {
14957:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14958:                                         if (defined($offloadstr{$sparetype})) {
14959:                                             $showoffload .= $offloadstr{$sparetype};
14960:                                         } else {
14961:                                             $showoffload .= &mt('None');
14962:                                         }
14963:                                         $showoffload .= ('&nbsp;'x3);
14964:                                     }
14965:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14966:                                 }
14967:                             }
14968:                         }
14969:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14970:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14971:                                 foreach my $type (@{$alltypes}) {
14972:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14973:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14974:                                         my $balancetext;
14975:                                         if ($rule eq '') {
14976:                                             $balancetext =  $ruletitles{'default'};
14977:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14978:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14979:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14980:                                                 foreach my $sparetype (@sparestypes) {
14981:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14982:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14983:                                                     }
14984:                                                 }
14985:                                                 foreach my $item (@{$alltypes}) {
14986:                                                     next if ($item =~  /^_LC_ipchange/);
14987:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14988:                                                     if ($hasrule eq 'homeserver') {
14989:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14990:                                                     } else {
14991:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14992:                                                             if ($servers{$hasrule}) {
14993:                                                                 $toupdate{$hasrule} = 1;
14994:                                                             }
14995:                                                         }
14996:                                                     }
14997:                                                 }
14998:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14999:                                                     $balancetext =  $ruletitles{$rule};
15000:                                                 } else {
15001:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
15002:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
15003:                                                     if ($receiver) {
15004:                                                         $toupdate{$receiver};
15005:                                                     }
15006:                                                 }
15007:                                             } else {
15008:                                                 $balancetext =  $ruletitles{$rule};
15009:                                             }
15010:                                         } else {
15011:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
15012:                                         }
15013:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
15014:                                     }
15015:                                 }
15016:                             }
15017:                         }
15018:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
15019:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
15020:                                                       $balancer).'</li>';
15021:                         }
15022:                         if (keys(%toupdate)) {
15023:                             my %thismachine;
15024:                             my $updatedhere;
15025:                             my $cachetime = 60*60*24;
15026:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15027:                             foreach my $lonhost (keys(%toupdate)) {
15028:                                 if ($thismachine{$lonhost}) {
15029:                                     unless ($updatedhere) {
15030:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
15031:                                                                       $defaultshash{'loadbalancing'},
15032:                                                                       $cachetime);
15033:                                         $updatedhere = 1;
15034:                                     }
15035:                                 } else {
15036:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
15037:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
15038:                                 }
15039:                             }
15040:                         }
15041:                     }
15042:                 }
15043:                 if ($resulttext ne '') {
15044:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
15045:                 } else {
15046:                     $resulttext = $nochgmsg;
15047:                 }
15048:             } else {
15049:                 $resulttext = $nochgmsg;
15050:             }
15051:         } else {
15052:             $resulttext = '<span class="LC_error">'.
15053:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15054:         }
15055:     } else {
15056:         $resulttext = $nochgmsg;
15057:     }
15058:     return $resulttext;
15059: }
15060: 
15061: sub recurse_check {
15062:     my ($chkcats,$categories,$depth,$name) = @_;
15063:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
15064:         my $chg = 0;
15065:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
15066:             my $category = $chkcats->[$depth]{$name}[$j];
15067:             my $item;
15068:             if ($category eq '') {
15069:                 $chg ++;
15070:             } else {
15071:                 my $deeper = $depth + 1;
15072:                 $item = &escape($category).':'.&escape($name).':'.$depth;
15073:                 if ($chg) {
15074:                     $categories->{$item} -= $chg;
15075:                 }
15076:                 &recurse_check($chkcats,$categories,$deeper,$category);
15077:                 $deeper --;
15078:             }
15079:         }
15080:     }
15081:     return;
15082: }
15083: 
15084: sub recurse_cat_deletes {
15085:     my ($item,$coursecategories,$deletions) = @_;
15086:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
15087:     my $subdepth = $depth + 1;
15088:     if (ref($coursecategories) eq 'HASH') {
15089:         foreach my $subitem (keys(%{$coursecategories})) {
15090:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
15091:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
15092:                 delete($coursecategories->{$subitem});
15093:                 $deletions->{$subitem} = 1;
15094:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
15095:             }
15096:         }
15097:     }
15098:     return;
15099: }
15100: 
15101: sub active_dc_picker {
15102:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
15103:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
15104:     my @domcoord = keys(%domcoords);
15105:     if (keys(%currhash)) {
15106:         foreach my $dc (keys(%currhash)) {
15107:             unless (exists($domcoords{$dc})) {
15108:                 push(@domcoord,$dc);
15109:             }
15110:         }
15111:     }
15112:     @domcoord = sort(@domcoord);
15113:     my $numdcs = scalar(@domcoord);
15114:     my $rows = 0;
15115:     my $table;
15116:     if ($numdcs > 1) {
15117:         $table = '<table>';
15118:         for (my $i=0; $i<@domcoord; $i++) {
15119:             my $rem = $i%($numinrow);
15120:             if ($rem == 0) {
15121:                 if ($i > 0) {
15122:                     $table .= '</tr>';
15123:                 }
15124:                 $table .= '<tr>';
15125:                 $rows ++;
15126:             }
15127:             my $check = '';
15128:             if ($inputtype eq 'radio') {
15129:                 if (keys(%currhash) == 0) {
15130:                     if (!$i) {
15131:                         $check = ' checked="checked"';
15132:                     }
15133:                 } elsif (exists($currhash{$domcoord[$i]})) {
15134:                     $check = ' checked="checked"';
15135:                 }
15136:             } else {
15137:                 if (exists($currhash{$domcoord[$i]})) {
15138:                     $check = ' checked="checked"';
15139:                 }
15140:             }
15141:             if ($i == @domcoord - 1) {
15142:                 my $colsleft = $numinrow - $rem;
15143:                 if ($colsleft > 1) {
15144:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
15145:                 } else {
15146:                     $table .= '<td class="LC_left_item">';
15147:                 }
15148:             } else {
15149:                 $table .= '<td class="LC_left_item">';
15150:             }
15151:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
15152:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15153:             $table .= '<span class="LC_nobreak"><label>'.
15154:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
15155:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
15156:             if ($user ne $dcname.':'.$dcdom) {
15157:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15158:             }
15159:             $table .= '</label></span></td>';
15160:         }
15161:         $table .= '</tr></table>';
15162:     } elsif ($numdcs == 1) {
15163:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
15164:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15165:         if ($inputtype eq 'radio') {
15166:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
15167:             if ($user ne $dcname.':'.$dcdom) {
15168:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15169:             }
15170:         } else {
15171:             my $check;
15172:             if (exists($currhash{$domcoord[0]})) {
15173:                 $check = ' checked="checked"';
15174:             }
15175:             $table = '<span class="LC_nobreak"><label>'.
15176:                      '<input type="checkbox" name="'.$name.'" '.
15177:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
15178:             if ($user ne $dcname.':'.$dcdom) {
15179:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15180:             }
15181:             $table .= '</label></span>';
15182:             $rows ++;
15183:         }
15184:     }
15185:     return ($numdcs,$table,$rows);
15186: }
15187: 
15188: sub usersession_titles {
15189:     return &Apache::lonlocal::texthash(
15190:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
15191:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
15192:                spares => 'Servers offloaded to, when busy',
15193:                version => 'LON-CAPA version requirement',
15194:                excludedomain => 'Allow all, but exclude specific domains',
15195:                includedomain => 'Deny all, but include specific domains',
15196:                primary => 'Primary (checked first)',
15197:                default => 'Default',
15198:            );
15199: }
15200: 
15201: sub id_for_thisdom {
15202:     my (%servers) = @_;
15203:     my %altids;
15204:     foreach my $server (keys(%servers)) {
15205:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
15206:         if ($serverhome ne $server) {
15207:             $altids{$serverhome} = $server;
15208:         }
15209:     }
15210:     return %altids;
15211: }
15212: 
15213: sub count_servers {
15214:     my ($currbalancer,%servers) = @_;
15215:     my (@spares,$numspares);
15216:     foreach my $lonhost (sort(keys(%servers))) {
15217:         next if ($currbalancer eq $lonhost);
15218:         push(@spares,$lonhost);
15219:     }
15220:     if ($currbalancer) {
15221:         $numspares = scalar(@spares);
15222:     } else {
15223:         $numspares = scalar(@spares) - 1;
15224:     }
15225:     return ($numspares,@spares);
15226: }
15227: 
15228: sub lonbalance_targets_js {
15229:     my ($dom,$types,$servers,$settings) = @_;
15230:     my $select = &mt('Select');
15231:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
15232:     if (ref($servers) eq 'HASH') {
15233:         $alltargets = join("','",sort(keys(%{$servers})));
15234:         my @homedoms;
15235:         foreach my $server (sort(keys(%{$servers}))) {
15236:             if (&Apache::lonnet::host_domain($server) eq $dom) {
15237:                 push(@homedoms,'1');
15238:             } else {
15239:                 push(@homedoms,'0');
15240:             }
15241:         }
15242:         $allishome = join("','",@homedoms);
15243:     }
15244:     if (ref($types) eq 'ARRAY') {
15245:         if (@{$types} > 0) {
15246:             @alltypes = @{$types};
15247:         }
15248:     }
15249:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
15250:     $allinsttypes = join("','",@alltypes);
15251:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
15252:     if (ref($settings) eq 'HASH') {
15253:         %existing = %{$settings};
15254:     }
15255:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
15256:                               \%currtargets,\%currrules,\%currcookies);
15257:     my $balancers = join("','",sort(keys(%currbalancer)));
15258:     return <<"END";
15259: 
15260: <script type="text/javascript">
15261: // <![CDATA[
15262: 
15263: currBalancers = new Array('$balancers');
15264: 
15265: function toggleTargets(balnum) {
15266:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15267:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
15268:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
15269:     var prevbalancer = prevhostitem.value;
15270:     var baltotal = document.getElementById('loadbalancing_total').value;
15271:     prevhostitem.value = balancer;
15272:     if (prevbalancer != '') {
15273:         var prevIdx = currBalancers.indexOf(prevbalancer);
15274:         if (prevIdx != -1) {
15275:             currBalancers.splice(prevIdx,1);
15276:         }
15277:     }
15278:     if (balancer == '') {
15279:         hideSpares(balnum);
15280:     } else {
15281:         var currIdx = currBalancers.indexOf(balancer);
15282:         if (currIdx == -1) {
15283:             currBalancers.push(balancer);
15284:         }
15285:         var homedoms = new Array('$allishome');
15286:         var ishomedom = homedoms[lonhostitem.selectedIndex];
15287:         showSpares(balancer,ishomedom,balnum);
15288:     }
15289:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
15290:     return;
15291: }
15292: 
15293: function showSpares(balancer,ishomedom,balnum) {
15294:     var alltargets = new Array('$alltargets');
15295:     var insttypes = new Array('$allinsttypes');
15296:     var offloadtypes = new Array('primary','default');
15297: 
15298:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
15299:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
15300:  
15301:     for (var i=0; i<offloadtypes.length; i++) {
15302:         var count = 0;
15303:         for (var j=0; j<alltargets.length; j++) {
15304:             if (alltargets[j] != balancer) {
15305:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
15306:                 item.value = alltargets[j];
15307:                 item.style.textAlign='left';
15308:                 item.style.textFace='normal';
15309:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
15310:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
15311:                     item.disabled = '';
15312:                 } else {
15313:                     item.disabled = 'disabled';
15314:                     item.checked = false;
15315:                 }
15316:                 count ++;
15317:             }
15318:         }
15319:     }
15320:     for (var k=0; k<insttypes.length; k++) {
15321:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
15322:             if (ishomedom == 1) {
15323:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15324:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15325:             } else {
15326:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15327:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15328:             }
15329:         } else {
15330:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15331:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15332:         }
15333:         if ((insttypes[k] != '_LC_external') && 
15334:             ((insttypes[k] != '_LC_internetdom') ||
15335:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
15336:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
15337:             item.options.length = 0;
15338:             item.options[0] = new Option("","",true,true);
15339:             var idx = 0;
15340:             for (var m=0; m<alltargets.length; m++) {
15341:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
15342:                     idx ++;
15343:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15344:                 }
15345:             }
15346:         }
15347:     }
15348:     return;
15349: }
15350: 
15351: function hideSpares(balnum) {
15352:     var alltargets = new Array('$alltargets');
15353:     var insttypes = new Array('$allinsttypes');
15354:     var offloadtypes = new Array('primary','default');
15355: 
15356:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
15357:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
15358: 
15359:     var total = alltargets.length - 1;
15360:     for (var i=0; i<offloadtypes; i++) {
15361:         for (var j=0; j<total; j++) {
15362:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
15363:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
15364:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
15365:         }
15366:     }
15367:     for (var k=0; k<insttypes.length; k++) {
15368:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15369:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15370:         if (insttypes[k] != '_LC_external') {
15371:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
15372:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
15373:         }
15374:     }
15375:     return;
15376: }
15377: 
15378: function checkOffloads(item,balnum,type) {
15379:     var alltargets = new Array('$alltargets');
15380:     var offloadtypes = new Array('primary','default');
15381:     if (item.checked) {
15382:         var total = alltargets.length - 1;
15383:         var other;
15384:         if (type == offloadtypes[0]) {
15385:             other = offloadtypes[1];
15386:         } else {
15387:             other = offloadtypes[0];
15388:         }
15389:         for (var i=0; i<total; i++) {
15390:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
15391:             if (server == item.value) {
15392:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
15393:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
15394:                 }
15395:             }
15396:         }
15397:     }
15398:     return;
15399: }
15400: 
15401: function singleServerToggle(balnum,type) {
15402:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
15403:     if (offloadtoSelIdx == 0) {
15404:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
15405:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15406: 
15407:     } else {
15408:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
15409:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15410:     }
15411:     return;
15412: }
15413: 
15414: function balanceruleChange(formname,balnum,type) {
15415:     if (type == '_LC_external') {
15416:         return;
15417:     }
15418:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
15419:     for (var i=0; i<typesRules.length; i++) {
15420:         if (formname.elements[typesRules[i]].checked) {
15421:             if (formname.elements[typesRules[i]].value != 'specific') {
15422:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
15423:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15424:             } else {
15425:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15426:             }
15427:         }
15428:     }
15429:     return;
15430: }
15431: 
15432: function balancerDeleteChange(balnum) {
15433:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15434:     var baltotal = document.getElementById('loadbalancing_total').value;
15435:     var addtarget;
15436:     var removetarget;
15437:     var action = 'delete';
15438:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
15439:         var lonhost = hostitem.value;
15440:         var currIdx = currBalancers.indexOf(lonhost);
15441:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
15442:             if (currIdx != -1) {
15443:                 currBalancers.splice(currIdx,1);
15444:             }
15445:             addtarget = lonhost;
15446:         } else {
15447:             if (currIdx == -1) {
15448:                 currBalancers.push(lonhost);
15449:             }
15450:             removetarget = lonhost;
15451:             action = 'undelete';
15452:         }
15453:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
15454:     }
15455:     return;
15456: }
15457: 
15458: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
15459:     if (baltotal > 1) {
15460:         var offloadtypes = new Array('primary','default');
15461:         var alltargets = new Array('$alltargets');
15462:         var insttypes = new Array('$allinsttypes');
15463:         for (var i=0; i<baltotal; i++) {
15464:             if (i != balnum) {
15465:                 for (var j=0; j<offloadtypes.length; j++) {
15466:                     var total = alltargets.length - 1;
15467:                     for (var k=0; k<total; k++) {
15468:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
15469:                         var server = serveritem.value;
15470:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
15471:                             if (server == addtarget) {
15472:                                 serveritem.disabled = '';
15473:                             }
15474:                         }
15475:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15476:                             if (server == removetarget) {
15477:                                 serveritem.disabled = 'disabled';
15478:                                 serveritem.checked = false;
15479:                             }
15480:                         }
15481:                     }
15482:                 }
15483:                 for (var j=0; j<insttypes.length; j++) {
15484:                     if (insttypes[j] != '_LC_external') {
15485:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
15486:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
15487:                             var currSel = singleserver.selectedIndex;
15488:                             var currVal = singleserver.options[currSel].value;
15489:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
15490:                                 var numoptions = singleserver.options.length;
15491:                                 var needsnew = 1;
15492:                                 for (var k=0; k<numoptions; k++) {
15493:                                     if (singleserver.options[k] == addtarget) {
15494:                                         needsnew = 0;
15495:                                         break;
15496:                                     }
15497:                                 }
15498:                                 if (needsnew == 1) {
15499:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
15500:                                 }
15501:                             }
15502:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15503:                                 singleserver.options.length = 0;
15504:                                 if ((currVal) && (currVal != removetarget)) {
15505:                                     singleserver.options[0] = new Option("","",false,false);
15506:                                 } else {
15507:                                     singleserver.options[0] = new Option("","",true,true);
15508:                                 }
15509:                                 var idx = 0;
15510:                                 for (var m=0; m<alltargets.length; m++) {
15511:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
15512:                                         idx ++;
15513:                                         if (currVal == alltargets[m]) {
15514:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
15515:                                         } else {
15516:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15517:                                         }
15518:                                     }
15519:                                 }
15520:                             }
15521:                         }
15522:                     }
15523:                 }
15524:             }
15525:         }
15526:     }
15527:     return;
15528: }
15529: 
15530: // ]]>
15531: </script>
15532: 
15533: END
15534: }
15535: 
15536: sub new_spares_js {
15537:     my @sparestypes = ('primary','default');
15538:     my $types = join("','",@sparestypes);
15539:     my $select = &mt('Select');
15540:     return <<"END";
15541: 
15542: <script type="text/javascript">
15543: // <![CDATA[
15544: 
15545: function updateNewSpares(formname,lonhost) {
15546:     var types = new Array('$types');
15547:     var include = new Array();
15548:     var exclude = new Array();
15549:     for (var i=0; i<types.length; i++) {
15550:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
15551:         for (var j=0; j<spareboxes.length; j++) {
15552:             if (formname.elements[spareboxes[j]].checked) {
15553:                 exclude.push(formname.elements[spareboxes[j]].value);
15554:             } else {
15555:                 include.push(formname.elements[spareboxes[j]].value);
15556:             }
15557:         }
15558:     }
15559:     for (var i=0; i<types.length; i++) {
15560:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
15561:         var selIdx = newSpare.selectedIndex;
15562:         var currnew = newSpare.options[selIdx].value;
15563:         var okSpares = new Array();
15564:         for (var j=0; j<newSpare.options.length; j++) {
15565:             var possible = newSpare.options[j].value;
15566:             if (possible != '') {
15567:                 if (exclude.indexOf(possible) == -1) {
15568:                     okSpares.push(possible);
15569:                 } else {
15570:                     if (currnew == possible) {
15571:                         selIdx = 0;
15572:                     }
15573:                 }
15574:             }
15575:         }
15576:         for (var k=0; k<include.length; k++) {
15577:             if (okSpares.indexOf(include[k]) == -1) {
15578:                 okSpares.push(include[k]);
15579:             }
15580:         }
15581:         okSpares.sort();
15582:         newSpare.options.length = 0;
15583:         if (selIdx == 0) {
15584:             newSpare.options[0] = new Option("$select","",true,true);
15585:         } else {
15586:             newSpare.options[0] = new Option("$select","",false,false);
15587:         }
15588:         for (var m=0; m<okSpares.length; m++) {
15589:             var idx = m+1;
15590:             var selThis = 0;
15591:             if (selIdx != 0) {
15592:                 if (okSpares[m] == currnew) {
15593:                     selThis = 1;
15594:                 }
15595:             }
15596:             if (selThis == 1) {
15597:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
15598:             } else {
15599:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
15600:             }
15601:         }
15602:     }
15603:     return;
15604: }
15605: 
15606: function checkNewSpares(lonhost,type) {
15607:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
15608:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
15609:     if (chosen != '') { 
15610:         var othertype;
15611:         var othernewSpare;
15612:         if (type == 'primary') {
15613:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
15614:         }
15615:         if (type == 'default') {
15616:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
15617:         }
15618:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
15619:             othernewSpare.selectedIndex = 0;
15620:         }
15621:     }
15622:     return;
15623: }
15624: 
15625: // ]]>
15626: </script>
15627: 
15628: END
15629: 
15630: }
15631: 
15632: sub common_domprefs_js {
15633:     return <<"END";
15634: 
15635: <script type="text/javascript">
15636: // <![CDATA[
15637: 
15638: function getIndicesByName(formname,item) {
15639:     var group = new Array();
15640:     for (var i=0;i<formname.elements.length;i++) {
15641:         if (formname.elements[i].name == item) {
15642:             group.push(formname.elements[i].id);
15643:         }
15644:     }
15645:     return group;
15646: }
15647: 
15648: // ]]>
15649: </script>
15650: 
15651: END
15652: 
15653: }
15654: 
15655: sub recaptcha_js {
15656:     my %lt = &captcha_phrases();
15657:     return <<"END";
15658: 
15659: <script type="text/javascript">
15660: // <![CDATA[
15661: 
15662: function updateCaptcha(caller,context) {
15663:     var privitem;
15664:     var pubitem;
15665:     var privtext;
15666:     var pubtext;
15667:     var versionitem;
15668:     var versiontext;
15669:     if (document.getElementById(context+'_recaptchapub')) {
15670:         pubitem = document.getElementById(context+'_recaptchapub');
15671:     } else {
15672:         return;
15673:     }
15674:     if (document.getElementById(context+'_recaptchapriv')) {
15675:         privitem = document.getElementById(context+'_recaptchapriv');
15676:     } else {
15677:         return;
15678:     }
15679:     if (document.getElementById(context+'_recaptchapubtxt')) {
15680:         pubtext = document.getElementById(context+'_recaptchapubtxt');
15681:     } else {
15682:         return;
15683:     }
15684:     if (document.getElementById(context+'_recaptchaprivtxt')) {
15685:         privtext = document.getElementById(context+'_recaptchaprivtxt');
15686:     } else {
15687:         return;
15688:     }
15689:     if (document.getElementById(context+'_recaptchaversion')) {
15690:         versionitem = document.getElementById(context+'_recaptchaversion');
15691:     } else {
15692:         return;
15693:     }
15694:     if (document.getElementById(context+'_recaptchavertxt')) {
15695:         versiontext = document.getElementById(context+'_recaptchavertxt');
15696:     } else {
15697:         return;
15698:     }
15699:     if (caller.checked) {
15700:         if (caller.value == 'recaptcha') {
15701:             pubitem.type = 'text';
15702:             privitem.type = 'text';
15703:             pubitem.size = '40';
15704:             privitem.size = '40';
15705:             pubtext.innerHTML = "$lt{'pub'}";
15706:             privtext.innerHTML = "$lt{'priv'}";
15707:             versionitem.type = 'text';
15708:             versionitem.size = '3';
15709:             versiontext.innerHTML = "$lt{'ver'}";
15710:         } else {
15711:             pubitem.type = 'hidden';
15712:             privitem.type = 'hidden';
15713:             versionitem.type = 'hidden';
15714:             pubtext.innerHTML = '';
15715:             privtext.innerHTML = '';
15716:             versiontext.innerHTML = '';
15717:         }
15718:     }
15719:     return;
15720: }
15721: 
15722: // ]]>
15723: </script>
15724: 
15725: END
15726: 
15727: }
15728: 
15729: sub toggle_display_js {
15730:     return <<"END";
15731: 
15732: <script type="text/javascript">
15733: // <![CDATA[
15734: 
15735: function toggleDisplay(domForm,caller) {
15736:     if (document.getElementById(caller)) {
15737:         var divitem = document.getElementById(caller);
15738:         var optionsElement = domForm.coursecredits;
15739:         var checkval = 1;
15740:         var dispval = 'block';
15741:         var selfcreateRegExp = /^cancreate_emailverified/;
15742:         if (caller == 'emailoptions') {
15743:             optionsElement = domForm.cancreate_email; 
15744:         }
15745:         if (caller == 'studentsubmission') {
15746:             optionsElement = domForm.postsubmit;
15747:         }
15748:         if (caller == 'cloneinstcode') {
15749:             optionsElement = domForm.canclone;
15750:             checkval = 'instcode';
15751:         }
15752:         if (selfcreateRegExp.test(caller)) {
15753:             optionsElement = domForm.elements[caller];
15754:             checkval = 'other';
15755:             dispval = 'inline'
15756:         }
15757:         if (optionsElement.length) {
15758:             var currval;
15759:             for (var i=0; i<optionsElement.length; i++) {
15760:                 if (optionsElement[i].checked) {
15761:                    currval = optionsElement[i].value;
15762:                 }
15763:             }
15764:             if (currval == checkval) {
15765:                 divitem.style.display = dispval;
15766:             } else {
15767:                 divitem.style.display = 'none';
15768:             }
15769:         }
15770:     }
15771:     return;
15772: }
15773: 
15774: // ]]>
15775: </script>
15776: 
15777: END
15778: 
15779: }
15780: 
15781: sub captcha_phrases {
15782:     return &Apache::lonlocal::texthash (
15783:                  priv => 'Private key',
15784:                  pub  => 'Public key',
15785:                  original  => 'original (CAPTCHA)',
15786:                  recaptcha => 'successor (ReCAPTCHA)',
15787:                  notused   => 'unused',
15788:                  ver => 'ReCAPTCHA version (1 or 2)',
15789:     );
15790: }
15791: 
15792: sub devalidate_remote_domconfs {
15793:     my ($dom,$cachekeys) = @_;
15794:     return unless (ref($cachekeys) eq 'HASH');
15795:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15796:     my %thismachine;
15797:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15798:     my @posscached = ('domainconfig','domdefaults','usersessions',
15799:                       'directorysrch','passwdconf','cats');
15800:     if (keys(%servers)) {
15801:         foreach my $server (keys(%servers)) {
15802:             next if ($thismachine{$server});
15803:             my @cached;
15804:             foreach my $name (@posscached) {
15805:                 if ($cachekeys->{$name}) {
15806:                     push(@cached,&escape($name).':'.&escape($dom));
15807:                 }
15808:             }
15809:             if (@cached) {
15810:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
15811:             }
15812:         }
15813:     }
15814:     return;
15815: }
15816: 
15817: 1;

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