File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.103: download - view: text, annotated - select for diffs
Tue Feb 4 03:44:19 2020 UTC (4 years, 3 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  Typo in 1.160.6.87 backport of 1.327.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.103 2020/02/04 03:44:19 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   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:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  722:             ($action eq 'directorysrch') ||
  723:             (($action eq 'login') && ($numheaders < 4))) {
  724:             $colspan = ' colspan="2"';
  725:         }
  726:         if ($action eq 'usersessions') {
  727:             $rightcolspan = ' colspan="3"'; 
  728:         }
  729:         $output .= '
  730:           <tr>
  731:            <td>
  732:             <table class="LC_nested">
  733:              <tr class="LC_info_row">
  734:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  735:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  736:              </tr>';
  737:         $rowtotal ++;
  738:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  739:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  740:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  741:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  742:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  743:         } elsif ($action eq 'passwords') {
  744:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  745:         } elsif ($action eq 'coursecategories') {
  746:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  747:         } elsif ($action eq 'scantron') {
  748:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  749:         } elsif ($action eq 'login') {
  750:             if ($numheaders == 4) {
  751:                 $colspan = ' colspan="2"';
  752:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  753:             } else {
  754:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  755:             }
  756:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  757:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  758:         } elsif ($action eq 'rolecolors') {
  759:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  760:         }
  761:         $output .= '
  762:            </table>
  763:           </td>
  764:          </tr>
  765:          <tr>
  766:            <td>
  767:             <table class="LC_nested">
  768:              <tr class="LC_info_row">
  769:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  770:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  771:              </tr>';
  772:             $rowtotal ++;
  773:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  774:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  775:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  776:             ($action eq 'contacts') || ($action eq 'passwords')) {
  777:             if ($action eq 'coursecategories') {
  778:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  779:                 $colspan = ' colspan="2"';
  780:             } elsif ($action eq 'passwords') {
  781:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  782:             } else {
  783:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  784:             }
  785:             $output .= '
  786:            </table>
  787:           </td>
  788:          </tr>
  789:          <tr>
  790:            <td>
  791:             <table class="LC_nested">
  792:              <tr class="LC_info_row">
  793:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  794:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  795:              </tr>'."\n";
  796:             if ($action eq 'coursecategories') {
  797:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  798:             } elsif ($action eq 'passwords') {
  799:                 $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal).'
  800:              </tr>
  801:             </table>
  802:            </td>
  803:           </tr>
  804:           <tr>
  805:            <td>
  806:             <table class="LC_nested">
  807:              <tr class="LC_info_row">
  808:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  809:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n".
  810:               $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal).'
  811:             </table>
  812:           </td>
  813:          </tr>
  814:          <tr>';
  815:             } else {
  816:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  817:             }
  818:             $rowtotal ++;
  819:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  820:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  821:                  ($action eq 'helpsettings')) {
  822:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  823:         } elsif ($action eq 'scantron') {
  824:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
  825:         } elsif ($action eq 'login') {
  826:             if ($numheaders == 4) {
  827:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  828:            </table>
  829:           </td>
  830:          </tr>
  831:          <tr>
  832:            <td>
  833:             <table class="LC_nested">
  834:              <tr class="LC_info_row">
  835:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  836:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  837:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  838:                 $rowtotal ++;
  839:             } else {
  840:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  841:             }
  842:             $output .= '
  843:            </table>
  844:           </td>
  845:          </tr>
  846:          <tr>
  847:            <td>
  848:             <table class="LC_nested">
  849:              <tr class="LC_info_row">';
  850:             if ($numheaders == 4) {
  851:                 $output .= '
  852:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  853:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  854:              </tr>';
  855:             } else {
  856:                 $output .= '
  857:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  858:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  859:              </tr>';
  860:             }
  861:             $rowtotal ++;
  862:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  863:         } elsif ($action eq 'requestcourses') {
  864:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  865:             $rowtotal ++;
  866:             $output .= &print_studentcode($settings,\$rowtotal).'
  867:            </table>
  868:           </td>
  869:          </tr>
  870:          <tr>
  871:            <td>
  872:             <table class="LC_nested">
  873:              <tr class="LC_info_row">
  874:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  875:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  876:                        &textbookcourses_javascript($settings).
  877:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  878:             </table>
  879:            </td>
  880:           </tr>
  881:          <tr>
  882:            <td>
  883:             <table class="LC_nested">
  884:              <tr class="LC_info_row">
  885:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  886:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  887:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  888:             </table>
  889:            </td>
  890:           </tr>
  891:           <tr>
  892:            <td>
  893:             <table class="LC_nested">
  894:              <tr class="LC_info_row">
  895:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  896:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  897:              </tr>'.
  898:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  899:         } elsif ($action eq 'requestauthor') {
  900:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  901:             $rowtotal ++;
  902:         } elsif ($action eq 'rolecolors') {
  903:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  904:            </table>
  905:           </td>
  906:          </tr>
  907:          <tr>
  908:            <td>
  909:             <table class="LC_nested">
  910:              <tr class="LC_info_row">
  911:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  912:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  913:               <td class="LC_right_item" valign="top">'.
  914:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  915:              </tr>'.
  916:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  917:            </table>
  918:           </td>
  919:          </tr>
  920:          <tr>
  921:            <td>
  922:             <table class="LC_nested">
  923:              <tr class="LC_info_row">
  924:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  925:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  926:              </tr>'.
  927:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  928:             $rowtotal += 2;
  929:         }
  930:     } else {
  931:         $output .= '
  932:           <tr>
  933:            <td>
  934:             <table class="LC_nested">
  935:              <tr class="LC_info_row">';
  936:         if ($action eq 'login') {
  937:             $output .= '  
  938:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  939:         } elsif ($action eq 'serverstatuses') {
  940:             $output .= '
  941:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  942:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  943: 
  944:         } else {
  945:             $output .= '
  946:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  947:         }
  948:         if (defined($item->{'header'}->[0]->{'col3'})) {
  949:             $output .= '<td class="LC_left_item" valign="top">'.
  950:                        &mt($item->{'header'}->[0]->{'col2'});
  951:             if ($action eq 'serverstatuses') {
  952:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  953:             } 
  954:         } else {
  955:             $output .= '<td class="LC_right_item" valign="top">'.
  956:                        &mt($item->{'header'}->[0]->{'col2'});
  957:         }
  958:         $output .= '</td>';
  959:         if ($item->{'header'}->[0]->{'col3'}) {
  960:             if (defined($item->{'header'}->[0]->{'col4'})) {
  961:                 $output .= '<td class="LC_left_item" valign="top">'.
  962:                             &mt($item->{'header'}->[0]->{'col3'});
  963:             } else {
  964:                 $output .= '<td class="LC_right_item" valign="top">'.
  965:                            &mt($item->{'header'}->[0]->{'col3'});
  966:             }
  967:             if ($action eq 'serverstatuses') {
  968:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  969:             }
  970:             $output .= '</td>';
  971:         }
  972:         if ($item->{'header'}->[0]->{'col4'}) {
  973:             $output .= '<td class="LC_right_item" valign="top">'.
  974:                        &mt($item->{'header'}->[0]->{'col4'});
  975:         }
  976:         $output .= '</tr>';
  977:         $rowtotal ++;
  978:         if ($action eq 'quotas') {
  979:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  980:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
  981:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  982:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  983:         }
  984:     }
  985:     $output .= '
  986:    </table>
  987:   </td>
  988:  </tr>
  989: </table><br />';
  990:     return ($output,$rowtotal);
  991: }
  992: 
  993: sub print_login {
  994:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  995:     my ($css_class,$datatable);
  996:     my %choices = &login_choices();
  997: 
  998:     if ($caller eq 'service') {
  999:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1000:         my $choice = $choices{'disallowlogin'};
 1001:         $css_class = ' class="LC_odd_row"';
 1002:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1003:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1004:                       '<th>'.$choices{'server'}.'</th>'.
 1005:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1006:                       '<th>'.$choices{'custompath'}.'</th>'.
 1007:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1008:         my %disallowed;
 1009:         if (ref($settings) eq 'HASH') {
 1010:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1011:                %disallowed = %{$settings->{'loginvia'}};
 1012:             }
 1013:         }
 1014:         foreach my $lonhost (sort(keys(%servers))) {
 1015:             my $direct = 'selected="selected"';
 1016:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1017:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1018:                     $direct = '';
 1019:                 }
 1020:             }
 1021:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1022:                           '<td><select name="'.$lonhost.'_server">'.
 1023:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1024:                           '</option>';
 1025:             foreach my $hostid (sort(keys(%servers))) {
 1026:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1027:                 my $selected = '';
 1028:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1029:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1030:                         $selected = 'selected="selected"';
 1031:                     }
 1032:                 }
 1033:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1034:                               $servers{$hostid}.'</option>';
 1035:             }
 1036:             $datatable .= '</select></td>'.
 1037:                           '<td><select name="'.$lonhost.'_serverpath">';
 1038:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1039:                 my $pathname = $path;
 1040:                 if ($path eq 'custom') {
 1041:                     $pathname = &mt('Custom Path').' ->';
 1042:                 }
 1043:                 my $selected = '';
 1044:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1045:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1046:                         $selected = 'selected="selected"';
 1047:                     }
 1048:                 } elsif ($path eq '') {
 1049:                     $selected = 'selected="selected"';
 1050:                 }
 1051:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1052:             }
 1053:             $datatable .= '</select></td>';
 1054:             my ($custom,$exempt);
 1055:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1056:                 $custom = $disallowed{$lonhost}{'custompath'};
 1057:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1058:             }
 1059:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1060:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1061:                           '</tr>';
 1062:         }
 1063:         $datatable .= '</table></td></tr>';
 1064:         return $datatable;
 1065:     } elsif ($caller eq 'page') {
 1066:         my %defaultchecked = ( 
 1067:                                'coursecatalog' => 'on',
 1068:                                'helpdesk'      => 'on',
 1069:                                'adminmail'     => 'off',
 1070:                                'newuser'       => 'off',
 1071:                              );
 1072:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1073:         my (%checkedon,%checkedoff);
 1074:         foreach my $item (@toggles) {
 1075:             if ($defaultchecked{$item} eq 'on') { 
 1076:                 $checkedon{$item} = ' checked="checked" ';
 1077:                 $checkedoff{$item} = ' ';
 1078:             } elsif ($defaultchecked{$item} eq 'off') {
 1079:                 $checkedoff{$item} = ' checked="checked" ';
 1080:                 $checkedon{$item} = ' ';
 1081:             }
 1082:         }
 1083:         my @images = ('img','logo','domlogo','login');
 1084:         my @logintext = ('textcol','bgcol');
 1085:         my @bgs = ('pgbg','mainbg','sidebg');
 1086:         my @links = ('link','alink','vlink');
 1087:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1088:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1089:         my (%is_custom,%designs);
 1090:         my %defaults = (
 1091:                        font => $defaultdesign{'login.font'},
 1092:                        );
 1093:         foreach my $item (@images) {
 1094:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1095:             $defaults{'showlogo'}{$item} = 1;
 1096:         }
 1097:         foreach my $item (@bgs) {
 1098:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1099:         }
 1100:         foreach my $item (@logintext) {
 1101:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1102:         }
 1103:         foreach my $item (@links) {
 1104:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1105:         }
 1106:         if (ref($settings) eq 'HASH') {
 1107:             foreach my $item (@toggles) {
 1108:                 if ($settings->{$item} eq '1') {
 1109:                     $checkedon{$item} =  ' checked="checked" ';
 1110:                     $checkedoff{$item} = ' ';
 1111:                 } elsif ($settings->{$item} eq '0') {
 1112:                     $checkedoff{$item} =  ' checked="checked" ';
 1113:                     $checkedon{$item} = ' ';
 1114:                 }
 1115:             }
 1116:             foreach my $item (@images) {
 1117:                 if (defined($settings->{$item})) {
 1118:                     $designs{$item} = $settings->{$item};
 1119:                     $is_custom{$item} = 1;
 1120:                 }
 1121:                 if (defined($settings->{'showlogo'}{$item})) {
 1122:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1123:                 }
 1124:             }
 1125:             foreach my $item (@logintext) {
 1126:                 if ($settings->{$item} ne '') {
 1127:                     $designs{'logintext'}{$item} = $settings->{$item};
 1128:                     $is_custom{$item} = 1;
 1129:                 }
 1130:             }
 1131:             if ($settings->{'font'} ne '') {
 1132:                 $designs{'font'} = $settings->{'font'};
 1133:                 $is_custom{'font'} = 1;
 1134:             }
 1135:             foreach my $item (@bgs) {
 1136:                 if ($settings->{$item} ne '') {
 1137:                     $designs{'bgs'}{$item} = $settings->{$item};
 1138:                     $is_custom{$item} = 1;
 1139:                 }
 1140:             }
 1141:             foreach my $item (@links) {
 1142:                 if ($settings->{$item} ne '') {
 1143:                     $designs{'links'}{$item} = $settings->{$item};
 1144:                     $is_custom{$item} = 1;
 1145:                 }
 1146:             }
 1147:         } else {
 1148:             if ($designhash{$dom.'.login.font'} ne '') {
 1149:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1150:                 $is_custom{'font'} = 1;
 1151:             }
 1152:             foreach my $item (@images) {
 1153:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1154:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1155:                     $is_custom{$item} = 1;
 1156:                 }
 1157:             }
 1158:             foreach my $item (@bgs) {
 1159:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1160:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1161:                     $is_custom{$item} = 1;
 1162:                 }
 1163:             }
 1164:             foreach my $item (@links) {
 1165:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1166:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1167:                     $is_custom{$item} = 1;
 1168:                 }
 1169:             }
 1170:         }
 1171:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1172:                                                       logo => 'Institution Logo',
 1173:                                                       domlogo => 'Domain Logo',
 1174:                                                       login => 'Login box');
 1175:         my $itemcount = 1;
 1176:         foreach my $item (@toggles) {
 1177:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1178:             $datatable .=  
 1179:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1180:                 '</td><td>'.
 1181:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1182:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1183:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1184:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1185:                 '</tr>';
 1186:             $itemcount ++;
 1187:         }
 1188:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1189:         $datatable .= '</tr></table></td></tr>';
 1190:     } elsif ($caller eq 'help') {
 1191:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1192:         my $switchserver = &check_switchserver($dom,$confname);
 1193:         my $itemcount = 1;
 1194:         $defaulturl = '/adm/loginproblems.html';
 1195:         $defaulttype = 'default';
 1196:         %lt = &Apache::lonlocal::texthash (
 1197:                      del     => 'Delete?',
 1198:                      rep     => 'Replace:',
 1199:                      upl     => 'Upload:',
 1200:                      default => 'Default',
 1201:                      custom  => 'Custom',
 1202:                                              );
 1203:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1204:         my @currlangs;
 1205:         if (ref($settings) eq 'HASH') {
 1206:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1207:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1208:                     next if ($settings->{'helpurl'}{$key} eq '');
 1209:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1210:                     $type{$key} = 'custom';
 1211:                     unless ($key eq 'nolang') {
 1212:                         push(@currlangs,$key);
 1213:                     }
 1214:                 }
 1215:             } elsif ($settings->{'helpurl'} ne '') {
 1216:                 $type{'nolang'} = 'custom';
 1217:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1218:             }
 1219:         }
 1220:         foreach my $lang ('nolang',sort(@currlangs)) {
 1221:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1222:             $datatable .= '<tr'.$css_class.'>';
 1223:             if ($url{$lang} eq '') {
 1224:                 $url{$lang} = $defaulturl;
 1225:             }
 1226:             if ($type{$lang} eq '') {
 1227:                 $type{$lang} = $defaulttype;
 1228:             }
 1229:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1230:             if ($lang eq 'nolang') {
 1231:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1232:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1233:             } else {
 1234:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1235:                                   $langchoices{$lang},
 1236:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1237:             }
 1238:             $datatable .= '</span></td>'."\n".
 1239:                           '<td class="LC_left_item">';
 1240:             if ($type{$lang} eq 'custom') {
 1241:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1242:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1243:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1244:             } else {
 1245:                 $datatable .= $lt{'upl'};
 1246:             }
 1247:             $datatable .='<br />';
 1248:             if ($switchserver) {
 1249:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1250:             } else {
 1251:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1252:             }
 1253:             $datatable .= '</td></tr>';
 1254:             $itemcount ++;
 1255:         }
 1256:         my @addlangs;
 1257:         foreach my $lang (sort(keys(%langchoices))) {
 1258:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1259:             push(@addlangs,$lang);
 1260:         }
 1261:         if (@addlangs > 0) {
 1262:             my %toadd;
 1263:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1264:             $toadd{''} = &mt('Select');
 1265:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1266:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1267:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1268:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1269:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1270:             if ($switchserver) {
 1271:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1272:             } else {
 1273:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1274:             }
 1275:             $datatable .= '</td></tr>';
 1276:             $itemcount ++;
 1277:         }
 1278:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1279:     } elsif ($caller eq 'headtag') {
 1280:         my %domservers = &Apache::lonnet::get_servers($dom);
 1281:         my $choice = $choices{'headtag'};
 1282:         $css_class = ' class="LC_odd_row"';
 1283:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1284:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1285:                       '<th>'.$choices{'current'}.'</th>'.
 1286:                       '<th>'.$choices{'action'}.'</th>'.
 1287:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1288:         my (%currurls,%currexempt);
 1289:         if (ref($settings) eq 'HASH') {
 1290:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1291:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1292:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1293:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1294:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1295:                     }
 1296:                 }
 1297:             }
 1298:         }
 1299:         my %lt = &Apache::lonlocal::texthash(
 1300:                                                del  => 'Delete?',
 1301:                                                rep  => 'Replace:',
 1302:                                                upl  => 'Upload:',
 1303:                                                curr => 'View contents',
 1304:                                                none => 'None',
 1305:         );
 1306:         my $switchserver = &check_switchserver($dom,$confname);
 1307:         foreach my $lonhost (sort(keys(%domservers))) {
 1308:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1309:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1310:             if ($currurls{$lonhost}) {
 1311:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1312:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1313:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1314:                               '">'.$lt{'curr'}.'</a></td>'.
 1315:                               '<td><span class="LC_nobreak"><label>'.
 1316:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1317:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1318:             } else {
 1319:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1320:             }
 1321:             $datatable .='<br />';
 1322:             if ($switchserver) {
 1323:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1324:             } else {
 1325:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1326:             }
 1327:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1328:         }
 1329:         $datatable .= '</table></td></tr>';
 1330:     }
 1331:     return $datatable;
 1332: }
 1333: 
 1334: sub login_choices {
 1335:     my %choices =
 1336:         &Apache::lonlocal::texthash (
 1337:             coursecatalog => 'Display Course/Community Catalog link?',
 1338:             adminmail     => "Display Administrator's E-mail Address?",
 1339:             helpdesk      => 'Display "Contact Helpdesk" link',
 1340:             disallowlogin => "Login page requests redirected",
 1341:             hostid        => "Server",
 1342:             server        => "Redirect to:",
 1343:             serverpath    => "Path",
 1344:             custompath    => "Custom", 
 1345:             exempt        => "Exempt IP(s)",
 1346:             directlogin   => "No redirect",
 1347:             newuser       => "Link to create a user account",
 1348:             img           => "Header",
 1349:             logo          => "Main Logo",
 1350:             domlogo       => "Domain Logo",
 1351:             login         => "Log-in Header", 
 1352:             textcol       => "Text color",
 1353:             bgcol         => "Box color",
 1354:             bgs           => "Background colors",
 1355:             links         => "Link colors",
 1356:             font          => "Font color",
 1357:             pgbg          => "Header",
 1358:             mainbg        => "Page",
 1359:             sidebg        => "Login box",
 1360:             link          => "Link",
 1361:             alink         => "Active link",
 1362:             vlink         => "Visited link",
 1363:             headtag       => "Custom markup",
 1364:             action        => "Action",
 1365:             current       => "Current",
 1366:         );
 1367:     return %choices;
 1368: }
 1369: 
 1370: sub print_rolecolors {
 1371:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1372:     my %choices = &color_font_choices();
 1373:     my @bgs = ('pgbg','tabbg','sidebg');
 1374:     my @links = ('link','alink','vlink');
 1375:     my @images = ('img');
 1376:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1377:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1378:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1379:     my (%is_custom,%designs);
 1380:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1381:     if (ref($settings) eq 'HASH') {
 1382:         if (ref($settings->{$role}) eq 'HASH') {
 1383:             if ($settings->{$role}->{'img'} ne '') {
 1384:                 $designs{'img'} = $settings->{$role}->{'img'};
 1385:                 $is_custom{'img'} = 1;
 1386:             }
 1387:             if ($settings->{$role}->{'font'} ne '') {
 1388:                 $designs{'font'} = $settings->{$role}->{'font'};
 1389:                 $is_custom{'font'} = 1;
 1390:             }
 1391:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1392:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1393:                 $is_custom{'fontmenu'} = 1;
 1394:             }
 1395:             foreach my $item (@bgs) {
 1396:                 if ($settings->{$role}->{$item} ne '') {
 1397:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1398:                     $is_custom{$item} = 1;
 1399:                 }
 1400:             }
 1401:             foreach my $item (@links) {
 1402:                 if ($settings->{$role}->{$item} ne '') {
 1403:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1404:                     $is_custom{$item} = 1;
 1405:                 }
 1406:             }
 1407:         }
 1408:     } else {
 1409:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1410:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1411:             $is_custom{'img'} = 1;
 1412:         }
 1413:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1414:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1415:             $is_custom{'fontmenu'} = 1; 
 1416:         }
 1417:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1418:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1419:             $is_custom{'font'} = 1;
 1420:         }
 1421:         foreach my $item (@bgs) {
 1422:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1423:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1424:                 $is_custom{$item} = 1;
 1425:             
 1426:             }
 1427:         }
 1428:         foreach my $item (@links) {
 1429:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1430:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1431:                 $is_custom{$item} = 1;
 1432:             }
 1433:         }
 1434:     }
 1435:     my $itemcount = 1;
 1436:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1437:     $datatable .= '</tr></table></td></tr>';
 1438:     return $datatable;
 1439: }
 1440: 
 1441: sub role_defaults {
 1442:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1443:     my %defaults;
 1444:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1445:         return %defaults;
 1446:     }
 1447:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1448:     if ($role eq 'login') {
 1449:         %defaults = (
 1450:                        font => $defaultdesign{$role.'.font'},
 1451:                     );
 1452:         if (ref($logintext) eq 'ARRAY') {
 1453:             foreach my $item (@{$logintext}) {
 1454:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1455:             }
 1456:         }
 1457:         foreach my $item (@{$images}) {
 1458:             $defaults{'showlogo'}{$item} = 1;
 1459:         }
 1460:     } else {
 1461:         %defaults = (
 1462:                        img => $defaultdesign{$role.'.img'},
 1463:                        font => $defaultdesign{$role.'.font'},
 1464:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1465:                     );
 1466:     }
 1467:     foreach my $item (@{$bgs}) {
 1468:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1469:     }
 1470:     foreach my $item (@{$links}) {
 1471:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1472:     }
 1473:     foreach my $item (@{$images}) {
 1474:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1475:     }
 1476:     return %defaults;
 1477: }
 1478: 
 1479: sub display_color_options {
 1480:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1481:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1482:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1483:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1484:     my $datatable = '<tr'.$css_class.'>'.
 1485:         '<td>'.$choices->{'font'}.'</td>';
 1486:     if (!$is_custom->{'font'}) {
 1487:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1488:     } else {
 1489:         $datatable .= '<td>&nbsp;</td>';
 1490:     }
 1491:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1492: 
 1493:     $datatable .= '<td><span class="LC_nobreak">'.
 1494:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1495:                   ' value="'.$current_color.'" />&nbsp;'.
 1496:                   '&nbsp;</span></td></tr>';
 1497:     unless ($role eq 'login') { 
 1498:         $datatable .= '<tr'.$css_class.'>'.
 1499:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1500:         if (!$is_custom->{'fontmenu'}) {
 1501:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1502:         } else {
 1503:             $datatable .= '<td>&nbsp;</td>';
 1504:         }
 1505: 	$current_color = $designs->{'fontmenu'} ?
 1506: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1507:         $datatable .= '<td><span class="LC_nobreak">'.
 1508:                       '<input class="colorchooser" type="text" size="10" name="'
 1509: 		      .$role.'_fontmenu"'.
 1510:                       ' value="'.$current_color.'" />&nbsp;'.
 1511:                       '&nbsp;</span></td></tr>';
 1512:     }
 1513:     my $switchserver = &check_switchserver($dom,$confname);
 1514:     foreach my $img (@{$images}) {
 1515: 	$itemcount ++;
 1516:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1517:         $datatable .= '<tr'.$css_class.'>'.
 1518:                       '<td>'.$choices->{$img};
 1519:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1520:         if ($role eq 'login') {
 1521:             if ($img eq 'login') {
 1522:                 $login_hdr_pick =
 1523:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1524:                 $logincolors =
 1525:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1526:                                        $designs,$defaults);
 1527:             } elsif ($img ne 'domlogo') {
 1528:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1529:             }
 1530:         }
 1531:         $datatable .= '</td>';
 1532:         if ($designs->{$img} ne '') {
 1533:             $imgfile = $designs->{$img};
 1534: 	    $img_import = ($imgfile =~ m{^/adm/});
 1535:         } else {
 1536:             $imgfile = $defaults->{$img};
 1537:         }
 1538:         if ($imgfile) {
 1539:             my ($showfile,$fullsize);
 1540:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1541:                 my $urldir = $1;
 1542:                 my $filename = $2;
 1543:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1544:                 if (@info) {
 1545:                     my $thumbfile = 'tn-'.$filename;
 1546:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1547:                     if (@thumb) {
 1548:                         $showfile = $urldir.'/'.$thumbfile;
 1549:                     } else {
 1550:                         $showfile = $imgfile;
 1551:                     }
 1552:                 } else {
 1553:                     $showfile = '';
 1554:                 }
 1555:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1556:                 $showfile = $imgfile;
 1557:                 my $imgdir = $1;
 1558:                 my $filename = $2;
 1559:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1560:                     $showfile = "/$imgdir/tn-".$filename;
 1561:                 } else {
 1562:                     my $input = $londocroot.$imgfile;
 1563:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1564:                     if (!-e $output) {
 1565:                         my ($width,$height) = &thumb_dimensions();
 1566:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1567:                         if ($fullwidth ne '' && $fullheight ne '') {
 1568:                             if ($fullwidth > $width && $fullheight > $height) { 
 1569:                                 my $size = $width.'x'.$height;
 1570:                                 my @args = ('convert','-sample',$size,$input,$output);
 1571:                                 system({$args[0]} @args);
 1572:                                 $showfile = "/$imgdir/tn-".$filename;
 1573:                             }
 1574:                         }
 1575:                     }
 1576:                 }
 1577:             }
 1578:             if ($showfile) {
 1579:                 if ($showfile =~ m{^/(adm|res)/}) {
 1580:                     if ($showfile =~ m{^/res/}) {
 1581:                         my $local_showfile =
 1582:                             &Apache::lonnet::filelocation('',$showfile);
 1583:                         &Apache::lonnet::repcopy($local_showfile);
 1584:                     }
 1585:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1586:                 }
 1587:                 if ($imgfile) {
 1588:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1589:                         if ($imgfile =~ m{^/res/}) {
 1590:                             my $local_imgfile =
 1591:                                 &Apache::lonnet::filelocation('',$imgfile);
 1592:                             &Apache::lonnet::repcopy($local_imgfile);
 1593:                         }
 1594:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1595:                     } else {
 1596:                         $fullsize = $imgfile;
 1597:                     }
 1598:                 }
 1599:                 $datatable .= '<td>';
 1600:                 if ($img eq 'login') {
 1601:                     $datatable .= $login_hdr_pick;
 1602:                 } 
 1603:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1604:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1605:             } else {
 1606:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1607:                               &mt('Upload:').'<br />';
 1608:             }
 1609:         } else {
 1610:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1611:                           &mt('Upload:').'<br />';
 1612:         }
 1613:         if ($switchserver) {
 1614:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1615:         } else {
 1616:             if ($img ne 'login') { # suppress file selection for Log-in header
 1617:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1618:             }
 1619:         }
 1620:         $datatable .= '</td></tr>';
 1621:     }
 1622:     $itemcount ++;
 1623:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1624:     $datatable .= '<tr'.$css_class.'>'.
 1625:                   '<td>'.$choices->{'bgs'}.'</td>';
 1626:     my $bgs_def;
 1627:     foreach my $item (@{$bgs}) {
 1628:         if (!$is_custom->{$item}) {
 1629:             $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>';
 1630:         }
 1631:     }
 1632:     if ($bgs_def) {
 1633:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1634:     } else {
 1635:         $datatable .= '<td>&nbsp;</td>';
 1636:     }
 1637:     $datatable .= '<td class="LC_right_item">'.
 1638:                   '<table border="0"><tr>';
 1639: 
 1640:     foreach my $item (@{$bgs}) {
 1641:         $datatable .= '<td align="center">'.$choices->{$item};
 1642: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1643:         if ($designs->{'bgs'}{$item}) {
 1644:             $datatable .= '&nbsp;';
 1645:         }
 1646:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1647:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1648:     }
 1649:     $datatable .= '</tr></table></td></tr>';
 1650:     $itemcount ++;
 1651:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1652:     $datatable .= '<tr'.$css_class.'>'.
 1653:                   '<td>'.$choices->{'links'}.'</td>';
 1654:     my $links_def;
 1655:     foreach my $item (@{$links}) {
 1656:         if (!$is_custom->{$item}) {
 1657:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1658:         }
 1659:     }
 1660:     if ($links_def) {
 1661:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1662:     } else {
 1663:         $datatable .= '<td>&nbsp;</td>';
 1664:     }
 1665:     $datatable .= '<td class="LC_right_item">'.
 1666:                   '<table border="0"><tr>';
 1667:     foreach my $item (@{$links}) {
 1668: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1669:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1670:         if ($designs->{'links'}{$item}) {
 1671:             $datatable.='&nbsp;';
 1672:         }
 1673:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1674:                       '" /></td>';
 1675:     }
 1676:     $$rowtotal += $itemcount;
 1677:     return $datatable;
 1678: }
 1679: 
 1680: sub logo_display_options {
 1681:     my ($img,$defaults,$designs) = @_;
 1682:     my $checkedon;
 1683:     if (ref($defaults) eq 'HASH') {
 1684:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1685:             if ($defaults->{'showlogo'}{$img}) {
 1686:                 $checkedon = 'checked="checked" ';     
 1687:             }
 1688:         } 
 1689:     }
 1690:     if (ref($designs) eq 'HASH') {
 1691:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1692:             if (defined($designs->{'showlogo'}{$img})) {
 1693:                 if ($designs->{'showlogo'}{$img} == 0) {
 1694:                     $checkedon = '';
 1695:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1696:                     $checkedon = 'checked="checked" ';
 1697:                 }
 1698:             }
 1699:         }
 1700:     }
 1701:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1702:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1703:            &mt('show').'</label>'."\n";
 1704: }
 1705: 
 1706: sub login_header_options  {
 1707:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1708:     my $output = '';
 1709:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1710:         $output .= &mt('Text default(s):').'<br />';
 1711:         if (!$is_custom->{'textcol'}) {
 1712:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1713:                        '&nbsp;&nbsp;&nbsp;';
 1714:         }
 1715:         if (!$is_custom->{'bgcol'}) {
 1716:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1717:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1718:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1719:         }
 1720:         $output .= '<br />';
 1721:     }
 1722:     $output .='<br />';
 1723:     return $output;
 1724: }
 1725: 
 1726: sub login_text_colors {
 1727:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1728:     my $color_menu = '<table border="0"><tr>';
 1729:     foreach my $item (@{$logintext}) {
 1730:         $color_menu .= '<td align="center">'.$choices->{$item};
 1731:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1732:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1733:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1734:     }
 1735:     $color_menu .= '</tr></table><br />';
 1736:     return $color_menu;
 1737: }
 1738: 
 1739: sub image_changes {
 1740:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1741:     my $output;
 1742:     if ($img eq 'login') {
 1743:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1744:     } elsif (!$is_custom) {
 1745:         if ($img ne 'domlogo') {
 1746:             $output = &mt('Default image:').'<br />';
 1747:         } else {
 1748:             $output = &mt('Default in use:').'<br />';
 1749:         }
 1750:     }
 1751:     if ($img ne 'login') {
 1752:         if ($img_import) {
 1753:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1754:         }
 1755:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1756:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1757:         if ($is_custom) {
 1758:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1759:                        '<input type="checkbox" name="'.
 1760:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1761:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1762:         } else {
 1763:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1764:         }
 1765:     }
 1766:     return $output;
 1767: }
 1768: 
 1769: sub print_quotas {
 1770:     my ($dom,$settings,$rowtotal,$action) = @_;
 1771:     my $context;
 1772:     if ($action eq 'quotas') {
 1773:         $context = 'tools';
 1774:     } else {
 1775:         $context = $action;
 1776:     }
 1777:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1778:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1779:     my $typecount = 0;
 1780:     my ($css_class,%titles);
 1781:     if ($context eq 'requestcourses') {
 1782:         @usertools = ('official','unofficial','community','textbook');
 1783:         @options =('norequest','approval','validate','autolimit');
 1784:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1785:         %titles = &courserequest_titles();
 1786:     } elsif ($context eq 'requestauthor') {
 1787:         @usertools = ('author');
 1788:         @options = ('norequest','approval','automatic');
 1789:         %titles = &authorrequest_titles();
 1790:     } else {
 1791:         @usertools = ('aboutme','blog','webdav','portfolio');
 1792:         %titles = &tool_titles();
 1793:     }
 1794:     if (ref($types) eq 'ARRAY') {
 1795:         foreach my $type (@{$types}) {
 1796:             my ($currdefquota,$currauthorquota);
 1797:             unless (($context eq 'requestcourses') ||
 1798:                     ($context eq 'requestauthor')) {
 1799:                 if (ref($settings) eq 'HASH') {
 1800:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1801:                         $currdefquota = $settings->{defaultquota}->{$type};
 1802:                     } else {
 1803:                         $currdefquota = $settings->{$type};
 1804:                     }
 1805:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1806:                         $currauthorquota = $settings->{authorquota}->{$type};
 1807:                     }
 1808:                 }
 1809:             }
 1810:             if (defined($usertypes->{$type})) {
 1811:                 $typecount ++;
 1812:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1813:                 $datatable .= '<tr'.$css_class.'>'.
 1814:                               '<td>'.$usertypes->{$type}.'</td>'.
 1815:                               '<td class="LC_left_item">';
 1816:                 if ($context eq 'requestcourses') {
 1817:                     $datatable .= '<table><tr>';
 1818:                 }
 1819:                 my %cell;  
 1820:                 foreach my $item (@usertools) {
 1821:                     if ($context eq 'requestcourses') {
 1822:                         my ($curroption,$currlimit);
 1823:                         if (ref($settings) eq 'HASH') {
 1824:                             if (ref($settings->{$item}) eq 'HASH') {
 1825:                                 $curroption = $settings->{$item}->{$type};
 1826:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1827:                                     $currlimit = $1; 
 1828:                                 }
 1829:                             }
 1830:                         }
 1831:                         if (!$curroption) {
 1832:                             $curroption = 'norequest';
 1833:                         }
 1834:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1835:                         foreach my $option (@options) {
 1836:                             my $val = $option;
 1837:                             if ($option eq 'norequest') {
 1838:                                 $val = 0;  
 1839:                             }
 1840:                             if ($option eq 'validate') {
 1841:                                 my $canvalidate = 0;
 1842:                                 if (ref($validations{$item}) eq 'HASH') { 
 1843:                                     if ($validations{$item}{$type}) {
 1844:                                         $canvalidate = 1;
 1845:                                     }
 1846:                                 }
 1847:                                 next if (!$canvalidate);
 1848:                             }
 1849:                             my $checked = '';
 1850:                             if ($option eq $curroption) {
 1851:                                 $checked = ' checked="checked"';
 1852:                             } elsif ($option eq 'autolimit') {
 1853:                                 if ($curroption =~ /^autolimit/) {
 1854:                                     $checked = ' checked="checked"';
 1855:                                 }                       
 1856:                             } 
 1857:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1858:                                   '<input type="radio" name="crsreq_'.$item.
 1859:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1860:                                   $titles{$option}.'</label>';
 1861:                             if ($option eq 'autolimit') {
 1862:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1863:                                                 $item.'_limit_'.$type.'" size="1" '.
 1864:                                                 'value="'.$currlimit.'" />';
 1865:                             }
 1866:                             $cell{$item} .= '</span> ';
 1867:                             if ($option eq 'autolimit') {
 1868:                                 $cell{$item} .= $titles{'unlimited'};
 1869:                             }
 1870:                         }
 1871:                     } elsif ($context eq 'requestauthor') {
 1872:                         my $curroption;
 1873:                         if (ref($settings) eq 'HASH') {
 1874:                             $curroption = $settings->{$type};
 1875:                         }
 1876:                         if (!$curroption) {
 1877:                             $curroption = 'norequest';
 1878:                         }
 1879:                         foreach my $option (@options) {
 1880:                             my $val = $option;
 1881:                             if ($option eq 'norequest') {
 1882:                                 $val = 0;
 1883:                             }
 1884:                             my $checked = '';
 1885:                             if ($option eq $curroption) {
 1886:                                 $checked = ' checked="checked"';
 1887:                             }
 1888:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1889:                                   '<input type="radio" name="authorreq_'.$type.
 1890:                                   '" value="'.$val.'"'.$checked.' />'.
 1891:                                   $titles{$option}.'</label></span>&nbsp; ';
 1892:                         }
 1893:                     } else {
 1894:                         my $checked = 'checked="checked" ';
 1895:                         if (ref($settings) eq 'HASH') {
 1896:                             if (ref($settings->{$item}) eq 'HASH') {
 1897:                                 if ($settings->{$item}->{$type} == 0) {
 1898:                                     $checked = '';
 1899:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1900:                                     $checked =  'checked="checked" ';
 1901:                                 }
 1902:                             }
 1903:                         }
 1904:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1905:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1906:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1907:                                       '</label></span>&nbsp; ';
 1908:                     }
 1909:                 }
 1910:                 if ($context eq 'requestcourses') {
 1911:                     $datatable .= '</tr><tr>';
 1912:                     foreach my $item (@usertools) {
 1913:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1914:                     }
 1915:                     $datatable .= '</tr></table>';
 1916:                 }
 1917:                 $datatable .= '</td>';
 1918:                 unless (($context eq 'requestcourses') ||
 1919:                         ($context eq 'requestauthor')) {
 1920:                     $datatable .= 
 1921:                               '<td class="LC_right_item">'.
 1922:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1923:                               '<input type="text" name="quota_'.$type.
 1924:                               '" value="'.$currdefquota.
 1925:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1926:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1927:                               '<input type="text" name="authorquota_'.$type.
 1928:                               '" value="'.$currauthorquota.
 1929:                               '" size="5" /></span></td>';
 1930:                 }
 1931:                 $datatable .= '</tr>';
 1932:             }
 1933:         }
 1934:     }
 1935:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1936:         $defaultquota = '20';
 1937:         $authorquota = '500';
 1938:         if (ref($settings) eq 'HASH') {
 1939:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1940:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1941:             } elsif (defined($settings->{'default'})) {
 1942:                 $defaultquota = $settings->{'default'};
 1943:             }
 1944:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1945:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1946:             }
 1947:         }
 1948:     }
 1949:     $typecount ++;
 1950:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1951:     $datatable .= '<tr'.$css_class.'>'.
 1952:                   '<td>'.$othertitle.'</td>'.
 1953:                   '<td class="LC_left_item">';
 1954:     if ($context eq 'requestcourses') {
 1955:         $datatable .= '<table><tr>';
 1956:     }
 1957:     my %defcell;
 1958:     foreach my $item (@usertools) {
 1959:         if ($context eq 'requestcourses') {
 1960:             my ($curroption,$currlimit);
 1961:             if (ref($settings) eq 'HASH') {
 1962:                 if (ref($settings->{$item}) eq 'HASH') {
 1963:                     $curroption = $settings->{$item}->{'default'};
 1964:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1965:                         $currlimit = $1;
 1966:                     }
 1967:                 }
 1968:             }
 1969:             if (!$curroption) {
 1970:                 $curroption = 'norequest';
 1971:             }
 1972:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1973:             foreach my $option (@options) {
 1974:                 my $val = $option;
 1975:                 if ($option eq 'norequest') {
 1976:                     $val = 0;
 1977:                 }
 1978:                 if ($option eq 'validate') {
 1979:                     my $canvalidate = 0;
 1980:                     if (ref($validations{$item}) eq 'HASH') {
 1981:                         if ($validations{$item}{'default'}) {
 1982:                             $canvalidate = 1;
 1983:                         }
 1984:                     }
 1985:                     next if (!$canvalidate);
 1986:                 }
 1987:                 my $checked = '';
 1988:                 if ($option eq $curroption) {
 1989:                     $checked = ' checked="checked"';
 1990:                 } elsif ($option eq 'autolimit') {
 1991:                     if ($curroption =~ /^autolimit/) {
 1992:                         $checked = ' checked="checked"';
 1993:                     }
 1994:                 }
 1995:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1996:                                   '<input type="radio" name="crsreq_'.$item.
 1997:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1998:                                   $titles{$option}.'</label>';
 1999:                 if ($option eq 'autolimit') {
 2000:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2001:                                        $item.'_limit_default" size="1" '.
 2002:                                        'value="'.$currlimit.'" />';
 2003:                 }
 2004:                 $defcell{$item} .= '</span> ';
 2005:                 if ($option eq 'autolimit') {
 2006:                     $defcell{$item} .= $titles{'unlimited'};
 2007:                 }
 2008:             }
 2009:         } elsif ($context eq 'requestauthor') {
 2010:             my $curroption;
 2011:             if (ref($settings) eq 'HASH') {
 2012:                 $curroption = $settings->{'default'};
 2013:             }
 2014:             if (!$curroption) {
 2015:                 $curroption = 'norequest';
 2016:             }
 2017:             foreach my $option (@options) {
 2018:                 my $val = $option;
 2019:                 if ($option eq 'norequest') {
 2020:                     $val = 0;
 2021:                 }
 2022:                 my $checked = '';
 2023:                 if ($option eq $curroption) {
 2024:                     $checked = ' checked="checked"';
 2025:                 }
 2026:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2027:                               '<input type="radio" name="authorreq_default"'.
 2028:                               ' value="'.$val.'"'.$checked.' />'.
 2029:                               $titles{$option}.'</label></span>&nbsp; ';
 2030:             }
 2031:         } else {
 2032:             my $checked = 'checked="checked" ';
 2033:             if (ref($settings) eq 'HASH') {
 2034:                 if (ref($settings->{$item}) eq 'HASH') {
 2035:                     if ($settings->{$item}->{'default'} == 0) {
 2036:                         $checked = '';
 2037:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2038:                         $checked = 'checked="checked" ';
 2039:                     }
 2040:                 }
 2041:             }
 2042:             $datatable .= '<span class="LC_nobreak"><label>'.
 2043:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2044:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2045:                           '</label></span>&nbsp; ';
 2046:         }
 2047:     }
 2048:     if ($context eq 'requestcourses') {
 2049:         $datatable .= '</tr><tr>';
 2050:         foreach my $item (@usertools) {
 2051:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2052:         }
 2053:         $datatable .= '</tr></table>';
 2054:     }
 2055:     $datatable .= '</td>';
 2056:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2057:         $datatable .= '<td class="LC_right_item">'.
 2058:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2059:                       '<input type="text" name="defaultquota" value="'.
 2060:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2061:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2062:                       '<input type="text" name="authorquota" value="'.
 2063:                       $authorquota.'" size="5" /></span></td>';
 2064:     }
 2065:     $datatable .= '</tr>';
 2066:     $typecount ++;
 2067:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2068:     $datatable .= '<tr'.$css_class.'>'.
 2069:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2070:     if ($context eq 'requestcourses') {
 2071:         $datatable .= &mt('(overrides affiliation, if set)').
 2072:                       '</td>'.
 2073:                       '<td class="LC_left_item">'.
 2074:                       '<table><tr>';
 2075:     } else {
 2076:         $datatable .= &mt('(overrides affiliation, if checked)').
 2077:                       '</td>'.
 2078:                       '<td class="LC_left_item" colspan="2">'.
 2079:                       '<br />';
 2080:     }
 2081:     my %advcell;
 2082:     foreach my $item (@usertools) {
 2083:         if ($context eq 'requestcourses') {
 2084:             my ($curroption,$currlimit);
 2085:             if (ref($settings) eq 'HASH') {
 2086:                 if (ref($settings->{$item}) eq 'HASH') {
 2087:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2088:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2089:                         $currlimit = $1;
 2090:                     }
 2091:                 }
 2092:             }
 2093:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2094:             my $checked = '';
 2095:             if ($curroption eq '') {
 2096:                 $checked = ' checked="checked"';
 2097:             }
 2098:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2099:                                '<input type="radio" name="crsreq_'.$item.
 2100:                                '__LC_adv" value=""'.$checked.' />'.
 2101:                                &mt('No override set').'</label></span>&nbsp; ';
 2102:             foreach my $option (@options) {
 2103:                 my $val = $option;
 2104:                 if ($option eq 'norequest') {
 2105:                     $val = 0;
 2106:                 }
 2107:                 if ($option eq 'validate') {
 2108:                     my $canvalidate = 0;
 2109:                     if (ref($validations{$item}) eq 'HASH') {
 2110:                         if ($validations{$item}{'_LC_adv'}) {
 2111:                             $canvalidate = 1;
 2112:                         }
 2113:                     }
 2114:                     next if (!$canvalidate);
 2115:                 }
 2116:                 my $checked = '';
 2117:                 if ($val eq $curroption) {
 2118:                     $checked = ' checked="checked"';
 2119:                 } elsif ($option eq 'autolimit') {
 2120:                     if ($curroption =~ /^autolimit/) {
 2121:                         $checked = ' checked="checked"';
 2122:                     }
 2123:                 }
 2124:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2125:                                   '<input type="radio" name="crsreq_'.$item.
 2126:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2127:                                   $titles{$option}.'</label>';
 2128:                 if ($option eq 'autolimit') {
 2129:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2130:                                        $item.'_limit__LC_adv" size="1" '.
 2131:                                        'value="'.$currlimit.'" />';
 2132:                 }
 2133:                 $advcell{$item} .= '</span> ';
 2134:                 if ($option eq 'autolimit') {
 2135:                     $advcell{$item} .= $titles{'unlimited'};
 2136:                 }
 2137:             }
 2138:         } elsif ($context eq 'requestauthor') {
 2139:             my $curroption;
 2140:             if (ref($settings) eq 'HASH') {
 2141:                 $curroption = $settings->{'_LC_adv'};
 2142:             }
 2143:             my $checked = '';
 2144:             if ($curroption eq '') {
 2145:                 $checked = ' checked="checked"';
 2146:             }
 2147:             $datatable .= '<span class="LC_nobreak"><label>'.
 2148:                           '<input type="radio" name="authorreq__LC_adv"'.
 2149:                           ' value=""'.$checked.' />'.
 2150:                           &mt('No override set').'</label></span>&nbsp; ';
 2151:             foreach my $option (@options) {
 2152:                 my $val = $option;
 2153:                 if ($option eq 'norequest') {
 2154:                     $val = 0;
 2155:                 }
 2156:                 my $checked = '';
 2157:                 if ($val eq $curroption) {
 2158:                     $checked = ' checked="checked"';
 2159:                 }
 2160:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2161:                               '<input type="radio" name="authorreq__LC_adv"'.
 2162:                               ' value="'.$val.'"'.$checked.' />'.
 2163:                               $titles{$option}.'</label></span>&nbsp; ';
 2164:             }
 2165:         } else {
 2166:             my $checked = 'checked="checked" ';
 2167:             if (ref($settings) eq 'HASH') {
 2168:                 if (ref($settings->{$item}) eq 'HASH') {
 2169:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2170:                         $checked = '';
 2171:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2172:                         $checked = 'checked="checked" ';
 2173:                     }
 2174:                 }
 2175:             }
 2176:             $datatable .= '<span class="LC_nobreak"><label>'.
 2177:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2178:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2179:                           '</label></span>&nbsp; ';
 2180:         }
 2181:     }
 2182:     if ($context eq 'requestcourses') {
 2183:         $datatable .= '</tr><tr>';
 2184:         foreach my $item (@usertools) {
 2185:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2186:         }
 2187:         $datatable .= '</tr></table>';
 2188:     }
 2189:     $datatable .= '</td></tr>';
 2190:     $$rowtotal += $typecount;
 2191:     return $datatable;
 2192: }
 2193: 
 2194: sub print_requestmail {
 2195:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2196:     my ($now,$datatable,%currapp);
 2197:     $now = time;
 2198:     if (ref($settings) eq 'HASH') {
 2199:         if (ref($settings->{'notify'}) eq 'HASH') {
 2200:             if ($settings->{'notify'}{'approval'} ne '') {
 2201:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2202:             }
 2203:         }
 2204:     }
 2205:     my $numinrow = 2;
 2206:     my $css_class;
 2207:     if ($$rowtotal%2) {
 2208:         $css_class = 'LC_odd_row';
 2209:     }
 2210:     if ($customcss) {
 2211:         $css_class .= " $customcss";
 2212:     }
 2213:     $css_class =~ s/^\s+//;
 2214:     if ($css_class) {
 2215:         $css_class = ' class="'.$css_class.'"';
 2216:     }
 2217:     if ($rowstyle) {
 2218:         $css_class .= ' style="'.$rowstyle.'"';
 2219:     }
 2220:     my $text;
 2221:     if ($action eq 'requestcourses') {
 2222:         $text = &mt('Receive notification of course requests requiring approval');
 2223:     } elsif ($action eq 'requestauthor') {
 2224:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2225:     } else {
 2226:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2227:     }
 2228:     $datatable = '<tr'.$css_class.'>'.
 2229:                  ' <td>'.$text.'</td>'.
 2230:                  ' <td class="LC_left_item">';
 2231:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2232:                                                  $action.'notifyapproval',%currapp);
 2233:     if ($numdc > 0) {
 2234:         $datatable .= $table;
 2235:     } else {
 2236:         $datatable .= &mt('There are no active Domain Coordinators');
 2237:     }
 2238:     $datatable .='</td></tr>';
 2239:     return $datatable;
 2240: }
 2241: 
 2242: sub print_studentcode {
 2243:     my ($settings,$rowtotal) = @_;
 2244:     my $rownum = 0; 
 2245:     my ($output,%current);
 2246:     my @crstypes = ('official','unofficial','community','textbook');
 2247:     if (ref($settings) eq 'HASH') {
 2248:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2249:             foreach my $type (@crstypes) {
 2250:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2251:             }
 2252:         }
 2253:     }
 2254:     $output .= '<tr>'.
 2255:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2256:                '<td class="LC_left_item">';
 2257:     foreach my $type (@crstypes) {
 2258:         my $check = ' ';
 2259:         if ($current{$type}) {
 2260:             $check = ' checked="checked" ';
 2261:         }
 2262:         $output .= '<span class="LC_nobreak"><label>'.
 2263:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2264:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2265:     }
 2266:     $output .= '</td></tr>';
 2267:     $$rowtotal ++;
 2268:     return $output;
 2269: }
 2270: 
 2271: sub print_textbookcourses {
 2272:     my ($dom,$type,$settings,$rowtotal) = @_;
 2273:     my $rownum = 0;
 2274:     my $css_class;
 2275:     my $itemcount = 1;
 2276:     my $maxnum = 0;
 2277:     my $bookshash;
 2278:     if (ref($settings) eq 'HASH') {
 2279:         $bookshash = $settings->{$type};
 2280:     }
 2281:     my %ordered;
 2282:     if (ref($bookshash) eq 'HASH') {
 2283:         foreach my $item (keys(%{$bookshash})) {
 2284:             if (ref($bookshash->{$item}) eq 'HASH') {
 2285:                 my $num = $bookshash->{$item}{'order'};
 2286:                 $ordered{$num} = $item;
 2287:             }
 2288:         }
 2289:     }
 2290:     my $confname = $dom.'-domainconfig';
 2291:     my $switchserver = &check_switchserver($dom,$confname);
 2292:     my $maxnum = scalar(keys(%ordered));
 2293:     my $datatable;
 2294:     if (keys(%ordered)) {
 2295:         my @items = sort { $a <=> $b } keys(%ordered);
 2296:         for (my $i=0; $i<@items; $i++) {
 2297:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2298:             my $key = $ordered{$items[$i]};
 2299:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2300:             my $coursetitle = $coursehash{'description'};
 2301:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2302:             if (ref($bookshash->{$key}) eq 'HASH') {
 2303:                 $subject = $bookshash->{$key}->{'subject'};
 2304:                 $title = $bookshash->{$key}->{'title'};
 2305:                 if ($type eq 'textbooks') {
 2306:                     $publisher = $bookshash->{$key}->{'publisher'};
 2307:                     $author = $bookshash->{$key}->{'author'};
 2308:                     $image = $bookshash->{$key}->{'image'};
 2309:                     if ($image ne '') {
 2310:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2311:                         my $imagethumb = "$path/tn-".$imagefile;
 2312:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2313:                     }
 2314:                 }
 2315:             }
 2316:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2317:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2318:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2319:             for (my $k=0; $k<=$maxnum; $k++) {
 2320:                 my $vpos = $k+1;
 2321:                 my $selstr;
 2322:                 if ($k == $i) {
 2323:                     $selstr = ' selected="selected" ';
 2324:                 }
 2325:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2326:             }
 2327:             $datatable .= '</select>'.('&nbsp;'x2).
 2328:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2329:                 &mt('Delete?').'</label></span></td>'.
 2330:                 '<td colspan="2">'.
 2331:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2332:                 ('&nbsp;'x2).
 2333:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2334:             if ($type eq 'textbooks') {
 2335:                 $datatable .= ('&nbsp;'x2).
 2336:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2337:                               ('&nbsp;'x2).
 2338:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2339:                               ('&nbsp;'x2).
 2340:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2341:                 if ($image) {
 2342:                     $datatable .= '<span class="LC_nobreak">'.
 2343:                                   $imgsrc.
 2344:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2345:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2346:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2347:                 }
 2348:                 if ($switchserver) {
 2349:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2350:                 } else {
 2351:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2352:                 }
 2353:             }
 2354:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2355:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2356:                           $coursetitle.'</span></td></tr>'."\n";
 2357:             $itemcount ++;
 2358:         }
 2359:     }
 2360:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2361:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2362:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2363:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2364:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2365:     for (my $k=0; $k<$maxnum+1; $k++) {
 2366:         my $vpos = $k+1;
 2367:         my $selstr;
 2368:         if ($k == $maxnum) {
 2369:             $selstr = ' selected="selected" ';
 2370:         }
 2371:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2372:     }
 2373:     $datatable .= '</select>&nbsp;'."\n".
 2374:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2375:                   '<td colspan="2">'.
 2376:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2377:                   ('&nbsp;'x2).
 2378:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2379:                   ('&nbsp;'x2);
 2380:     if ($type eq 'textbooks') {
 2381:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2382:                       ('&nbsp;'x2).
 2383:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2384:                       ('&nbsp;'x2).
 2385:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2386:         if ($switchserver) {
 2387:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2388:         } else {
 2389:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2390:         }
 2391:         $datatable .= '</span>'."\n";
 2392:     }
 2393:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2394:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2395:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2396:                   &Apache::loncommon::selectcourse_link
 2397:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2398:                   '</span></td>'."\n".
 2399:                   '</tr>'."\n";
 2400:     $itemcount ++;
 2401:     return $datatable;
 2402: }
 2403: 
 2404: sub textbookcourses_javascript {
 2405:     my ($settings) = @_;
 2406:     return unless(ref($settings) eq 'HASH');
 2407:     my (%ordered,%total,%jstext);
 2408:     foreach my $type ('textbooks','templates') {
 2409:         $total{$type} = 0;
 2410:         if (ref($settings->{$type}) eq 'HASH') {
 2411:             foreach my $item (keys(%{$settings->{$type}})) {
 2412:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2413:                     my $num = $settings->{$type}->{$item}{'order'};
 2414:                     $ordered{$type}{$num} = $item;
 2415:                 }
 2416:             }
 2417:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2418:         }
 2419:         my @jsarray = ();
 2420:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2421:             push(@jsarray,$ordered{$type}{$item});
 2422:         }
 2423:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2424:     }
 2425:     return <<"ENDSCRIPT";
 2426: <script type="text/javascript">
 2427: // <![CDATA[
 2428: function reorderBooks(form,item,caller) {
 2429:     var changedVal;
 2430: $jstext{'textbooks'};
 2431: $jstext{'templates'};
 2432:     var newpos;
 2433:     var maxh;
 2434:     if (caller == 'textbooks') {  
 2435:         newpos = 'textbooks_addbook_pos';
 2436:         maxh = 1 + $total{'textbooks'};
 2437:     } else {
 2438:         newpos = 'templates_addbook_pos';
 2439:         maxh = 1 + $total{'templates'};
 2440:     }
 2441:     var current = new Array;
 2442:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2443:     if (item == newpos) {
 2444:         changedVal = newitemVal;
 2445:     } else {
 2446:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2447:         current[newitemVal] = newpos;
 2448:     }
 2449:     if (caller == 'textbooks') {
 2450:         for (var i=0; i<textbooks.length; i++) {
 2451:             var elementName = 'textbooks_'+textbooks[i];
 2452:             if (elementName != item) {
 2453:                 if (form.elements[elementName]) {
 2454:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2455:                     current[currVal] = elementName;
 2456:                 }
 2457:             }
 2458:         }
 2459:     }
 2460:     if (caller == 'templates') {
 2461:         for (var i=0; i<templates.length; i++) {
 2462:             var elementName = 'templates_'+templates[i];
 2463:             if (elementName != item) {
 2464:                 if (form.elements[elementName]) {
 2465:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2466:                     current[currVal] = elementName;
 2467:                 }
 2468:             }
 2469:         }
 2470:     }
 2471:     var oldVal;
 2472:     for (var j=0; j<maxh; j++) {
 2473:         if (current[j] == undefined) {
 2474:             oldVal = j;
 2475:         }
 2476:     }
 2477:     if (oldVal < changedVal) {
 2478:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2479:            var elementName = current[k];
 2480:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2481:         }
 2482:     } else {
 2483:         for (var k=changedVal; k<oldVal; k++) {
 2484:             var elementName = current[k];
 2485:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2486:         }
 2487:     }
 2488:     return;
 2489: }
 2490: 
 2491: // ]]>
 2492: </script>
 2493: 
 2494: ENDSCRIPT
 2495: }
 2496: 
 2497: sub print_autoenroll {
 2498:     my ($dom,$settings,$rowtotal) = @_;
 2499:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2500:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2501:     if (ref($settings) eq 'HASH') {
 2502:         if (exists($settings->{'run'})) {
 2503:             if ($settings->{'run'} eq '0') {
 2504:                 $runoff = ' checked="checked" ';
 2505:                 $runon = ' ';
 2506:             } else {
 2507:                 $runon = ' checked="checked" ';
 2508:                 $runoff = ' ';
 2509:             }
 2510:         } else {
 2511:             if ($autorun) {
 2512:                 $runon = ' checked="checked" ';
 2513:                 $runoff = ' ';
 2514:             } else {
 2515:                 $runoff = ' checked="checked" ';
 2516:                 $runon = ' ';
 2517:             }
 2518:         }
 2519:         if (exists($settings->{'co-owners'})) {
 2520:             if ($settings->{'co-owners'} eq '0') {
 2521:                 $coownersoff = ' checked="checked" ';
 2522:                 $coownerson = ' ';
 2523:             } else {
 2524:                 $coownerson = ' checked="checked" ';
 2525:                 $coownersoff = ' ';
 2526:             }
 2527:         } else {
 2528:             $coownersoff = ' checked="checked" ';
 2529:             $coownerson = ' ';
 2530:         }
 2531:         if (exists($settings->{'sender_domain'})) {
 2532:             $defdom = $settings->{'sender_domain'};
 2533:         }
 2534:         if (exists($settings->{'autofailsafe'})) {
 2535:             $failsafe = $settings->{'autofailsafe'};
 2536:         }
 2537:     } else {
 2538:         if ($autorun) {
 2539:             $runon = ' checked="checked" ';
 2540:             $runoff = ' ';
 2541:         } else {
 2542:             $runoff = ' checked="checked" ';
 2543:             $runon = ' ';
 2544:         }
 2545:     }
 2546:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2547:     my $notif_sender;
 2548:     if (ref($settings) eq 'HASH') {
 2549:         $notif_sender = $settings->{'sender_uname'};
 2550:     }
 2551:     my $datatable='<tr class="LC_odd_row">'.
 2552:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2553:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2554:                   '<input type="radio" name="autoenroll_run"'.
 2555:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2556:                   '<label><input type="radio" name="autoenroll_run"'.
 2557:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2558:                   '</tr><tr>'.
 2559:                   '<td>'.&mt('Notification messages - sender').
 2560:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2561:                   &mt('username').':&nbsp;'.
 2562:                   '<input type="text" name="sender_uname" value="'.
 2563:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2564:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2565:                   '<tr class="LC_odd_row">'.
 2566:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2567:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2568:                   '<input type="radio" name="autoassign_coowners"'.
 2569:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2570:                   '<label><input type="radio" name="autoassign_coowners"'.
 2571:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2572:                   '</tr><tr>'.
 2573:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2574:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2575:                   '<input type="text" name="autoenroll_failsafe"'.
 2576:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 2577:     $$rowtotal += 4;
 2578:     return $datatable;
 2579: }
 2580: 
 2581: sub print_autoupdate {
 2582:     my ($position,$dom,$settings,$rowtotal) = @_;
 2583:     my $datatable;
 2584:     if ($position eq 'top') {
 2585:         my $updateon = ' ';
 2586:         my $updateoff = ' checked="checked" ';
 2587:         my $classlistson = ' ';
 2588:         my $classlistsoff = ' checked="checked" ';
 2589:         if (ref($settings) eq 'HASH') {
 2590:             if ($settings->{'run'} eq '1') {
 2591:                 $updateon = $updateoff;
 2592:                 $updateoff = ' ';
 2593:             }
 2594:             if ($settings->{'classlists'} eq '1') {
 2595:                 $classlistson = $classlistsoff;
 2596:                 $classlistsoff = ' ';
 2597:             }
 2598:         }
 2599:         my %title = (
 2600:                    run => 'Auto-update active?',
 2601:                    classlists => 'Update information in classlists?',
 2602:                     );
 2603:         $datatable = '<tr class="LC_odd_row">'. 
 2604:                   '<td>'.&mt($title{'run'}).'</td>'.
 2605:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2606:                   '<input type="radio" name="autoupdate_run"'.
 2607:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2608:                   '<label><input type="radio" name="autoupdate_run"'.
 2609:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2610:                   '</tr><tr>'.
 2611:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2612:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2613:                   '<label><input type="radio" name="classlists"'.
 2614:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2615:                   '<label><input type="radio" name="classlists"'.
 2616:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2617:                   '</tr>';
 2618:         $$rowtotal += 2;
 2619:     } elsif ($position eq 'middle') {
 2620:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2621:         my $numinrow = 3;
 2622:         my $locknamesettings;
 2623:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2624:                                      $dom,$numinrow,$othertitle,
 2625:                                     'lockablenames',$rowtotal);
 2626:         $$rowtotal ++;
 2627:     } else {
 2628:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2629:         my @fields = ('lastname','firstname','middlename','generation',
 2630:                       'permanentemail','id');
 2631:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2632:         my $numrows = 0;
 2633:         if (ref($types) eq 'ARRAY') {
 2634:             if (@{$types} > 0) {
 2635:                 $datatable = 
 2636:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2637:                                          \@fields,$types,\$numrows);
 2638:                     $$rowtotal += @{$types}; 
 2639:             }
 2640:         }
 2641:         $datatable .= 
 2642:             &usertype_update_row($settings,{'default' => $othertitle},
 2643:                                  \%fieldtitles,\@fields,['default'],
 2644:                                  \$numrows);
 2645:         $$rowtotal ++;     
 2646:     }
 2647:     return $datatable;
 2648: }
 2649: 
 2650: sub print_autocreate {
 2651:     my ($dom,$settings,$rowtotal) = @_;
 2652:     my (%createon,%createoff,%currhash);
 2653:     my @types = ('xml','req');
 2654:     if (ref($settings) eq 'HASH') {
 2655:         foreach my $item (@types) {
 2656:             $createoff{$item} = ' checked="checked" ';
 2657:             $createon{$item} = ' ';
 2658:             if (exists($settings->{$item})) {
 2659:                 if ($settings->{$item}) {
 2660:                     $createon{$item} = ' checked="checked" ';
 2661:                     $createoff{$item} = ' ';
 2662:                 }
 2663:             }
 2664:         }
 2665:         if ($settings->{'xmldc'} ne '') {
 2666:             $currhash{$settings->{'xmldc'}} = 1;
 2667:         }
 2668:     } else {
 2669:         foreach my $item (@types) {
 2670:             $createoff{$item} = ' checked="checked" ';
 2671:             $createon{$item} = ' ';
 2672:         }
 2673:     }
 2674:     $$rowtotal += 2;
 2675:     my $numinrow = 2;
 2676:     my $datatable='<tr class="LC_odd_row">'.
 2677:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2678:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2679:                   '<input type="radio" name="autocreate_xml"'.
 2680:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2681:                   '<label><input type="radio" name="autocreate_xml"'.
 2682:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2683:                   '</td></tr><tr>'.
 2684:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2685:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2686:                   '<input type="radio" name="autocreate_req"'.
 2687:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2688:                   '<label><input type="radio" name="autocreate_req"'.
 2689:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2690:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2691:                                                    'autocreate_xmldc',%currhash);
 2692:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2693:     if ($numdc > 1) {
 2694:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2695:                       '</td><td class="LC_left_item">';
 2696:     } else {
 2697:         $datatable .= &mt('Course creation processed as:').
 2698:                       '</td><td class="LC_right_item">';
 2699:     }
 2700:     $datatable .= $dctable.'</td></tr>';
 2701:     $$rowtotal += $rows;
 2702:     return $datatable;
 2703: }
 2704: 
 2705: sub print_directorysrch {
 2706:     my ($position,$dom,$settings,$rowtotal) = @_;
 2707:     my $datatable;
 2708:     if ($position eq 'top') {
 2709:         my $instsrchon = ' ';
 2710:         my $instsrchoff = ' checked="checked" ';
 2711:         my ($exacton,$containson,$beginson);
 2712:         my $instlocalon = ' ';
 2713:         my $instlocaloff = ' checked="checked" ';
 2714:         if (ref($settings) eq 'HASH') {
 2715:             if ($settings->{'available'} eq '1') {
 2716:                 $instsrchon = $instsrchoff;
 2717:                 $instsrchoff = ' ';
 2718:             }
 2719:             if ($settings->{'localonly'} eq '1') {
 2720:                 $instlocalon = $instlocaloff;
 2721:                 $instlocaloff = ' ';
 2722:             }
 2723:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2724:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2725:                     if ($type eq 'exact') {
 2726:                         $exacton = ' checked="checked" ';
 2727:                     } elsif ($type eq 'contains') {
 2728:                         $containson = ' checked="checked" ';
 2729:                     } elsif ($type eq 'begins') {
 2730:                         $beginson = ' checked="checked" ';
 2731:                     }
 2732:                 }
 2733:             } else {
 2734:                 if ($settings->{'searchtypes'} eq 'exact') {
 2735:                     $exacton = ' checked="checked" ';
 2736:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2737:                     $containson = ' checked="checked" ';
 2738:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2739:                     $exacton = ' checked="checked" ';
 2740:                     $containson = ' checked="checked" ';
 2741:                 }
 2742:             }
 2743:         }
 2744:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2745:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2746: 
 2747:         my $numinrow = 4;
 2748:         my $cansrchrow = 0;
 2749:         $datatable='<tr class="LC_odd_row">'.
 2750:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2751:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2752:                    '<input type="radio" name="dirsrch_available"'.
 2753:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2754:                    '<label><input type="radio" name="dirsrch_available"'.
 2755:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2756:                    '</tr><tr>'.
 2757:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2758:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2759:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2760:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2761:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2762:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2763:                    '</tr>';
 2764:         $$rowtotal += 2;
 2765:         if (ref($usertypes) eq 'HASH') {
 2766:             if (keys(%{$usertypes}) > 0) {
 2767:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2768:                                              $numinrow,$othertitle,'cansearch',
 2769:                                              $rowtotal);
 2770:                 $cansrchrow = 1;
 2771:             }
 2772:         }
 2773:         if ($cansrchrow) {
 2774:             $$rowtotal ++;
 2775:             $datatable .= '<tr>';
 2776:         } else {
 2777:             $datatable .= '<tr class="LC_odd_row">';
 2778:         }
 2779:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2780:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2781:         foreach my $title (@{$titleorder}) {
 2782:             if (defined($searchtitles->{$title})) {
 2783:                 my $check = ' ';
 2784:                 if (ref($settings) eq 'HASH') {
 2785:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2786:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2787:                             $check = ' checked="checked" ';
 2788:                         }
 2789:                     }
 2790:                 }
 2791:                 $datatable .= '<td class="LC_left_item">'.
 2792:                               '<span class="LC_nobreak"><label>'.
 2793:                               '<input type="checkbox" name="searchby" '.
 2794:                               'value="'.$title.'"'.$check.'/>'.
 2795:                               $searchtitles->{$title}.'</label></span></td>';
 2796:             }
 2797:         }
 2798:         $datatable .= '</tr></table></td></tr>';
 2799:         $$rowtotal ++;
 2800:         if ($cansrchrow) {
 2801:             $datatable .= '<tr class="LC_odd_row">';
 2802:         } else {
 2803:             $datatable .= '<tr>';
 2804:         }
 2805:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2806:                       '<td class="LC_left_item" colspan="2">'.
 2807:                       '<span class="LC_nobreak"><label>'.
 2808:                       '<input type="checkbox" name="searchtypes" '.
 2809:                       $exacton.' value="exact" />'.&mt('Exact match').
 2810:                       '</label>&nbsp;'.
 2811:                       '<label><input type="checkbox" name="searchtypes" '.
 2812:                       $beginson.' value="begins" />'.&mt('Begins with').
 2813:                       '</label>&nbsp;'.
 2814:                       '<label><input type="checkbox" name="searchtypes" '.
 2815:                       $containson.' value="contains" />'.&mt('Contains').
 2816:                       '</label></span></td></tr>';
 2817:         $$rowtotal ++;
 2818:     } else {
 2819:         my $domsrchon = ' checked="checked" ';
 2820:         my $domsrchoff = ' ';
 2821:         my $domlocalon = ' ';
 2822:         my $domlocaloff = ' checked="checked" ';
 2823:         if (ref($settings) eq 'HASH') {
 2824:             if ($settings->{'lclocalonly'} eq '1') {
 2825:                 $domlocalon = $domlocaloff;
 2826:                 $domlocaloff = ' ';
 2827:             }
 2828:             if ($settings->{'lcavailable'} eq '0') {
 2829:                 $domsrchoff = $domsrchon;
 2830:                 $domsrchon = ' ';
 2831:             }
 2832:         }
 2833:         $datatable='<tr class="LC_odd_row">'.
 2834:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2835:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2836:                       '<input type="radio" name="dirsrch_domavailable"'.
 2837:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2838:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2839:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2840:                       '</tr><tr>'.
 2841:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2842:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2843:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2844:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2845:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2846:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2847:                       '</tr>';
 2848:         $$rowtotal += 2;
 2849:     }
 2850:     return $datatable;
 2851: }
 2852: 
 2853: sub print_contacts {
 2854:     my ($position,$dom,$settings,$rowtotal) = @_;
 2855:     my $datatable;
 2856:     my @contacts = ('adminemail','supportemail');
 2857:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2858:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2859:     if ($position eq 'top') {
 2860:         if (ref($settings) eq 'HASH') {
 2861:             foreach my $item (@contacts) {
 2862:                 if (exists($settings->{$item})) {
 2863:                     $to{$item} = $settings->{$item};
 2864:                 }
 2865:             }
 2866:         }
 2867:     } elsif ($position eq 'middle') {
 2868:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2869:                      'updatesmail','idconflictsmail','hostipmail');
 2870:         foreach my $type (@mailings) {
 2871:             $otheremails{$type} = '';
 2872:         }
 2873:     } else {
 2874:         @mailings = ('helpdeskmail','otherdomsmail');
 2875:         foreach my $type (@mailings) {
 2876:             $otheremails{$type} = '';
 2877:         }
 2878:         $bccemails{'helpdeskmail'} = '';
 2879:         $bccemails{'otherdomsmail'} = '';
 2880:         $includestr{'helpdeskmail'} = '';
 2881:         $includestr{'otherdomsmail'} = '';
 2882:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2883:     }
 2884:     if (ref($settings) eq 'HASH') {
 2885:         unless ($position eq 'top') {
 2886:             foreach my $type (@mailings) {
 2887:                 if (exists($settings->{$type})) {
 2888:                     if (ref($settings->{$type}) eq 'HASH') {
 2889:                         foreach my $item (@contacts) {
 2890:                             if ($settings->{$type}{$item}) {
 2891:                                 $checked{$type}{$item} = ' checked="checked" ';
 2892:                             }
 2893:                         }
 2894:                         $otheremails{$type} = $settings->{$type}{'others'};
 2895:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2896:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2897:                             if ($settings->{$type}{'include'} ne '') {
 2898:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2899:                                 $includestr{$type} = &unescape($includestr{$type});
 2900:                             }
 2901:                         }
 2902:                     }
 2903:                 } elsif ($type eq 'lonstatusmail') {
 2904:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2905:                 }
 2906:             }
 2907:         }
 2908:         if ($position eq 'bottom') {
 2909:             foreach my $type (@mailings) {
 2910:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 2911:                 if ($settings->{$type}{'include'} ne '') {
 2912:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2913:                     $includestr{$type} = &unescape($includestr{$type});
 2914:                 }
 2915:             }
 2916:             if (ref($settings->{'helpform'}) eq 'HASH') {
 2917:                 if (ref($fields) eq 'ARRAY') {
 2918:                     foreach my $field (@{$fields}) {
 2919:                         $currfield{$field} = $settings->{'helpform'}{$field};
 2920:                     }
 2921:                 }
 2922:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 2923:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 2924:                 } else {
 2925:                     $maxsize = '1.0';
 2926:                 }
 2927:             } else {
 2928:                 if (ref($fields) eq 'ARRAY') {
 2929:                     foreach my $field (@{$fields}) {
 2930:                         $currfield{$field} = 'yes';
 2931:                     }
 2932:                 }
 2933:                 $maxsize = '1.0';
 2934:             }
 2935:         }
 2936:     } else {
 2937:         if ($position eq 'top') {
 2938:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2939:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2940:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2941:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2942:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2943:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2944:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2945:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2946:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 2947:         } elsif ($position eq 'bottom') {
 2948:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2949:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 2950:             if (ref($fields) eq 'ARRAY') {
 2951:                 foreach my $field (@{$fields}) {
 2952:                     $currfield{$field} = 'yes';
 2953:                 }
 2954:             }
 2955:             $maxsize = '1.0';
 2956:         }
 2957:     }
 2958:     my ($titles,$short_titles) = &contact_titles();
 2959:     my $rownum = 0;
 2960:     my $css_class;
 2961:     if ($position eq 'top') {
 2962:         foreach my $item (@contacts) {
 2963:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2964:             $datatable .= '<tr'.$css_class.'>'. 
 2965:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 2966:                           '</span></td><td class="LC_right_item">'.
 2967:                           '<input type="text" name="'.$item.'" value="'.
 2968:                           $to{$item}.'" /></td></tr>';
 2969:             $rownum ++;
 2970:         }
 2971:     } elsif ($position eq 'bottom') {
 2972:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2973:         $datatable .= '<tr'.$css_class.'>'.
 2974:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 2975:                       &mt('(e-mail, subject, and description always shown)').
 2976:                       '</td><td class="LC_left_item">';
 2977:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 2978:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 2979:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 2980:             foreach my $field (@{$fields}) {
 2981:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 2982:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 2983:                     $datatable .= ' '.&mt('(logged-in users)');
 2984:                 }
 2985:                 $datatable .='</td><td>';
 2986:                 my $clickaction;
 2987:                 if ($field eq 'screenshot') {
 2988:                     $clickaction = ' onclick="screenshotSize(this);"';
 2989:                 }
 2990:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 2991:                     foreach my $option (@{$possoptions->{$field}}) {
 2992:                         my $checked;
 2993:                         if ($currfield{$field} eq $option) {
 2994:                             $checked = ' checked="checked"';
 2995:                         }
 2996:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2997:                                       '<input type="radio" name="helpform_'.$field.'" '.
 2998:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 2999:                                       '</label></span>'.('&nbsp;'x2);
 3000:                     }
 3001:                 }
 3002:                 if ($field eq 'screenshot') {
 3003:                     my $display;
 3004:                     if ($currfield{$field} eq 'no') {
 3005:                         $display = ' style="display:none"';
 3006:                     }
 3007:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3008:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3009:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3010:                 }
 3011:                 $datatable .= '</td></tr>';
 3012:             }
 3013:             $datatable .= '</table>';
 3014:         }
 3015:         $datatable .= '</td></tr>'."\n";
 3016:         $rownum ++;
 3017:     }
 3018:     unless ($position eq 'top') {
 3019:         foreach my $type (@mailings) {
 3020:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3021:             $datatable .= '<tr'.$css_class.'>'.
 3022:                           '<td><span class="LC_nobreak">'.
 3023:                           $titles->{$type}.': </span></td>'.
 3024:                           '<td class="LC_left_item">';
 3025:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3026:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3027:             }
 3028:             $datatable .= '<span class="LC_nobreak">';
 3029:             foreach my $item (@contacts) {
 3030:                 $datatable .= '<label>'.
 3031:                               '<input type="checkbox" name="'.$type.'"'.
 3032:                               $checked{$type}{$item}.
 3033:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3034:                               '</label>&nbsp;';
 3035:             }
 3036:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3037:                           '<input type="text" name="'.$type.'_others" '.
 3038:                           'value="'.$otheremails{$type}.'"  />';
 3039:             my %locchecked;
 3040:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3041:                 foreach my $loc ('s','b') {
 3042:                     if ($includeloc{$type} eq $loc) {
 3043:                         $locchecked{$loc} = ' checked="checked"';
 3044:                         last;
 3045:                     }
 3046:                 }
 3047:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3048:                               '<input type="text" name="'.$type.'_bcc" '.
 3049:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3050:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3051:                               &mt('Text automatically added to e-mail:').' '.
 3052:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3053:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3054:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3055:                               ('&nbsp;'x2).
 3056:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3057:                               '</span></fieldset>';
 3058:             }
 3059:             $datatable .= '</td></tr>'."\n";
 3060:             $rownum ++;
 3061:         }
 3062:     }
 3063:     if ($position eq 'middle') {
 3064:         my %choices;
 3065:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3066:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3067:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3068:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3069:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3070:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3071:         my @toggles = ('reporterrors','reportupdates');
 3072:         my %defaultchecked = ('reporterrors'  => 'on',
 3073:                               'reportupdates' => 'on');
 3074:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3075:                                                    \%choices,$rownum);
 3076:         $datatable .= $reports;
 3077:     } elsif ($position eq 'bottom') {
 3078:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3079:         my (@posstypes,%usertypeshash);
 3080:         if (ref($types) eq 'ARRAY') {
 3081:             @posstypes = @{$types};
 3082:         }
 3083:         if (@posstypes) {
 3084:             if (ref($usertypes) eq 'HASH') {
 3085:                 %usertypeshash = %{$usertypes};
 3086:             }
 3087:             my @overridden;
 3088:             my $numinrow = 4;
 3089:             if (ref($settings) eq 'HASH') {
 3090:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3091:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3092:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3093:                             push(@overridden,$key);
 3094:                             foreach my $item (@contacts) {
 3095:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3096:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3097:                                 }
 3098:                             }
 3099:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3100:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3101:                             $includeloc{'override_'.$key} = '';
 3102:                             $includestr{'override_'.$key} = '';
 3103:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3104:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 3105:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3106:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3107:                             }
 3108:                         }
 3109:                     }
 3110:                 }
 3111:             }
 3112:             my $customclass = 'LC_helpdesk_override';
 3113:             my $optionsprefix = 'LC_options_helpdesk_';
 3114: 
 3115:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3116: 
 3117:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3118:                                          $numinrow,$othertitle,'overrides',
 3119:                                          \$rownum,$onclicktypes,$customclass);
 3120:             $rownum ++;
 3121:             $usertypeshash{'default'} = $othertitle;
 3122:             foreach my $status (@posstypes) {
 3123:                 my $css_class;
 3124:                 if ($rownum%2) {
 3125:                     $css_class = 'LC_odd_row ';
 3126:                 }
 3127:                 $css_class .= $customclass;
 3128:                 my $rowid = $optionsprefix.$status;
 3129:                 my $hidden = 1;
 3130:                 my $currstyle = 'display:none';
 3131:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3132:                     $currstyle = 'display:table-row';
 3133:                     $hidden = 0;
 3134:                 }
 3135:                 my $key = 'override_'.$status;
 3136:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3137:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3138:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3139:                                                   \@contacts,$short_titles);
 3140:                 unless ($hidden) {
 3141:                     $rownum ++;
 3142:                 }
 3143:             }
 3144:         }
 3145:     }
 3146:     $$rowtotal += $rownum;
 3147:     return $datatable;
 3148: }
 3149: 
 3150: sub overridden_helpdesk {
 3151:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3152:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3153:     my $class = 'LC_left_item';
 3154:     if ($css_class) {
 3155:         $css_class = ' class="'.$css_class.'"';
 3156:     }
 3157:     if ($rowid) {
 3158:         $rowid = ' id="'.$rowid.'"';
 3159:     }
 3160:     if ($rowstyle) {
 3161:         $rowstyle = ' style="'.$rowstyle.'"';
 3162:     }
 3163:     my ($output,$description);
 3164:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3165:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3166:               "<td>$description</td>\n".
 3167:               '<td class="'.$class.'" colspan="2">'.
 3168:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3169:               '<span class="LC_nobreak">';
 3170:     if (ref($contacts) eq 'ARRAY') {
 3171:         foreach my $item (@{$contacts}) {
 3172:             my $check;
 3173:             if (ref($checked) eq 'HASH') {
 3174:                $check = $checked->{$item};
 3175:             }
 3176:             my $title;
 3177:             if (ref($short_titles) eq 'HASH') {
 3178:                 $title = $short_titles->{$item};
 3179:             }
 3180:             $output .= '<label>'.
 3181:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3182:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3183:         }
 3184:     }
 3185:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3186:                '<input type="text" name="override_'.$type.'_others" '.
 3187:                'value="'.$otheremails.'"  />';
 3188:     my %locchecked;
 3189:     foreach my $loc ('s','b') {
 3190:         if ($includeloc eq $loc) {
 3191:             $locchecked{$loc} = ' checked="checked"';
 3192:             last;
 3193:         }
 3194:     }
 3195:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3196:                '<input type="text" name="override_'.$type.'_bcc" '.
 3197:                'value="'.$bccemails.'"  /></fieldset>'.
 3198:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3199:                &mt('Text automatically added to e-mail:').' '.
 3200:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3201:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3202:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3203:                ('&nbsp;'x2).
 3204:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3205:                '</span></fieldset>'.
 3206:                '</td></tr>'."\n";
 3207:     return $output;
 3208: }
 3209: 
 3210: sub contacts_javascript {
 3211:     return <<"ENDSCRIPT";
 3212: 
 3213: <script type="text/javascript">
 3214: // <![CDATA[
 3215: 
 3216: function screenshotSize(field) {
 3217:     if (document.getElementById('help_screenshotsize')) {
 3218:         if (field.value == 'no') {
 3219:             document.getElementById('help_screenshotsize').style.display="none";
 3220:         } else {
 3221:             document.getElementById('help_screenshotsize').style.display="";
 3222:         }
 3223:     }
 3224:     return;
 3225: }
 3226: 
 3227: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3228:     if (form.elements[checkbox].length != undefined) {
 3229:         var count = 0;
 3230:         if (docount) {
 3231:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3232:                 if (form.elements[checkbox][i].checked) {
 3233:                     count ++;
 3234:                 }
 3235:             }
 3236:         }
 3237:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3238:             var type = form.elements[checkbox][i].value;
 3239:             if (document.getElementById(prefix+type)) {
 3240:                 if (form.elements[checkbox][i].checked) {
 3241:                     document.getElementById(prefix+type).style.display = 'table-row';
 3242:                     if (count % 2 == 1) {
 3243:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3244:                     } else {
 3245:                         document.getElementById(prefix+type).className = target;
 3246:                     }
 3247:                     count ++;
 3248:                 } else {
 3249:                     document.getElementById(prefix+type).style.display = 'none';
 3250:                 }
 3251:             }
 3252:         }
 3253:     }
 3254:     return;
 3255: }
 3256: 
 3257: // ]]>
 3258: </script>
 3259: 
 3260: ENDSCRIPT
 3261: }
 3262: 
 3263: sub print_helpsettings {
 3264:     my ($position,$dom,$settings,$rowtotal) = @_;
 3265:     my $confname = $dom.'-domainconfig';
 3266:     my $formname = 'display';
 3267:     my ($datatable,$itemcount);
 3268:     if ($position eq 'top') {
 3269:         $itemcount = 1;
 3270:         my (%choices,%defaultchecked,@toggles);
 3271:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3272:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3273:                                      &mt('LON-CAPA bug tracker'),600,500));
 3274:         %defaultchecked = ('submitbugs' => 'on');
 3275:         @toggles = ('submitbugs');
 3276:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3277:                                                      \%choices,$itemcount);
 3278:         $$rowtotal ++;
 3279:     } else {
 3280:         my $css_class;
 3281:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3282:         my (%customroles,%ordered,%current);
 3283:         if (ref($settings) eq 'HASH') {
 3284:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3285:                 %current = %{$settings->{'adhoc'}};
 3286:             }
 3287:         }
 3288:         my $count = 0;
 3289:         foreach my $key (sort(keys(%existing))) {
 3290:             if ($key=~/^rolesdef\_(\w+)$/) {
 3291:                 my $rolename = $1;
 3292:                 my (%privs,$order);
 3293:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3294:                 $customroles{$rolename} = \%privs;
 3295:                 if (ref($current{$rolename}) eq 'HASH') {
 3296:                     $order = $current{$rolename}{'order'};
 3297:                 }
 3298:                 if ($order eq '') {
 3299:                     $order = $count;
 3300:                 }
 3301:                 $ordered{$order} = $rolename;
 3302:                 $count++;
 3303:             }
 3304:         }
 3305:         my $maxnum = scalar(keys(%ordered));
 3306:         my @roles_by_num = ();
 3307:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3308:             push(@roles_by_num,$item);
 3309:         }
 3310:         my $context = 'domprefs';
 3311:         my $crstype = 'Course';
 3312:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3313:         my @accesstypes = ('all','dh','da','none');
 3314:         my ($numstatustypes,@jsarray);
 3315:         if (ref($types) eq 'ARRAY') {
 3316:             if (@{$types} > 0) {
 3317:                 $numstatustypes = scalar(@{$types});
 3318:                 push(@accesstypes,'status');
 3319:                 @jsarray = ('bystatus');
 3320:             }
 3321:         }
 3322:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3323:         if (keys(%domhelpdesk)) {
 3324:             push(@accesstypes,('inc','exc'));
 3325:             push(@jsarray,('notinc','notexc'));
 3326:         }
 3327:         my $hiddenstr = join("','",@jsarray);
 3328:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3329:         my $context = 'domprefs';
 3330:         my $crstype = 'Course';
 3331:         my $prefix = 'helproles_';
 3332:         my $add_class = 'LC_hidden';
 3333:         foreach my $num (@roles_by_num) {
 3334:             my $role = $ordered{$num};
 3335:             my ($desc,$access,@statuses);
 3336:             if (ref($current{$role}) eq 'HASH') {
 3337:                 $desc = $current{$role}{'desc'};
 3338:                 $access = $current{$role}{'access'};
 3339:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3340:                     @statuses = @{$current{$role}{'insttypes'}};
 3341:                 }
 3342:             }
 3343:             if ($desc eq '') {
 3344:                 $desc = $role;
 3345:             }
 3346:             my $identifier = 'custhelp'.$num;
 3347:             my %full=();
 3348:             my %levels= (
 3349:                          course => {},
 3350:                          domain => {},
 3351:                          system => {},
 3352:                         );
 3353:             my %levelscurrent=(
 3354:                                course => {},
 3355:                                domain => {},
 3356:                                system => {},
 3357:                               );
 3358:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3359:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3360:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3361:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3362:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3363:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3364:             for (my $k=0; $k<=$maxnum; $k++) {
 3365:                 my $vpos = $k+1;
 3366:                 my $selstr;
 3367:                 if ($k == $num) {
 3368:                     $selstr = ' selected="selected" ';
 3369:                 }
 3370:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3371:             }
 3372:             $datatable .= '</select>'.('&nbsp;'x2).
 3373:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3374:                           '</td>'.
 3375:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3376:                           &mt('Name shown to users:').
 3377:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3378:                           '</fieldset>'.
 3379:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3380:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3381:                           '<fieldset>'.
 3382:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3383:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3384:                                                                    \%levelscurrent,$identifier,
 3385:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3386:                           '</fieldset></td>';
 3387:             $itemcount ++;
 3388:         }
 3389:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3390:         my $newcust = 'custhelp'.$count;
 3391:         my (%privs,%levelscurrent);
 3392:         my %full=();
 3393:         my %levels= (
 3394:                      course => {},
 3395:                      domain => {},
 3396:                      system => {},
 3397:                     );
 3398:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3399:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3400:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3401:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3402:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3403:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3404:         for (my $k=0; $k<$maxnum+1; $k++) {
 3405:             my $vpos = $k+1;
 3406:             my $selstr;
 3407:             if ($k == $maxnum) {
 3408:                 $selstr = ' selected="selected" ';
 3409:             }
 3410:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3411:         }
 3412:         $datatable .= '</select>&nbsp;'."\n".
 3413:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3414:                       '</label></span></td>'.
 3415:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3416:                       '<span class="LC_nobreak">'.
 3417:                       &mt('Internal name:').
 3418:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3419:                       '</span>'.('&nbsp;'x4).
 3420:                       '<span class="LC_nobreak">'.
 3421:                       &mt('Name shown to users:').
 3422:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3423:                       '</span></fieldset>'.
 3424:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3425:                                              $usertypes,$types,\%domhelpdesk).
 3426:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3427:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3428:                                                                 \@templateroles,$newcust).
 3429:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3430:                                                                \%levelscurrent,$newcust).
 3431:                       '</fieldset>'.
 3432:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 3433:                       '</td></tr>';
 3434:         $count ++;
 3435:         $$rowtotal += $count;
 3436:     }
 3437:     return $datatable;
 3438: }
 3439: 
 3440: sub adhocbutton {
 3441:     my ($prefix,$num,$field,$visibility) = @_;
 3442:     my %lt = &Apache::lonlocal::texthash(
 3443:                                           show => 'Show details',
 3444:                                           hide => 'Hide details',
 3445:                                         );
 3446:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3447:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3448:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3449:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3450: }
 3451: 
 3452: sub helpsettings_javascript {
 3453:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3454:     return unless(ref($roles_by_num) eq 'ARRAY');
 3455:     my %html_js_lt = &Apache::lonlocal::texthash(
 3456:                                           show => 'Show details',
 3457:                                           hide => 'Hide details',
 3458:                                         );
 3459:     &html_escape(\%html_js_lt);
 3460:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3461:     return <<"ENDSCRIPT";
 3462: <script type="text/javascript">
 3463: // <![CDATA[
 3464: 
 3465: function reorderHelpRoles(form,item) {
 3466:     var changedVal;
 3467: $jstext
 3468:     var newpos = 'helproles_${total}_pos';
 3469:     var maxh = 1 + $total;
 3470:     var current = new Array();
 3471:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3472:     if (item == newpos) {
 3473:         changedVal = newitemVal;
 3474:     } else {
 3475:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3476:         current[newitemVal] = newpos;
 3477:     }
 3478:     for (var i=0; i<helproles.length; i++) {
 3479:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3480:         if (elementName != item) {
 3481:             if (form.elements[elementName]) {
 3482:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3483:                 current[currVal] = elementName;
 3484:             }
 3485:         }
 3486:     }
 3487:     var oldVal;
 3488:     for (var j=0; j<maxh; j++) {
 3489:         if (current[j] == undefined) {
 3490:             oldVal = j;
 3491:         }
 3492:     }
 3493:     if (oldVal < changedVal) {
 3494:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3495:            var elementName = current[k];
 3496:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3497:         }
 3498:     } else {
 3499:         for (var k=changedVal; k<oldVal; k++) {
 3500:             var elementName = current[k];
 3501:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3502:         }
 3503:     }
 3504:     return;
 3505: }
 3506: 
 3507: function helpdeskAccess(num) {
 3508:     var curraccess = null;
 3509:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3510:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3511:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3512:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3513:             }
 3514:         }
 3515:     }
 3516:     var shown = Array();
 3517:     var hidden = Array();
 3518:     if (curraccess == 'none') {
 3519:         hidden = Array('$hiddenstr');
 3520:     } else {
 3521:         if (curraccess == 'status') {
 3522:             shown = Array('bystatus');
 3523:             hidden = Array('notinc','notexc');
 3524:         } else {
 3525:             if (curraccess == 'exc') {
 3526:                 shown = Array('notexc');
 3527:                 hidden = Array('notinc','bystatus');
 3528:             }
 3529:             if (curraccess == 'inc') {
 3530:                 shown = Array('notinc');
 3531:                 hidden = Array('notexc','bystatus');
 3532:             }
 3533:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3534:                 hidden = Array('notinc','notexc','bystatus');
 3535:             }
 3536:         }
 3537:     }
 3538:     if (hidden.length > 0) {
 3539:         for (var i=0; i<hidden.length; i++) {
 3540:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3541:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3542:             }
 3543:         }
 3544:     }
 3545:     if (shown.length > 0) {
 3546:         for (var i=0; i<shown.length; i++) {
 3547:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3548:                 if (shown[i] == 'privs') {
 3549:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3550:                 } else {
 3551:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3552:                 }
 3553:             }
 3554:         }
 3555:     }
 3556:     return;
 3557: }
 3558: 
 3559: function toggleHelpdeskItem(num,field) {
 3560:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3561:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3562:             document.getElementById('helproles_'+num+'_'+field).className =
 3563:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3564:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3565:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3566:             }
 3567:         } else {
 3568:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3569:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3570:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3571:             }
 3572:         }
 3573:     }
 3574:     return;
 3575: }
 3576: 
 3577: // ]]>
 3578: </script>
 3579: 
 3580: ENDSCRIPT
 3581: }
 3582: 
 3583: sub helpdeskroles_access {
 3584:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3585:         $usertypes,$types,$domhelpdesk) = @_;
 3586:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3587:     my %lt = &Apache::lonlocal::texthash(
 3588:                     'rou'    => 'Role usage',
 3589:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3590:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3591:                     'dh'     => 'All with domain helpdesk role',
 3592:                     'da'     => 'All with domain helpdesk assistant role',
 3593:                     'none'   => 'None',
 3594:                     'status' => 'Determined based on institutional status',
 3595:                     'inc'    => 'Include all, but exclude specific personnel',
 3596:                     'exc'    => 'Exclude all, but include specific personnel',
 3597:                   );
 3598:     my %usecheck = (
 3599:                      all => ' checked="checked"',
 3600:                    );
 3601:     my %displaydiv = (
 3602:                       status => 'none',
 3603:                       inc    => 'none',
 3604:                       exc    => 'none',
 3605:                       priv   => 'block',
 3606:                      );
 3607:     my $output;
 3608:     if (ref($current) eq 'HASH') {
 3609:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3610:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3611:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3612:                 delete($usecheck{'all'});
 3613:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3614:                     my $access = $1;
 3615:                     $displaydiv{$access} = 'inline';
 3616:                 } elsif ($current->{access} eq 'none') {
 3617:                     $displaydiv{'priv'} = 'none';
 3618:                 }
 3619:             }
 3620:         }
 3621:     }
 3622:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3623:               '<p>'.$lt{'whi'}.'</p>';
 3624:     foreach my $access (@{$accesstypes}) {
 3625:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3626:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3627:                    $lt{$access}.'</label>';
 3628:         if ($access eq 'status') {
 3629:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3630:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3631:                                                                  $othertitle,$usertypes,$types).
 3632:                        '</div>';
 3633:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3634:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3635:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3636:                        '</div>';
 3637:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3638:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3639:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3640:                        '</div>';
 3641:         }
 3642:         $output .= '</p>';
 3643:     }
 3644:     $output .= '</fieldset>';
 3645:     return $output;
 3646: }
 3647: 
 3648: sub radiobutton_prefs {
 3649:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3650:         $additional,$align) = @_;
 3651:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3652:                    (ref($choices) eq 'HASH'));
 3653: 
 3654:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3655: 
 3656:     foreach my $item (@{$toggles}) {
 3657:         if ($defaultchecked->{$item} eq 'on') {
 3658:             $checkedon{$item} = ' checked="checked" ';
 3659:             $checkedoff{$item} = ' ';
 3660:         } elsif ($defaultchecked->{$item} eq 'off') {
 3661:             $checkedoff{$item} = ' checked="checked" ';
 3662:             $checkedon{$item} = ' ';
 3663:         }
 3664:     }
 3665:     if (ref($settings) eq 'HASH') {
 3666:         foreach my $item (@{$toggles}) {
 3667:             if ($settings->{$item} eq '1') {
 3668:                 $checkedon{$item} =  ' checked="checked" ';
 3669:                 $checkedoff{$item} = ' ';
 3670:             } elsif ($settings->{$item} eq '0') {
 3671:                 $checkedoff{$item} =  ' checked="checked" ';
 3672:                 $checkedon{$item} = ' ';
 3673:             }
 3674:         }
 3675:     }
 3676:     if ($onclick) {
 3677:         $onclick = ' onclick="'.$onclick.'"';
 3678:     }
 3679:     foreach my $item (@{$toggles}) {
 3680:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3681:         $datatable .=
 3682:             '<tr'.$css_class.'><td valign="top">'.
 3683:             '<span class="LC_nobreak">'.$choices->{$item}.
 3684:             '</span></td>';
 3685:         if ($align eq 'left') {
 3686:             $datatable .= '<td class="LC_left_item">';
 3687:         } else {
 3688:             $datatable .= '<td class="LC_right_item">';
 3689:         }
 3690:         $datatable .=
 3691:             '<span class="LC_nobreak">'.
 3692:             '<label><input type="radio" name="'.
 3693:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3694:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3695:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3696:             '</span>'.$additional.
 3697:             '</td>'.
 3698:             '</tr>';
 3699:         $itemcount ++;
 3700:     }
 3701:     return ($datatable,$itemcount);
 3702: }
 3703: 
 3704: sub print_coursedefaults {
 3705:     my ($position,$dom,$settings,$rowtotal) = @_;
 3706:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 3707:     my $itemcount = 1;
 3708:     my %choices =  &Apache::lonlocal::texthash (
 3709:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 3710:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 3711:         coursecredits        => 'Credits can be specified for courses',
 3712:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 3713:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 3714:         texengine            => 'Default method to display mathematics',
 3715:         postsubmit           => 'Disable submit button/keypress following student submission',
 3716:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 3717:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 3718:     );
 3719:     my %staticdefaults = (
 3720:                            anonsurvey_threshold => 10,
 3721:                            uploadquota          => 500,
 3722:                            postsubmit           => 60,
 3723:                            mysqltables          => 172800,
 3724:                          );
 3725:     if ($position eq 'top') {
 3726:         %defaultchecked = (
 3727:                             'uselcmath'       => 'on',
 3728:                             'usejsme'         => 'on',
 3729:                             'canclone'        => 'none',
 3730:                           );
 3731:         @toggles = ('uselcmath','usejsme');
 3732:         my $deftex = $Apache::lonnet::deftex;
 3733:         if (ref($settings) eq 'HASH') {
 3734:             if ($settings->{'texengine'}) {
 3735:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 3736:                     $deftex = $settings->{'texengine'};
 3737:                 }
 3738:             }
 3739:         }
 3740:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3741:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 3742:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 3743:                        '</span></td><td class="LC_right_item">'.
 3744:                        '<select name="texengine">'."\n";
 3745:         my %texoptions = (
 3746:                             MathJax  => 'MathJax',
 3747:                             mimetex  => &mt('Convert to Images'),
 3748:                             tth      => &mt('TeX to HTML'),
 3749:                          );
 3750:         foreach my $renderer ('MathJax','mimetex','tth') {
 3751:             my $selected = '';
 3752:             if ($renderer eq $deftex) {
 3753:                 $selected = ' selected="selected"';
 3754:             }
 3755:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 3756:         }
 3757:         $mathdisp .= '</select></td></tr>'."\n";
 3758:         $itemcount ++;
 3759:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3760:                                                      \%choices,$itemcount);
 3761:         $datatable = $mathdisp.$datatable;
 3762:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3763:         $datatable .=
 3764:             '<tr'.$css_class.'><td valign="top">'.
 3765:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 3766:             '</span></td><td class="LC_left_item">';
 3767:         my $currcanclone = 'none';
 3768:         my $onclick;
 3769:         my @cloneoptions = ('none','domain');
 3770:         my %clonetitles = (
 3771:                              none     => 'No additional course requesters',
 3772:                              domain   => "Any course requester in course's domain",
 3773:                              instcode => 'Course requests for official courses ...',
 3774:                           );
 3775:         my (%codedefaults,@code_order,@posscodes);
 3776:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 3777:                                                     \@code_order) eq 'ok') {
 3778:             if (@code_order > 0) {
 3779:                 push(@cloneoptions,'instcode');
 3780:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 3781:             }
 3782:         }
 3783:         if (ref($settings) eq 'HASH') {
 3784:             if ($settings->{'canclone'}) {
 3785:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 3786:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 3787:                         if (@code_order > 0) {
 3788:                             $currcanclone = 'instcode';
 3789:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 3790:                         }
 3791:                     }
 3792:                 } elsif ($settings->{'canclone'} eq 'domain') {
 3793:                     $currcanclone = $settings->{'canclone'};
 3794:                 }
 3795:             }
 3796:         }
 3797:         foreach my $option (@cloneoptions) {
 3798:             my ($checked,$additional);
 3799:             if ($currcanclone eq $option) {
 3800:                 $checked = ' checked="checked"';
 3801:             }
 3802:             if ($option eq 'instcode') {
 3803:                 if (@code_order) {
 3804:                     my $show = 'none';
 3805:                     if ($checked) {
 3806:                         $show = 'block';
 3807:                     }
 3808:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 3809:                                   &mt('Institutional codes for new and cloned course have identical:').
 3810:                                   '<br />';
 3811:                     foreach my $item (@code_order) {
 3812:                         my $codechk;
 3813:                         if ($checked) {
 3814:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 3815:                                 $codechk = ' checked="checked"';
 3816:                             }
 3817:                         }
 3818:                         $additional .= '<label>'.
 3819:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 3820:                                        $item.'</label>';
 3821:                     }
 3822:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 3823:                 }
 3824:             }
 3825:             $datatable .=
 3826:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 3827:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 3828:                 '</label>&nbsp;'.$additional.'</span><br />';
 3829:         }
 3830:         $datatable .= '</td>'.
 3831:                       '</tr>';
 3832:         $itemcount ++;
 3833:     } else {
 3834:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3835:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 3836:         my $currusecredits = 0;
 3837:         my $postsubmitclient = 1;
 3838:         my @types = ('official','unofficial','community','textbook');
 3839:         if (ref($settings) eq 'HASH') {
 3840:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 3841:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 3842:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3843:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3844:                 }
 3845:             }
 3846:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3847:                 foreach my $type (@types) {
 3848:                     next if ($type eq 'community');
 3849:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3850:                     if ($defcredits{$type} ne '') {
 3851:                         $currusecredits = 1;
 3852:                     }
 3853:                 }
 3854:             }
 3855:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3856:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3857:                     $postsubmitclient = 0;
 3858:                     foreach my $type (@types) {
 3859:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3860:                     }
 3861:                 } else {
 3862:                     foreach my $type (@types) {
 3863:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3864:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3865:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 3866:                             } else {
 3867:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3868:                             }
 3869:                         } else {
 3870:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3871:                         }
 3872:                     }
 3873:                 }
 3874:             } else {
 3875:                 foreach my $type (@types) {
 3876:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3877:                 }
 3878:             }
 3879:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 3880:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 3881:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 3882:                 }
 3883:             } else {
 3884:                 foreach my $type (@types) {
 3885:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 3886:                 }
 3887:             }
 3888:         } else {
 3889:             foreach my $type (@types) {
 3890:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3891:             }
 3892:         }
 3893:         if (!$currdefresponder) {
 3894:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3895:         } elsif ($currdefresponder < 1) {
 3896:             $currdefresponder = 1;
 3897:         }
 3898:         foreach my $type (@types) {
 3899:             if ($curruploadquota{$type} eq '') {
 3900:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3901:             }
 3902:         }
 3903:         $datatable .=
 3904:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3905:                 $choices{'anonsurvey_threshold'}.
 3906:                 '</span></td>'.
 3907:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3908:                 '<input type="text" name="anonsurvey_threshold"'.
 3909:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3910:                 '</td></tr>'."\n";
 3911:         $itemcount ++;
 3912:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3913:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3914:                       $choices{'uploadquota'}.
 3915:                       '</span></td>'.
 3916:                       '<td align="right" class="LC_right_item">'.
 3917:                       '<table><tr>';
 3918:         foreach my $type (@types) {
 3919:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3920:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3921:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3922:         }
 3923:         $datatable .= '</tr></table></td></tr>'."\n";
 3924:         $itemcount ++;
 3925:         my $onclick = "toggleDisplay(this.form,'credits');";
 3926:         my $display = 'none';
 3927:         if ($currusecredits) {
 3928:             $display = 'block';
 3929:         }
 3930:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3931:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3932:         foreach my $type (@types) {
 3933:             next if ($type eq 'community');
 3934:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3935:                            '<input type="text" name="'.$type.'_credits"'.
 3936:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3937:         }
 3938:         $additional .= '</tr></table></div>'."\n";
 3939:         %defaultchecked = ('coursecredits' => 'off');
 3940:         @toggles = ('coursecredits');
 3941:         my $current = {
 3942:                         'coursecredits' => $currusecredits,
 3943:                       };
 3944:         (my $table,$itemcount) =
 3945:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3946:                                \%choices,$itemcount,$onclick,$additional,'left');
 3947:         $datatable .= $table;
 3948:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3949:         my $display = 'none';
 3950:         if ($postsubmitclient) {
 3951:             $display = 'block';
 3952:         }
 3953:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3954:                       &mt('Number of seconds submit is disabled').'<br />'.
 3955:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3956:                       '<table><tr>';
 3957:         foreach my $type (@types) {
 3958:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3959:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3960:                            $deftimeout{$type}.'" size="5" /></td>';
 3961:         }
 3962:         $additional .= '</tr></table></div>'."\n";
 3963:         %defaultchecked = ('postsubmit' => 'on');
 3964:         @toggles = ('postsubmit');
 3965:         $current = {
 3966:                        'postsubmit' => $postsubmitclient,
 3967:                    };
 3968:         ($table,$itemcount) =
 3969:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3970:                                \%choices,$itemcount,$onclick,$additional,'left');
 3971:         $datatable .= $table;
 3972:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3973:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3974:                       $choices{'mysqltables'}.
 3975:                       '</span></td>'.
 3976:                       '<td align="right" class="LC_right_item">'.
 3977:                       '<table><tr>';
 3978:         foreach my $type (@types) {
 3979:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3980:                            '<input type="text" name="mysqltables_'.$type.'"'.
 3981:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 3982:         }
 3983:         $datatable .= '</tr></table></td></tr>'."\n";
 3984:         $itemcount ++;
 3985: 
 3986:     }
 3987:     $$rowtotal += $itemcount;
 3988:     return $datatable;
 3989: }
 3990: 
 3991: sub print_selfenrollment {
 3992:     my ($position,$dom,$settings,$rowtotal) = @_;
 3993:     my ($css_class,$datatable);
 3994:     my $itemcount = 1;
 3995:     my @types = ('official','unofficial','community','textbook');
 3996:     if (($position eq 'top') || ($position eq 'middle')) {
 3997:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 3998:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 3999:         my @rows;
 4000:         my $key;
 4001:         if ($position eq 'top') {
 4002:             $key = 'admin'; 
 4003:             if (ref($rowsref) eq 'ARRAY') {
 4004:                 @rows = @{$rowsref};
 4005:             }
 4006:         } elsif ($position eq 'middle') {
 4007:             $key = 'default';
 4008:             @rows = ('types','registered','approval','limit');
 4009:         }
 4010:         foreach my $row (@rows) {
 4011:             if (defined($titlesref->{$row})) {
 4012:                 $itemcount ++;
 4013:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4014:                 $datatable .= '<tr'.$css_class.'>'.
 4015:                               '<td>'.$titlesref->{$row}.'</td>'.
 4016:                               '<td class="LC_left_item">'.
 4017:                               '<table><tr>';
 4018:                 my (%current,%currentcap);
 4019:                 if (ref($settings) eq 'HASH') {
 4020:                     if (ref($settings->{$key}) eq 'HASH') {
 4021:                         foreach my $type (@types) {
 4022:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4023:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4024:                             }
 4025:                             if (($row eq 'limit') && ($key eq 'default')) {
 4026:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4027:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4028:                                 }
 4029:                             }
 4030:                         }
 4031:                     }
 4032:                 }
 4033:                 my %roles = (
 4034:                              '0' => &Apache::lonnet::plaintext('dc'),
 4035:                             ); 
 4036:             
 4037:                 foreach my $type (@types) {
 4038:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4039:                         $datatable .= '<th>'.&mt($type).'</th>';
 4040:                     }
 4041:                 }
 4042:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4043:                     $datatable .= '</tr><tr>';
 4044:                 }
 4045:                 foreach my $type (@types) {
 4046:                     if ($type eq 'community') {
 4047:                         $roles{'1'} = &mt('Community personnel');
 4048:                     } else {
 4049:                         $roles{'1'} = &mt('Course personnel');
 4050:                     }
 4051:                     $datatable .= '<td style="vertical-align: top">';
 4052:                     if ($position eq 'top') {
 4053:                         my %checked;
 4054:                         if ($current{$type} eq '0') {
 4055:                             $checked{'0'} = ' checked="checked"';
 4056:                         } else {
 4057:                             $checked{'1'} = ' checked="checked"';
 4058:                         }
 4059:                         foreach my $role ('1','0') {
 4060:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4061:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4062:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4063:                                           $roles{$role}.'</label></span> ';
 4064:                         }
 4065:                     } else {
 4066:                         if ($row eq 'types') {
 4067:                             my %checked;
 4068:                             if ($current{$type} =~ /^(all|dom)$/) {
 4069:                                 $checked{$1} = ' checked="checked"';
 4070:                             } else {
 4071:                                 $checked{''} = ' checked="checked"';
 4072:                             }
 4073:                             foreach my $val ('','dom','all') {
 4074:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4075:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4076:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4077:                             }
 4078:                         } elsif ($row eq 'registered') {
 4079:                             my %checked;
 4080:                             if ($current{$type} eq '1') {
 4081:                                 $checked{'1'} = ' checked="checked"';
 4082:                             } else {
 4083:                                 $checked{'0'} = ' checked="checked"';
 4084:                             }
 4085:                             foreach my $val ('0','1') {
 4086:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4087:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4088:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4089:                             }
 4090:                         } elsif ($row eq 'approval') {
 4091:                             my %checked;
 4092:                             if ($current{$type} =~ /^([12])$/) {
 4093:                                 $checked{$1} = ' checked="checked"';
 4094:                             } else {
 4095:                                 $checked{'0'} = ' checked="checked"';
 4096:                             }
 4097:                             for my $val (0..2) {
 4098:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4099:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4100:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4101:                             }
 4102:                         } elsif ($row eq 'limit') {
 4103:                             my %checked;
 4104:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4105:                                 $checked{$1} = ' checked="checked"';
 4106:                             } else {
 4107:                                 $checked{'none'} = ' checked="checked"';
 4108:                             }
 4109:                             my $cap;
 4110:                             if ($currentcap{$type} =~ /^\d+$/) {
 4111:                                 $cap = $currentcap{$type};
 4112:                             }
 4113:                             foreach my $val ('none','allstudents','selfenrolled') {
 4114:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4115:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4116:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4117:                             }
 4118:                             $datatable .= '<br />'.
 4119:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4120:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4121:                                           '</span>'; 
 4122:                         }
 4123:                     }
 4124:                     $datatable .= '</td>';
 4125:                 }
 4126:                 $datatable .= '</tr>';
 4127:             }
 4128:             $datatable .= '</table></td></tr>';
 4129:         }
 4130:     } elsif ($position eq 'bottom') {
 4131:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4132:     }
 4133:     $$rowtotal += $itemcount;
 4134:     return $datatable;
 4135: }
 4136: 
 4137: sub print_validation_rows {
 4138:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4139:     my ($itemsref,$namesref,$fieldsref);
 4140:     if ($caller eq 'selfenroll') { 
 4141:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4142:     } elsif ($caller eq 'requestcourses') {
 4143:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4144:     }
 4145:     my %currvalidation;
 4146:     if (ref($settings) eq 'HASH') {
 4147:         if (ref($settings->{'validation'}) eq 'HASH') {
 4148:             %currvalidation = %{$settings->{'validation'}};
 4149:         }
 4150:     }
 4151:     my $datatable;
 4152:     my $itemcount = 0;
 4153:     foreach my $item (@{$itemsref}) {
 4154:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4155:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4156:                       $namesref->{$item}.
 4157:                       '</span></td>'.
 4158:                       '<td class="LC_left_item">';
 4159:         if (($item eq 'url') || ($item eq 'button')) {
 4160:             $datatable .= '<span class="LC_nobreak">'.
 4161:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4162:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4163:         } elsif ($item eq 'fields') {
 4164:             my @currfields;
 4165:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4166:                 @currfields = @{$currvalidation{$item}};
 4167:             }
 4168:             foreach my $field (@{$fieldsref}) {
 4169:                 my $check = '';
 4170:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4171:                     $check = ' checked="checked"';
 4172:                 }
 4173:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4174:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4175:                               ' value="'.$field.'"'.$check.' />'.$field.
 4176:                               '</label></span> ';
 4177:             }
 4178:         } elsif ($item eq 'markup') {
 4179:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 4180:                            $currvalidation{$item}.
 4181:                               '</textarea>';
 4182:         }
 4183:         $datatable .= '</td></tr>'."\n";
 4184:         if (ref($rowtotal)) {
 4185:             $itemcount ++;
 4186:         }
 4187:     }
 4188:     if ($caller eq 'requestcourses') {
 4189:         my %currhash;
 4190:         if (ref($settings) eq 'HASH') {
 4191:             if (ref($settings->{'validation'}) eq 'HASH') {
 4192:                 if ($settings->{'validation'}{'dc'} ne '') {
 4193:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4194:                 }
 4195:             }
 4196:         }
 4197:         my $numinrow = 2;
 4198:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4199:                                                        'validationdc',%currhash);
 4200:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4201:         $datatable .= '<tr'.$css_class.'><td>';
 4202:         if ($numdc > 1) {
 4203:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4204:         } else {
 4205:             $datatable .=  &mt('Course creation processed as: ');
 4206:         }
 4207:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4208:         $itemcount ++;
 4209:     }
 4210:     if (ref($rowtotal)) {
 4211:         $$rowtotal += $itemcount;
 4212:     }
 4213:     return $datatable;
 4214: }
 4215: 
 4216: sub print_passwords {
 4217:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 4218:     my ($datatable,$css_class);
 4219:     my $itemcount = 0;
 4220:     my %titles = &Apache::lonlocal::texthash (
 4221:         captcha        => '"Forgot Password" CAPTCHA validation',
 4222:         link           => 'Reset link expiration (hours)',
 4223:         case           => 'Case-sensitive usernames/e-mail',
 4224:         prelink        => 'Information required (form 1)',
 4225:         postlink       => 'Information required (form 2)',
 4226:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 4227:         customtext     => 'Domain specific text (HTML)',
 4228:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 4229:         intauth_check  => 'Check bcrypt cost if authenticated',
 4230:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 4231:         permanent      => 'Permanent e-mail address',
 4232:         critical       => 'Critical notification address',
 4233:         notify         => 'Notification address',
 4234:         min            => 'Minimum password length',
 4235:         max            => 'Maximum password length',
 4236:         chars          => 'Required characters',
 4237:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 4238:     );
 4239:     if ($position eq 'top') {
 4240:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4241:         my $shownlinklife = 2;
 4242:         my $prelink = 'both';
 4243:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 4244:         if (ref($settings) eq 'HASH') {
 4245:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 4246:                 $shownlinklife = $settings->{resetlink};
 4247:             }
 4248:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 4249:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 4250:             }
 4251:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 4252:                 $prelink = $settings->{resetprelink};
 4253:             }
 4254:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 4255:                 %postlink = %{$settings->{resetpostlink}};
 4256:             }
 4257:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 4258:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 4259:             }
 4260:             if ($settings->{resetremove}) {
 4261:                 $nostdtext = 1;
 4262:             }
 4263:             if ($settings->{resetcustom}) {
 4264:                 $customurl = $settings->{resetcustom};
 4265:             }
 4266:         } else {
 4267:             if (ref($types) eq 'ARRAY') {
 4268:                 foreach my $item (@{$types}) {
 4269:                     $casesens{$item} = 1;
 4270:                     $postlink{$item} = ['username','email'];
 4271:                 }
 4272:             }
 4273:             $casesens{'default'} = 1;
 4274:             $postlink{'default'} = ['username','email'];
 4275:             $prelink = 'both';
 4276:             %emailsrc = (
 4277:                           permanent => 1,
 4278:                           critical  => 1,
 4279:                           notify    => 1,
 4280:             );
 4281:         }
 4282:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 4283:         $itemcount ++;
 4284:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4285:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 4286:                       '<td class="LC_left_item">'.
 4287:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 4288:                       'name="passwords_link" size="3" /></td></tr>';
 4289:         $itemcount ++;
 4290:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4291:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 4292:                       '<td class="LC_left_item">';
 4293:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4294:             foreach my $item (@{$types}) {
 4295:                 my $checkedcase;
 4296:                 if ($casesens{$item}) {
 4297:                     $checkedcase = ' checked="checked"';
 4298:                 }
 4299:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4300:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 4301:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 4302:                               '<span>&nbsp;&nbsp; ';
 4303:             }
 4304:         }
 4305:         my $checkedcase;
 4306:         if ($casesens{'default'}) {
 4307:             $checkedcase = ' checked="checked"';
 4308:         }
 4309:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 4310:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 4311:                       $othertitle.'</label></span></td>';
 4312:         $itemcount ++;
 4313:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4314:         my %checkedpre = (
 4315:                              both => ' checked="checked"',
 4316:                              either => '',
 4317:                          );
 4318:         if ($prelink eq 'either') {
 4319:             $checkedpre{either} = ' checked="checked"';
 4320:             $checkedpre{both} = '';
 4321:         }
 4322:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 4323:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4324:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 4325:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 4326:                       '<span class="LC_nobreak"><label>'.
 4327:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 4328:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 4329:         $itemcount ++;
 4330:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4331:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 4332:                       '<td class="LC_left_item">';
 4333:         my %postlinked;
 4334:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4335:             foreach my $item (@{$types}) {
 4336:                 undef(%postlinked);
 4337:                 $datatable .= '<fieldset style="display: inline-block;">'.
 4338:                               '<legend>'.$usertypes->{$item}.'</legend>';
 4339:                 if (ref($postlink{$item}) eq 'ARRAY') {
 4340:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 4341:                 }
 4342:                 foreach my $field ('email','username') {
 4343:                     my $checked;
 4344:                     if ($postlinked{$field}) {
 4345:                         $checked = ' checked="checked"';
 4346:                     }
 4347:                     $datatable .= '<span class="LC_nobreak"><label>'.
 4348:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 4349:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 4350:                                   '<span>&nbsp;&nbsp; ';
 4351:                 }
 4352:                 $datatable .= '</fieldset>';
 4353:             }
 4354:         }
 4355:         if (ref($postlink{'default'}) eq 'ARRAY') {
 4356:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 4357:         }
 4358:         $datatable .= '<fieldset style="display: inline-block;">'.
 4359:                       '<legend>'.$othertitle.'</legend>';
 4360:         foreach my $field ('email','username') {
 4361:             my $checked;
 4362:             if ($postlinked{$field}) {
 4363:                 $checked = ' checked="checked"';
 4364:             }
 4365:             $datatable .= '<span class="LC_nobreak"><label>'.
 4366:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 4367:                           $field.'"'.$checked.' />'.$field.'</label>'.
 4368:                           '<span>&nbsp;&nbsp; ';
 4369:         }
 4370:         $datatable .= '</fieldset></td></tr>';
 4371:         $itemcount ++;
 4372:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4373:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 4374:                       '<td class="LC_left_item">';
 4375:         foreach my $type ('permanent','critical','notify') {
 4376:             my $checkedemail;
 4377:             if ($emailsrc{$type}) {
 4378:                 $checkedemail = ' checked="checked"';
 4379:             }
 4380:             $datatable .= '<span class="LC_nobreak"><label>'.
 4381:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 4382:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 4383:                           '<span>&nbsp;&nbsp; ';
 4384:         }
 4385:         $datatable .= '</td></tr>';
 4386:         $itemcount ++;
 4387:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4388:         my $switchserver = &check_switchserver($dom,$confname);
 4389:         my ($showstd,$noshowstd);
 4390:         if ($nostdtext) {
 4391:             $noshowstd = ' checked="checked"';
 4392:         } else {
 4393:             $showstd = ' checked="checked"';
 4394:         }
 4395:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 4396:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4397:                       &mt('Retain standard text:').
 4398:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 4399:                       &mt('Yes').'</label>'.'&nbsp;'.
 4400:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 4401:                       &mt('No').'</label></span><br />'.
 4402:                       '<span class="LC_fontsize_small">'.
 4403:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 4404:                       &mt('Include custom text:');
 4405:         if ($customurl) {
 4406:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('Custom text file'),600,500,
 4407:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4408:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 4409:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 4410:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 4411:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 4412:         }
 4413:         if ($switchserver) {
 4414:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 4415:         } else {
 4416:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4417:                          '<input type="file" name="passwords_customfile" /></span>';
 4418:         }
 4419:         $datatable .= '</td></tr>';
 4420:     } elsif ($position eq 'middle') {
 4421:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 4422:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 4423:         my %defaults;
 4424:         if (ref($domconf{'defaults'}) eq 'HASH') {
 4425:             %defaults = %{$domconf{'defaults'}};
 4426:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 4427:                 $defaults{'intauth_cost'} = 10;
 4428:             }
 4429:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 4430:                 $defaults{'intauth_check'} = 0;
 4431:             }
 4432:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 4433:                 $defaults{'intauth_switch'} = 0;
 4434:             }
 4435:         } else {
 4436:             %defaults = (
 4437:                           'intauth_cost'   => 10,
 4438:                           'intauth_check'  => 0,
 4439:                           'intauth_switch' => 0,
 4440:                         );
 4441:         }
 4442:         foreach my $item (@items) {
 4443:             if ($itemcount%2) {
 4444:                 $css_class = '';
 4445:             } else {
 4446:                 $css_class = ' class="LC_odd_row" ';
 4447:             }
 4448:             $datatable .= '<tr'.$css_class.'>'.
 4449:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 4450:                           '</span></td><td class="LC_left_item" colspan="3">';
 4451:             if ($item eq 'intauth_switch') {
 4452:                 my @options = (0,1,2);
 4453:                 my %optiondesc = &Apache::lonlocal::texthash (
 4454:                                    0 => 'No',
 4455:                                    1 => 'Yes',
 4456:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 4457:                                  );
 4458:                 $datatable .= '<table width="100%">';
 4459:                 foreach my $option (@options) {
 4460:                     my $checked = ' ';
 4461:                     if ($defaults{$item} eq $option) {
 4462:                         $checked = ' checked="checked"';
 4463:                     }
 4464:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4465:                                   '<label><input type="radio" name="'.$item.
 4466:                                   '" value="'.$option.'"'.$checked.' />'.
 4467:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4468:                 }
 4469:                 $datatable .= '</table>';
 4470:             } elsif ($item eq 'intauth_check') {
 4471:                 my @options = (0,1,2);
 4472:                 my %optiondesc = &Apache::lonlocal::texthash (
 4473:                                    0 => 'No',
 4474:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 4475:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 4476:                                  );
 4477:                 $datatable .= '<table width="100%">';
 4478:                 foreach my $option (@options) {
 4479:                     my $checked = ' ';
 4480:                     my $onclick;
 4481:                     if ($defaults{$item} eq $option) {
 4482:                         $checked = ' checked="checked"';
 4483:                     }
 4484:                     if ($option == 2) {
 4485:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 4486:                     }
 4487:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4488:                                   '<label><input type="radio" name="'.$item.
 4489:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 4490:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4491:                 }
 4492:                 $datatable .= '</table>';
 4493:             } else {
 4494:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4495:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 4496:             }
 4497:             $datatable .= '</td></tr>';
 4498:             $itemcount ++;
 4499:         }
 4500:     } elsif ($position eq 'lower') {
 4501:         my ($min,$max,%chars,$numsaved);
 4502:         $min = $Apache::lonnet::passwdmin;
 4503:         if (ref($settings) eq 'HASH') {
 4504:             if ($settings->{min}) {
 4505:                 $min = $settings->{min};
 4506:             }
 4507:             if ($settings->{max}) {
 4508:                 $max = $settings->{max};
 4509:             }
 4510:             if (ref($settings->{chars}) eq 'ARRAY') {
 4511:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 4512:             }
 4513:             if ($settings->{numsaved}) {
 4514:                 $numsaved = $settings->{numsaved};
 4515:             }
 4516:         }
 4517:         my %rulenames = &Apache::lonlocal::texthash(
 4518:                                                      uc => 'At least one upper case letter',
 4519:                                                      lc => 'At least one lower case letter',
 4520:                                                      num => 'At least one number',
 4521:                                                      spec => 'At least one non-alphanumeric',
 4522:                                                    );
 4523:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4524:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 4525:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4526:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 4527:                       'onblur="javascript:warnIntPass(this);" />'.
 4528:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 4529:                       '</span></td></tr>';
 4530:         $itemcount ++;
 4531:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4532:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 4533:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4534:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 4535:                       'onblur="javascript:warnIntPass(this);" />'.
 4536:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 4537:                       '</span></td></tr>';
 4538:         $itemcount ++;
 4539:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4540:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 4541:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 4542:                       '</span></td>';
 4543:         my $numinrow = 2;
 4544:         my @possrules = ('uc','lc','num','spec');
 4545:         $datatable .= '<td class="LC_left_item"><table>';
 4546:         for (my $i=0; $i<@possrules; $i++) {
 4547:             my ($rem,$checked);
 4548:             if ($chars{$possrules[$i]}) {
 4549:                 $checked = ' checked="checked"';
 4550:             }
 4551:             $rem = $i%($numinrow);
 4552:             if ($rem == 0) {
 4553:                 if ($i > 0) {
 4554:                     $datatable .= '</tr>';
 4555:                 }
 4556:                 $datatable .= '<tr>';
 4557:             }
 4558:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 4559:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 4560:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 4561:         }
 4562:         my $rem = @possrules%($numinrow);
 4563:         my $colsleft = $numinrow - $rem;
 4564:         if ($colsleft > 1 ) {
 4565:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4566:                           '&nbsp;</td>';
 4567:         } elsif ($colsleft == 1) {
 4568:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4569:         }
 4570:         $datatable .='</table></td></tr>';
 4571:         $itemcount ++;
 4572:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4573:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 4574:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4575:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 4576:                       'onblur="javascript:warnIntPass(this);" />'.
 4577:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 4578:                       '</span></td></tr>';
 4579:     } else {
 4580:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4581:         my %ownerchg = (
 4582:                           by  => {},
 4583:                           for => {},
 4584:                        );
 4585:         my %ownertitles = &Apache::lonlocal::texthash (
 4586:                             by  => 'Course owner status(es) allowed',
 4587:                             for => 'Student status(es) allowed',
 4588:                           );
 4589:         if (ref($settings) eq 'HASH') {
 4590:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 4591:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 4592:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 4593:                 }
 4594:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 4595:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 4596:                 }
 4597:             }
 4598:         }
 4599:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4600:         $datatable .= '<tr '.$css_class.'>'.
 4601:                       '<td>'.
 4602:                       &mt('Requirements').'<ul>'.
 4603:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 4604:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 4605:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 4606:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 4607:                       '</ul>'.
 4608:                       '</td>'.
 4609:                       '<td class="LC_left_item">';
 4610:         foreach my $item ('by','for') {
 4611:             $datatable .= '<fieldset style="display: inline-block;">'.
 4612:                           '<legend>'.$ownertitles{$item}.'</legend>';
 4613:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4614:                 foreach my $type (@{$types}) {
 4615:                     my $checked;
 4616:                     if ($ownerchg{$item}{$type}) {
 4617:                         $checked = ' checked="checked"';
 4618:                     }
 4619:                     $datatable .= '<span class="LC_nobreak"><label>'.
 4620:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 4621:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 4622:                                   '<span>&nbsp;&nbsp; ';
 4623:                 }
 4624:             }
 4625:             my $checked;
 4626:             if ($ownerchg{$item}{'default'}) {
 4627:                 $checked = ' checked="checked"';
 4628:             }
 4629:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 4630:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 4631:                           $othertitle.'</label></span></fieldset>';
 4632:         }
 4633:         $datatable .= '</td></tr>';
 4634:     }
 4635:     return $datatable;
 4636: }
 4637: 
 4638: sub print_usersessions {
 4639:     my ($position,$dom,$settings,$rowtotal) = @_;
 4640:     my ($css_class,$datatable,%checked,%choices);
 4641:     my (%by_ip,%by_location,@intdoms);
 4642:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 4643: 
 4644:     my @alldoms = &Apache::lonnet::all_domains();
 4645:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4646:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4647:     my %altids = &id_for_thisdom(%servers);
 4648:     my $itemcount = 1;
 4649:     if ($position eq 'top') {
 4650:         if (keys(%serverhomes) > 1) {
 4651:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4652:             my $curroffloadnow;
 4653:             if (ref($settings) eq 'HASH') {
 4654:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4655:                     $curroffloadnow = $settings->{'offloadnow'};
 4656:                 }
 4657:             }
 4658:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4659:         } else {
 4660:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4661:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 4662:         }
 4663:     } else {
 4664:         if (keys(%by_location) == 0) {
 4665:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4666:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 4667:         } else {
 4668:             my %lt = &usersession_titles();
 4669:             my $numinrow = 5;
 4670:             my $prefix;
 4671:             my @types;
 4672:             if ($position eq 'bottom') {
 4673:                 $prefix = 'remote';
 4674:                 @types = ('version','excludedomain','includedomain');
 4675:             } else {
 4676:                 $prefix = 'hosted';
 4677:                 @types = ('excludedomain','includedomain');
 4678:             }
 4679:             my (%current,%checkedon,%checkedoff);
 4680:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 4681:             my @locations = sort(keys(%by_location));
 4682:             foreach my $type (@types) {
 4683:                 $checkedon{$type} = '';
 4684:                 $checkedoff{$type} = ' checked="checked"';
 4685:             }
 4686:             if (ref($settings) eq 'HASH') {
 4687:                 if (ref($settings->{$prefix}) eq 'HASH') {
 4688:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 4689:                         $current{$key} = $settings->{$prefix}{$key};
 4690:                         if ($key eq 'version') {
 4691:                             if ($current{$key} ne '') {
 4692:                                 $checkedon{$key} = ' checked="checked"';
 4693:                                 $checkedoff{$key} = '';
 4694:                             }
 4695:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 4696:                             $checkedon{$key} = ' checked="checked"';
 4697:                             $checkedoff{$key} = '';
 4698:                         }
 4699:                     }
 4700:                 }
 4701:             }
 4702:             foreach my $type (@types) {
 4703:                 next if ($type ne 'version' && !@locations);
 4704:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4705:                 $datatable .= '<tr'.$css_class.'>
 4706:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 4707:                                <span class="LC_nobreak">&nbsp;
 4708:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4709:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4710:                 if ($type eq 'version') {
 4711:                     my $selector = '<select name="'.$prefix.'_version">';
 4712:                     foreach my $version (@lcversions) {
 4713:                         my $selected = '';
 4714:                         if ($current{'version'} eq $version) {
 4715:                             $selected = ' selected="selected"';
 4716:                         }
 4717:                         $selector .= ' <option value="'.$version.'"'.
 4718:                                      $selected.'>'.$version.'</option>';
 4719:                     }
 4720:                     $selector .= '</select> ';
 4721:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4722:                 } else {
 4723:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4724:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4725:                                  ' />'.('&nbsp;'x2).
 4726:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 4727:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4728:                                  "\n".
 4729:                                  '</div><div><table>';
 4730:                     my $rem;
 4731:                     for (my $i=0; $i<@locations; $i++) {
 4732:                         my ($showloc,$value,$checkedtype);
 4733:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 4734:                             my $ip = $by_location{$locations[$i]}->[0];
 4735:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 4736:                                  $value = join(':',@{$by_ip{$ip}});
 4737:                                 $showloc = join(', ',@{$by_ip{$ip}});
 4738:                                 if (ref($current{$type}) eq 'ARRAY') {
 4739:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 4740:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4741:                                             $checkedtype = ' checked="checked"';
 4742:                                             last;
 4743:                                         }
 4744:                                     }
 4745:                                 }
 4746:                             }
 4747:                         }
 4748:                         $rem = $i%($numinrow);
 4749:                         if ($rem == 0) {
 4750:                             if ($i > 0) {
 4751:                                 $datatable .= '</tr>';
 4752:                             }
 4753:                             $datatable .= '<tr>';
 4754:                         }
 4755:                         $datatable .= '<td class="LC_left_item">'.
 4756:                                       '<span class="LC_nobreak"><label>'.
 4757:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 4758:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4759:                                       '</label></span></td>';
 4760:                     }
 4761:                     $rem = @locations%($numinrow);
 4762:                     my $colsleft = $numinrow - $rem;
 4763:                     if ($colsleft > 1 ) {
 4764:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4765:                                       '&nbsp;</td>';
 4766:                     } elsif ($colsleft == 1) {
 4767:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4768:                     }
 4769:                     $datatable .= '</tr></table>';
 4770:                 }
 4771:                 $datatable .= '</td></tr>';
 4772:                 $itemcount ++;
 4773:             }
 4774:         }
 4775:     }
 4776:     $$rowtotal += $itemcount;
 4777:     return $datatable;
 4778: }
 4779: 
 4780: sub build_location_hashes {
 4781:     my ($intdoms,$by_ip,$by_location) = @_;
 4782:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4783:                   (ref($by_location) eq 'HASH')); 
 4784:     my %iphost = &Apache::lonnet::get_iphost();
 4785:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4786:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4787:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4788:         foreach my $id (@{$iphost{$primary_ip}}) {
 4789:             my $intdom = &Apache::lonnet::internet_dom($id);
 4790:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4791:                 push(@{$intdoms},$intdom);
 4792:             }
 4793:         }
 4794:     }
 4795:     foreach my $ip (keys(%iphost)) {
 4796:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4797:             foreach my $id (@{$iphost{$ip}}) {
 4798:                 my $location = &Apache::lonnet::internet_dom($id);
 4799:                 if ($location) {
 4800:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 4801:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4802:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4803:                             push(@{$by_ip->{$ip}},$location);
 4804:                         }
 4805:                     } else {
 4806:                         $by_ip->{$ip} = [$location];
 4807:                     }
 4808:                 }
 4809:             }
 4810:         }
 4811:     }
 4812:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4813:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4814:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4815:             my $first = $by_ip->{$ip}->[0];
 4816:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4817:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4818:                     push(@{$by_location->{$first}},$ip);
 4819:                 }
 4820:             } else {
 4821:                 $by_location->{$first} = [$ip];
 4822:             }
 4823:         }
 4824:     }
 4825:     return;
 4826: }
 4827: 
 4828: sub current_offloads_to {
 4829:     my ($dom,$settings,$servers) = @_;
 4830:     my (%spareid,%otherdomconfigs);
 4831:     if (ref($servers) eq 'HASH') {
 4832:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4833:             my $gotspares;
 4834:             if (ref($settings) eq 'HASH') {
 4835:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4836:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4837:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4838:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4839:                         $gotspares = 1;
 4840:                     }
 4841:                 }
 4842:             }
 4843:             unless ($gotspares) {
 4844:                 my $gotspares;
 4845:                 my $serverhomeID =
 4846:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4847:                 my $serverhomedom =
 4848:                     &Apache::lonnet::host_domain($serverhomeID);
 4849:                 if ($serverhomedom ne $dom) {
 4850:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4851:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4852:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4853:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4854:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4855:                                 $gotspares = 1;
 4856:                             }
 4857:                         }
 4858:                     } else {
 4859:                         $otherdomconfigs{$serverhomedom} =
 4860:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4861:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4862:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4863:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4864:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4865:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4866:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4867:                                         $gotspares = 1;
 4868:                                     }
 4869:                                 }
 4870:                             }
 4871:                         }
 4872:                     }
 4873:                 }
 4874:             }
 4875:             unless ($gotspares) {
 4876:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4877:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4878:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4879:                } else {
 4880:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4881:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4882:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4883:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4884:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4885:                     } else {
 4886:                         my %what = (
 4887:                              spareid => 1,
 4888:                         );
 4889:                         my ($result,$returnhash) = 
 4890:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4891:                         if ($result eq 'ok') { 
 4892:                             if (ref($returnhash) eq 'HASH') {
 4893:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4894:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4895:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4896:                                 }
 4897:                             }
 4898:                         }
 4899:                     }
 4900:                 }
 4901:             }
 4902:         }
 4903:     }
 4904:     return %spareid;
 4905: }
 4906: 
 4907: sub spares_row {
 4908:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4909:     my $css_class;
 4910:     my $numinrow = 4;
 4911:     my $itemcount = 1;
 4912:     my $datatable;
 4913:     my %typetitles = &sparestype_titles();
 4914:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4915:         foreach my $server (sort(keys(%{$servers}))) {
 4916:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4917:             my ($othercontrol,$serverdom);
 4918:             if ($serverhome ne $server) {
 4919:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4920:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4921:             } else {
 4922:                 $serverdom = &Apache::lonnet::host_domain($server);
 4923:                 if ($serverdom ne $dom) {
 4924:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4925:                 }
 4926:             }
 4927:             next unless (ref($spareid->{$server}) eq 'HASH');
 4928:             my $checkednow;
 4929:             if (ref($curroffloadnow) eq 'HASH') {
 4930:                 if ($curroffloadnow->{$server}) {
 4931:                     $checkednow = ' checked="checked"';
 4932:                 }
 4933:             }
 4934:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4935:             $datatable .= '<tr'.$css_class.'>
 4936:                            <td rowspan="2">
 4937:                             <span class="LC_nobreak">'.
 4938:                           &mt('[_1] when busy, offloads to:'
 4939:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4940:                           '<span class="LC_nobreak">'."\n".
 4941:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4942:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4943:                           "\n";
 4944:             my (%current,%canselect);
 4945:             my @choices = 
 4946:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4947:             foreach my $type ('primary','default') {
 4948:                 if (ref($spareid->{$server}) eq 'HASH') {
 4949:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4950:                         my @spares = @{$spareid->{$server}{$type}};
 4951:                         if (@spares > 0) {
 4952:                             if ($othercontrol) {
 4953:                                 $current{$type} = join(', ',@spares);
 4954:                             } else {
 4955:                                 $current{$type} .= '<table>';
 4956:                                 my $numspares = scalar(@spares);
 4957:                                 for (my $i=0;  $i<@spares; $i++) {
 4958:                                     my $rem = $i%($numinrow);
 4959:                                     if ($rem == 0) {
 4960:                                         if ($i > 0) {
 4961:                                             $current{$type} .= '</tr>';
 4962:                                         }
 4963:                                         $current{$type} .= '<tr>';
 4964:                                     }
 4965:                                     $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;'.
 4966:                                                        $spareid->{$server}{$type}[$i].
 4967:                                                        '</label></td>'."\n";
 4968:                                 }
 4969:                                 my $rem = @spares%($numinrow);
 4970:                                 my $colsleft = $numinrow - $rem;
 4971:                                 if ($colsleft > 1 ) {
 4972:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4973:                                                        '" class="LC_left_item">'.
 4974:                                                        '&nbsp;</td>';
 4975:                                 } elsif ($colsleft == 1) {
 4976:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4977:                                 }
 4978:                                 $current{$type} .= '</tr></table>';
 4979:                             }
 4980:                         }
 4981:                     }
 4982:                     if ($current{$type} eq '') {
 4983:                         $current{$type} = &mt('None specified');
 4984:                     }
 4985:                     if ($othercontrol) {
 4986:                         if ($type eq 'primary') {
 4987:                             $canselect{$type} = $othercontrol;
 4988:                         }
 4989:                     } else {
 4990:                         $canselect{$type} = 
 4991:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4992:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4993:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4994:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4995:                         if (@choices > 0) {
 4996:                             foreach my $lonhost (@choices) {
 4997:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4998:                             }
 4999:                         }
 5000:                         $canselect{$type} .= '</select>'."\n";
 5001:                     }
 5002:                 } else {
 5003:                     $current{$type} = &mt('Could not be determined');
 5004:                     if ($type eq 'primary') {
 5005:                         $canselect{$type} =  $othercontrol;
 5006:                     }
 5007:                 }
 5008:                 if ($type eq 'default') {
 5009:                     $datatable .= '<tr'.$css_class.'>';
 5010:                 }
 5011:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5012:                               '<td>'.$current{$type}.'</td>'."\n".
 5013:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5014:             }
 5015:             $itemcount ++;
 5016:         }
 5017:     }
 5018:     $$rowtotal += $itemcount;
 5019:     return $datatable;
 5020: }
 5021: 
 5022: sub possible_newspares {
 5023:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5024:     my $serverhostname = &Apache::lonnet::hostname($server);
 5025:     my %excluded;
 5026:     if ($serverhostname ne '') {
 5027:         %excluded = (
 5028:                        $serverhostname => 1,
 5029:                     );
 5030:     }
 5031:     if (ref($currspares) eq 'HASH') {
 5032:         foreach my $type (keys(%{$currspares})) {
 5033:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5034:                 if (@{$currspares->{$type}} > 0) {
 5035:                     foreach my $curr (@{$currspares->{$type}}) {
 5036:                         my $hostname = &Apache::lonnet::hostname($curr);
 5037:                         $excluded{$hostname} = 1;
 5038:                     }
 5039:                 }
 5040:             }
 5041:         }
 5042:     }
 5043:     my @choices;
 5044:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5045:         if (keys(%{$serverhomes}) > 1) {
 5046:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5047:                 unless ($excluded{$name}) {
 5048:                     if (exists($altids->{$serverhomes->{$name}})) {
 5049:                         push(@choices,$altids->{$serverhomes->{$name}});
 5050:                     } else {
 5051:                         push(@choices,$serverhomes->{$name});
 5052:                     }
 5053:                 }
 5054:             }
 5055:         }
 5056:     }
 5057:     return sort(@choices);
 5058: }
 5059: 
 5060: sub print_loadbalancing {
 5061:     my ($dom,$settings,$rowtotal) = @_;
 5062:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5063:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5064:     my $numinrow = 1;
 5065:     my $datatable;
 5066:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5067:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 5068:     if (ref($settings) eq 'HASH') {
 5069:         %existing = %{$settings};
 5070:     }
 5071:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5072:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5073:                                   \%currtargets,\%currrules,\%currcookies);
 5074:     } else {
 5075:         return;
 5076:     }
 5077:     my ($othertitle,$usertypes,$types) =
 5078:         &Apache::loncommon::sorted_inst_types($dom);
 5079:     my $rownum = 8;
 5080:     if (ref($types) eq 'ARRAY') {
 5081:         $rownum += scalar(@{$types});
 5082:     }
 5083:     my @css_class = ('LC_odd_row','LC_even_row');
 5084:     my $balnum = 0;
 5085:     my $islast;
 5086:     my (@toshow,$disabledtext);
 5087:     if (keys(%currbalancer) > 0) {
 5088:         @toshow = sort(keys(%currbalancer));
 5089:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5090:             push(@toshow,'');
 5091:         }
 5092:     } else {
 5093:         @toshow = ('');
 5094:         $disabledtext = &mt('No existing load balancer');
 5095:     }
 5096:     foreach my $lonhost (@toshow) {
 5097:         if ($balnum == scalar(@toshow)-1) {
 5098:             $islast = 1;
 5099:         } else {
 5100:             $islast = 0;
 5101:         }
 5102:         my $cssidx = $balnum%2;
 5103:         my $targets_div_style = 'display: none';
 5104:         my $disabled_div_style = 'display: block';
 5105:         my $homedom_div_style = 'display: none';
 5106:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5107:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5108:                       '<p>';
 5109:         if ($lonhost eq '') {
 5110:             $datatable .= '<span class="LC_nobreak">';
 5111:             if (keys(%currbalancer) > 0) {
 5112:                 $datatable .= &mt('Add balancer:');
 5113:             } else {
 5114:                 $datatable .= &mt('Enable balancer:');
 5115:             }
 5116:             $datatable .= '&nbsp;'.
 5117:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5118:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5119:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5120:                           '<option value="" selected="selected">'.&mt('None').
 5121:                           '</option>'."\n";
 5122:             foreach my $server (sort(keys(%servers))) {
 5123:                 next if ($currbalancer{$server});
 5124:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5125:             }
 5126:             $datatable .=
 5127:                 '</select>'."\n".
 5128:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5129:         } else {
 5130:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5131:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5132:                            &mt('Stop balancing').'</label>'.
 5133:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5134:             $targets_div_style = 'display: block';
 5135:             $disabled_div_style = 'display: none';
 5136:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5137:                 $homedom_div_style = 'display: block';
 5138:             }
 5139:         }
 5140:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5141:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5142:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5143:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5144:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5145:         my @sparestypes = ('primary','default');
 5146:         my %typetitles = &sparestype_titles();
 5147:         my %hostherechecked = (
 5148:                                   no => ' checked="checked"',
 5149:                               );
 5150:         my %balcookiechecked = (
 5151:                                   no => ' checked="checked"',
 5152:                                );
 5153:         foreach my $sparetype (@sparestypes) {
 5154:             my $targettable;
 5155:             for (my $i=0; $i<$numspares; $i++) {
 5156:                 my $checked;
 5157:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5158:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5159:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5160:                             $checked = ' checked="checked"';
 5161:                         }
 5162:                     }
 5163:                 }
 5164:                 my ($chkboxval,$disabled);
 5165:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5166:                     $chkboxval = $spares[$i];
 5167:                 }
 5168:                 if (exists($currbalancer{$spares[$i]})) {
 5169:                     $disabled = ' disabled="disabled"';
 5170:                 }
 5171:                 $targettable .=
 5172:                     '<td><span class="LC_nobreak"><label>'.
 5173:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5174:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5175:                     '</span></label></span></td>';
 5176:                 my $rem = $i%($numinrow);
 5177:                 if ($rem == 0) {
 5178:                     if (($i > 0) && ($i < $numspares-1)) {
 5179:                         $targettable .= '</tr>';
 5180:                     }
 5181:                     if ($i < $numspares-1) {
 5182:                         $targettable .= '<tr>';
 5183:                     }
 5184:                 }
 5185:             }
 5186:             if ($targettable ne '') {
 5187:                 my $rem = $numspares%($numinrow);
 5188:                 my $colsleft = $numinrow - $rem;
 5189:                 if ($colsleft > 1 ) {
 5190:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5191:                                     '&nbsp;</td>';
 5192:                 } elsif ($colsleft == 1) {
 5193:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5194:                 }
 5195:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5196:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5197:             }
 5198:             $hostherechecked{$sparetype} = '';
 5199:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5200:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5201:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5202:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5203:                         $hostherechecked{'no'} = '';
 5204:                     }
 5205:                 }
 5206:             }
 5207:         }
 5208:         if ($currcookies{$lonhost}) {
 5209:             %balcookiechecked = (
 5210:                                     yes => ' checked="checked"',
 5211:                                 );
 5212:         }
 5213:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5214:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5215:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5216:         foreach my $sparetype (@sparestypes) {
 5217:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5218:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5219:                           '</i></label><br />';
 5220:         }
 5221:         $datatable .= &mt('Use balancer cookie').'<br />'.
 5222:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 5223:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 5224:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 5225:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 5226:                       '</div></td></tr>'.
 5227:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5228:                                            $othertitle,$usertypes,$types,\%servers,
 5229:                                            \%currbalancer,$lonhost,
 5230:                                            $targets_div_style,$homedom_div_style,
 5231:                                            $css_class[$cssidx],$balnum,$islast);
 5232:         $$rowtotal += $rownum;
 5233:         $balnum ++;
 5234:     }
 5235:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5236:     return $datatable;
 5237: }
 5238: 
 5239: sub get_loadbalancers_config {
 5240:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 5241:     return unless ((ref($servers) eq 'HASH') &&
 5242:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5243:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 5244:                    (ref($currcookies) eq 'HASH'));
 5245:     if (keys(%{$existing}) > 0) {
 5246:         my $oldlonhost;
 5247:         foreach my $key (sort(keys(%{$existing}))) {
 5248:             if ($key eq 'lonhost') {
 5249:                 $oldlonhost = $existing->{'lonhost'};
 5250:                 $currbalancer->{$oldlonhost} = 1;
 5251:             } elsif ($key eq 'targets') {
 5252:                 if ($oldlonhost) {
 5253:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5254:                 }
 5255:             } elsif ($key eq 'rules') {
 5256:                 if ($oldlonhost) {
 5257:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5258:                 }
 5259:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5260:                 $currbalancer->{$key} = 1;
 5261:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5262:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5263:                 if ($existing->{$key}{'cookie'}) {
 5264:                     $currcookies->{$key} = 1;
 5265:                 }
 5266:             }
 5267:         }
 5268:     } else {
 5269:         my ($balancerref,$targetsref) =
 5270:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5271:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5272:             foreach my $server (sort(keys(%{$balancerref}))) {
 5273:                 $currbalancer->{$server} = 1;
 5274:                 $currtargets->{$server} = $targetsref->{$server};
 5275:             }
 5276:         }
 5277:     }
 5278:     return;
 5279: }
 5280: 
 5281: sub loadbalancing_rules {
 5282:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5283:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5284:         $css_class,$balnum,$islast) = @_;
 5285:     my $output;
 5286:     my $num = 0;
 5287:     my ($alltypes,$othertypes,$titles) =
 5288:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5289:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5290:         foreach my $type (@{$alltypes}) {
 5291:             $num ++;
 5292:             my $current;
 5293:             if (ref($currrules) eq 'HASH') {
 5294:                 $current = $currrules->{$type};
 5295:             }
 5296:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5297:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5298:                     $current = '';
 5299:                 }
 5300:             }
 5301:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5302:                                              $servers,$currbalancer,$lonhost,$dom,
 5303:                                              $targets_div_style,$homedom_div_style,
 5304:                                              $css_class,$balnum,$num,$islast);
 5305:         }
 5306:     }
 5307:     return $output;
 5308: }
 5309: 
 5310: sub loadbalancing_titles {
 5311:     my ($dom,$intdom,$usertypes,$types) = @_;
 5312:     my %othertypes = (
 5313:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5314:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5315:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5316:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5317:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5318:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5319:                      );
 5320:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5321:     my @available;
 5322:     if (ref($types) eq 'ARRAY') {
 5323:         @available = @{$types};
 5324:     }
 5325:     unless (grep(/^default$/,@available)) {
 5326:         push(@available,'default');
 5327:     }
 5328:     unshift(@alltypes,@available);
 5329:     my %titles;
 5330:     foreach my $type (@alltypes) {
 5331:         if ($type =~ /^_LC_/) {
 5332:             $titles{$type} = $othertypes{$type};
 5333:         } elsif ($type eq 'default') {
 5334:             $titles{$type} = &mt('All users from [_1]',$dom);
 5335:             if (ref($types) eq 'ARRAY') {
 5336:                 if (@{$types} > 0) {
 5337:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5338:                 }
 5339:             }
 5340:         } elsif (ref($usertypes) eq 'HASH') {
 5341:             $titles{$type} = $usertypes->{$type};
 5342:         }
 5343:     }
 5344:     return (\@alltypes,\%othertypes,\%titles);
 5345: }
 5346: 
 5347: sub loadbalance_rule_row {
 5348:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5349:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5350:     my @rulenames;
 5351:     my %ruletitles = &offloadtype_text();
 5352:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5353:         @rulenames = ('balancer','offloadedto','specific');
 5354:     } else {
 5355:         @rulenames = ('default','homeserver');
 5356:         if ($type eq '_LC_external') {
 5357:             push(@rulenames,'externalbalancer');
 5358:         } else {
 5359:             push(@rulenames,'specific');
 5360:         }
 5361:         push(@rulenames,'none');
 5362:     }
 5363:     my $style = $targets_div_style;
 5364:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5365:         $style = $homedom_div_style;
 5366:     }
 5367:     my $space;
 5368:     if ($islast && $num == 1) {
 5369:         $space = '<div display="inline-block">&nbsp;</div>';
 5370:     }
 5371:     my $output =
 5372:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5373:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5374:         '<td valaign="top">'.$space.
 5375:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5376:     for (my $i=0; $i<@rulenames; $i++) {
 5377:         my $rule = $rulenames[$i];
 5378:         my ($checked,$extra);
 5379:         if ($rulenames[$i] eq 'default') {
 5380:             $rule = '';
 5381:         }
 5382:         if ($rulenames[$i] eq 'specific') {
 5383:             if (ref($servers) eq 'HASH') {
 5384:                 my $default;
 5385:                 if (($current ne '') && (exists($servers->{$current}))) {
 5386:                     $checked = ' checked="checked"';
 5387:                 }
 5388:                 unless ($checked) {
 5389:                     $default = ' selected="selected"';
 5390:                 }
 5391:                 $extra =
 5392:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5393:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5394:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5395:                     '<option value=""'.$default.'></option>'."\n";
 5396:                 foreach my $server (sort(keys(%{$servers}))) {
 5397:                     if (ref($currbalancer) eq 'HASH') {
 5398:                         next if (exists($currbalancer->{$server}));
 5399:                     }
 5400:                     my $selected;
 5401:                     if ($server eq $current) {
 5402:                         $selected = ' selected="selected"';
 5403:                     }
 5404:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5405:                 }
 5406:                 $extra .= '</select>';
 5407:             }
 5408:         } elsif ($rule eq $current) {
 5409:             $checked = ' checked="checked"';
 5410:         }
 5411:         $output .= '<span class="LC_nobreak"><label>'.
 5412:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5413:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5414:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5415:                    ')"'.$checked.' />&nbsp;';
 5416:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5417:             $output .= $ruletitles{'particular'};
 5418:         } else {
 5419:             $output .= $ruletitles{$rulenames[$i]};
 5420:         }
 5421:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5422:     }
 5423:     $output .= '</div></td></tr>'."\n";
 5424:     return $output;
 5425: }
 5426: 
 5427: sub offloadtype_text {
 5428:     my %ruletitles = &Apache::lonlocal::texthash (
 5429:            'default'          => 'Offloads to default destinations',
 5430:            'homeserver'       => "Offloads to user's home server",
 5431:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5432:            'specific'         => 'Offloads to specific server',
 5433:            'none'             => 'No offload',
 5434:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5435:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5436:            'particular'       => 'Session hosted (after re-auth) on server:',
 5437:     );
 5438:     return %ruletitles;
 5439: }
 5440: 
 5441: sub sparestype_titles {
 5442:     my %typestitles = &Apache::lonlocal::texthash (
 5443:                           'primary' => 'primary',
 5444:                           'default' => 'default',
 5445:                       );
 5446:     return %typestitles;
 5447: }
 5448: 
 5449: sub contact_titles {
 5450:     my %titles = &Apache::lonlocal::texthash (
 5451:                    'supportemail'    => 'Support E-mail address',
 5452:                    'adminemail'      => 'Default Server Admin E-mail address',
 5453:                    'errormail'       => 'Error reports to be e-mailed to',
 5454:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5455:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 5456:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 5457:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5458:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5459:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5460:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5461:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 5462:                  );
 5463:     my %short_titles = &Apache::lonlocal::texthash (
 5464:                            adminemail   => 'Admin E-mail address',
 5465:                            supportemail => 'Support E-mail',
 5466:                        );   
 5467:     return (\%titles,\%short_titles);
 5468: }
 5469: 
 5470: sub helpform_fields {
 5471:     my %titles =  &Apache::lonlocal::texthash (
 5472:                        'username'   => 'Name',
 5473:                        'user'       => 'Username/domain',
 5474:                        'phone'      => 'Phone',
 5475:                        'cc'         => 'Cc e-mail',
 5476:                        'course'     => 'Course Details',
 5477:                        'section'    => 'Sections',
 5478:                        'screenshot' => 'File upload',
 5479:     );
 5480:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5481:     my %possoptions = (
 5482:                         username     => ['yes','no','req'],
 5483:                         phone        => ['yes','no','req'],
 5484:                         user         => ['yes','no'],
 5485:                         cc           => ['yes','no'],
 5486:                         course       => ['yes','no'],
 5487:                         section      => ['yes','no'],
 5488:                         screenshot   => ['yes','no'],
 5489:                       );
 5490:     my %fieldoptions = &Apache::lonlocal::texthash (
 5491:                          'yes'  => 'Optional',
 5492:                          'req'  => 'Required',
 5493:                          'no'   => "Not shown",
 5494:     );
 5495:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5496: }
 5497: 
 5498: sub tool_titles {
 5499:     my %titles = &Apache::lonlocal::texthash (
 5500:                      aboutme    => 'Personal web page',
 5501:                      blog       => 'Blog',
 5502:                      webdav     => 'WebDAV',
 5503:                      portfolio  => 'Portfolio',
 5504:                      official   => 'Official courses (with institutional codes)',
 5505:                      unofficial => 'Unofficial courses',
 5506:                      community  => 'Communities',
 5507:                      textbook   => 'Textbook courses',
 5508:                  );
 5509:     return %titles;
 5510: }
 5511: 
 5512: sub courserequest_titles {
 5513:     my %titles = &Apache::lonlocal::texthash (
 5514:                                    official   => 'Official',
 5515:                                    unofficial => 'Unofficial',
 5516:                                    community  => 'Communities',
 5517:                                    textbook   => 'Textbook',
 5518:                                    norequest  => 'Not allowed',
 5519:                                    approval   => 'Approval by Dom. Coord.',
 5520:                                    validate   => 'With validation',
 5521:                                    autolimit  => 'Numerical limit',
 5522:                                    unlimited  => '(blank for unlimited)',
 5523:                  );
 5524:     return %titles;
 5525: }
 5526: 
 5527: sub authorrequest_titles {
 5528:     my %titles = &Apache::lonlocal::texthash (
 5529:                                    norequest  => 'Not allowed',
 5530:                                    approval   => 'Approval by Dom. Coord.',
 5531:                                    automatic  => 'Automatic approval',
 5532:                  );
 5533:     return %titles;
 5534: }
 5535: 
 5536: sub courserequest_conditions {
 5537:     my %conditions = &Apache::lonlocal::texthash (
 5538:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5539:        validate   => '(Processing of request subject to institutional validation).',
 5540:                  );
 5541:     return %conditions;
 5542: }
 5543: 
 5544: 
 5545: sub print_usercreation {
 5546:     my ($position,$dom,$settings,$rowtotal) = @_;
 5547:     my $numinrow = 4;
 5548:     my $datatable;
 5549:     if ($position eq 'top') {
 5550:         $$rowtotal ++;
 5551:         my $rowcount = 0;
 5552:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5553:         if (ref($rules) eq 'HASH') {
 5554:             if (keys(%{$rules}) > 0) {
 5555:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5556:                                                 $ruleorder,$numinrow,$rowcount);
 5557:                 $$rowtotal ++;
 5558:                 $rowcount ++;
 5559:             }
 5560:         }
 5561:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5562:         if (ref($idrules) eq 'HASH') {
 5563:             if (keys(%{$idrules}) > 0) {
 5564:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5565:                                                 $idruleorder,$numinrow,$rowcount);
 5566:                 $$rowtotal ++;
 5567:                 $rowcount ++;
 5568:             }
 5569:         }
 5570:         if ($rowcount == 0) {
 5571:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5572:             $$rowtotal ++;
 5573:             $rowcount ++;
 5574:         }
 5575:     } elsif ($position eq 'middle') {
 5576:         my @creators = ('author','course','requestcrs');
 5577:         my ($rules,$ruleorder) =
 5578:             &Apache::lonnet::inst_userrules($dom,'username');
 5579:         my %lt = &usercreation_types();
 5580:         my %checked;
 5581:         if (ref($settings) eq 'HASH') {
 5582:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5583:                 foreach my $item (@creators) {
 5584:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5585:                 }
 5586:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5587:                 foreach my $item (@creators) {
 5588:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5589:                         $checked{$item} = 'none';
 5590:                     }
 5591:                 }
 5592:             }
 5593:         }
 5594:         my $rownum = 0;
 5595:         foreach my $item (@creators) {
 5596:             $rownum ++;
 5597:             if ($checked{$item} eq '') {
 5598:                 $checked{$item} = 'any';
 5599:             }
 5600:             my $css_class;
 5601:             if ($rownum%2) {
 5602:                 $css_class = '';
 5603:             } else {
 5604:                 $css_class = ' class="LC_odd_row" ';
 5605:             }
 5606:             $datatable .= '<tr'.$css_class.'>'.
 5607:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5608:                          '</span></td><td align="right">';
 5609:             my @options = ('any');
 5610:             if (ref($rules) eq 'HASH') {
 5611:                 if (keys(%{$rules}) > 0) {
 5612:                     push(@options,('official','unofficial'));
 5613:                 }
 5614:             }
 5615:             push(@options,'none');
 5616:             foreach my $option (@options) {
 5617:                 my $type = 'radio';
 5618:                 my $check = ' ';
 5619:                 if ($checked{$item} eq $option) {
 5620:                     $check = ' checked="checked" ';
 5621:                 } 
 5622:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5623:                               '<input type="'.$type.'" name="can_createuser_'.
 5624:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5625:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5626:             }
 5627:             $datatable .= '</td></tr>';
 5628:         }
 5629:     } else {
 5630:         my @contexts = ('author','course','domain');
 5631:         my @authtypes = ('int','krb4','krb5','loc');
 5632:         my %checked;
 5633:         if (ref($settings) eq 'HASH') {
 5634:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5635:                 foreach my $item (@contexts) {
 5636:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5637:                         foreach my $auth (@authtypes) {
 5638:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5639:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5640:                             }
 5641:                         }
 5642:                     }
 5643:                 }
 5644:             }
 5645:         } else {
 5646:             foreach my $item (@contexts) {
 5647:                 foreach my $auth (@authtypes) {
 5648:                     $checked{$item}{$auth} = ' checked="checked" ';
 5649:                 }
 5650:             }
 5651:         }
 5652:         my %title = &context_names();
 5653:         my %authname = &authtype_names();
 5654:         my $rownum = 0;
 5655:         my $css_class; 
 5656:         foreach my $item (@contexts) {
 5657:             if ($rownum%2) {
 5658:                 $css_class = '';
 5659:             } else {
 5660:                 $css_class = ' class="LC_odd_row" ';
 5661:             }
 5662:             $datatable .=   '<tr'.$css_class.'>'.
 5663:                             '<td>'.$title{$item}.
 5664:                             '</td><td class="LC_left_item">'.
 5665:                             '<span class="LC_nobreak">';
 5666:             foreach my $auth (@authtypes) {
 5667:                 $datatable .= '<label>'. 
 5668:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5669:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5670:                               $authname{$auth}.'</label>&nbsp;';
 5671:             }
 5672:             $datatable .= '</span></td></tr>';
 5673:             $rownum ++;
 5674:         }
 5675:         $$rowtotal += $rownum;
 5676:     }
 5677:     return $datatable;
 5678: }
 5679: 
 5680: sub print_selfcreation {
 5681:     my ($position,$dom,$settings,$rowtotal) = @_;
 5682:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 5683:         $emaildomain,$datatable);
 5684:     if (ref($settings) eq 'HASH') {
 5685:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5686:             $createsettings = $settings->{'cancreate'};
 5687:             if (ref($createsettings) eq 'HASH') {
 5688:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5689:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5690:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5691:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5692:                         @selfcreate = ('email','login','sso');
 5693:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5694:                         @selfcreate = ($createsettings->{'selfcreate'});
 5695:                     }
 5696:                 }
 5697:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5698:                     $processing = $createsettings->{'selfcreateprocessing'};
 5699:                 }
 5700:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 5701:                     $emailoptions = $createsettings->{'emailoptions'};
 5702:                 }
 5703:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 5704:                     $emailverified = $createsettings->{'emailverified'};
 5705:                 }
 5706:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 5707:                     $emaildomain = $createsettings->{'emaildomain'};
 5708:                 }
 5709:             }
 5710:         }
 5711:     }
 5712:     my %radiohash;
 5713:     my $numinrow = 4;
 5714:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5715:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5716:     if ($position eq 'top') {
 5717:         my %choices = &Apache::lonlocal::texthash (
 5718:                                                       cancreate_login      => 'Institutional Login',
 5719:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5720:                                                   );
 5721:         my @toggles = sort(keys(%choices));
 5722:         my %defaultchecked = (
 5723:                                'cancreate_login' => 'off',
 5724:                                'cancreate_sso'   => 'off',
 5725:                              );
 5726:         my ($onclick,$itemcount);
 5727:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5728:                                                      \%choices,$itemcount,$onclick);
 5729:         $$rowtotal += $itemcount;
 5730: 
 5731:         if (ref($usertypes) eq 'HASH') {
 5732:             if (keys(%{$usertypes}) > 0) {
 5733:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5734:                                              $dom,$numinrow,$othertitle,
 5735:                                              'statustocreate',$rowtotal);
 5736:                 $$rowtotal ++;
 5737:             }
 5738:         }
 5739:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5740:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5741:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5742:         my $rem;
 5743:         my $numperrow = 2;
 5744:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5745:         $datatable .= '<tr'.$css_class.'>'.
 5746:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5747:                      '<td class="LC_left_item">'."\n".
 5748:                      '<table>'."\n";
 5749:         for (my $i=0; $i<@fields; $i++) {
 5750:             $rem = $i%($numperrow);
 5751:             if ($rem == 0) {
 5752:                 if ($i > 0) {
 5753:                     $datatable .= '</tr>';
 5754:                 }
 5755:                 $datatable .= '<tr>';
 5756:             }
 5757:             my $currval;
 5758:             if (ref($createsettings) eq 'HASH') {
 5759:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5760:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5761:                 }
 5762:             }
 5763:             $datatable .= '<td class="LC_left_item">'.
 5764:                           '<span class="LC_nobreak">'.
 5765:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5766:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5767:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5768:         }
 5769:         my $colsleft = $numperrow - $rem;
 5770:         if ($colsleft > 1 ) {
 5771:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5772:                          '&nbsp;</td>';
 5773:         } elsif ($colsleft == 1) {
 5774:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5775:         }
 5776:         $datatable .= '</tr></table></td></tr>';
 5777:         $$rowtotal ++;
 5778:     } elsif ($position eq 'middle') {
 5779:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5780:         my @posstypes;
 5781:         if (ref($types) eq 'ARRAY') {
 5782:             @posstypes = @{$types};
 5783:         }
 5784:         unless (grep(/^default$/,@posstypes)) {
 5785:             push(@posstypes,'default');
 5786:         }
 5787:         my %usertypeshash;
 5788:         if (ref($usertypes) eq 'HASH') {
 5789:             %usertypeshash = %{$usertypes};
 5790:         }
 5791:         $usertypeshash{'default'} = $othertitle;
 5792:         foreach my $status (@posstypes) {
 5793:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5794:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 5795:             $$rowtotal ++;
 5796:         }
 5797:     } else {
 5798:         my %choices = &Apache::lonlocal::texthash (
 5799:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 5800:                                                   );
 5801:         my @toggles = sort(keys(%choices));
 5802:         my %defaultchecked = (
 5803:                                'cancreate_email' => 'off',
 5804:                              );
 5805:         my $customclass = 'LC_selfcreate_email';
 5806:         my $classprefix = 'LC_canmodify_emailusername_';
 5807:         my $optionsprefix = 'LC_options_emailusername_';
 5808:         my $display = 'none';
 5809:         my $rowstyle = 'display:none';
 5810:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5811:             $display = 'block';
 5812:             $rowstyle = 'display:table-row';
 5813:         }
 5814:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 5815:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5816:                                                      \%choices,$$rowtotal,$onclick);
 5817:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 5818:                                          $rowstyle);
 5819:         $$rowtotal ++;
 5820:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 5821:                                       $rowstyle);
 5822:         $$rowtotal ++;
 5823:         my (@ordered,@posstypes,%usertypeshash);
 5824:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5825:         my ($emailrules,$emailruleorder) =
 5826:             &Apache::lonnet::inst_userrules($dom,'email');
 5827:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5828:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5829:         if (ref($types) eq 'ARRAY') {
 5830:             @posstypes = @{$types};
 5831:         }
 5832:         if (@posstypes) {
 5833:             unless (grep(/^default$/,@posstypes)) {
 5834:                 push(@posstypes,'default');
 5835:             }
 5836:             if (ref($usertypes) eq 'HASH') {
 5837:                 %usertypeshash = %{$usertypes};
 5838:             }
 5839:             my $currassign;
 5840:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 5841:                 $currassign = {
 5842:                                   selfassign => $domdefaults{'inststatusguest'},
 5843:                               };
 5844:                 @ordered = @{$domdefaults{'inststatusguest'}};
 5845:             } else {
 5846:                 $currassign = { selfassign => [] };
 5847:             }
 5848:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 5849:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 5850:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 5851:                                          $numinrow,$othertitle,'selfassign',
 5852:                                          $rowtotal,$onclicktypes,$customclass,
 5853:                                          $rowstyle);
 5854:             $$rowtotal ++;
 5855:             $usertypeshash{'default'} = $othertitle;
 5856:             foreach my $status (@posstypes) {
 5857:                 my $css_class;
 5858:                 if ($$rowtotal%2) {
 5859:                     $css_class = 'LC_odd_row ';
 5860:                 }
 5861:                 $css_class .= $customclass;
 5862:                 my $rowid = $optionsprefix.$status;
 5863:                 my $hidden = 1;
 5864:                 my $currstyle = 'display:none';
 5865:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5866:                     $currstyle = $rowstyle;
 5867:                     $hidden = 0;
 5868:                 }
 5869:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5870:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 5871:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 5872:                 unless ($hidden) {
 5873:                     $$rowtotal ++;
 5874:                 }
 5875:             }
 5876:         } else {
 5877:             my $css_class;
 5878:             if ($$rowtotal%2) {
 5879:                 $css_class = 'LC_odd_row ';
 5880:             }
 5881:             $css_class .= $customclass;
 5882:             $usertypeshash{'default'} = $othertitle;
 5883:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5884:                                          $emailrules,$emailruleorder,$settings,'default','',
 5885:                                          $othertitle,$css_class,$rowstyle,$intdom);
 5886:             $$rowtotal ++;
 5887:         }
 5888:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5889:         $numinrow = 1;
 5890:         if (@posstypes) {
 5891:             foreach my $status (@posstypes) {
 5892:                 my $rowid = $classprefix.$status;
 5893:                 my $datarowstyle = 'display:none';
 5894:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5895:                     $datarowstyle = $rowstyle;
 5896:                 }
 5897:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5898:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5899:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 5900:                 unless ($datarowstyle eq 'display:none') {
 5901:                     $$rowtotal ++;
 5902:                 }
 5903:             }
 5904:         } else {
 5905:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 5906:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5907:                                                    $infotitles,'',$customclass,$rowstyle);
 5908:         }
 5909:     }
 5910:     return $datatable;
 5911: }
 5912: 
 5913: sub selfcreate_javascript {
 5914:     return <<"ENDSCRIPT";
 5915: 
 5916: <script type="text/javascript">
 5917: // <![CDATA[
 5918: 
 5919: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 5920:     var x = document.getElementsByClassName(target);
 5921:     var insttypes = 0;
 5922:     var insttypeRegExp = new RegExp(prefix);
 5923:     if ((x.length != undefined) && (x.length > 0)) {
 5924:         if (form.elements[radio].length != undefined) {
 5925:             for (var i=0; i<form.elements[radio].length; i++) {
 5926:                 if (form.elements[radio][i].checked) {
 5927:                     if (form.elements[radio][i].value == 1) {
 5928:                         for (var j=0; j<x.length; j++) {
 5929:                             if (x[j].id == 'undefined') {
 5930:                                 x[j].style.display = 'table-row';
 5931:                             } else if (insttypeRegExp.test(x[j].id)) {
 5932:                                 insttypes ++;
 5933:                             } else {
 5934:                                 x[j].style.display = 'table-row';
 5935:                             }
 5936:                         }
 5937:                     } else {
 5938:                         for (var j=0; j<x.length; j++) {
 5939:                             x[j].style.display = 'none';
 5940:                         }
 5941:                     }
 5942:                     break;
 5943:                 }
 5944:             }
 5945:             if (insttypes > 0) {
 5946:                 toggleDataRow(form,checkbox,target,altprefix);
 5947:                 toggleDataRow(form,checkbox,target,prefix,1);
 5948:             }
 5949:         }
 5950:     }
 5951:     return;
 5952: }
 5953: 
 5954: function toggleDataRow(form,checkbox,target,prefix,docount) {
 5955:     if (form.elements[checkbox].length != undefined) {
 5956:         var count = 0;
 5957:         if (docount) {
 5958:             for (var i=0; i<form.elements[checkbox].length; i++) {
 5959:                 if (form.elements[checkbox][i].checked) {
 5960:                     count ++;
 5961:                 }
 5962:             }
 5963:         }
 5964:         for (var i=0; i<form.elements[checkbox].length; i++) {
 5965:             var type = form.elements[checkbox][i].value;
 5966:             if (document.getElementById(prefix+type)) {
 5967:                 if (form.elements[checkbox][i].checked) {
 5968:                     document.getElementById(prefix+type).style.display = 'table-row';
 5969:                     if (count % 2 == 1) {
 5970:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 5971:                     } else {
 5972:                         document.getElementById(prefix+type).className = target;
 5973:                     }
 5974:                     count ++;
 5975:                 } else {
 5976:                     document.getElementById(prefix+type).style.display = 'none';
 5977:                 }
 5978:             }
 5979:         }
 5980:     }
 5981:     return;
 5982: }
 5983: 
 5984: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 5985:     var caller = radio+'_'+status;
 5986:     if (form.elements[caller].length != undefined) {
 5987:         for (var i=0; i<form.elements[caller].length; i++) {
 5988:             if (form.elements[caller][i].checked) {
 5989:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 5990:                     var curr = form.elements[caller][i].value;
 5991:                     if (prefix) {
 5992:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 5993:                     }
 5994:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 5995:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 5996:                     if (curr == 'custom') {
 5997:                         if (prefix) {
 5998:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 5999:                         }
 6000:                     } else if (curr == 'inst') {
 6001:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 6002:                     } else if (curr == 'noninst') {
 6003:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 6004:                     }
 6005:                     break;
 6006:                 }
 6007:             }
 6008:         }
 6009:     }
 6010: }
 6011: 
 6012: // ]]>
 6013: </script>
 6014: 
 6015: ENDSCRIPT
 6016: }
 6017: 
 6018: sub noninst_users {
 6019:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 6020:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 6021:     my $class = 'LC_left_item';
 6022:     if ($css_class) {
 6023:         $css_class = ' class="'.$css_class.'"';
 6024:     }
 6025:     if ($rowid) {
 6026:         $rowid = ' id="'.$rowid.'"';
 6027:     }
 6028:     if ($rowstyle) {
 6029:         $rowstyle = ' style="'.$rowstyle.'"';
 6030:     }
 6031:     my ($output,$description);
 6032:     if ($type eq 'default') {
 6033:         $description = &mt('Requests for: [_1]',$typetitle);
 6034:     } else {
 6035:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 6036:     }
 6037:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 6038:               "<td>$description</td>\n".
 6039:               '<td class="'.$class.'" colspan="2">'.
 6040:               '<table><tr>';
 6041:     my %headers = &Apache::lonlocal::texthash(
 6042:               approve  => 'Processing',
 6043:               email    => 'E-mail',
 6044:               username => 'Username',
 6045:     );
 6046:     foreach my $item ('approve','email','username') {
 6047:         $output .= '<th>'.$headers{$item}.'</th>';
 6048:     }
 6049:     $output .= '</tr><tr>';
 6050:     foreach my $item ('approve','email','username') {
 6051:         $output .= '<td valign="top">';
 6052:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 6053:         if ($item eq 'approve') {
 6054:             %choices = &Apache::lonlocal::texthash (
 6055:                                                      automatic => 'Automatically approved',
 6056:                                                      approval  => 'Queued for approval',
 6057:                                                    );
 6058:             @options = ('automatic','approval');
 6059:             $hashref = $processing;
 6060:             $defoption = 'automatic';
 6061:             $name = 'cancreate_emailprocess_'.$type;
 6062:         } elsif ($item eq 'email') {
 6063:             %choices = &Apache::lonlocal::texthash (
 6064:                                                      any     => 'Any e-mail',
 6065:                                                      inst    => 'Institutional only',
 6066:                                                      noninst => 'Non-institutional only',
 6067:                                                      custom  => 'Custom restrictions',
 6068:                                                    );
 6069:             @options = ('any','inst','noninst');
 6070:             my $showcustom;
 6071:             if (ref($emailrules) eq 'HASH') {
 6072:                 if (keys(%{$emailrules}) > 0) {
 6073:                     push(@options,'custom');
 6074:                     $showcustom = 'cancreate_emailrule';
 6075:                     if (ref($settings) eq 'HASH') {
 6076:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 6077:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 6078:                                 if (exists($emailrules->{$rule})) {
 6079:                                     $hascustom ++;
 6080:                                 }
 6081:                             }
 6082:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 6083:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 6084:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 6085:                                     if (exists($emailrules->{$rule})) {
 6086:                                         $hascustom ++;
 6087:                                     }
 6088:                                 }
 6089:                             }
 6090:                         }
 6091:                     }
 6092:                 }
 6093:             }
 6094:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 6095:                                                      "'cancreate_emaildomain','$type'".');"';
 6096:             $hashref = $emailoptions;
 6097:             $defoption = 'any';
 6098:             $name = 'cancreate_emailoptions_'.$type;
 6099:         } elsif ($item eq 'username') {
 6100:             %choices = &Apache::lonlocal::texthash (
 6101:                                                      all    => 'Same as e-mail',
 6102:                                                      first  => 'Omit @domain',
 6103:                                                      free   => 'Free to choose',
 6104:                                                    );
 6105:             @options = ('all','first','free');
 6106:             $hashref = $emailverified;
 6107:             $defoption = 'all';
 6108:             $name = 'cancreate_usernameoptions_'.$type;
 6109:         }
 6110:         foreach my $option (@options) {
 6111:             my $checked;
 6112:             if (ref($hashref) eq 'HASH') {
 6113:                 if ($type eq '') {
 6114:                     if (!exists($hashref->{'default'})) {
 6115:                         if ($option eq $defoption) {
 6116:                             $checked = ' checked="checked"';
 6117:                         }
 6118:                     } else {
 6119:                         if ($hashref->{'default'} eq $option) {
 6120:                             $checked = ' checked="checked"';
 6121:                         }
 6122:                     }
 6123:                 } else {
 6124:                     if (!exists($hashref->{$type})) {
 6125:                         if ($option eq $defoption) {
 6126:                             $checked = ' checked="checked"';
 6127:                         }
 6128:                     } else {
 6129:                         if ($hashref->{$type} eq $option) {
 6130:                             $checked = ' checked="checked"';
 6131:                         }
 6132:                     }
 6133:                 }
 6134:             } elsif (($item eq 'email') && ($hascustom)) {
 6135:                 if ($option eq 'custom') {
 6136:                     $checked = ' checked="checked"';
 6137:                 }
 6138:             } elsif ($option eq $defoption) {
 6139:                 $checked = ' checked="checked"';
 6140:             }
 6141:             $output .= '<span class="LC_nobreak"><label>'.
 6142:                        '<input type="radio" name="'.$name.'"'.
 6143:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 6144:                        $choices{$option}.'</label></span><br />';
 6145:             if ($item eq 'email') {
 6146:                 if ($option eq 'custom') {
 6147:                     my $id = 'cancreate_emailrule_'.$type;
 6148:                     my $display = 'none';
 6149:                     if ($checked) {
 6150:                         $display = 'inline';
 6151:                     }
 6152:                     my $numinrow = 2;
 6153:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 6154:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 6155:                                &user_formats_row('email',$settings,$emailrules,
 6156:                                                  $emailruleorder,$numinrow,'',$type);
 6157:                               '</table></fieldset>';
 6158:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 6159:                     my %text = &Apache::lonlocal::texthash (
 6160:                                                              inst    => 'must end:',
 6161:                                                              noninst => 'cannot end:',
 6162:                                                            );
 6163:                     my $value;
 6164:                     if (ref($emaildomain) eq 'HASH') {
 6165:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 6166:                             $value = $emaildomain->{$type}->{$option};
 6167:                         }
 6168:                     }
 6169:                     if ($value eq '') {
 6170:                         $value = '@'.$intdom;
 6171:                     }
 6172:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 6173:                     my $display = 'none';
 6174:                     if ($checked) {
 6175:                         $display = 'inline';
 6176:                     }
 6177:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 6178:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 6179:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 6180:                                '</div>';
 6181:                 }
 6182:             }
 6183:         }
 6184:         $output .= '</td>'."\n";
 6185:     }
 6186:     $output .= "</tr></table></td></tr>\n";
 6187:     return $output;
 6188: }
 6189: 
 6190: sub captcha_choice {
 6191:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 6192:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 6193:         $vertext,$currver); 
 6194:     my %lt = &captcha_phrases();
 6195:     $keyentry = 'hidden';
 6196:     my $colspan=2;
 6197:     if ($context eq 'cancreate') {
 6198:         $rowname = &mt('CAPTCHA validation');
 6199:     } elsif ($context eq 'login') {
 6200:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 6201:     } elsif ($context eq 'passwords') {
 6202:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 6203:         $colspan=1;
 6204:     }
 6205:     if (ref($settings) eq 'HASH') {
 6206:         if ($settings->{'captcha'}) {
 6207:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6208:         } else {
 6209:             $checked{'original'} = ' checked="checked"';
 6210:         }
 6211:         if ($settings->{'captcha'} eq 'recaptcha') {
 6212:             $pubtext = $lt{'pub'};
 6213:             $privtext = $lt{'priv'};
 6214:             $keyentry = 'text';
 6215:             $vertext = $lt{'ver'};
 6216:             $currver = $settings->{'recaptchaversion'};
 6217:             if ($currver ne '2') {
 6218:                 $currver = 1;
 6219:             }
 6220:         }
 6221:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6222:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6223:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6224:         }
 6225:     } else {
 6226:         $checked{'original'} = ' checked="checked"';
 6227:     }
 6228:     my $css_class;
 6229:     if ($itemcount%2) {
 6230:         $css_class = 'LC_odd_row';
 6231:     }
 6232:     if ($customcss) {
 6233:         $css_class .= " $customcss";
 6234:     }
 6235:     $css_class =~ s/^\s+//;
 6236:     if ($css_class) {
 6237:         $css_class = ' class="'.$css_class.'"';
 6238:     }
 6239:     if ($rowstyle) {
 6240:         $css_class .= ' style="'.$rowstyle.'"';
 6241:     }
 6242:     my $output = '<tr'.$css_class.'>'.
 6243:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 6244:                  '<table><tr><td>'."\n";
 6245:     foreach my $option ('original','recaptcha','notused') {
 6246:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6247:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6248:                    $lt{$option}.'</label></span>';
 6249:         unless ($option eq 'notused') {
 6250:             $output .= ('&nbsp;'x2)."\n";
 6251:         }
 6252:     }
 6253: #
 6254: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6255: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6256: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6257: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6258: #
 6259:     $output .= '</td></tr>'."\n".
 6260:                '<tr><td class="LC_zero_height">'."\n".
 6261:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6262:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6263:                $currpub.'" size="40" /></span><br />'."\n".
 6264:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6265:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6266:                $currpriv.'" size="40" /></span><br />'.
 6267:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6268:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6269:                $currver.'" size="3" /></span><br />'.
 6270:                '</td></tr></table>'."\n".
 6271:                '</td></tr>';
 6272:     return $output;
 6273: }
 6274: 
 6275: sub user_formats_row {
 6276:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 6277:     my $output;
 6278:     my %text = (
 6279:                    'username' => 'new usernames',
 6280:                    'id'       => 'IDs',
 6281:                );
 6282:     unless ($type eq 'email') {
 6283:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6284:         $output = '<tr '.$css_class.'>'.
 6285:                   '<td><span class="LC_nobreak">'.
 6286:                   &mt("Format rules to check for $text{$type}: ").
 6287:                   '</td><td class="LC_left_item" colspan="2"><table>';
 6288:     }
 6289:     my $rem;
 6290:     if (ref($ruleorder) eq 'ARRAY') {
 6291:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6292:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6293:                 my $rem = $i%($numinrow);
 6294:                 if ($rem == 0) {
 6295:                     if ($i > 0) {
 6296:                         $output .= '</tr>';
 6297:                     }
 6298:                     $output .= '<tr>';
 6299:                 }
 6300:                 my $check = ' ';
 6301:                 if (ref($settings) eq 'HASH') {
 6302:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6303:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6304:                             $check = ' checked="checked" ';
 6305:                         }
 6306:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 6307:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 6308:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 6309:                                 $check = ' checked="checked" ';
 6310:                             }
 6311:                         }
 6312:                     }
 6313:                 }
 6314:                 my $name = $type.'_rule';
 6315:                 if ($type eq 'email') {
 6316:                     $name .= '_'.$status;
 6317:                 }
 6318:                 $output .= '<td class="LC_left_item">'.
 6319:                            '<span class="LC_nobreak"><label>'.
 6320:                            '<input type="checkbox" name="'.$name.'" '.
 6321:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6322:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6323:             }
 6324:         }
 6325:         $rem = @{$ruleorder}%($numinrow);
 6326:     }
 6327:     my $colsleft;
 6328:     if ($rem) {
 6329:         $colsleft = $numinrow - $rem;
 6330:     }
 6331:     if ($colsleft > 1 ) {
 6332:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6333:                    '&nbsp;</td>';
 6334:     } elsif ($colsleft == 1) {
 6335:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6336:     }
 6337:     $output .= '</tr></table>';
 6338:     unless ($type eq 'email') {
 6339:         $output .= '</td></tr>';
 6340:     }
 6341:     return $output;
 6342: }
 6343: 
 6344: sub usercreation_types {
 6345:     my %lt = &Apache::lonlocal::texthash (
 6346:                     author     => 'When adding a co-author',
 6347:                     course     => 'When adding a user to a course',
 6348:                     requestcrs => 'When requesting a course',
 6349:                     any        => 'Any',
 6350:                     official   => 'Institutional only ',
 6351:                     unofficial => 'Non-institutional only',
 6352:                     none       => 'None',
 6353:     );
 6354:     return %lt;
 6355: }
 6356: 
 6357: sub selfcreation_types {
 6358:     my %lt = &Apache::lonlocal::texthash (
 6359:                     selfcreate => 'User creates own account',
 6360:                     any        => 'Any',
 6361:                     official   => 'Institutional only ',
 6362:                     unofficial => 'Non-institutional only',
 6363:                     email      => 'E-mail address',
 6364:                     login      => 'Institutional Login',
 6365:                     sso        => 'SSO',
 6366:              );
 6367: }
 6368: 
 6369: sub authtype_names {
 6370:     my %lt = &Apache::lonlocal::texthash(
 6371:                       int    => 'Internal',
 6372:                       krb4   => 'Kerberos 4',
 6373:                       krb5   => 'Kerberos 5',
 6374:                       loc    => 'Local',
 6375:                   );
 6376:     return %lt;
 6377: }
 6378: 
 6379: sub context_names {
 6380:     my %context_title = &Apache::lonlocal::texthash(
 6381:        author => 'Creating users when an Author',
 6382:        course => 'Creating users when in a course',
 6383:        domain => 'Creating users when a Domain Coordinator',
 6384:     );
 6385:     return %context_title;
 6386: }
 6387: 
 6388: sub print_usermodification {
 6389:     my ($position,$dom,$settings,$rowtotal) = @_;
 6390:     my $numinrow = 4;
 6391:     my ($context,$datatable,$rowcount);
 6392:     if ($position eq 'top') {
 6393:         $rowcount = 0;
 6394:         $context = 'author'; 
 6395:         foreach my $role ('ca','aa') {
 6396:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6397:                                                    $numinrow,$rowcount);
 6398:             $$rowtotal ++;
 6399:             $rowcount ++;
 6400:         }
 6401:     } elsif ($position eq 'bottom') {
 6402:         $context = 'course';
 6403:         $rowcount = 0;
 6404:         foreach my $role ('st','ep','ta','in','cr') {
 6405:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6406:                                                    $numinrow,$rowcount);
 6407:             $$rowtotal ++;
 6408:             $rowcount ++;
 6409:         }
 6410:     }
 6411:     return $datatable;
 6412: }
 6413: 
 6414: sub print_defaults {
 6415:     my ($position,$dom,$settings,$rowtotal) = @_;
 6416:     my $rownum = 0;
 6417:     my ($datatable,$css_class,$titles);
 6418:     unless ($position eq 'bottom') {
 6419:         $titles = &defaults_titles($dom);
 6420:     }
 6421:     if ($position eq 'top') {
 6422:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6423:                      'datelocale_def','portal_def');
 6424:         my %defaults;
 6425:         if (ref($settings) eq 'HASH') {
 6426:             %defaults = %{$settings};
 6427:         } else {
 6428:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6429:             foreach my $item (@items) {
 6430:                 $defaults{$item} = $domdefaults{$item};
 6431:             }
 6432:         }
 6433:         foreach my $item (@items) {
 6434:             if ($rownum%2) {
 6435:                 $css_class = '';
 6436:             } else {
 6437:                 $css_class = ' class="LC_odd_row" ';
 6438:             }
 6439:             $datatable .= '<tr'.$css_class.'>'.
 6440:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6441:                           '</span></td><td class="LC_right_item" colspan="3">';
 6442:             if ($item eq 'auth_def') {
 6443:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6444:                 my %shortauth = (
 6445:                                  internal => 'int',
 6446:                                  krb4 => 'krb4',
 6447:                                  krb5 => 'krb5',
 6448:                                  localauth  => 'loc'
 6449:                                 );
 6450:                 my %authnames = &authtype_names();
 6451:                 foreach my $auth (@authtypes) {
 6452:                     my $checked = ' ';
 6453:                     if ($defaults{$item} eq $auth) {
 6454:                         $checked = ' checked="checked" ';
 6455:                     }
 6456:                     $datatable .= '<label><input type="radio" name="'.$item.
 6457:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6458:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6459:                 }
 6460:             } elsif ($item eq 'timezone_def') {
 6461:                 my $includeempty = 1;
 6462:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6463:             } elsif ($item eq 'datelocale_def') {
 6464:                 my $includeempty = 1;
 6465:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6466:             } elsif ($item eq 'lang_def') {
 6467:                 my $includeempty = 1;
 6468:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6469:             } else {
 6470:                 my $size;
 6471:                 if ($item eq 'portal_def') {
 6472:                     $size = ' size="25"';
 6473:                 }
 6474:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6475:                               $defaults{$item}.'"'.$size.' />';
 6476:             }
 6477:             $datatable .= '</td></tr>';
 6478:             $rownum ++;
 6479:         }
 6480:     } else {
 6481:         my %defaults;
 6482:         if (ref($settings) eq 'HASH') {
 6483:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6484:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6485:                 for (my $i=0; $i<$maxnum; $i++) {
 6486:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6487:                     my $item = $settings->{'inststatusorder'}->[$i];
 6488:                     my $title = $settings->{'inststatustypes'}->{$item};
 6489:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6490:                     $datatable .= '<tr'.$css_class.'>'.
 6491:                                   '<td><span class="LC_nobreak">'.
 6492:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6493:                     for (my $k=0; $k<=$maxnum; $k++) {
 6494:                         my $vpos = $k+1;
 6495:                         my $selstr;
 6496:                         if ($k == $i) {
 6497:                             $selstr = ' selected="selected" ';
 6498:                         }
 6499:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6500:                     }
 6501:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6502:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6503:                                   &mt('delete').'</span></td>'.
 6504:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6505:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6506:                                   '</span></td></tr>';
 6507:                 }
 6508:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6509:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6510:                 $datatable .= '<tr '.$css_class.'>'.
 6511:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6512:                 for (my $k=0; $k<=$maxnum; $k++) {
 6513:                     my $vpos = $k+1;
 6514:                     my $selstr;
 6515:                     if ($k == $maxnum) {
 6516:                         $selstr = ' selected="selected" ';
 6517:                     }
 6518:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6519:                 }
 6520:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6521:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6522:                               '&nbsp;'.&mt('(new)').
 6523:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6524:                               &mt('Name displayed:').
 6525:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6526:                               '</tr>'."\n";
 6527:                 $rownum ++;
 6528:             }
 6529:         }
 6530:     }
 6531:     $$rowtotal += $rownum;
 6532:     return $datatable;
 6533: }
 6534: 
 6535: sub get_languages_hash {
 6536:     my %langchoices;
 6537:     foreach my $id (&Apache::loncommon::languageids()) {
 6538:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6539:         if ($code ne '') {
 6540:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6541:         }
 6542:     }
 6543:     return %langchoices;
 6544: }
 6545: 
 6546: sub defaults_titles {
 6547:     my ($dom) = @_;
 6548:     my %titles = &Apache::lonlocal::texthash (
 6549:                    'auth_def'      => 'Default authentication type',
 6550:                    'auth_arg_def'  => 'Default authentication argument',
 6551:                    'lang_def'      => 'Default language',
 6552:                    'timezone_def'  => 'Default timezone',
 6553:                    'datelocale_def' => 'Default locale for dates',
 6554:                    'portal_def'     => 'Portal/Default URL',
 6555:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6556:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6557:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6558:                  );
 6559:     if ($dom) {
 6560:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6561:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6562:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6563:         $protocol = 'http' if ($protocol ne 'https');
 6564:         if ($uint_dom) {
 6565:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6566:                                          $uint_dom);
 6567:         }
 6568:     }
 6569:     return (\%titles);
 6570: }
 6571: 
 6572: sub print_scantron {
 6573:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 6574:     if ($position eq 'top') {
 6575:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 6576:     } else {
 6577:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 6578:     }
 6579: }
 6580: 
 6581: sub scantron_javascript {
 6582:     return <<"ENDSCRIPT";
 6583: 
 6584: <script type="text/javascript">
 6585: // <![CDATA[
 6586: 
 6587: function toggleScantron(form) {
 6588:     var csvfieldset = new Array();
 6589:     if (document.getElementById('scantroncsv_cols')) {
 6590:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 6591:     }
 6592:     if (document.getElementById('scantroncsv_options')) {
 6593:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 6594:     }
 6595:     if (csvfieldset.length) {
 6596:         if (document.getElementById('scantronconfcsv')) {
 6597:             var scantroncsv = document.getElementById('scantronconfcsv');
 6598:             if (scantroncsv.checked) {
 6599:                 for (var i=0; i<csvfieldset.length; i++) {
 6600:                     csvfieldset[i].style.display = 'block';
 6601:                 }
 6602:             } else {
 6603:                 for (var i=0; i<csvfieldset.length; i++) {
 6604:                     csvfieldset[i].style.display = 'none';
 6605:                 }
 6606:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 6607:                 if (csvselects.length) {
 6608:                     for (var j=0; j<csvselects.length; j++) {
 6609:                         csvselects[j].selectedIndex = 0;
 6610:                     }
 6611:                 }
 6612:             }
 6613:         }
 6614:     }
 6615:     return;
 6616: }
 6617: // ]]>
 6618: </script>
 6619: 
 6620: ENDSCRIPT
 6621: 
 6622: }
 6623: 
 6624: sub print_scantronformat {
 6625:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6626:     my $itemcount = 1;
 6627:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6628:         %confhash);
 6629:     my $switchserver = &check_switchserver($dom,$confname);
 6630:     my %lt = &Apache::lonlocal::texthash (
 6631:                 default => 'Default bubblesheet format file error',
 6632:                 custom  => 'Custom bubblesheet format file error',
 6633:              );
 6634:     my %scantronfiles = (
 6635:         default => 'default.tab',
 6636:         custom => 'custom.tab',
 6637:     );
 6638:     foreach my $key (keys(%scantronfiles)) {
 6639:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6640:                               .$scantronfiles{$key};
 6641:     }
 6642:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6643:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6644:         if (!$switchserver) {
 6645:             my $servadm = $r->dir_config('lonAdmEMail');
 6646:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6647:             if ($configuserok eq 'ok') {
 6648:                 if ($author_ok eq 'ok') {
 6649:                     my %legacyfile = (
 6650:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 6651:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 6652:                     );
 6653:                     my %md5chk;
 6654:                     foreach my $type (keys(%legacyfile)) {
 6655:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6656:                         chomp($md5chk{$type});
 6657:                     }
 6658:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6659:                         foreach my $type (keys(%legacyfile)) {
 6660:                             ($scantronurls{$type},my $error) =
 6661:                                 &legacy_scantronformat($r,$dom,$confname,
 6662:                                                  $type,$legacyfile{$type},
 6663:                                                  $scantronurls{$type},
 6664:                                                  $scantronfiles{$type});
 6665:                             if ($error ne '') {
 6666:                                 $error{$type} = $error;
 6667:                             }
 6668:                         }
 6669:                         if (keys(%error) == 0) {
 6670:                             $is_custom = 1;
 6671:                             $confhash{'scantron'}{'scantronformat'} =
 6672:                                 $scantronurls{'custom'};
 6673:                             my $putresult =
 6674:                                 &Apache::lonnet::put_dom('configuration',
 6675:                                                          \%confhash,$dom);
 6676:                             if ($putresult ne 'ok') {
 6677:                                 $error{'custom'} =
 6678:                                     '<span class="LC_error">'.
 6679:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6680:                             }
 6681:                         }
 6682:                     } else {
 6683:                         ($scantronurls{'default'},my $error) =
 6684:                             &legacy_scantronformat($r,$dom,$confname,
 6685:                                           'default',$legacyfile{'default'},
 6686:                                           $scantronurls{'default'},
 6687:                                           $scantronfiles{'default'});
 6688:                         if ($error eq '') {
 6689:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6690:                             my $putresult =
 6691:                                 &Apache::lonnet::put_dom('configuration',
 6692:                                                          \%confhash,$dom);
 6693:                             if ($putresult ne 'ok') {
 6694:                                 $error{'default'} =
 6695:                                     '<span class="LC_error">'.
 6696:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6697:                             }
 6698:                         } else {
 6699:                             $error{'default'} = $error;
 6700:                         }
 6701:                     }
 6702:                 }
 6703:             }
 6704:         } else {
 6705:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6706:         }
 6707:     }
 6708:     if (ref($settings) eq 'HASH') {
 6709:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6710:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6711:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6712:                 $scantronurl = '';
 6713:             } else {
 6714:                 $scantronurl = $settings->{'scantronformat'};
 6715:             }
 6716:             $is_custom = 1;
 6717:         } else {
 6718:             $scantronurl = $scantronurls{'default'};
 6719:         }
 6720:     } else {
 6721:         if ($is_custom) {
 6722:             $scantronurl = $scantronurls{'custom'};
 6723:         } else {
 6724:             $scantronurl = $scantronurls{'default'};
 6725:         }
 6726:     }
 6727:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6728:     $datatable .= '<tr'.$css_class.'>';
 6729:     if (!$is_custom) {
 6730:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6731:                       '<span class="LC_nobreak">';
 6732:         if ($scantronurl) {
 6733:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6734:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6735:         } else {
 6736:             $datatable = &mt('File unavailable for display');
 6737:         }
 6738:         $datatable .= '</span></td>';
 6739:         if (keys(%error) == 0) { 
 6740:             $datatable .= '<td valign="bottom">';
 6741:             if (!$switchserver) {
 6742:                 $datatable .= &mt('Upload:').'<br />';
 6743:             }
 6744:         } else {
 6745:             my $errorstr;
 6746:             foreach my $key (sort(keys(%error))) {
 6747:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6748:             }
 6749:             $datatable .= '<td>'.$errorstr;
 6750:         }
 6751:     } else {
 6752:         if (keys(%error) > 0) {
 6753:             my $errorstr;
 6754:             foreach my $key (sort(keys(%error))) {
 6755:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6756:             } 
 6757:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6758:         } elsif ($scantronurl) {
 6759:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6760:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6761:             $datatable .= '<td><span class="LC_nobreak">'.
 6762:                           $link.
 6763:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6764:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6765:                           '<td><span class="LC_nobreak">&nbsp;'.
 6766:                           &mt('Replace:').'</span><br />';
 6767:         }
 6768:     }
 6769:     if (keys(%error) == 0) {
 6770:         if ($switchserver) {
 6771:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6772:         } else {
 6773:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6774:                          '<input type="file" name="scantronformat" /></span>';
 6775:         }
 6776:     }
 6777:     $datatable .= '</td></tr>';
 6778:     $$rowtotal ++;
 6779:     return $datatable;
 6780: }
 6781: 
 6782: sub legacy_scantronformat {
 6783:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6784:     my ($url,$error);
 6785:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6786:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6787:         (my $result,$url) =
 6788:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6789:                          '','',$newfile);
 6790:         if ($result ne 'ok') {
 6791:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6792:         }
 6793:     }
 6794:     return ($url,$error);
 6795: }
 6796: 
 6797: sub print_scantronconfig {
 6798:     my ($dom,$settings,$rowtotal) = @_;
 6799:     my $itemcount = 2;
 6800:     my $is_checked = ' checked="checked"';
 6801:     my %optionson = (
 6802:                      hdr => ' checked="checked"',
 6803:                      pad => ' checked="checked"',
 6804:                      rem => ' checked="checked"',
 6805:                     );
 6806:     my %optionsoff = (
 6807:                       hdr => '',
 6808:                       pad => '',
 6809:                       rem => '',
 6810:                      );
 6811:     my $currcsvsty = 'none';
 6812:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 6813:     my @fields = &scantroncsv_fields();
 6814:     my %titles = &scantronconfig_titles();
 6815:     if (ref($settings) eq 'HASH') {
 6816:         if (ref($settings->{config}) eq 'HASH') {
 6817:             if ($settings->{config}->{dat}) {
 6818:                 $checked{'dat'} = $is_checked;
 6819:             }
 6820:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 6821:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 6822:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 6823:                     if (keys(%csvfields) > 0) {
 6824:                         $checked{'csv'} = $is_checked;
 6825:                         $currcsvsty = 'block';
 6826:                     }
 6827:                 }
 6828:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 6829:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 6830:                     foreach my $option (keys(%optionson)) {
 6831:                         unless ($csvoptions{$option}) {
 6832:                             $optionsoff{$option} = $optionson{$option};
 6833:                             $optionson{$option} = '';
 6834:                         }
 6835:                     }
 6836:                 }
 6837:             }
 6838:         } else {
 6839:             $checked{'dat'} = $is_checked;
 6840:         }
 6841:     } else {
 6842:         $checked{'dat'} = $is_checked;
 6843:     }
 6844:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 6845:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6846:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 6847:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 6848:     foreach my $item ('dat','csv') {
 6849:         my $id;
 6850:         if ($item eq 'csv') {
 6851:             $id = 'id="scantronconfcsv" ';
 6852:         }
 6853:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 6854:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 6855:         if ($item eq 'csv') {
 6856:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 6857:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 6858:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 6859:             foreach my $col (@fields) {
 6860:                 my $selnone;
 6861:                 if ($csvfields{$col} eq '') {
 6862:                     $selnone = ' selected="selected"';
 6863:                 }
 6864:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 6865:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 6866:                               '<option value=""'.$selnone.'></option>';
 6867:                 for (my $i=0; $i<20; $i++) {
 6868:                     my $shown = $i+1;
 6869:                     my $sel;
 6870:                     unless ($selnone) {
 6871:                         if (exists($csvfields{$col})) {
 6872:                             if ($csvfields{$col} == $i) {
 6873:                                 $sel = ' selected="selected"';
 6874:                             }
 6875:                         }
 6876:                     }
 6877:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 6878:                 }
 6879:                 $datatable .= '</select></td></tr>';
 6880:            }
 6881:            $datatable .= '</table></fieldset>'.
 6882:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 6883:                          '<legend>'.&mt('CSV Options').'</legend>';
 6884:            foreach my $option ('hdr','pad','rem') {
 6885:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 6886:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 6887:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 6888:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 6889:            }
 6890:            $datatable .= '</fieldset>';
 6891:            $itemcount ++;
 6892:         }
 6893:     }
 6894:     $datatable .= '</td></tr>';
 6895:     $$rowtotal ++;
 6896:     return $datatable;
 6897: }
 6898: 
 6899: sub scantronconfig_titles {
 6900:     return &Apache::lonlocal::texthash(
 6901:                                           dat => 'Standard format (.dat)',
 6902:                                           csv => 'Comma separated values (.csv)',
 6903:                                           hdr => 'Remove first line in file (contains column titles)',
 6904:                                           pad => 'Prepend 0s to PaperID',
 6905:                                           rem => 'Remove leading spaces (except Question Response columns)',
 6906:                                           CODE => 'CODE',
 6907:                                           ID   => 'Student ID',
 6908:                                           PaperID => 'Paper ID',
 6909:                                           FirstName => 'First Name',
 6910:                                           LastName => 'Last Name',
 6911:                                           FirstQuestion => 'First Question Response',
 6912:                                           Section => 'Section',
 6913:     );
 6914: }
 6915: 
 6916: sub scantroncsv_fields {
 6917:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 6918: }
 6919: 
 6920: sub print_coursecategories {
 6921:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6922:     my $datatable;
 6923:     if ($position eq 'top') {
 6924:         my (%checked);
 6925:         my @catitems = ('unauth','auth');
 6926:         my @cattypes = ('std','domonly','codesrch','none');
 6927:         $checked{'unauth'} = 'std';
 6928:         $checked{'auth'} = 'std';
 6929:         if (ref($settings) eq 'HASH') {
 6930:             foreach my $type (@cattypes) {
 6931:                 if ($type eq $settings->{'unauth'}) {
 6932:                     $checked{'unauth'} = $type;
 6933:                 }
 6934:                 if ($type eq $settings->{'auth'}) {
 6935:                     $checked{'auth'} = $type;
 6936:                 }
 6937:             }
 6938:         }
 6939:         my %lt = &Apache::lonlocal::texthash (
 6940:                                                unauth   => 'Catalog type for unauthenticated users',
 6941:                                                auth     => 'Catalog type for authenticated users',
 6942:                                                none     => 'No catalog',
 6943:                                                std      => 'Standard catalog',
 6944:                                                domonly  => 'Domain-only catalog',
 6945:                                                codesrch => "Code search form",
 6946:                                              );
 6947:        my $itemcount = 0;
 6948:        foreach my $item (@catitems) {
 6949:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6950:            $datatable .= '<tr '.$css_class.'>'.
 6951:                          '<td>'.$lt{$item}.'</td>'.
 6952:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6953:            foreach my $type (@cattypes) {
 6954:                my $ischecked;
 6955:                if ($checked{$item} eq $type) {
 6956:                    $ischecked=' checked="checked"';
 6957:                }
 6958:                $datatable .= '<label>'.
 6959:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6960:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6961:            }
 6962:            $datatable .= '</span></td></tr>';
 6963:            $itemcount ++;
 6964:         }
 6965:         $$rowtotal += $itemcount;
 6966:     } elsif ($position eq 'middle') {
 6967:         my $toggle_cats_crs = ' ';
 6968:         my $toggle_cats_dom = ' checked="checked" ';
 6969:         my $can_cat_crs = ' ';
 6970:         my $can_cat_dom = ' checked="checked" ';
 6971:         my $toggle_catscomm_comm = ' ';
 6972:         my $toggle_catscomm_dom = ' checked="checked" ';
 6973:         my $can_catcomm_comm = ' ';
 6974:         my $can_catcomm_dom = ' checked="checked" ';
 6975: 
 6976:         if (ref($settings) eq 'HASH') {
 6977:             if ($settings->{'togglecats'} eq 'crs') {
 6978:                 $toggle_cats_crs = $toggle_cats_dom;
 6979:                 $toggle_cats_dom = ' ';
 6980:             }
 6981:             if ($settings->{'categorize'} eq 'crs') {
 6982:                 $can_cat_crs = $can_cat_dom;
 6983:                 $can_cat_dom = ' ';
 6984:             }
 6985:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6986:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6987:                 $toggle_catscomm_dom = ' ';
 6988:             }
 6989:             if ($settings->{'categorizecomm'} eq 'comm') {
 6990:                 $can_catcomm_comm = $can_catcomm_dom;
 6991:                 $can_catcomm_dom = ' ';
 6992:             }
 6993:         }
 6994:         my %title = &Apache::lonlocal::texthash (
 6995:                      togglecats     => 'Show/Hide a course in catalog',
 6996:                      togglecatscomm => 'Show/Hide a community in catalog',
 6997:                      categorize     => 'Assign a category to a course',
 6998:                      categorizecomm => 'Assign a category to a community',
 6999:                     );
 7000:         my %level = &Apache::lonlocal::texthash (
 7001:                      dom  => 'Set in Domain',
 7002:                      crs  => 'Set in Course',
 7003:                      comm => 'Set in Community',
 7004:                     );
 7005:         $datatable = '<tr class="LC_odd_row">'.
 7006:                   '<td>'.$title{'togglecats'}.'</td>'.
 7007:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7008:                   '<input type="radio" name="togglecats"'.
 7009:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7010:                   '<label><input type="radio" name="togglecats"'.
 7011:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7012:                   '</tr><tr>'.
 7013:                   '<td>'.$title{'categorize'}.'</td>'.
 7014:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7015:                   '<label><input type="radio" name="categorize"'.
 7016:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7017:                   '<label><input type="radio" name="categorize"'.
 7018:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7019:                   '</tr><tr class="LC_odd_row">'.
 7020:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 7021:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7022:                   '<input type="radio" name="togglecatscomm"'.
 7023:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7024:                   '<label><input type="radio" name="togglecatscomm"'.
 7025:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7026:                   '</tr><tr>'.
 7027:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 7028:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7029:                   '<label><input type="radio" name="categorizecomm"'.
 7030:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7031:                   '<label><input type="radio" name="categorizecomm"'.
 7032:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7033:                   '</tr>';
 7034:         $$rowtotal += 4;
 7035:     } else {
 7036:         my $css_class;
 7037:         my $itemcount = 1;
 7038:         my $cathash; 
 7039:         if (ref($settings) eq 'HASH') {
 7040:             $cathash = $settings->{'cats'};
 7041:         }
 7042:         if (ref($cathash) eq 'HASH') {
 7043:             my (@cats,@trails,%allitems,%idx,@jsarray);
 7044:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 7045:                                                    \%allitems,\%idx,\@jsarray);
 7046:             my $maxdepth = scalar(@cats);
 7047:             my $colattrib = '';
 7048:             if ($maxdepth > 2) {
 7049:                 $colattrib = ' colspan="2" ';
 7050:             }
 7051:             my @path;
 7052:             if (@cats > 0) {
 7053:                 if (ref($cats[0]) eq 'ARRAY') {
 7054:                     my $numtop = @{$cats[0]};
 7055:                     my $maxnum = $numtop;
 7056:                     my %default_names = (
 7057:                           instcode    => &mt('Official courses'),
 7058:                           communities => &mt('Communities'),
 7059:                     );
 7060: 
 7061:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 7062:                         ($cathash->{'instcode::0'} eq '') ||
 7063:                         (!grep(/^communities$/,@{$cats[0]})) || 
 7064:                         ($cathash->{'communities::0'} eq '')) {
 7065:                         $maxnum ++;
 7066:                     }
 7067:                     my $lastidx;
 7068:                     for (my $i=0; $i<$numtop; $i++) {
 7069:                         my $parent = $cats[0][$i];
 7070:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7071:                         my $item = &escape($parent).'::0';
 7072:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 7073:                         $lastidx = $idx{$item};
 7074:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7075:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 7076:                         for (my $k=0; $k<=$maxnum; $k++) {
 7077:                             my $vpos = $k+1;
 7078:                             my $selstr;
 7079:                             if ($k == $i) {
 7080:                                 $selstr = ' selected="selected" ';
 7081:                             }
 7082:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7083:                         }
 7084:                         $datatable .= '</select></span></td><td>';
 7085:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 7086:                             $datatable .=  '<span class="LC_nobreak">'
 7087:                                            .$default_names{$parent}.'</span>';
 7088:                             if ($parent eq 'instcode') {
 7089:                                 $datatable .= '<br /><span class="LC_nobreak">('
 7090:                                               .&mt('with institutional codes')
 7091:                                               .')</span></td><td'.$colattrib.'>';
 7092:                             } else {
 7093:                                 $datatable .= '<table><tr><td>';
 7094:                             }
 7095:                             $datatable .= '<span class="LC_nobreak">'
 7096:                                           .'<label><input type="radio" name="'
 7097:                                           .$parent.'" value="1" checked="checked" />'
 7098:                                           .&mt('Display').'</label>';
 7099:                             if ($parent eq 'instcode') {
 7100:                                 $datatable .= '&nbsp;';
 7101:                             } else {
 7102:                                 $datatable .= '</span></td></tr><tr><td>'
 7103:                                               .'<span class="LC_nobreak">';
 7104:                             }
 7105:                             $datatable .= '<label><input type="radio" name="'
 7106:                                           .$parent.'" value="0" />'
 7107:                                           .&mt('Do not display').'</label></span>';
 7108:                             if ($parent eq 'communities') {
 7109:                                 $datatable .= '</td></tr></table>';
 7110:                             }
 7111:                             $datatable .= '</td>';
 7112:                         } else {
 7113:                             $datatable .= $parent
 7114:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 7115:                                           .'<input type="checkbox" name="deletecategory" '
 7116:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 7117:                         }
 7118:                         my $depth = 1;
 7119:                         push(@path,$parent);
 7120:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 7121:                         pop(@path);
 7122:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 7123:                         $itemcount ++;
 7124:                     }
 7125:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7126:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 7127:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 7128:                     for (my $k=0; $k<=$maxnum; $k++) {
 7129:                         my $vpos = $k+1;
 7130:                         my $selstr;
 7131:                         if ($k == $numtop) {
 7132:                             $selstr = ' selected="selected" ';
 7133:                         }
 7134:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7135:                     }
 7136:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 7137:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 7138:                                   .'</tr>'."\n";
 7139:                     $itemcount ++;
 7140:                     foreach my $default ('instcode','communities') {
 7141:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 7142:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7143:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 7144:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 7145:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 7146:                             for (my $k=0; $k<=$maxnum; $k++) {
 7147:                                 my $vpos = $k+1;
 7148:                                 my $selstr;
 7149:                                 if ($k == $maxnum) {
 7150:                                     $selstr = ' selected="selected" ';
 7151:                                 }
 7152:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7153:                             }
 7154:                             $datatable .= '</select></span></td>'.
 7155:                                           '<td><span class="LC_nobreak">'.
 7156:                                           $default_names{$default}.'</span>';
 7157:                             if ($default eq 'instcode') {
 7158:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 7159:                                               .&mt('with institutional codes').')</span>';
 7160:                             }
 7161:                             $datatable .= '</td>'
 7162:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 7163:                                           .&mt('Display').'</label>&nbsp;'
 7164:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 7165:                                           .&mt('Do not display').'</label></span></td></tr>';
 7166:                         }
 7167:                     }
 7168:                 }
 7169:             } else {
 7170:                 $datatable .= &initialize_categories($itemcount);
 7171:             }
 7172:         } else {
 7173:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 7174:                           .&initialize_categories($itemcount);
 7175:         }
 7176:         $$rowtotal += $itemcount;
 7177:     }
 7178:     return $datatable;
 7179: }
 7180: 
 7181: sub print_serverstatuses {
 7182:     my ($dom,$settings,$rowtotal) = @_;
 7183:     my $datatable;
 7184:     my @pages = &serverstatus_pages();
 7185:     my (%namedaccess,%machineaccess);
 7186:     foreach my $type (@pages) {
 7187:         $namedaccess{$type} = '';
 7188:         $machineaccess{$type}= '';
 7189:     }
 7190:     if (ref($settings) eq 'HASH') {
 7191:         foreach my $type (@pages) {
 7192:             if (exists($settings->{$type})) {
 7193:                 if (ref($settings->{$type}) eq 'HASH') {
 7194:                     foreach my $key (keys(%{$settings->{$type}})) {
 7195:                         if ($key eq 'namedusers') {
 7196:                             $namedaccess{$type} = $settings->{$type}->{$key};
 7197:                         } elsif ($key eq 'machines') {
 7198:                             $machineaccess{$type} = $settings->{$type}->{$key};
 7199:                         }
 7200:                     }
 7201:                 }
 7202:             }
 7203:         }
 7204:     }
 7205:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7206:     my $rownum = 0;
 7207:     my $css_class;
 7208:     foreach my $type (@pages) {
 7209:         $rownum ++;
 7210:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 7211:         $datatable .= '<tr'.$css_class.'>'.
 7212:                       '<td><span class="LC_nobreak">'.
 7213:                       $titles->{$type}.'</span></td>'.
 7214:                       '<td class="LC_left_item">'.
 7215:                       '<input type="text" name="'.$type.'_namedusers" '.
 7216:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 7217:                       '<td class="LC_right_item">'.
 7218:                       '<span class="LC_nobreak">'.
 7219:                       '<input type="text" name="'.$type.'_machines" '.
 7220:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 7221:                       '</span></td></tr>'."\n";
 7222:     }
 7223:     $$rowtotal += $rownum;
 7224:     return $datatable;
 7225: }
 7226: 
 7227: sub serverstatus_pages {
 7228:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 7229:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 7230:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 7231:             'uniquecodes','diskusage','coursecatalog');
 7232: }
 7233: 
 7234: sub defaults_javascript {
 7235:     my ($settings) = @_;
 7236:     return unless (ref($settings) eq 'HASH');
 7237:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7238:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 7239:         if ($maxnum eq '') {
 7240:             $maxnum = 0;
 7241:         }
 7242:         $maxnum ++;
 7243:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 7244:         return <<"ENDSCRIPT";
 7245: <script type="text/javascript">
 7246: // <![CDATA[
 7247: function reorderTypes(form,caller) {
 7248:     var changedVal;
 7249: $jstext 
 7250:     var newpos = 'addinststatus_pos';
 7251:     var current = new Array;
 7252:     var maxh = $maxnum;
 7253:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7254:     var oldVal;
 7255:     if (caller == newpos) {
 7256:         changedVal = newitemVal;
 7257:     } else {
 7258:         var curritem = 'inststatus_pos_'+caller;
 7259:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7260:         current[newitemVal] = newpos;
 7261:     }
 7262:     for (var i=0; i<inststatuses.length; i++) {
 7263:         if (inststatuses[i] != caller) {
 7264:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7265:             if (form.elements[elementName]) {
 7266:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7267:                 current[currVal] = elementName;
 7268:             }
 7269:         }
 7270:     }
 7271:     for (var j=0; j<maxh; j++) {
 7272:         if (current[j] == undefined) {
 7273:             oldVal = j;
 7274:         }
 7275:     }
 7276:     if (oldVal < changedVal) {
 7277:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7278:            var elementName = current[k];
 7279:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7280:         }
 7281:     } else {
 7282:         for (var k=changedVal; k<oldVal; k++) {
 7283:             var elementName = current[k];
 7284:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7285:         }
 7286:     }
 7287:     return;
 7288: }
 7289: 
 7290: // ]]>
 7291: </script>
 7292: 
 7293: ENDSCRIPT
 7294:     }
 7295: }
 7296: 
 7297: sub passwords_javascript {
 7298:     my %intalert = &Apache::lonlocal::texthash (
 7299:         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.',
 7300:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
 7301:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
 7302:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
 7303:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
 7304:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
 7305:     );
 7306:     &js_escape(\%intalert);
 7307:     my $defmin = $Apache::lonnet::passwdmin;
 7308:     my $intauthjs = <<"ENDSCRIPT";
 7309: 
 7310: function warnIntAuth(field) {
 7311:     if (field.name == 'intauth_check') {
 7312:         if (field.value == '2') {
 7313:             alert('$intalert{authcheck}');
 7314:         }
 7315:     }
 7316:     if (field.name == 'intauth_cost') {
 7317:         field.value.replace(/\s/g,'');
 7318:         if (field.value != '') {
 7319:             var regexdigit=/^\\d+\$/;
 7320:             if (!regexdigit.test(field.value)) {
 7321:                 alert('$intalert{authcost}');
 7322:             }
 7323:         }
 7324:     }
 7325:     return;
 7326: }
 7327: 
 7328: function warnIntPass(field) {
 7329:     field.value.replace(/^\s+/,'');
 7330:     field.value.replace(/\s+\$/,'');
 7331:     var regexdigit=/^\\d+\$/;
 7332:     if (field.name == 'passwords_min') {
 7333:         if (field.value == '') {
 7334:             alert('$intalert{passmin}');
 7335:             field.value = '$defmin';
 7336:         } else {
 7337:             if (!regexdigit.test(field.value)) {
 7338:                 alert('$intalert{passmin}');
 7339:                 field.value = '$defmin';
 7340:             }
 7341:             var minval = parseInt(field.value,10);
 7342:             if (minval < $defmin) {
 7343:                 alert('$intalert{passmin}');
 7344:                 field.value = '$defmin';
 7345:             }
 7346:         }
 7347:     } else {
 7348:         if (field.value == '0') {
 7349:             field.value = '';
 7350:         }
 7351:         if (field.value != '') {
 7352:             if (field.name == 'passwords_expire') {
 7353:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/;
 7354:                 if (!regexpposnum.test(field.value)) {
 7355:                     alert('$intalert{passexp}');
 7356:                     field.value = '';
 7357:                 } else {
 7358:                     var expval = parseFloat(field.value);
 7359:                     if (expval == 0) {
 7360:                         alert('$intalert{passexp}');
 7361:                         field.value = '';
 7362:                     }
 7363:                 }
 7364:             } else {
 7365:                 if (!regexdigit.test(field.value)) {
 7366:                     if (field.name == 'passwords_max') {
 7367:                         alert('$intalert{passmax}');
 7368:                     } else {
 7369:                         if (field.name == 'passwords_numsaved') {
 7370:                             alert('$intalert{passnum}');
 7371:                         }
 7372:                     }
 7373:                 }
 7374:                 field.value = '';
 7375:             }
 7376:         }
 7377:     }
 7378:     return;
 7379: }
 7380: 
 7381: ENDSCRIPT
 7382:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7383: }
 7384: 
 7385: sub coursecategories_javascript {
 7386:     my ($settings) = @_;
 7387:     my ($output,$jstext,$cathash);
 7388:     if (ref($settings) eq 'HASH') {
 7389:         $cathash = $settings->{'cats'};
 7390:     }
 7391:     if (ref($cathash) eq 'HASH') {
 7392:         my (@cats,@jsarray,%idx);
 7393:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7394:         if (@jsarray > 0) {
 7395:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7396:             for (my $i=0; $i<@jsarray; $i++) {
 7397:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7398:                     my $catstr = join('","',@{$jsarray[$i]});
 7399:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7400:                 }
 7401:             }
 7402:         }
 7403:     } else {
 7404:         $jstext  = '    var categories = Array(1);'."\n".
 7405:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7406:     }
 7407:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7408:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7409:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7410:     &js_escape(\$instcode_reserved);
 7411:     &js_escape(\$communities_reserved);
 7412:     &js_escape(\$choose_again);
 7413:     $output = <<"ENDSCRIPT";
 7414: <script type="text/javascript">
 7415: // <![CDATA[
 7416: function reorderCats(form,parent,item,idx) {
 7417:     var changedVal;
 7418: $jstext
 7419:     var newpos = 'addcategory_pos';
 7420:     if (parent == '') {
 7421:         var has_instcode = 0;
 7422:         var maxtop = categories[idx].length;
 7423:         for (var j=0; j<maxtop; j++) {
 7424:             if (categories[idx][j] == 'instcode::0') {
 7425:                 has_instcode == 1;
 7426:             }
 7427:         }
 7428:         if (has_instcode == 0) {
 7429:             categories[idx][maxtop] = 'instcode_pos';
 7430:         }
 7431:     } else {
 7432:         newpos += '_'+parent;
 7433:     }
 7434:     var maxh = 1 + categories[idx].length;
 7435:     var current = new Array;
 7436:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7437:     if (item == newpos) {
 7438:         changedVal = newitemVal;
 7439:     } else {
 7440:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7441:         current[newitemVal] = newpos;
 7442:     }
 7443:     for (var i=0; i<categories[idx].length; i++) {
 7444:         var elementName = categories[idx][i];
 7445:         if (elementName != item) {
 7446:             if (form.elements[elementName]) {
 7447:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7448:                 current[currVal] = elementName;
 7449:             }
 7450:         }
 7451:     }
 7452:     var oldVal;
 7453:     for (var j=0; j<maxh; j++) {
 7454:         if (current[j] == undefined) {
 7455:             oldVal = j;
 7456:         }
 7457:     }
 7458:     if (oldVal < changedVal) {
 7459:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7460:            var elementName = current[k];
 7461:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7462:         }
 7463:     } else {
 7464:         for (var k=changedVal; k<oldVal; k++) {
 7465:             var elementName = current[k];
 7466:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7467:         }
 7468:     }
 7469:     return;
 7470: }
 7471: 
 7472: function categoryCheck(form) {
 7473:     if (form.elements['addcategory_name'].value == 'instcode') {
 7474:         alert('$instcode_reserved\\n$choose_again');
 7475:         return false;
 7476:     }
 7477:     if (form.elements['addcategory_name'].value == 'communities') {
 7478:         alert('$communities_reserved\\n$choose_again');
 7479:         return false;
 7480:     }
 7481:     return true;
 7482: }
 7483: 
 7484: // ]]>
 7485: </script>
 7486: 
 7487: ENDSCRIPT
 7488:     return $output;
 7489: }
 7490: 
 7491: sub initialize_categories {
 7492:     my ($itemcount) = @_;
 7493:     my ($datatable,$css_class,$chgstr);
 7494:     my %default_names = (
 7495:                       instcode    => 'Official courses (with institutional codes)',
 7496:                       communities => 'Communities',
 7497:                         );
 7498:     my $select0 = ' selected="selected"';
 7499:     my $select1 = '';
 7500:     foreach my $default ('instcode','communities') {
 7501:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7502:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 7503:         if ($default eq 'communities') {
 7504:             $select1 = $select0;
 7505:             $select0 = '';
 7506:         }
 7507:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7508:                      .'<select name="'.$default.'_pos">'
 7509:                      .'<option value="0"'.$select0.'>1</option>'
 7510:                      .'<option value="1"'.$select1.'>2</option>'
 7511:                      .'<option value="2">3</option></select>&nbsp;'
 7512:                      .$default_names{$default}
 7513:                      .'</span></td><td><span class="LC_nobreak">'
 7514:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7515:                      .&mt('Display').'</label>&nbsp;<label>'
 7516:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7517:                  .'</label></span></td></tr>';
 7518:         $itemcount ++;
 7519:     }
 7520:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7521:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7522:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7523:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7524:                   .'<option value="0">1</option>'
 7525:                   .'<option value="1">2</option>'
 7526:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7527:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 7528:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 7529:                   .'</td></tr>';
 7530:     return $datatable;
 7531: }
 7532: 
 7533: sub build_category_rows {
 7534:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7535:     my ($text,$name,$item,$chgstr);
 7536:     if (ref($cats) eq 'ARRAY') {
 7537:         my $maxdepth = scalar(@{$cats});
 7538:         if (ref($cats->[$depth]) eq 'HASH') {
 7539:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7540:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7541:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7542:                 $text .= '<td><table class="LC_data_table">';
 7543:                 my ($idxnum,$parent_name,$parent_item);
 7544:                 my $higher = $depth - 1;
 7545:                 if ($higher == 0) {
 7546:                     $parent_name = &escape($parent).'::'.$higher;
 7547:                 } else {
 7548:                     if (ref($path) eq 'ARRAY') {
 7549:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7550:                     }
 7551:                 }
 7552:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7553:                 for (my $j=0; $j<=$numchildren; $j++) {
 7554:                     if ($j < $numchildren) {
 7555:                         $name = $cats->[$depth]{$parent}[$j];
 7556:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7557:                         $idxnum = $idx->{$item};
 7558:                     } else {
 7559:                         $name = $parent_name;
 7560:                         $item = $parent_item;
 7561:                     }
 7562:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7563:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7564:                     for (my $i=0; $i<=$numchildren; $i++) {
 7565:                         my $vpos = $i+1;
 7566:                         my $selstr;
 7567:                         if ($j == $i) {
 7568:                             $selstr = ' selected="selected" ';
 7569:                         }
 7570:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7571:                     }
 7572:                     $text .= '</select>&nbsp;';
 7573:                     if ($j < $numchildren) {
 7574:                         my $deeper = $depth+1;
 7575:                         $text .= $name.'&nbsp;'
 7576:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7577:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7578:                         if(ref($path) eq 'ARRAY') {
 7579:                             push(@{$path},$name);
 7580:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7581:                             pop(@{$path});
 7582:                         }
 7583:                     } else {
 7584:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 7585:                         if ($j == $numchildren) {
 7586:                             $text .= $name;
 7587:                         } else {
 7588:                             $text .= $item;
 7589:                         }
 7590:                         $text .= '" value="" />';
 7591:                     }
 7592:                     $text .= '</td></tr>';
 7593:                 }
 7594:                 $text .= '</table></td>';
 7595:             } else {
 7596:                 my $higher = $depth-1;
 7597:                 if ($higher == 0) {
 7598:                     $name = &escape($parent).'::'.$higher;
 7599:                 } else {
 7600:                     if (ref($path) eq 'ARRAY') {
 7601:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7602:                     }
 7603:                 }
 7604:                 my $colspan;
 7605:                 if ($parent ne 'instcode') {
 7606:                     $colspan = $maxdepth - $depth - 1;
 7607:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 7608:                 }
 7609:             }
 7610:         }
 7611:     }
 7612:     return $text;
 7613: }
 7614: 
 7615: sub modifiable_userdata_row {
 7616:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 7617:         $rowid,$customcss,$rowstyle) = @_;
 7618:     my ($role,$rolename,$statustype);
 7619:     $role = $item;
 7620:     if ($context eq 'cancreate') {
 7621:         if ($item =~ /^(emailusername)_(.+)$/) {
 7622:             $role = $1;
 7623:             $statustype = $2;
 7624:             if (ref($usertypes) eq 'HASH') {
 7625:                 if ($usertypes->{$statustype}) {
 7626:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7627:                 } else {
 7628:                     $rolename = &mt('Data provided by user');
 7629:                 }
 7630:             }
 7631:         }
 7632:     } elsif ($context eq 'selfcreate') {
 7633:         if (ref($usertypes) eq 'HASH') {
 7634:             $rolename = $usertypes->{$role};
 7635:         } else {
 7636:             $rolename = $role;
 7637:         }
 7638:     } else {
 7639:         if ($role eq 'cr') {
 7640:             $rolename = &mt('Custom role');
 7641:         } else {
 7642:             $rolename = &Apache::lonnet::plaintext($role);
 7643:         }
 7644:     }
 7645:     my (@fields,%fieldtitles);
 7646:     if (ref($fieldsref) eq 'ARRAY') {
 7647:         @fields = @{$fieldsref};
 7648:     } else {
 7649:         @fields = ('lastname','firstname','middlename','generation',
 7650:                    'permanentemail','id');
 7651:     }
 7652:     if ((ref($titlesref) eq 'HASH')) {
 7653:         %fieldtitles = %{$titlesref};
 7654:     } else {
 7655:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7656:     }
 7657:     my $output;
 7658:     my $css_class;
 7659:     if ($rowcount%2) {
 7660:         $css_class = 'LC_odd_row';
 7661:     }
 7662:     if ($customcss) {
 7663:         $css_class .= " $customcss";
 7664:     }
 7665:     $css_class =~ s/^\s+//;
 7666:     if ($css_class) {
 7667:         $css_class = ' class="'.$css_class.'"';
 7668:     }
 7669:     if ($rowstyle) {
 7670:         $css_class .= ' style="'.$rowstyle.'"';
 7671:     }
 7672:     if ($rowid) {
 7673:         $rowid = ' id="'.$rowid.'"';
 7674:     }
 7675: 
 7676:     $output = '<tr '.$css_class.$rowid.'>'.
 7677:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7678:               '<td class="LC_left_item" colspan="2"><table>';
 7679:     my $rem;
 7680:     my %checks;
 7681:     if (ref($settings) eq 'HASH') {
 7682:         if (ref($settings->{$context}) eq 'HASH') {
 7683:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7684:                 my $hashref = $settings->{$context}->{$role};
 7685:                 if ($role eq 'emailusername') {
 7686:                     if ($statustype) {
 7687:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7688:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7689:                             if (ref($hashref) eq 'HASH') { 
 7690:                                 foreach my $field (@fields) {
 7691:                                     if ($hashref->{$field}) {
 7692:                                         $checks{$field} = $hashref->{$field};
 7693:                                     }
 7694:                                 }
 7695:                             }
 7696:                         }
 7697:                     }
 7698:                 } else {
 7699:                     if (ref($hashref) eq 'HASH') {
 7700:                         foreach my $field (@fields) {
 7701:                             if ($hashref->{$field}) {
 7702:                                 $checks{$field} = ' checked="checked" ';
 7703:                             }
 7704:                         }
 7705:                     }
 7706:                 }
 7707:             }
 7708:         }
 7709:     }
 7710: 
 7711:     my $total = scalar(@fields);
 7712:     for (my $i=0; $i<$total; $i++) {
 7713:         $rem = $i%($numinrow);
 7714:         if ($rem == 0) {
 7715:             if ($i > 0) {
 7716:                 $output .= '</tr>';
 7717:             }
 7718:             $output .= '<tr>';
 7719:         }
 7720:         my $check = ' ';
 7721:         unless ($role eq 'emailusername') {
 7722:             if (exists($checks{$fields[$i]})) {
 7723:                 $check = $checks{$fields[$i]};
 7724:             } else {
 7725:                 if ($role eq 'st') {
 7726:                     if (ref($settings) ne 'HASH') {
 7727:                         $check = ' checked="checked" '; 
 7728:                     }
 7729:                 }
 7730:             }
 7731:         }
 7732:         $output .= '<td class="LC_left_item">'.
 7733:                    '<span class="LC_nobreak">';
 7734:         if ($role eq 'emailusername') {
 7735:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7736:                 $checks{$fields[$i]} = 'omit';
 7737:             }
 7738:             foreach my $option ('required','optional','omit') {
 7739:                 my $checked='';
 7740:                 if ($checks{$fields[$i]} eq $option) {
 7741:                     $checked='checked="checked" ';
 7742:                 }
 7743:                 $output .= '<label>'.
 7744:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7745:                            &mt($option).'</label>'.('&nbsp;' x2);
 7746:             }
 7747:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7748:         } else {
 7749:             $output .= '<label>'.
 7750:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7751:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7752:                        '</label>';
 7753:         }
 7754:         $output .= '</span></td>';
 7755:     }
 7756:     $rem = $total%$numinrow;
 7757:     my $colsleft;
 7758:     if ($rem) {
 7759:         $colsleft = $numinrow - $rem;
 7760:     }
 7761:     if ($colsleft > 1) {
 7762:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7763:                    '&nbsp;</td>';
 7764:     } elsif ($colsleft == 1) {
 7765:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7766:     }
 7767:     $output .= '</tr></table></td></tr>';
 7768:     return $output;
 7769: }
 7770: 
 7771: sub insttypes_row {
 7772:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 7773:         $customcss,$rowstyle) = @_;
 7774:     my %lt = &Apache::lonlocal::texthash (
 7775:                       cansearch => 'Users allowed to search',
 7776:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7777:                       lockablenames => 'User preference to lock name',
 7778:                       selfassign    => 'Self-reportable affiliations',
 7779:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 7780:              );
 7781:     my $showdom;
 7782:     if ($context eq 'cansearch') {
 7783:         $showdom = ' ('.$dom.')';
 7784:     }
 7785:     my $class = 'LC_left_item';
 7786:     if ($context eq 'statustocreate') {
 7787:         $class = 'LC_right_item';
 7788:     }
 7789:     my $css_class;
 7790:     if ($$rowtotal%2) {
 7791:         $css_class = 'LC_odd_row';
 7792:     }
 7793:     if ($customcss) {
 7794:         $css_class .= ' '.$customcss;
 7795:     }
 7796:     $css_class =~ s/^\s+//;
 7797:     if ($css_class) {
 7798:         $css_class = ' class="'.$css_class.'"';
 7799:     }
 7800:     if ($rowstyle) {
 7801:         $css_class .= ' style="'.$rowstyle.'"';
 7802:     }
 7803:     if ($onclick) {
 7804:         $onclick = 'onclick="'.$onclick.'" ';
 7805:     }
 7806:     my $output = '<tr'.$css_class.'>'.
 7807:                  '<td>'.$lt{$context}.$showdom.
 7808:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7809:     my $rem;
 7810:     if (ref($types) eq 'ARRAY') {
 7811:         for (my $i=0; $i<@{$types}; $i++) {
 7812:             if (defined($usertypes->{$types->[$i]})) {
 7813:                 my $rem = $i%($numinrow);
 7814:                 if ($rem == 0) {
 7815:                     if ($i > 0) {
 7816:                         $output .= '</tr>';
 7817:                     }
 7818:                     $output .= '<tr>';
 7819:                 }
 7820:                 my $check = ' ';
 7821:                 if (ref($settings) eq 'HASH') {
 7822:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7823:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7824:                             $check = ' checked="checked" ';
 7825:                         }
 7826:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 7827:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 7828:                             $check = ' checked="checked" ';
 7829:                         }
 7830:                     } elsif ($context eq 'statustocreate') {
 7831:                         $check = ' checked="checked" ';
 7832:                     }
 7833:                 }
 7834:                 $output .= '<td class="LC_left_item">'.
 7835:                            '<span class="LC_nobreak"><label>'.
 7836:                            '<input type="checkbox" name="'.$context.'" '.
 7837:                            'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
 7838:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7839:             }
 7840:         }
 7841:         $rem = @{$types}%($numinrow);
 7842:     }
 7843:     my $colsleft = $numinrow - $rem;
 7844:     if ($context eq 'overrides') {
 7845:         if ($colsleft > 1) {
 7846:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7847:         } else {
 7848:             $output .= '<td class="LC_left_item">';
 7849:         }
 7850:         $output .= '&nbsp;';
 7851:     } else {
 7852:         if ($rem == 0) {
 7853:             $output .= '<tr>';
 7854:         }
 7855:         if ($colsleft > 1) {
 7856:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7857:         } else {
 7858:             $output .= '<td class="LC_left_item">';
 7859:         }
 7860:         my $defcheck = ' ';
 7861:         if (ref($settings) eq 'HASH') {  
 7862:             if (ref($settings->{$context}) eq 'ARRAY') {
 7863:                 if (grep(/^default$/,@{$settings->{$context}})) {
 7864:                     $defcheck = ' checked="checked" ';
 7865:                 }
 7866:             } elsif ($context eq 'statustocreate') {
 7867:                 $defcheck = ' checked="checked" ';
 7868:             }
 7869:         }
 7870:         $output .= '<span class="LC_nobreak"><label>'.
 7871:                    '<input type="checkbox" name="'.$context.'" '.
 7872:                    'value="default"'.$defcheck.$onclick.' />'.
 7873:                    $othertitle.'</label></span>';
 7874:     }
 7875:     $output .= '</td></tr></table></td></tr>';
 7876:     return $output;
 7877: }
 7878: 
 7879: sub sorted_searchtitles {
 7880:     my %searchtitles = &Apache::lonlocal::texthash(
 7881:                          'uname' => 'username',
 7882:                          'lastname' => 'last name',
 7883:                          'lastfirst' => 'last name, first name',
 7884:                      );
 7885:     my @titleorder = ('uname','lastname','lastfirst');
 7886:     return (\%searchtitles,\@titleorder);
 7887: }
 7888: 
 7889: sub sorted_searchtypes {
 7890:     my %srchtypes_desc = (
 7891:                            exact    => 'is exact match',
 7892:                            contains => 'contains ..',
 7893:                            begins   => 'begins with ..',
 7894:                          );
 7895:     my @srchtypeorder = ('exact','begins','contains');
 7896:     return (\%srchtypes_desc,\@srchtypeorder);
 7897: }
 7898: 
 7899: sub usertype_update_row {
 7900:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7901:     my $datatable;
 7902:     my $numinrow = 4;
 7903:     foreach my $type (@{$types}) {
 7904:         if (defined($usertypes->{$type})) {
 7905:             $$rownums ++;
 7906:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7907:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7908:                           '</td><td class="LC_left_item"><table>';
 7909:             for (my $i=0; $i<@{$fields}; $i++) {
 7910:                 my $rem = $i%($numinrow);
 7911:                 if ($rem == 0) {
 7912:                     if ($i > 0) {
 7913:                         $datatable .= '</tr>';
 7914:                     }
 7915:                     $datatable .= '<tr>';
 7916:                 }
 7917:                 my $check = ' ';
 7918:                 if (ref($settings) eq 'HASH') {
 7919:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7920:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7921:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7922:                                 $check = ' checked="checked" ';
 7923:                             }
 7924:                         }
 7925:                     }
 7926:                 }
 7927: 
 7928:                 if ($i == @{$fields}-1) {
 7929:                     my $colsleft = $numinrow - $rem;
 7930:                     if ($colsleft > 1) {
 7931:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7932:                     } else {
 7933:                         $datatable .= '<td>';
 7934:                     }
 7935:                 } else {
 7936:                     $datatable .= '<td>';
 7937:                 }
 7938:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7939:                               '<input type="checkbox" name="updateable_'.$type.
 7940:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7941:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7942:             }
 7943:             $datatable .= '</tr></table></td></tr>';
 7944:         }
 7945:     }
 7946:     return $datatable;
 7947: }
 7948: 
 7949: sub modify_login {
 7950:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7951:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7952:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7953:     %title = ( coursecatalog => 'Display course catalog',
 7954:                adminmail => 'Display administrator E-mail address',
 7955:                helpdesk  => 'Display "Contact Helpdesk" link',
 7956:                newuser => 'Link for visitors to create a user account',
 7957:                loginheader => 'Log-in box header');
 7958:     @offon = ('off','on');
 7959:     if (ref($domconfig{login}) eq 'HASH') {
 7960:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7961:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7962:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7963:             }
 7964:         }
 7965:     }
 7966:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7967:                                            \%domconfig,\%loginhash);
 7968:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7969:     foreach my $item (@toggles) {
 7970:         $loginhash{login}{$item} = $env{'form.'.$item};
 7971:     }
 7972:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7973:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7974:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7975:                                          \%loginhash);
 7976:     }
 7977: 
 7978:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7979:     my %domservers = &Apache::lonnet::get_servers($dom);
 7980:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7981:     if (keys(%servers) > 1) {
 7982:         foreach my $lonhost (keys(%servers)) {
 7983:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7984:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7985:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7986:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7987:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7988:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7989:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7990:                         $changes{'loginvia'}{$lonhost} = 1;
 7991:                     } else {
 7992:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7993:                         $changes{'loginvia'}{$lonhost} = 1;
 7994:                     }
 7995:                 } else {
 7996:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7997:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7998:                         $changes{'loginvia'}{$lonhost} = 1;
 7999:                     }
 8000:                 }
 8001:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 8002:                     foreach my $item (@loginvia_attribs) {
 8003:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 8004:                     }
 8005:                 } else {
 8006:                     foreach my $item (@loginvia_attribs) {
 8007:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8008:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8009:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 8010:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8011:                                 $new = '/';
 8012:                             }
 8013:                         }
 8014:                         if (($item eq 'custompath') && 
 8015:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8016:                             $new = '';
 8017:                         }
 8018:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 8019:                             $changes{'loginvia'}{$lonhost} = 1;
 8020:                         }
 8021:                         if ($item eq 'exempt') {
 8022:                             $new = &check_exempt_addresses($new);
 8023:                         }
 8024:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8025:                     }
 8026:                 }
 8027:             } else {
 8028:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8029:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8030:                     $changes{'loginvia'}{$lonhost} = 1;
 8031:                     foreach my $item (@loginvia_attribs) {
 8032:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8033:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8034:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8035:                                 $new = '/';
 8036:                             }
 8037:                         }
 8038:                         if (($item eq 'custompath') && 
 8039:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8040:                             $new = '';
 8041:                         }
 8042:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8043:                     }
 8044:                 }
 8045:             }
 8046:         }
 8047:     }
 8048: 
 8049:     my $servadm = $r->dir_config('lonAdmEMail');
 8050:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 8051:     if (ref($domconfig{'login'}) eq 'HASH') {
 8052:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 8053:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 8054:                 if ($lang eq 'nolang') {
 8055:                     push(@currlangs,$lang);
 8056:                 } elsif (defined($langchoices{$lang})) {
 8057:                     push(@currlangs,$lang);
 8058:                 } else {
 8059:                     next;
 8060:                 }
 8061:             }
 8062:         }
 8063:     }
 8064:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 8065:     if (@currlangs > 0) {
 8066:         foreach my $lang (@currlangs) {
 8067:             if (grep(/^\Q$lang\E$/,@delurls)) {
 8068:                 $changes{'helpurl'}{$lang} = 1;
 8069:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 8070:                 $changes{'helpurl'}{$lang} = 1;
 8071:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 8072:                 push(@newlangs,$lang);
 8073:             } else {
 8074:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8075:             }
 8076:         }
 8077:     }
 8078:     unless (grep(/^nolang$/,@currlangs)) {
 8079:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 8080:             $changes{'helpurl'}{'nolang'} = 1;
 8081:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 8082:             push(@newlangs,'nolang');
 8083:         }
 8084:     }
 8085:     if ($env{'form.loginhelpurl_add_lang'}) {
 8086:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 8087:             ($env{'form.loginhelpurl_add_file.filename'})) {
 8088:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 8089:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 8090:         }
 8091:     }
 8092:     if ((@newlangs > 0) || ($addedfile)) {
 8093:         my $error;
 8094:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8095:         if ($configuserok eq 'ok') {
 8096:             if ($switchserver) {
 8097:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 8098:             } elsif ($author_ok eq 'ok') {
 8099:                 my @allnew = @newlangs;
 8100:                 if ($addedfile ne '') {
 8101:                     push(@allnew,$addedfile);
 8102:                 }
 8103:                 foreach my $lang (@allnew) {
 8104:                     my $formelem = 'loginhelpurl_'.$lang;
 8105:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 8106:                         $formelem = 'loginhelpurl_add_file';
 8107:                     }
 8108:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8109:                                                                "help/$lang",'','',$newfile{$lang});
 8110:                     if ($result eq 'ok') {
 8111:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 8112:                         $changes{'helpurl'}{$lang} = 1;
 8113:                     } else {
 8114:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 8115:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8116:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 8117:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 8118:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8119:                         }
 8120:                     }
 8121:                 }
 8122:             } else {
 8123:                 $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);
 8124:             }
 8125:         } else {
 8126:             $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);
 8127:         }
 8128:         if ($error) {
 8129:             &Apache::lonnet::logthis($error);
 8130:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8131:         }
 8132:     }
 8133: 
 8134:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 8135:     if (ref($domconfig{'login'}) eq 'HASH') {
 8136:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 8137:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 8138:                 if ($domservers{$lonhost}) {
 8139:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8140:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 8141:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 8142:                     }
 8143:                 }
 8144:             }
 8145:         }
 8146:     }
 8147:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 8148:     foreach my $lonhost (sort(keys(%domservers))) {
 8149:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8150:             $changes{'headtag'}{$lonhost} = 1;
 8151:         } else {
 8152:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 8153:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 8154:             }
 8155:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 8156:                 push(@newhosts,$lonhost);
 8157:             } elsif ($currheadtagurls{$lonhost}) {
 8158:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 8159:                 if ($currexempt{$lonhost}) {
 8160:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 8161:                         $changes{'headtag'}{$lonhost} = 1;
 8162:                     }
 8163:                 } elsif ($possexempt{$lonhost}) {
 8164:                     $changes{'headtag'}{$lonhost} = 1;
 8165:                 }
 8166:                 if ($possexempt{$lonhost}) {
 8167:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8168:                 }
 8169:             }
 8170:         }
 8171:     }
 8172:     if (@newhosts) {
 8173:         my $error;
 8174:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8175:         if ($configuserok eq 'ok') {
 8176:             if ($switchserver) {
 8177:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8178:             } elsif ($author_ok eq 'ok') {
 8179:                 foreach my $lonhost (@newhosts) {
 8180:                     my $formelem = 'loginheadtag_'.$lonhost;
 8181:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8182:                                                                           "login/headtag/$lonhost",'','',
 8183:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8184:                     if ($result eq 'ok') {
 8185:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8186:                         $changes{'headtag'}{$lonhost} = 1;
 8187:                         if ($possexempt{$lonhost}) {
 8188:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8189:                         }
 8190:                     } else {
 8191:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 8192:                                            $newheadtagurls{$lonhost},$result);
 8193:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8194:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 8195:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 8196:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 8197:                         }
 8198:                     }
 8199:                 }
 8200:             } else {
 8201:                 $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);
 8202:             }
 8203:         } else {
 8204:             $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);
 8205:         }
 8206:         if ($error) {
 8207:             &Apache::lonnet::logthis($error);
 8208:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8209:         }
 8210:     }
 8211:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 8212: 
 8213:     my $defaulthelpfile = '/adm/loginproblems.html';
 8214:     my $defaulttext = &mt('Default in use');
 8215: 
 8216:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 8217:                                              $dom);
 8218:     if ($putresult eq 'ok') {
 8219:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8220:         my %defaultchecked = (
 8221:                     'coursecatalog' => 'on',
 8222:                     'helpdesk'      => 'on',
 8223:                     'adminmail'     => 'off',
 8224:                     'newuser'       => 'off',
 8225:         );
 8226:         if (ref($domconfig{'login'}) eq 'HASH') {
 8227:             foreach my $item (@toggles) {
 8228:                 if ($defaultchecked{$item} eq 'on') { 
 8229:                     if (($domconfig{'login'}{$item} eq '0') &&
 8230:                         ($env{'form.'.$item} eq '1')) {
 8231:                         $changes{$item} = 1;
 8232:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8233:                               $domconfig{'login'}{$item} eq '1') &&
 8234:                              ($env{'form.'.$item} eq '0')) {
 8235:                         $changes{$item} = 1;
 8236:                     }
 8237:                 } elsif ($defaultchecked{$item} eq 'off') {
 8238:                     if (($domconfig{'login'}{$item} eq '1') &&
 8239:                         ($env{'form.'.$item} eq '0')) {
 8240:                         $changes{$item} = 1;
 8241:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8242:                               $domconfig{'login'}{$item} eq '0') &&
 8243:                              ($env{'form.'.$item} eq '1')) {
 8244:                         $changes{$item} = 1;
 8245:                     }
 8246:                 }
 8247:             }
 8248:         }
 8249:         if (keys(%changes) > 0 || $colchgtext) {
 8250:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8251:             if (ref($lastactref) eq 'HASH') {
 8252:                 $lastactref->{'domainconfig'} = 1;
 8253:             }
 8254:             $resulttext = &mt('Changes made:').'<ul>';
 8255:             foreach my $item (sort(keys(%changes))) {
 8256:                 if ($item eq 'loginvia') {
 8257:                     if (ref($changes{$item}) eq 'HASH') {
 8258:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 8259:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8260:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 8261:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 8262:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 8263:                                     $protocol = 'http' if ($protocol ne 'https');
 8264:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 8265: 
 8266:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 8267:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 8268:                                     } else {
 8269:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 8270:                                     }
 8271:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 8272:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 8273:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 8274:                                     }
 8275:                                     $resulttext .= '</li>';
 8276:                                 } else {
 8277:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 8278:                                 }
 8279:                             } else {
 8280:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 8281:                             }
 8282:                         }
 8283:                         $resulttext .= '</ul></li>';
 8284:                     }
 8285:                 } elsif ($item eq 'helpurl') {
 8286:                     if (ref($changes{$item}) eq 'HASH') {
 8287:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 8288:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 8289:                                 my ($chg,$link);
 8290:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 8291:                                 if ($lang eq 'nolang') {
 8292:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 8293:                                 } else {
 8294:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 8295:                                 }
 8296:                                 $resulttext .= '<li>'.$chg.'</li>';
 8297:                             } else {
 8298:                                 my $chg;
 8299:                                 if ($lang eq 'nolang') {
 8300:                                     $chg = &mt('custom log-in help file for no preferred language');
 8301:                                 } else {
 8302:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 8303:                                 }
 8304:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 8305:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 8306:                                                       '?inhibitmenu=yes',$chg,600,500).
 8307:                                                '</li>';
 8308:                             }
 8309:                         }
 8310:                     }
 8311:                 } elsif ($item eq 'headtag') {
 8312:                     if (ref($changes{$item}) eq 'HASH') {
 8313:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8314:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8315:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 8316:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8317:                                 $resulttext .= '<li><a href="'.
 8318:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 8319:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 8320:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 8321:                                 if ($possexempt{$lonhost}) {
 8322:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 8323:                                 } else {
 8324:                                     $resulttext .= &mt('included for any client IP');
 8325:                                 }
 8326:                                 $resulttext .= '</li>';
 8327:                             }
 8328:                         }
 8329:                     }
 8330:                 } elsif ($item eq 'captcha') {
 8331:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8332:                         my $chgtxt;
 8333:                         if ($loginhash{'login'}{$item} eq 'notused') {
 8334:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 8335:                         } else {
 8336:                             my %captchas = &captcha_phrases();
 8337:                             if ($captchas{$loginhash{'login'}{$item}}) {
 8338:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 8339:                             } else {
 8340:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 8341:                             }
 8342:                         }
 8343:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8344:                     }
 8345:                 } elsif ($item eq 'recaptchakeys') {
 8346:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8347:                         my ($privkey,$pubkey);
 8348:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 8349:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 8350:                             $privkey = $loginhash{'login'}{$item}{'private'};
 8351:                         }
 8352:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 8353:                         if (!$pubkey) {
 8354:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 8355:                         } else {
 8356:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8357:                         }
 8358:                         if (!$privkey) {
 8359:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 8360:                         } else {
 8361:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 8362:                         }
 8363:                         $chgtxt .= '</ul>';
 8364:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8365:                     }
 8366:                 } elsif ($item eq 'recaptchaversion') {
 8367:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8368:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 8369:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 8370:                                            '</li>';
 8371:                         }
 8372:                     }
 8373:                 } else {
 8374:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 8375:                 }
 8376:             }
 8377:             $resulttext .= $colchgtext.'</ul>';
 8378:         } else {
 8379:             $resulttext = &mt('No changes made to log-in page settings');
 8380:         }
 8381:     } else {
 8382:         $resulttext = '<span class="LC_error">'.
 8383: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8384:     }
 8385:     if ($errors) {
 8386:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8387:                        $errors.'</ul>';
 8388:     }
 8389:     return $resulttext;
 8390: }
 8391: 
 8392: sub check_exempt_addresses {
 8393:     my ($iplist) = @_;
 8394:     $iplist =~ s/^\s+//;
 8395:     $iplist =~ s/\s+$//;
 8396:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8397:     my (@okips,$new);
 8398:     foreach my $ip (@poss_ips) {
 8399:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8400:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8401:                 push(@okips,$ip);
 8402:             }
 8403:         }
 8404:     }
 8405:     if (@okips > 0) {
 8406:         $new = join(',',@okips);
 8407:     } else {
 8408:         $new = '';
 8409:     }
 8410:     return $new;
 8411: }
 8412: 
 8413: sub color_font_choices {
 8414:     my %choices =
 8415:         &Apache::lonlocal::texthash (
 8416:             img => "Header",
 8417:             bgs => "Background colors",
 8418:             links => "Link colors",
 8419:             images => "Images",
 8420:             font => "Font color",
 8421:             fontmenu => "Font menu",
 8422:             pgbg => "Page",
 8423:             tabbg => "Header",
 8424:             sidebg => "Border",
 8425:             link => "Link",
 8426:             alink => "Active link",
 8427:             vlink => "Visited link",
 8428:         );
 8429:     return %choices;
 8430: }
 8431: 
 8432: sub modify_rolecolors {
 8433:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8434:     my ($resulttext,%rolehash);
 8435:     $rolehash{'rolecolors'} = {};
 8436:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8437:         if ($domconfig{'rolecolors'} eq '') {
 8438:             $domconfig{'rolecolors'} = {};
 8439:         }
 8440:     }
 8441:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8442:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8443:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8444:                                              $dom);
 8445:     if ($putresult eq 'ok') {
 8446:         if (keys(%changes) > 0) {
 8447:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8448:             if (ref($lastactref) eq 'HASH') {
 8449:                 $lastactref->{'domainconfig'} = 1;
 8450:             }
 8451:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8452:                                              $rolehash{'rolecolors'});
 8453:         } else {
 8454:             $resulttext = &mt('No changes made to default color schemes');
 8455:         }
 8456:     } else {
 8457:         $resulttext = '<span class="LC_error">'.
 8458: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8459:     }
 8460:     if ($errors) {
 8461:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8462:                        $errors.'</ul>';
 8463:     }
 8464:     return $resulttext;
 8465: }
 8466: 
 8467: sub modify_colors {
 8468:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8469:     my (%changes,%choices);
 8470:     my @bgs;
 8471:     my @links = ('link','alink','vlink');
 8472:     my @logintext;
 8473:     my @images;
 8474:     my $servadm = $r->dir_config('lonAdmEMail');
 8475:     my $errors;
 8476:     my %defaults;
 8477:     foreach my $role (@{$roles}) {
 8478:         if ($role eq 'login') {
 8479:             %choices = &login_choices();
 8480:             @logintext = ('textcol','bgcol');
 8481:         } else {
 8482:             %choices = &color_font_choices();
 8483:         }
 8484:         if ($role eq 'login') {
 8485:             @images = ('img','logo','domlogo','login');
 8486:             @bgs = ('pgbg','mainbg','sidebg');
 8487:         } else {
 8488:             @images = ('img');
 8489:             @bgs = ('pgbg','tabbg','sidebg');
 8490:         }
 8491:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8492:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8493:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8494:         }
 8495:         if ($role eq 'login') {
 8496:             foreach my $item (@logintext) {
 8497:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8498:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8499:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8500:                 }
 8501:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8502:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8503:                 }
 8504:             }
 8505:         } else {
 8506:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8507:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8508:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8509:             }
 8510:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8511:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8512:             }
 8513:         }
 8514:         foreach my $item (@bgs) {
 8515:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8516:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8517:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8518:             }
 8519:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8520:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8521:             }
 8522:         }
 8523:         foreach my $item (@links) {
 8524:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8525:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8526:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8527:             }
 8528:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8529:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8530:             }
 8531:         }
 8532:         my ($configuserok,$author_ok,$switchserver) = 
 8533:             &config_check($dom,$confname,$servadm);
 8534:         my ($width,$height) = &thumb_dimensions();
 8535:         if (ref($domconfig->{$role}) ne 'HASH') {
 8536:             $domconfig->{$role} = {};
 8537:         }
 8538:         foreach my $img (@images) {
 8539:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8540:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8541:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8542:                 } else { 
 8543:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8544:                 }
 8545:             } 
 8546: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8547: 		 && !defined($domconfig->{$role}{$img})
 8548: 		 && !$env{'form.'.$role.'_del_'.$img}
 8549: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8550: 		# import the old configured image from the .tab setting
 8551: 		# if they haven't provided a new one 
 8552: 		$domconfig->{$role}{$img} = 
 8553: 		    $env{'form.'.$role.'_import_'.$img};
 8554: 	    }
 8555:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8556:                 my $error;
 8557:                 if ($configuserok eq 'ok') {
 8558:                     if ($switchserver) {
 8559:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8560:                     } else {
 8561:                         if ($author_ok eq 'ok') {
 8562:                             my ($result,$logourl) = 
 8563:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8564:                                            $dom,$confname,$img,$width,$height);
 8565:                             if ($result eq 'ok') {
 8566:                                 $confhash->{$role}{$img} = $logourl;
 8567:                                 $changes{$role}{'images'}{$img} = 1;
 8568:                             } else {
 8569:                                 $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);
 8570:                             }
 8571:                         } else {
 8572:                             $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);
 8573:                         }
 8574:                     }
 8575:                 } else {
 8576:                     $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);
 8577:                 }
 8578:                 if ($error) {
 8579:                     &Apache::lonnet::logthis($error);
 8580:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8581:                 }
 8582:             } elsif ($domconfig->{$role}{$img} ne '') {
 8583:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8584:                     my $error;
 8585:                     if ($configuserok eq 'ok') {
 8586: # is confname an author?
 8587:                         if ($switchserver eq '') {
 8588:                             if ($author_ok eq 'ok') {
 8589:                                 my ($result,$logourl) = 
 8590:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8591:                                             $dom,$confname,$img,$width,$height);
 8592:                                 if ($result eq 'ok') {
 8593:                                     $confhash->{$role}{$img} = $logourl;
 8594: 				    $changes{$role}{'images'}{$img} = 1;
 8595:                                 }
 8596:                             }
 8597:                         }
 8598:                     }
 8599:                 }
 8600:             }
 8601:         }
 8602:         if (ref($domconfig) eq 'HASH') {
 8603:             if (ref($domconfig->{$role}) eq 'HASH') {
 8604:                 foreach my $img (@images) {
 8605:                     if ($domconfig->{$role}{$img} ne '') {
 8606:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8607:                             $confhash->{$role}{$img} = '';
 8608:                             $changes{$role}{'images'}{$img} = 1;
 8609:                         } else {
 8610:                             if ($confhash->{$role}{$img} eq '') {
 8611:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8612:                             }
 8613:                         }
 8614:                     } else {
 8615:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8616:                             $confhash->{$role}{$img} = '';
 8617:                             $changes{$role}{'images'}{$img} = 1;
 8618:                         } 
 8619:                     }
 8620:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8621:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8622:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8623:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8624:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8625:                             }
 8626:                         } else {
 8627:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8628:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8629:                             }
 8630:                         }
 8631:                     }
 8632:                 }
 8633:                 if ($domconfig->{$role}{'font'} ne '') {
 8634:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8635:                         $changes{$role}{'font'} = 1;
 8636:                     }
 8637:                 } else {
 8638:                     if ($confhash->{$role}{'font'}) {
 8639:                         $changes{$role}{'font'} = 1;
 8640:                     }
 8641:                 }
 8642:                 if ($role ne 'login') {
 8643:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8644:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8645:                             $changes{$role}{'fontmenu'} = 1;
 8646:                         }
 8647:                     } else {
 8648:                         if ($confhash->{$role}{'fontmenu'}) {
 8649:                             $changes{$role}{'fontmenu'} = 1;
 8650:                         }
 8651:                     }
 8652:                 }
 8653:                 foreach my $item (@bgs) {
 8654:                     if ($domconfig->{$role}{$item} ne '') {
 8655:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8656:                             $changes{$role}{'bgs'}{$item} = 1;
 8657:                         } 
 8658:                     } else {
 8659:                         if ($confhash->{$role}{$item}) {
 8660:                             $changes{$role}{'bgs'}{$item} = 1;
 8661:                         }
 8662:                     }
 8663:                 }
 8664:                 foreach my $item (@links) {
 8665:                     if ($domconfig->{$role}{$item} ne '') {
 8666:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8667:                             $changes{$role}{'links'}{$item} = 1;
 8668:                         }
 8669:                     } else {
 8670:                         if ($confhash->{$role}{$item}) {
 8671:                             $changes{$role}{'links'}{$item} = 1;
 8672:                         }
 8673:                     }
 8674:                 }
 8675:                 foreach my $item (@logintext) {
 8676:                     if ($domconfig->{$role}{$item} ne '') {
 8677:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8678:                             $changes{$role}{'logintext'}{$item} = 1;
 8679:                         }
 8680:                     } else {
 8681:                         if ($confhash->{$role}{$item}) {
 8682:                             $changes{$role}{'logintext'}{$item} = 1;
 8683:                         }
 8684:                     }
 8685:                 }
 8686:             } else {
 8687:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8688:                                         \@logintext,$confhash,\%changes); 
 8689:             }
 8690:         } else {
 8691:             &default_change_checker($role,\@images,\@links,\@bgs,
 8692:                                     \@logintext,$confhash,\%changes); 
 8693:         }
 8694:     }
 8695:     return ($errors,%changes);
 8696: }
 8697: 
 8698: sub config_check {
 8699:     my ($dom,$confname,$servadm) = @_;
 8700:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8701:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8702:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8703:                                                    $confname,$servadm);
 8704:     if ($configuserok eq 'ok') {
 8705:         $switchserver = &check_switchserver($dom,$confname);
 8706:         if ($switchserver eq '') {
 8707:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8708:         }
 8709:     }
 8710:     return ($configuserok,$author_ok,$switchserver);
 8711: }
 8712: 
 8713: sub default_change_checker {
 8714:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8715:     foreach my $item (@{$links}) {
 8716:         if ($confhash->{$role}{$item}) {
 8717:             $changes->{$role}{'links'}{$item} = 1;
 8718:         }
 8719:     }
 8720:     foreach my $item (@{$bgs}) {
 8721:         if ($confhash->{$role}{$item}) {
 8722:             $changes->{$role}{'bgs'}{$item} = 1;
 8723:         }
 8724:     }
 8725:     foreach my $item (@{$logintext}) {
 8726:         if ($confhash->{$role}{$item}) {
 8727:             $changes->{$role}{'logintext'}{$item} = 1;
 8728:         }
 8729:     }
 8730:     foreach my $img (@{$images}) {
 8731:         if ($env{'form.'.$role.'_del_'.$img}) {
 8732:             $confhash->{$role}{$img} = '';
 8733:             $changes->{$role}{'images'}{$img} = 1;
 8734:         }
 8735:         if ($role eq 'login') {
 8736:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8737:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8738:             }
 8739:         }
 8740:     }
 8741:     if ($confhash->{$role}{'font'}) {
 8742:         $changes->{$role}{'font'} = 1;
 8743:     }
 8744: }
 8745: 
 8746: sub display_colorchgs {
 8747:     my ($dom,$changes,$roles,$confhash) = @_;
 8748:     my (%choices,$resulttext);
 8749:     if (!grep(/^login$/,@{$roles})) {
 8750:         $resulttext = &mt('Changes made:').'<br />';
 8751:     }
 8752:     foreach my $role (@{$roles}) {
 8753:         if ($role eq 'login') {
 8754:             %choices = &login_choices();
 8755:         } else {
 8756:             %choices = &color_font_choices();
 8757:         }
 8758:         if (ref($changes->{$role}) eq 'HASH') {
 8759:             if ($role ne 'login') {
 8760:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8761:             }
 8762:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8763:                 if ($role ne 'login') {
 8764:                     $resulttext .= '<ul>';
 8765:                 }
 8766:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8767:                     if ($role ne 'login') {
 8768:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8769:                     }
 8770:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8771:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8772:                             if ($confhash->{$role}{$key}{$item}) {
 8773:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8774:                             } else {
 8775:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8776:                             }
 8777:                         } elsif ($confhash->{$role}{$item} eq '') {
 8778:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8779:                         } else {
 8780:                             my $newitem = $confhash->{$role}{$item};
 8781:                             if ($key eq 'images') {
 8782:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8783:                             }
 8784:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8785:                         }
 8786:                     }
 8787:                     if ($role ne 'login') {
 8788:                         $resulttext .= '</ul></li>';
 8789:                     }
 8790:                 } else {
 8791:                     if ($confhash->{$role}{$key} eq '') {
 8792:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8793:                     } else {
 8794:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8795:                     }
 8796:                 }
 8797:                 if ($role ne 'login') {
 8798:                     $resulttext .= '</ul>';
 8799:                 }
 8800:             }
 8801:         }
 8802:     }
 8803:     return $resulttext;
 8804: }
 8805: 
 8806: sub thumb_dimensions {
 8807:     return ('200','50');
 8808: }
 8809: 
 8810: sub check_dimensions {
 8811:     my ($inputfile) = @_;
 8812:     my ($fullwidth,$fullheight);
 8813:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8814:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8815:             my $imageinfo = <PIPE>;
 8816:             if (!close(PIPE)) {
 8817:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8818:             }
 8819:             chomp($imageinfo);
 8820:             my ($fullsize) = 
 8821:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8822:             if ($fullsize) {
 8823:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8824:             }
 8825:         }
 8826:     }
 8827:     return ($fullwidth,$fullheight);
 8828: }
 8829: 
 8830: sub check_configuser {
 8831:     my ($uhome,$dom,$confname,$servadm) = @_;
 8832:     my ($configuserok,%currroles);
 8833:     if ($uhome eq 'no_host') {
 8834:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8835:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
 8836:         $configuserok = 
 8837:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8838:                              $configpass,'','','','','',undef,$servadm);
 8839:     } else {
 8840:         $configuserok = 'ok';
 8841:         %currroles = 
 8842:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8843:     }
 8844:     return ($configuserok,%currroles);
 8845: }
 8846: 
 8847: sub check_authorstatus {
 8848:     my ($dom,$confname,%currroles) = @_;
 8849:     my $author_ok;
 8850:     if (!$currroles{':'.$dom.':au'}) {
 8851:         my $start = time;
 8852:         my $end = 0;
 8853:         $author_ok = 
 8854:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8855:                                         'au',$end,$start,'','','domconfig');
 8856:     } else {
 8857:         $author_ok = 'ok';
 8858:     }
 8859:     return $author_ok;
 8860: }
 8861: 
 8862: sub publishlogo {
 8863:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8864:     my ($output,$fname,$logourl);
 8865:     if ($action eq 'upload') {
 8866:         $fname=$env{'form.'.$formname.'.filename'};
 8867:         chop($env{'form.'.$formname});
 8868:     } else {
 8869:         ($fname) = ($formname =~ /([^\/]+)$/);
 8870:     }
 8871:     if ($savefileas ne '') {
 8872:         $fname = $savefileas;
 8873:     }
 8874:     $fname=&Apache::lonnet::clean_filename($fname);
 8875: # See if there is anything left
 8876:     unless ($fname) { return ('error: no uploaded file'); }
 8877:     $fname="$subdir/$fname";
 8878:     my $docroot=$r->dir_config('lonDocRoot');
 8879:     my $filepath="$docroot/priv";
 8880:     my $relpath = "$dom/$confname";
 8881:     my ($fnamepath,$file,$fetchthumb);
 8882:     $file=$fname;
 8883:     if ($fname=~m|/|) {
 8884:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8885:     }
 8886:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8887:     my $count;
 8888:     for ($count=5;$count<=$#parts;$count++) {
 8889:         $filepath.="/$parts[$count]";
 8890:         if ((-e $filepath)!=1) {
 8891:             mkdir($filepath,02770);
 8892:         }
 8893:     }
 8894:     # Check for bad extension and disallow upload
 8895:     if ($file=~/\.(\w+)$/ &&
 8896:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8897:         $output = 
 8898:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8899:     } elsif ($file=~/\.(\w+)$/ &&
 8900:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8901:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8902:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8903:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8904:     } elsif (-d "$filepath/$file") {
 8905:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8906:     } else {
 8907:         my $source = $filepath.'/'.$file;
 8908:         my $logfile;
 8909:         if (!open($logfile,">>",$source.'.log')) {
 8910:             return (&mt('No write permission to Authoring Space'));
 8911:         }
 8912:         print $logfile
 8913: "\n================= Publish ".localtime()." ================\n".
 8914: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8915: # Save the file
 8916:         if (!open(FH,">",$source)) {
 8917:             &Apache::lonnet::logthis('Failed to create '.$source);
 8918:             return (&mt('Failed to create file'));
 8919:         }
 8920:         if ($action eq 'upload') {
 8921:             if (!print FH ($env{'form.'.$formname})) {
 8922:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8923:                 return (&mt('Failed to write file'));
 8924:             }
 8925:         } else {
 8926:             my $original = &Apache::lonnet::filelocation('',$formname);
 8927:             if(!copy($original,$source)) {
 8928:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8929:                 return (&mt('Failed to write file'));
 8930:             }
 8931:         }
 8932:         close(FH);
 8933:         chmod(0660, $source); # Permissions to rw-rw---.
 8934: 
 8935:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8936:         my $copyfile=$targetdir.'/'.$file;
 8937: 
 8938:         my @parts=split(/\//,$targetdir);
 8939:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8940:         for (my $count=5;$count<=$#parts;$count++) {
 8941:             $path.="/$parts[$count]";
 8942:             if (!-e $path) {
 8943:                 print $logfile "\nCreating directory ".$path;
 8944:                 mkdir($path,02770);
 8945:             }
 8946:         }
 8947:         my $versionresult;
 8948:         if (-e $copyfile) {
 8949:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8950:         } else {
 8951:             $versionresult = 'ok';
 8952:         }
 8953:         if ($versionresult eq 'ok') {
 8954:             if (copy($source,$copyfile)) {
 8955:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8956:                 $output = 'ok';
 8957:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8958:                 push(@{$modified_urls},[$copyfile,$source]);
 8959:                 my $metaoutput = 
 8960:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8961:                 unless ($registered_cleanup) {
 8962:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8963:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8964:                     $registered_cleanup=1;
 8965:                 }
 8966:             } else {
 8967:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8968:                 $output = &mt('Failed to copy file to RES space').", $!";
 8969:             }
 8970:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8971:                 my $inputfile = $filepath.'/'.$file;
 8972:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8973:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8974:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8975:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8976:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8977:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 8978:                         system({$args[0]} @args);
 8979:                         chmod(0660, $filepath.'/tn-'.$file);
 8980:                         if (-e $outfile) {
 8981:                             my $copyfile=$targetdir.'/tn-'.$file;
 8982:                             if (copy($outfile,$copyfile)) {
 8983:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8984:                                 my $thumb_metaoutput = 
 8985:                                     &write_metadata($dom,$confname,$formname,
 8986:                                                     $targetdir,'tn-'.$file,$logfile);
 8987:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8988:                                 unless ($registered_cleanup) {
 8989:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8990:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8991:                                     $registered_cleanup=1;
 8992:                                 }
 8993:                             } else {
 8994:                                 print $logfile "\nUnable to write ".$copyfile.
 8995:                                                ':'.$!."\n";
 8996:                             }
 8997:                         }
 8998:                     }
 8999:                 }
 9000:             }
 9001:         } else {
 9002:             $output = $versionresult;
 9003:         }
 9004:     }
 9005:     return ($output,$logourl);
 9006: }
 9007: 
 9008: sub logo_versioning {
 9009:     my ($targetdir,$file,$logfile) = @_;
 9010:     my $target = $targetdir.'/'.$file;
 9011:     my ($maxversion,$fn,$extn,$output);
 9012:     $maxversion = 0;
 9013:     if ($file =~ /^(.+)\.(\w+)$/) {
 9014:         $fn=$1;
 9015:         $extn=$2;
 9016:     }
 9017:     opendir(DIR,$targetdir);
 9018:     while (my $filename=readdir(DIR)) {
 9019:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 9020:             $maxversion=($1>$maxversion)?$1:$maxversion;
 9021:         }
 9022:     }
 9023:     $maxversion++;
 9024:     print $logfile "\nCreating old version ".$maxversion."\n";
 9025:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 9026:     if (copy($target,$copyfile)) {
 9027:         print $logfile "Copied old target to ".$copyfile."\n";
 9028:         $copyfile=$copyfile.'.meta';
 9029:         if (copy($target.'.meta',$copyfile)) {
 9030:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 9031:             $output = 'ok';
 9032:         } else {
 9033:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 9034:             $output = &mt('Failed to copy old meta').", $!, ";
 9035:         }
 9036:     } else {
 9037:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 9038:         $output = &mt('Failed to copy old target').", $!, ";
 9039:     }
 9040:     return $output;
 9041: }
 9042: 
 9043: sub write_metadata {
 9044:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 9045:     my (%metadatafields,%metadatakeys,$output);
 9046:     $metadatafields{'title'}=$formname;
 9047:     $metadatafields{'creationdate'}=time;
 9048:     $metadatafields{'lastrevisiondate'}=time;
 9049:     $metadatafields{'copyright'}='public';
 9050:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 9051:                                          $env{'user.domain'};
 9052:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 9053:     $metadatafields{'domain'}=$dom;
 9054:     {
 9055:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 9056:         my $mfh;
 9057:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 9058:             foreach (sort(keys(%metadatafields))) {
 9059:                 unless ($_=~/\./) {
 9060:                     my $unikey=$_;
 9061:                     $unikey=~/^([A-Za-z]+)/;
 9062:                     my $tag=$1;
 9063:                     $tag=~tr/A-Z/a-z/;
 9064:                     print $mfh "\n\<$tag";
 9065:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 9066:                         my $value=$metadatafields{$unikey.'.'.$_};
 9067:                         $value=~s/\"/\'\'/g;
 9068:                         print $mfh ' '.$_.'="'.$value.'"';
 9069:                     }
 9070:                     print $mfh '>'.
 9071:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 9072:                             .'</'.$tag.'>';
 9073:                 }
 9074:             }
 9075:             $output = 'ok';
 9076:             print $logfile "\nWrote metadata";
 9077:             close($mfh);
 9078:         } else {
 9079:             print $logfile "\nFailed to open metadata file";
 9080:             $output = &mt('Could not write metadata');
 9081:         }
 9082:     }
 9083:     return $output;
 9084: }
 9085: 
 9086: sub notifysubscribed {
 9087:     foreach my $targetsource (@{$modified_urls}){
 9088:         next unless (ref($targetsource) eq 'ARRAY');
 9089:         my ($target,$source)=@{$targetsource};
 9090:         if ($source ne '') {
 9091:             if (open(my $logfh,">>",$source.'.log')) {
 9092:                 print $logfh "\nCleanup phase: Notifications\n";
 9093:                 my @subscribed=&subscribed_hosts($target);
 9094:                 foreach my $subhost (@subscribed) {
 9095:                     print $logfh "\nNotifying host ".$subhost.':';
 9096:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 9097:                     print $logfh $reply;
 9098:                 }
 9099:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 9100:                 foreach my $subhost (@subscribedmeta) {
 9101:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 9102:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 9103:                                                         $subhost);
 9104:                     print $logfh $reply;
 9105:                 }
 9106:                 print $logfh "\n============ Done ============\n";
 9107:                 close($logfh);
 9108:             }
 9109:         }
 9110:     }
 9111:     return OK;
 9112: }
 9113: 
 9114: sub subscribed_hosts {
 9115:     my ($target) = @_;
 9116:     my @subscribed;
 9117:     if (open(my $fh,"<","$target.subscription")) {
 9118:         while (my $subline=<$fh>) {
 9119:             if ($subline =~ /^($match_lonid):/) {
 9120:                 my $host = $1;
 9121:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 9122:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 9123:                         push(@subscribed,$host);
 9124:                     }
 9125:                 }
 9126:             }
 9127:         }
 9128:     }
 9129:     return @subscribed;
 9130: }
 9131: 
 9132: sub check_switchserver {
 9133:     my ($dom,$confname) = @_;
 9134:     my ($allowed,$switchserver);
 9135:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 9136:     if ($home eq 'no_host') {
 9137:         $home = &Apache::lonnet::domain($dom,'primary');
 9138:     }
 9139:     my @ids=&Apache::lonnet::current_machine_ids();
 9140:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 9141:     if (!$allowed) {
 9142: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 9143:     }
 9144:     return $switchserver;
 9145: }
 9146: 
 9147: sub modify_quotas {
 9148:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9149:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 9150:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 9151:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 9152:         $validationfieldsref);
 9153:     if ($action eq 'quotas') {
 9154:         $context = 'tools'; 
 9155:     } else {
 9156:         $context = $action;
 9157:     }
 9158:     if ($context eq 'requestcourses') {
 9159:         @usertools = ('official','unofficial','community','textbook');
 9160:         @options =('norequest','approval','validate','autolimit');
 9161:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 9162:         %titles = &courserequest_titles();
 9163:         $toolregexp = join('|',@usertools);
 9164:         %conditions = &courserequest_conditions();
 9165:         $confname = $dom.'-domainconfig';
 9166:         my $servadm = $r->dir_config('lonAdmEMail');
 9167:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9168:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 9169:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 9170:     } elsif ($context eq 'requestauthor') {
 9171:         @usertools = ('author');
 9172:         %titles = &authorrequest_titles();
 9173:     } else {
 9174:         @usertools = ('aboutme','blog','webdav','portfolio');
 9175:         %titles = &tool_titles();
 9176:     }
 9177:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9178:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9179:     foreach my $key (keys(%env)) {
 9180:         if ($context eq 'requestcourses') {
 9181:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9182:                 my $item = $1;
 9183:                 my $type = $2;
 9184:                 if ($type =~ /^limit_(.+)/) {
 9185:                     $limithash{$item}{$1} = $env{$key};
 9186:                 } else {
 9187:                     $confhash{$item}{$type} = $env{$key};
 9188:                 }
 9189:             }
 9190:         } elsif ($context eq 'requestauthor') {
 9191:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 9192:                 $confhash{$1} = $env{$key};
 9193:             }
 9194:         } else {
 9195:             if ($key =~ /^form\.quota_(.+)$/) {
 9196:                 $confhash{'defaultquota'}{$1} = $env{$key};
 9197:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 9198:                 $confhash{'authorquota'}{$1} = $env{$key};
 9199:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 9200:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 9201:             }
 9202:         }
 9203:     }
 9204:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9205:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 9206:         @approvalnotify = sort(@approvalnotify);
 9207:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 9208:         my @crstypes = ('official','unofficial','community','textbook');
 9209:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 9210:         foreach my $type (@hasuniquecode) {
 9211:             if (grep(/^\Q$type\E$/,@crstypes)) {
 9212:                 $confhash{'uniquecode'}{$type} = 1;
 9213:             }
 9214:         }
 9215:         my (%newbook,%allpos);
 9216:         if ($context eq 'requestcourses') {
 9217:             foreach my $type ('textbooks','templates') {
 9218:                 @{$allpos{$type}} = (); 
 9219:                 my $invalid;
 9220:                 if ($type eq 'textbooks') {
 9221:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 9222:                 } else {
 9223:                     $invalid = &mt('Invalid LON-CAPA course for template');
 9224:                 }
 9225:                 if ($env{'form.'.$type.'_addbook'}) {
 9226:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 9227:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 9228:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 9229:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 9230:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9231:                         } else {
 9232:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 9233:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 9234:                             $position =~ s/\D+//g;
 9235:                             if ($position ne '') {
 9236:                                 $allpos{$type}[$position] = $newbook{$type};
 9237:                             }
 9238:                         }
 9239:                     } else {
 9240:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9241:                     }
 9242:                 }
 9243:             } 
 9244:         }
 9245:         if (ref($domconfig{$action}) eq 'HASH') {
 9246:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 9247:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 9248:                     $changes{'notify'}{'approval'} = 1;
 9249:                 }
 9250:             } else {
 9251:                 if ($confhash{'notify'}{'approval'}) {
 9252:                     $changes{'notify'}{'approval'} = 1;
 9253:                 }
 9254:             }
 9255:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 9256:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9257:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 9258:                         unless ($confhash{'uniquecode'}{$crstype}) {
 9259:                             $changes{'uniquecode'} = 1;
 9260:                         }
 9261:                     }
 9262:                     unless ($changes{'uniquecode'}) {
 9263:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 9264:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 9265:                                 $changes{'uniquecode'} = 1;
 9266:                             }
 9267:                         }
 9268:                     }
 9269:                } else {
 9270:                    $changes{'uniquecode'} = 1;
 9271:                }
 9272:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 9273:                 $changes{'uniquecode'} = 1;
 9274:             }
 9275:             if ($context eq 'requestcourses') {
 9276:                 foreach my $type ('textbooks','templates') {
 9277:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9278:                         my %deletions;
 9279:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 9280:                         if (@todelete) {
 9281:                             map { $deletions{$_} = 1; } @todelete;
 9282:                         }
 9283:                         my %imgdeletions;
 9284:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 9285:                         if (@todeleteimages) {
 9286:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9287:                         }
 9288:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 9289:                         for (my $i=0; $i<=$maxnum; $i++) {
 9290:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 9291:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 9292:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 9293:                                 if ($deletions{$key}) {
 9294:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 9295:                                         #FIXME need to obsolete item in RES space
 9296:                                     }
 9297:                                     next;
 9298:                                 } else {
 9299:                                     my $newpos = $env{'form.'.$itemid};
 9300:                                     $newpos =~ s/\D+//g;
 9301:                                     foreach my $item ('subject','title','publisher','author') {
 9302:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9303:                                                  ($type eq 'templates'));
 9304:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 9305:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 9306:                                             $changes{$type}{$key} = 1;
 9307:                                         }
 9308:                                     }
 9309:                                     $allpos{$type}[$newpos] = $key;
 9310:                                 }
 9311:                                 if ($imgdeletions{$key}) {
 9312:                                     $changes{$type}{$key} = 1;
 9313:                                     #FIXME need to obsolete item in RES space
 9314:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 9315:                                     my ($cdom,$cnum) = split(/_/,$key);
 9316:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9317:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9318:                                     } else {
 9319:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 9320:                                                                                       $cdom,$cnum,$type,$configuserok,
 9321:                                                                                       $switchserver,$author_ok);
 9322:                                         if ($imgurl) {
 9323:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 9324:                                             $changes{$type}{$key} = 1; 
 9325:                                         }
 9326:                                         if ($error) {
 9327:                                             &Apache::lonnet::logthis($error);
 9328:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9329:                                         }
 9330:                                     } 
 9331:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 9332:                                     $confhash{$type}{$key}{'image'} = 
 9333:                                         $domconfig{$action}{$type}{$key}{'image'};
 9334:                                 }
 9335:                             }
 9336:                         }
 9337:                     }
 9338:                 }
 9339:             }
 9340:         } else {
 9341:             if ($confhash{'notify'}{'approval'}) {
 9342:                 $changes{'notify'}{'approval'} = 1;
 9343:             }
 9344:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 9345:                 $changes{'uniquecode'} = 1;
 9346:             }
 9347:         }
 9348:         if ($context eq 'requestcourses') {
 9349:             foreach my $type ('textbooks','templates') {
 9350:                 if ($newbook{$type}) {
 9351:                     $changes{$type}{$newbook{$type}} = 1;
 9352:                     foreach my $item ('subject','title','publisher','author') {
 9353:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9354:                                  ($type eq 'template'));
 9355:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 9356:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 9357:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 9358:                         }
 9359:                     }
 9360:                     if ($type eq 'textbooks') {
 9361:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 9362:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 9363:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9364:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9365:                             } else {
 9366:                                 my ($imageurl,$error) =
 9367:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 9368:                                                             $configuserok,$switchserver,$author_ok);
 9369:                                 if ($imageurl) {
 9370:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 9371:                                 }
 9372:                                 if ($error) {
 9373:                                     &Apache::lonnet::logthis($error);
 9374:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9375:                                 }
 9376:                             }
 9377:                         }
 9378:                     }
 9379:                 }
 9380:                 if (@{$allpos{$type}} > 0) {
 9381:                     my $idx = 0;
 9382:                     foreach my $item (@{$allpos{$type}}) {
 9383:                         if ($item ne '') {
 9384:                             $confhash{$type}{$item}{'order'} = $idx;
 9385:                             if (ref($domconfig{$action}) eq 'HASH') {
 9386:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9387:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 9388:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 9389:                                             $changes{$type}{$item} = 1;
 9390:                                         }
 9391:                                     }
 9392:                                 }
 9393:                             }
 9394:                             $idx ++;
 9395:                         }
 9396:                     }
 9397:                 }
 9398:             }
 9399:             if (ref($validationitemsref) eq 'ARRAY') {
 9400:                 foreach my $item (@{$validationitemsref}) {
 9401:                     if ($item eq 'fields') {
 9402:                         my @changed;
 9403:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9404:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9405:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9406:                         }
 9407:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9408:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9409:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9410:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9411:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9412:                                 } else {
 9413:                                     @changed = @{$confhash{'validation'}{$item}};
 9414:                                 }
 9415:                             } else {
 9416:                                 @changed = @{$confhash{'validation'}{$item}};
 9417:                             }
 9418:                         } else {
 9419:                             @changed = @{$confhash{'validation'}{$item}};
 9420:                         }
 9421:                         if (@changed) {
 9422:                             if ($confhash{'validation'}{$item}) {
 9423:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9424:                             } else {
 9425:                                 $changes{'validation'}{$item} = &mt('None');
 9426:                             }
 9427:                         }
 9428:                     } else {
 9429:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9430:                         if ($item eq 'markup') {
 9431:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9432:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9433:                             }
 9434:                         }
 9435:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9436:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9437:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9438:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9439:                                 }
 9440:                             } else {
 9441:                                 if ($confhash{'validation'}{$item} ne '') {
 9442:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9443:                                 }
 9444:                             }
 9445:                         } else {
 9446:                             if ($confhash{'validation'}{$item} ne '') {
 9447:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9448:                             }
 9449:                         }
 9450:                     }
 9451:                 }
 9452:             }
 9453:             if ($env{'form.validationdc'}) {
 9454:                 my $newval = $env{'form.validationdc'};
 9455:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9456:                 if (exists($domcoords{$newval})) {
 9457:                     $confhash{'validation'}{'dc'} = $newval;
 9458:                 }
 9459:             }
 9460:             if (ref($confhash{'validation'}) eq 'HASH') {
 9461:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9462:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9463:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9464:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9465:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9466:                                     $changes{'validation'}{'dc'} = &mt('None');
 9467:                                 } else {
 9468:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9469:                                 }
 9470:                             }
 9471:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9472:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9473:                         }
 9474:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9475:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9476:                     }
 9477:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9478:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9479:                 }
 9480:             } else {
 9481:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9482:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9483:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9484:                             $changes{'validation'}{'dc'} = &mt('None');
 9485:                         }
 9486:                     }
 9487:                 }
 9488:             }
 9489:         }
 9490:     } else {
 9491:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9492:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9493:     }
 9494:     foreach my $item (@usertools) {
 9495:         foreach my $type (@{$types},'default','_LC_adv') {
 9496:             my $unset; 
 9497:             if ($context eq 'requestcourses') {
 9498:                 $unset = '0';
 9499:                 if ($type eq '_LC_adv') {
 9500:                     $unset = '';
 9501:                 }
 9502:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9503:                     $confhash{$item}{$type} .= '=';
 9504:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9505:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9506:                     }
 9507:                 }
 9508:             } elsif ($context eq 'requestauthor') {
 9509:                 $unset = '0';
 9510:                 if ($type eq '_LC_adv') {
 9511:                     $unset = '';
 9512:                 }
 9513:             } else {
 9514:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9515:                     $confhash{$item}{$type} = 1;
 9516:                 } else {
 9517:                     $confhash{$item}{$type} = 0;
 9518:                 }
 9519:             }
 9520:             if (ref($domconfig{$action}) eq 'HASH') {
 9521:                 if ($action eq 'requestauthor') {
 9522:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9523:                         $changes{$type} = 1;
 9524:                     }
 9525:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9526:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9527:                         $changes{$item}{$type} = 1;
 9528:                     }
 9529:                 } else {
 9530:                     if ($context eq 'requestcourses') {
 9531:                         if ($confhash{$item}{$type} ne $unset) {
 9532:                             $changes{$item}{$type} = 1;
 9533:                         }
 9534:                     } else {
 9535:                         if (!$confhash{$item}{$type}) {
 9536:                             $changes{$item}{$type} = 1;
 9537:                         }
 9538:                     }
 9539:                 }
 9540:             } else {
 9541:                 if ($context eq 'requestcourses') {
 9542:                     if ($confhash{$item}{$type} ne $unset) {
 9543:                         $changes{$item}{$type} = 1;
 9544:                     }
 9545:                 } elsif ($context eq 'requestauthor') {
 9546:                     if ($confhash{$type} ne $unset) {
 9547:                         $changes{$type} = 1;
 9548:                     }
 9549:                 } else {
 9550:                     if (!$confhash{$item}{$type}) {
 9551:                         $changes{$item}{$type} = 1;
 9552:                     }
 9553:                 }
 9554:             }
 9555:         }
 9556:     }
 9557:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9558:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9559:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9560:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9561:                     if (exists($confhash{'defaultquota'}{$key})) {
 9562:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9563:                             $changes{'defaultquota'}{$key} = 1;
 9564:                         }
 9565:                     } else {
 9566:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9567:                     }
 9568:                 }
 9569:             } else {
 9570:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9571:                     if (exists($confhash{'defaultquota'}{$key})) {
 9572:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9573:                             $changes{'defaultquota'}{$key} = 1;
 9574:                         }
 9575:                     } else {
 9576:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9577:                     }
 9578:                 }
 9579:             }
 9580:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9581:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9582:                     if (exists($confhash{'authorquota'}{$key})) {
 9583:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9584:                             $changes{'authorquota'}{$key} = 1;
 9585:                         }
 9586:                     } else {
 9587:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9588:                     }
 9589:                 }
 9590:             }
 9591:         }
 9592:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9593:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9594:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9595:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9596:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9597:                             $changes{'defaultquota'}{$key} = 1;
 9598:                         }
 9599:                     } else {
 9600:                         if (!exists($domconfig{'quotas'}{$key})) {
 9601:                             $changes{'defaultquota'}{$key} = 1;
 9602:                         }
 9603:                     }
 9604:                 } else {
 9605:                     $changes{'defaultquota'}{$key} = 1;
 9606:                 }
 9607:             }
 9608:         }
 9609:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9610:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9611:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9612:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9613:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9614:                             $changes{'authorquota'}{$key} = 1;
 9615:                         }
 9616:                     } else {
 9617:                         $changes{'authorquota'}{$key} = 1;
 9618:                     }
 9619:                 } else {
 9620:                     $changes{'authorquota'}{$key} = 1;
 9621:                 }
 9622:             }
 9623:         }
 9624:     }
 9625: 
 9626:     if ($context eq 'requestauthor') {
 9627:         $domdefaults{'requestauthor'} = \%confhash;
 9628:     } else {
 9629:         foreach my $key (keys(%confhash)) {
 9630:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9631:                 $domdefaults{$key} = $confhash{$key};
 9632:             }
 9633:         }
 9634:     }
 9635: 
 9636:     my %quotahash = (
 9637:                       $action => { %confhash }
 9638:                     );
 9639:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9640:                                              $dom);
 9641:     if ($putresult eq 'ok') {
 9642:         if (keys(%changes) > 0) {
 9643:             my $cachetime = 24*60*60;
 9644:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9645:             if (ref($lastactref) eq 'HASH') {
 9646:                 $lastactref->{'domdefaults'} = 1;
 9647:             }
 9648:             $resulttext = &mt('Changes made:').'<ul>';
 9649:             unless (($context eq 'requestcourses') ||
 9650:                     ($context eq 'requestauthor')) {
 9651:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9652:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9653:                     foreach my $type (@{$types},'default') {
 9654:                         if (defined($changes{'defaultquota'}{$type})) {
 9655:                             my $typetitle = $usertypes->{$type};
 9656:                             if ($type eq 'default') {
 9657:                                 $typetitle = $othertitle;
 9658:                             }
 9659:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9660:                         }
 9661:                     }
 9662:                     $resulttext .= '</ul></li>';
 9663:                 }
 9664:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9665:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9666:                     foreach my $type (@{$types},'default') {
 9667:                         if (defined($changes{'authorquota'}{$type})) {
 9668:                             my $typetitle = $usertypes->{$type};
 9669:                             if ($type eq 'default') {
 9670:                                 $typetitle = $othertitle;
 9671:                             }
 9672:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9673:                         }
 9674:                     }
 9675:                     $resulttext .= '</ul></li>';
 9676:                 }
 9677:             }
 9678:             my %newenv;
 9679:             foreach my $item (@usertools) {
 9680:                 my (%haschgs,%inconf);
 9681:                 if ($context eq 'requestauthor') {
 9682:                     %haschgs = %changes;
 9683:                     %inconf = %confhash;
 9684:                 } else {
 9685:                     if (ref($changes{$item}) eq 'HASH') {
 9686:                         %haschgs = %{$changes{$item}};
 9687:                     }
 9688:                     if (ref($confhash{$item}) eq 'HASH') {
 9689:                         %inconf = %{$confhash{$item}};
 9690:                     }
 9691:                 }
 9692:                 if (keys(%haschgs) > 0) {
 9693:                     my $newacc = 
 9694:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9695:                                                           $env{'user.domain'},
 9696:                                                           $item,'reload',$context);
 9697:                     if (($context eq 'requestcourses') ||
 9698:                         ($context eq 'requestauthor')) {
 9699:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9700:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9701:                         }
 9702:                     } else {
 9703:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9704:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9705:                         }
 9706:                     }
 9707:                     unless ($context eq 'requestauthor') {
 9708:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9709:                     }
 9710:                     foreach my $type (@{$types},'default','_LC_adv') {
 9711:                         if ($haschgs{$type}) {
 9712:                             my $typetitle = $usertypes->{$type};
 9713:                             if ($type eq 'default') {
 9714:                                 $typetitle = $othertitle;
 9715:                             } elsif ($type eq '_LC_adv') {
 9716:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9717:                             }
 9718:                             if ($inconf{$type}) {
 9719:                                 if ($context eq 'requestcourses') {
 9720:                                     my $cond;
 9721:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9722:                                         if ($1 eq '') {
 9723:                                             $cond = &mt('(Automatic processing of any request).');
 9724:                                         } else {
 9725:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9726:                                         }
 9727:                                     } else { 
 9728:                                         $cond = $conditions{$inconf{$type}};
 9729:                                     }
 9730:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9731:                                 } elsif ($context eq 'requestauthor') {
 9732:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9733:                                                              $titles{$inconf{$type}},$typetitle);
 9734: 
 9735:                                 } else {
 9736:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9737:                                 }
 9738:                             } else {
 9739:                                 if ($type eq '_LC_adv') {
 9740:                                     if ($inconf{$type} eq '0') {
 9741:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9742:                                     } else { 
 9743:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9744:                                     }
 9745:                                 } else {
 9746:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9747:                                 }
 9748:                             }
 9749:                         }
 9750:                     }
 9751:                     unless ($context eq 'requestauthor') {
 9752:                         $resulttext .= '</ul></li>';
 9753:                     }
 9754:                 }
 9755:             }
 9756:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9757:                 if (ref($changes{'notify'}) eq 'HASH') {
 9758:                     if ($changes{'notify'}{'approval'}) {
 9759:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9760:                             if ($confhash{'notify'}{'approval'}) {
 9761:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9762:                             } else {
 9763:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9764:                             }
 9765:                         }
 9766:                     }
 9767:                 }
 9768:             }
 9769:             if ($action eq 'requestcourses') {
 9770:                 my @offon = ('off','on');
 9771:                 if ($changes{'uniquecode'}) {
 9772:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9773:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9774:                         $resulttext .= '<li>'.
 9775:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9776:                                        '</li>';
 9777:                     } else {
 9778:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9779:                                        '</li>';
 9780:                     }
 9781:                 }
 9782:                 foreach my $type ('textbooks','templates') {
 9783:                     if (ref($changes{$type}) eq 'HASH') {
 9784:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9785:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9786:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9787:                             my $coursetitle = $coursehash{'description'};
 9788:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9789:                             $resulttext .= '<li>';
 9790:                             foreach my $item ('subject','title','publisher','author') {
 9791:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9792:                                          ($type eq 'templates'));
 9793:                                 my $name = $item.':';
 9794:                                 $name =~ s/^(\w)/\U$1/;
 9795:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9796:                             }
 9797:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9798:                             if ($type eq 'textbooks') {
 9799:                                 if ($confhash{$type}{$key}{'image'}) {
 9800:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9801:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9802:                                                    ' alt="Textbook cover" />').'<br />';
 9803:                                 }
 9804:                             }
 9805:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9806:                         }
 9807:                         $resulttext .= '</ul></li>';
 9808:                     }
 9809:                 }
 9810:                 if (ref($changes{'validation'}) eq 'HASH') {
 9811:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9812:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9813:                         foreach my $item (@{$validationitemsref}) {
 9814:                             if (exists($changes{'validation'}{$item})) {
 9815:                                 if ($item eq 'markup') {
 9816:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9817:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9818:                                 } else {
 9819:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9820:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9821:                                 }
 9822:                             }
 9823:                         }
 9824:                         if (exists($changes{'validation'}{'dc'})) {
 9825:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9826:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9827:                         }
 9828:                     }
 9829:                 }
 9830:             }
 9831:             $resulttext .= '</ul>';
 9832:             if (keys(%newenv)) {
 9833:                 &Apache::lonnet::appenv(\%newenv);
 9834:             }
 9835:         } else {
 9836:             if ($context eq 'requestcourses') {
 9837:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9838:             } elsif ($context eq 'requestauthor') {
 9839:                 $resulttext = &mt('No changes made to rights to request author space.');
 9840:             } else {
 9841:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9842:             }
 9843:         }
 9844:     } else {
 9845:         $resulttext = '<span class="LC_error">'.
 9846: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9847:     }
 9848:     if ($errors) {
 9849:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9850:                        '<ul>'.$errors.'</ul></p>';
 9851:     }
 9852:     return $resulttext;
 9853: }
 9854: 
 9855: sub process_textbook_image {
 9856:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9857:     my $filename = $env{'form.'.$caller.'.filename'};
 9858:     my ($error,$url);
 9859:     my ($width,$height) = (50,50);
 9860:     if ($configuserok eq 'ok') {
 9861:         if ($switchserver) {
 9862:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9863:                          $switchserver);
 9864:         } elsif ($author_ok eq 'ok') {
 9865:             my ($result,$imageurl) =
 9866:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9867:                              "$type/$cdom/$cnum/cover",$width,$height);
 9868:             if ($result eq 'ok') {
 9869:                 $url = $imageurl;
 9870:             } else {
 9871:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9872:             }
 9873:         } else {
 9874:             $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);
 9875:         }
 9876:     } else {
 9877:         $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);
 9878:     }
 9879:     return ($url,$error);
 9880: }
 9881: 
 9882: sub modify_autoenroll {
 9883:     my ($dom,$lastactref,%domconfig) = @_;
 9884:     my ($resulttext,%changes);
 9885:     my %currautoenroll;
 9886:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9887:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9888:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9889:         }
 9890:     }
 9891:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9892:     my %title = ( run => 'Auto-enrollment active',
 9893:                   sender => 'Sender for notification messages',
 9894:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9895:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9896:     my @offon = ('off','on');
 9897:     my $sender_uname = $env{'form.sender_uname'};
 9898:     my $sender_domain = $env{'form.sender_domain'};
 9899:     if ($sender_domain eq '') {
 9900:         $sender_uname = '';
 9901:     } elsif ($sender_uname eq '') {
 9902:         $sender_domain = '';
 9903:     }
 9904:     my $coowners = $env{'form.autoassign_coowners'};
 9905:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9906:     $failsafe =~ s{^\s+|\s+$}{}g;
 9907:     if ($failsafe =~ /\D/) {
 9908:         undef($failsafe);
 9909:     }
 9910:     my %autoenrollhash =  (
 9911:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9912:                                        'sender_uname' => $sender_uname,
 9913:                                        'sender_domain' => $sender_domain,
 9914:                                        'co-owners' => $coowners,
 9915:                                        'autofailsafe' => $failsafe,
 9916:                                 }
 9917:                      );
 9918:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9919:                                              $dom);
 9920:     if ($putresult eq 'ok') {
 9921:         if (exists($currautoenroll{'run'})) {
 9922:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9923:                  $changes{'run'} = 1;
 9924:              }
 9925:         } elsif ($autorun) {
 9926:             if ($env{'form.autoenroll_run'} ne '1') {
 9927:                  $changes{'run'} = 1;
 9928:             }
 9929:         }
 9930:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9931:             $changes{'sender'} = 1;
 9932:         }
 9933:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9934:             $changes{'sender'} = 1;
 9935:         }
 9936:         if ($currautoenroll{'co-owners'} ne '') {
 9937:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9938:                 $changes{'coowners'} = 1;
 9939:             }
 9940:         } elsif ($coowners) {
 9941:             $changes{'coowners'} = 1;
 9942:         }
 9943:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9944:             $changes{'autofailsafe'} = 1;
 9945:         }
 9946:         if (keys(%changes) > 0) {
 9947:             $resulttext = &mt('Changes made:').'<ul>';
 9948:             if ($changes{'run'}) {
 9949:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9950:             }
 9951:             if ($changes{'sender'}) {
 9952:                 if ($sender_uname eq '' || $sender_domain eq '') {
 9953:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 9954:                 } else {
 9955:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 9956:                 }
 9957:             }
 9958:             if ($changes{'coowners'}) {
 9959:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 9960:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9961:                 if (ref($lastactref) eq 'HASH') {
 9962:                     $lastactref->{'domainconfig'} = 1;
 9963:                 }
 9964:             }
 9965:             if ($changes{'autofailsafe'}) {
 9966:                 if ($failsafe ne '') {
 9967:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
 9968:                 } else {
 9969:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
 9970:                 }
 9971:                 &Apache::lonnet::get_domain_defaults($dom,1);
 9972:                 if (ref($lastactref) eq 'HASH') {
 9973:                     $lastactref->{'domdefaults'} = 1;
 9974:                 }
 9975:             }
 9976:             $resulttext .= '</ul>';
 9977:         } else {
 9978:             $resulttext = &mt('No changes made to auto-enrollment settings');
 9979:         }
 9980:     } else {
 9981:         $resulttext = '<span class="LC_error">'.
 9982: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9983:     }
 9984:     return $resulttext;
 9985: }
 9986: 
 9987: sub modify_autoupdate {
 9988:     my ($dom,%domconfig) = @_;
 9989:     my ($resulttext,%currautoupdate,%fields,%changes);
 9990:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 9991:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 9992:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 9993:         }
 9994:     }
 9995:     my @offon = ('off','on');
 9996:     my %title = &Apache::lonlocal::texthash (
 9997:                    run => 'Auto-update:',
 9998:                    classlists => 'Updates to user information in classlists?'
 9999:                 );
10000:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10001:     my %fieldtitles = &Apache::lonlocal::texthash (
10002:                         id => 'Student/Employee ID',
10003:                         permanentemail => 'E-mail address',
10004:                         lastname => 'Last Name',
10005:                         firstname => 'First Name',
10006:                         middlename => 'Middle Name',
10007:                         generation => 'Generation',
10008:                       );
10009:     $othertitle = &mt('All users');
10010:     if (keys(%{$usertypes}) >  0) {
10011:         $othertitle = &mt('Other users');
10012:     }
10013:     foreach my $key (keys(%env)) {
10014:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10015:             my ($usertype,$item) = ($1,$2);
10016:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10017:                 if ($usertype eq 'default') {   
10018:                     push(@{$fields{$1}},$2);
10019:                 } elsif (ref($types) eq 'ARRAY') {
10020:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10021:                         push(@{$fields{$1}},$2);
10022:                     }
10023:                 }
10024:             }
10025:         }
10026:     }
10027:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10028:     @lockablenames = sort(@lockablenames);
10029:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10030:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10031:         if (@changed) {
10032:             $changes{'lockablenames'} = 1;
10033:         }
10034:     } else {
10035:         if (@lockablenames) {
10036:             $changes{'lockablenames'} = 1;
10037:         }
10038:     }
10039:     my %updatehash = (
10040:                       autoupdate => { run => $env{'form.autoupdate_run'},
10041:                                       classlists => $env{'form.classlists'},
10042:                                       fields => {%fields},
10043:                                       lockablenames => \@lockablenames,
10044:                                     }
10045:                      );
10046:     foreach my $key (keys(%currautoupdate)) {
10047:         if (($key eq 'run') || ($key eq 'classlists')) {
10048:             if (exists($updatehash{autoupdate}{$key})) {
10049:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10050:                     $changes{$key} = 1;
10051:                 }
10052:             }
10053:         } elsif ($key eq 'fields') {
10054:             if (ref($currautoupdate{$key}) eq 'HASH') {
10055:                 foreach my $item (@{$types},'default') {
10056:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10057:                         my $change = 0;
10058:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10059:                             if (!exists($fields{$item})) {
10060:                                 $change = 1;
10061:                                 last;
10062:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10063:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10064:                                     $change = 1;
10065:                                     last;
10066:                                 }
10067:                             }
10068:                         }
10069:                         if ($change) {
10070:                             push(@{$changes{$key}},$item);
10071:                         }
10072:                     } 
10073:                 }
10074:             }
10075:         } elsif ($key eq 'lockablenames') {
10076:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10077:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10078:                 if (@changed) {
10079:                     $changes{'lockablenames'} = 1;
10080:                 }
10081:             } else {
10082:                 if (@lockablenames) {
10083:                     $changes{'lockablenames'} = 1;
10084:                 }
10085:             }
10086:         }
10087:     }
10088:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10089:         if (@lockablenames) {
10090:             $changes{'lockablenames'} = 1;
10091:         }
10092:     }
10093:     foreach my $item (@{$types},'default') {
10094:         if (defined($fields{$item})) {
10095:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10096:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10097:                     my $change = 0;
10098:                     if (ref($fields{$item}) eq 'ARRAY') {
10099:                         foreach my $type (@{$fields{$item}}) {
10100:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10101:                                 $change = 1;
10102:                                 last;
10103:                             }
10104:                         }
10105:                     }
10106:                     if ($change) {
10107:                         push(@{$changes{'fields'}},$item);
10108:                     }
10109:                 } else {
10110:                     push(@{$changes{'fields'}},$item);
10111:                 }
10112:             } else {
10113:                 push(@{$changes{'fields'}},$item);
10114:             }
10115:         }
10116:     }
10117:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10118:                                              $dom);
10119:     if ($putresult eq 'ok') {
10120:         if (keys(%changes) > 0) {
10121:             $resulttext = &mt('Changes made:').'<ul>';
10122:             foreach my $key (sort(keys(%changes))) {
10123:                 if ($key eq 'lockablenames') {
10124:                     $resulttext .= '<li>';
10125:                     if (@lockablenames) {
10126:                         $usertypes->{'default'} = $othertitle;
10127:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10128:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10129:                     } else {
10130:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10131:                     }
10132:                     $resulttext .= '</li>';
10133:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10134:                     foreach my $item (@{$changes{$key}}) {
10135:                         my @newvalues;
10136:                         foreach my $type (@{$fields{$item}}) {
10137:                             push(@newvalues,$fieldtitles{$type});
10138:                         }
10139:                         my $newvaluestr;
10140:                         if (@newvalues > 0) {
10141:                             $newvaluestr = join(', ',@newvalues);
10142:                         } else {
10143:                             $newvaluestr = &mt('none');
10144:                         }
10145:                         if ($item eq 'default') {
10146:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10147:                         } else {
10148:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10149:                         }
10150:                     }
10151:                 } else {
10152:                     my $newvalue;
10153:                     if ($key eq 'run') {
10154:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10155:                     } else {
10156:                         $newvalue = $offon[$env{'form.'.$key}];
10157:                     }
10158:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10159:                 }
10160:             }
10161:             $resulttext .= '</ul>';
10162:         } else {
10163:             $resulttext = &mt('No changes made to autoupdates');
10164:         }
10165:     } else {
10166:         $resulttext = '<span class="LC_error">'.
10167: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10168:     }
10169:     return $resulttext;
10170: }
10171: 
10172: sub modify_autocreate {
10173:     my ($dom,%domconfig) = @_;
10174:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10175:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10176:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10177:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10178:         }
10179:     }
10180:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10181:                  req => 'Auto-creation of validated requests for official courses',
10182:                  xmldc => 'Identity of course creator of courses from XML files',
10183:                );
10184:     my @types = ('xml','req');
10185:     foreach my $item (@types) {
10186:         $newvals{$item} = $env{'form.autocreate_'.$item};
10187:         $newvals{$item} =~ s/\D//g;
10188:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10189:     }
10190:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10191:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10192:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10193:         $newvals{'xmldc'} = '';
10194:     } 
10195:     %autocreatehash =  (
10196:                         autocreate => { xml => $newvals{'xml'},
10197:                                         req => $newvals{'req'},
10198:                                       }
10199:                        );
10200:     if ($newvals{'xmldc'} ne '') {
10201:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10202:     }
10203:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10204:                                              $dom);
10205:     if ($putresult eq 'ok') {
10206:         my @items = @types;
10207:         if ($newvals{'xml'}) {
10208:             push(@items,'xmldc');
10209:         }
10210:         foreach my $item (@items) {
10211:             if (exists($currautocreate{$item})) {
10212:                 if ($currautocreate{$item} ne $newvals{$item}) {
10213:                     $changes{$item} = 1;
10214:                 }
10215:             } elsif ($newvals{$item}) {
10216:                 $changes{$item} = 1;
10217:             }
10218:         }
10219:         if (keys(%changes) > 0) {
10220:             my @offon = ('off','on'); 
10221:             $resulttext = &mt('Changes made:').'<ul>';
10222:             foreach my $item (@types) {
10223:                 if ($changes{$item}) {
10224:                     my $newtxt = $offon[$newvals{$item}];
10225:                     $resulttext .= '<li>'.
10226:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10227:                                        '<b>','</b>').
10228:                                    '</li>';
10229:                 }
10230:             }
10231:             if ($changes{'xmldc'}) {
10232:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10233:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10234:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10235:             }
10236:             $resulttext .= '</ul>';
10237:         } else {
10238:             $resulttext = &mt('No changes made to auto-creation settings');
10239:         }
10240:     } else {
10241:         $resulttext = '<span class="LC_error">'.
10242:             &mt('An error occurred: [_1]',$putresult).'</span>';
10243:     }
10244:     return $resulttext;
10245: }
10246: 
10247: sub modify_directorysrch {
10248:     my ($dom,$lastactref,%domconfig) = @_;
10249:     my ($resulttext,%changes);
10250:     my %currdirsrch;
10251:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10252:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10253:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10254:         }
10255:     }
10256:     my %title = ( available => 'Institutional directory search available',
10257:                   localonly => 'Other domains can search institution',
10258:                   lcavailable => 'LON-CAPA directory search available',
10259:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10260:                   searchby => 'Search types',
10261:                   searchtypes => 'Search latitude');
10262:     my @offon = ('off','on');
10263:     my @otherdoms = ('Yes','No');
10264: 
10265:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10266:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10267:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10268: 
10269:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10270:     if (keys(%{$usertypes}) == 0) {
10271:         @cansearch = ('default');
10272:     } else {
10273:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10274:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10275:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10276:                     push(@{$changes{'cansearch'}},$type);
10277:                 }
10278:             }
10279:             foreach my $type (@cansearch) {
10280:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10281:                     push(@{$changes{'cansearch'}},$type);
10282:                 }
10283:             }
10284:         } else {
10285:             push(@{$changes{'cansearch'}},@cansearch);
10286:         }
10287:     }
10288: 
10289:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10290:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10291:             if (!grep(/^\Q$by\E$/,@searchby)) {
10292:                 push(@{$changes{'searchby'}},$by);
10293:             }
10294:         }
10295:         foreach my $by (@searchby) {
10296:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10297:                 push(@{$changes{'searchby'}},$by);
10298:             }
10299:         }
10300:     } else {
10301:         push(@{$changes{'searchby'}},@searchby);
10302:     }
10303: 
10304:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10305:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10306:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10307:                 push(@{$changes{'searchtypes'}},$type);
10308:             }
10309:         }
10310:         foreach my $type (@searchtypes) {
10311:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10312:                 push(@{$changes{'searchtypes'}},$type);
10313:             }
10314:         }
10315:     } else {
10316:         if (exists($currdirsrch{'searchtypes'})) {
10317:             foreach my $type (@searchtypes) {  
10318:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10319:                     push(@{$changes{'searchtypes'}},$type);
10320:                 }
10321:             }
10322:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10323:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10324:             }   
10325:         } else {
10326:             push(@{$changes{'searchtypes'}},@searchtypes); 
10327:         }
10328:     }
10329: 
10330:     my %dirsrch_hash =  (
10331:             directorysrch => { available => $env{'form.dirsrch_available'},
10332:                                cansearch => \@cansearch,
10333:                                localonly => $env{'form.dirsrch_instlocalonly'},
10334:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10335:                                lcavailable => $env{'form.dirsrch_domavailable'},
10336:                                searchby => \@searchby,
10337:                                searchtypes => \@searchtypes,
10338:                              }
10339:             );
10340:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10341:                                              $dom);
10342:     if ($putresult eq 'ok') {
10343:         if (exists($currdirsrch{'available'})) {
10344:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10345:                  $changes{'available'} = 1;
10346:              }
10347:         } else {
10348:             if ($env{'form.dirsrch_available'} eq '1') {
10349:                 $changes{'available'} = 1;
10350:             }
10351:         }
10352:         if (exists($currdirsrch{'lcavailable'})) {
10353:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10354:                 $changes{'lcavailable'} = 1;
10355:             }
10356:         } else {
10357:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10358:                 $changes{'lcavailable'} = 1;
10359:             }
10360:         }
10361:         if (exists($currdirsrch{'localonly'})) {
10362:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10363:                 $changes{'localonly'} = 1;
10364:             }
10365:         } else {
10366:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10367:                 $changes{'localonly'} = 1;
10368:             }
10369:         }
10370:         if (exists($currdirsrch{'lclocalonly'})) {
10371:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10372:                 $changes{'lclocalonly'} = 1;
10373:             }
10374:         } else {
10375:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10376:                 $changes{'lclocalonly'} = 1;
10377:             }
10378:         }
10379:         if (keys(%changes) > 0) {
10380:             $resulttext = &mt('Changes made:').'<ul>';
10381:             if ($changes{'available'}) {
10382:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10383:             }
10384:             if ($changes{'lcavailable'}) {
10385:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10386:             }
10387:             if ($changes{'localonly'}) {
10388:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10389:             }
10390:             if ($changes{'lclocalonly'}) {
10391:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10392:             }
10393:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10394:                 my $chgtext;
10395:                 if (ref($usertypes) eq 'HASH') {
10396:                     if (keys(%{$usertypes}) > 0) {
10397:                         foreach my $type (@{$types}) {
10398:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10399:                                 $chgtext .= $usertypes->{$type}.'; ';
10400:                             }
10401:                         }
10402:                         if (grep(/^default$/,@cansearch)) {
10403:                             $chgtext .= $othertitle;
10404:                         } else {
10405:                             $chgtext =~ s/\; $//;
10406:                         }
10407:                         $resulttext .=
10408:                             '<li>'.
10409:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10410:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10411:                             '</li>';
10412:                     }
10413:                 }
10414:             }
10415:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10416:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10417:                 my $chgtext;
10418:                 foreach my $type (@{$titleorder}) {
10419:                     if (grep(/^\Q$type\E$/,@searchby)) {
10420:                         if (defined($searchtitles->{$type})) {
10421:                             $chgtext .= $searchtitles->{$type}.'; ';
10422:                         }
10423:                     }
10424:                 }
10425:                 $chgtext =~ s/\; $//;
10426:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10427:             }
10428:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10429:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10430:                 my $chgtext;
10431:                 foreach my $type (@{$srchtypeorder}) {
10432:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10433:                         if (defined($srchtypes_desc->{$type})) {
10434:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10435:                         }
10436:                     }
10437:                 }
10438:                 $chgtext =~ s/\; $//;
10439:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10440:             }
10441:             $resulttext .= '</ul>';
10442:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10443:             if (ref($lastactref) eq 'HASH') {
10444:                 $lastactref->{'directorysrch'} = 1;
10445:             }
10446:         } else {
10447:             $resulttext = &mt('No changes made to directory search settings');
10448:         }
10449:     } else {
10450:         $resulttext = '<span class="LC_error">'.
10451:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10452:     }
10453:     return $resulttext;
10454: }
10455: 
10456: sub modify_contacts {
10457:     my ($dom,$lastactref,%domconfig) = @_;
10458:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10459:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10460:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10461:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10462:         }
10463:     }
10464:     my (%others,%to,%bcc,%includestr,%includeloc);
10465:     my @contacts = ('supportemail','adminemail');
10466:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10467:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
10468:     my @toggles = ('reporterrors','reportupdates');
10469:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10470:     foreach my $type (@mailings) {
10471:         @{$newsetting{$type}} = 
10472:             &Apache::loncommon::get_env_multiple('form.'.$type);
10473:         foreach my $item (@contacts) {
10474:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10475:                 $contacts_hash{contacts}{$type}{$item} = 1;
10476:             } else {
10477:                 $contacts_hash{contacts}{$type}{$item} = 0;
10478:             }
10479:         }
10480:         $others{$type} = $env{'form.'.$type.'_others'};
10481:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10482:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10483:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10484:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10485:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10486:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10487:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10488:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10489:             }
10490:         }
10491:     }
10492:     foreach my $item (@contacts) {
10493:         $to{$item} = $env{'form.'.$item};
10494:         $contacts_hash{'contacts'}{$item} = $to{$item};
10495:     }
10496:     foreach my $item (@toggles) {
10497:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10498:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10499:         }
10500:     }
10501:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10502:         foreach my $field (@{$fields}) {
10503:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10504:                 my $value = $env{'form.helpform_'.$field};
10505:                 $value =~ s/^\s+|\s+$//g;
10506:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10507:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
10508:                     if ($field eq 'screenshot') {
10509:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10510:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10511:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10512:                         }
10513:                     }
10514:                 }
10515:             }
10516:         }
10517:     }
10518:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10519:     my (@statuses,%usertypeshash,@overrides);
10520:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
10521:         @statuses = @{$types};
10522:         if (ref($usertypes) eq 'HASH') {
10523:             %usertypeshash = %{$usertypes};
10524:         }
10525:     }
10526:     if (@statuses) {
10527:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
10528:         foreach my $type (@possoverrides) {
10529:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
10530:                 push(@overrides,$type);
10531:             }
10532:         }
10533:         if (@overrides) {
10534:             foreach my $type (@overrides) {
10535:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
10536:                 foreach my $item (@contacts) {
10537:                     if (grep(/^\Q$item\E$/,@standard)) {
10538:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
10539:                         $newsetting{'override_'.$type}{$item} = 1;
10540:                     } else {
10541:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
10542:                         $newsetting{'override_'.$type}{$item} = 0;
10543:                     }
10544:                 }
10545:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
10546:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10547:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
10548:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
10549:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
10550:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
10551:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
10552:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10553:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
10554:                 }
10555:             }    
10556:         }
10557:     }
10558:     if (keys(%currsetting) > 0) {
10559:         foreach my $item (@contacts) {
10560:             if ($to{$item} ne $currsetting{$item}) {
10561:                 $changes{$item} = 1;
10562:             }
10563:         }
10564:         foreach my $type (@mailings) {
10565:             foreach my $item (@contacts) {
10566:                 if (ref($currsetting{$type}) eq 'HASH') {
10567:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10568:                         push(@{$changes{$type}},$item);
10569:                     }
10570:                 } else {
10571:                     push(@{$changes{$type}},@{$newsetting{$type}});
10572:                 }
10573:             }
10574:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10575:                 push(@{$changes{$type}},'others');
10576:             }
10577:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10578:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10579:                     push(@{$changes{$type}},'bcc'); 
10580:                 }
10581:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10582:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10583:                     push(@{$changes{$type}},'include');
10584:                 }
10585:             }
10586:         }
10587:         if (ref($fields) eq 'ARRAY') {
10588:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10589:                 foreach my $field (@{$fields}) {
10590:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10591:                         push(@{$changes{'helpform'}},$field);
10592:                     }
10593:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10594:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10595:                             push(@{$changes{'helpform'}},'maxsize');
10596:                         }
10597:                     }
10598:                 }
10599:             } else {
10600:                 foreach my $field (@{$fields}) {
10601:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10602:                         push(@{$changes{'helpform'}},$field);
10603:                     }
10604:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10605:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10606:                             push(@{$changes{'helpform'}},'maxsize');
10607:                         }
10608:                     }
10609:                 }
10610:             }
10611:         }
10612:         if (@statuses) {
10613:             if (ref($currsetting{'overrides'}) eq 'HASH') {
10614:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
10615:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
10616:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
10617:                             foreach my $item (@contacts,'bcc','others','include') {
10618:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
10619:                                     push(@{$changes{'overrides'}},$key);
10620:                                     last;
10621:                                 }
10622:                             }
10623:                         } else {
10624:                             push(@{$changes{'overrides'}},$key);
10625:                         }
10626:                     }
10627:                 }
10628:                 foreach my $key (@overrides) {
10629:                     unless (exists($currsetting{'overrides'}{$key})) {
10630:                         push(@{$changes{'overrides'}},$key);
10631:                     }
10632:                 }
10633:             } else {
10634:                 foreach my $key (@overrides) {
10635:                     push(@{$changes{'overrides'}},$key);
10636:                 }
10637:             }
10638:         }
10639:     } else {
10640:         my %default;
10641:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10642:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10643:         $default{'errormail'} = 'adminemail';
10644:         $default{'packagesmail'} = 'adminemail';
10645:         $default{'helpdeskmail'} = 'supportemail';
10646:         $default{'otherdomsmail'} = 'supportemail';
10647:         $default{'lonstatusmail'} = 'adminemail';
10648:         $default{'requestsmail'} = 'adminemail';
10649:         $default{'updatesmail'} = 'adminemail';
10650:         $default{'hostipmail'} = 'adminemail';
10651:         foreach my $item (@contacts) {
10652:            if ($to{$item} ne $default{$item}) {
10653:                $changes{$item} = 1;
10654:            }
10655:         }
10656:         foreach my $type (@mailings) {
10657:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10658:                 push(@{$changes{$type}},@{$newsetting{$type}});
10659:             }
10660:             if ($others{$type} ne '') {
10661:                 push(@{$changes{$type}},'others');
10662:             }
10663:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10664:                 if ($bcc{$type} ne '') {
10665:                     push(@{$changes{$type}},'bcc');
10666:                 }
10667:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10668:                     push(@{$changes{$type}},'include');
10669:                 }
10670:             }
10671:         }
10672:         if (ref($fields) eq 'ARRAY') {
10673:             foreach my $field (@{$fields}) {
10674:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10675:                     push(@{$changes{'helpform'}},$field);
10676:                 }
10677:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10678:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10679:                         push(@{$changes{'helpform'}},'maxsize');
10680:                     }
10681:                 }
10682:             }
10683:         }
10684:     }
10685:     foreach my $item (@toggles) {
10686:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10687:             $changes{$item} = 1;
10688:         } elsif ((!$env{'form.'.$item}) &&
10689:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10690:             $changes{$item} = 1;
10691:         }
10692:     }
10693:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10694:                                              $dom);
10695:     if ($putresult eq 'ok') {
10696:         if (keys(%changes) > 0) {
10697:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10698:             if (ref($lastactref) eq 'HASH') {
10699:                 $lastactref->{'domainconfig'} = 1;
10700:             }
10701:             my ($titles,$short_titles)  = &contact_titles();
10702:             $resulttext = &mt('Changes made:').'<ul>';
10703:             foreach my $item (@contacts) {
10704:                 if ($changes{$item}) {
10705:                     $resulttext .= '<li>'.$titles->{$item}.
10706:                                     &mt(' set to: ').
10707:                                     '<span class="LC_cusr_emph">'.
10708:                                     $to{$item}.'</span></li>';
10709:                 }
10710:             }
10711:             foreach my $type (@mailings) {
10712:                 if (ref($changes{$type}) eq 'ARRAY') {
10713:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10714:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10715:                     } else {
10716:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10717:                     }
10718:                     my @text;
10719:                     foreach my $item (@{$newsetting{$type}}) {
10720:                         push(@text,$short_titles->{$item});
10721:                     }
10722:                     if ($others{$type} ne '') {
10723:                         push(@text,$others{$type});
10724:                     }
10725:                     if (@text) {
10726:                         $resulttext .= '<span class="LC_cusr_emph">'.
10727:                                        join(', ',@text).'</span>';
10728:                     }
10729:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10730:                         if ($bcc{$type} ne '') {
10731:                             my $bcctext;
10732:                             if (@text) {
10733:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10734:                             } else {
10735:                                 $bcctext = '(Bcc)';
10736:                             }
10737:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10738:                         } elsif (!@text) {
10739:                             $resulttext .= &mt('No one');
10740:                         }
10741:                         if ($includestr{$type} ne '') {
10742:                             if ($includeloc{$type} eq 'b') {
10743:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10744:                             } elsif ($includeloc{$type} eq 's') {
10745:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10746:                             }
10747:                         }
10748:                     } elsif (!@text) {
10749:                         $resulttext .= &mt('No recipients');
10750:                     }
10751:                     $resulttext .= '</li>';
10752:                 }
10753:             }
10754:             if (ref($changes{'overrides'}) eq 'ARRAY') {
10755:                 my @deletions;
10756:                 foreach my $type (@{$changes{'overrides'}}) {
10757:                     if ($usertypeshash{$type}) {
10758:                         if (grep(/^\Q$type\E/,@overrides)) {
10759:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
10760:                                                       $usertypeshash{$type}).'<ul><li>';
10761:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
10762:                                 my @text;
10763:                                 foreach my $item (@contacts) {
10764:                                     if ($newsetting{'override_'.$type}{$item}) {
10765:                                         push(@text,$short_titles->{$item});
10766:                                     }
10767:                                 }
10768:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
10769:                                     push(@text,$newsetting{'override_'.$type}{'others'});
10770:                                 }
10771: 
10772:                                 if (@text) {
10773:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
10774:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
10775:                                 }
10776:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
10777:                                     my $bcctext;
10778:                                     if (@text) {
10779:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
10780:                                     } else {
10781:                                         $bcctext = '(Bcc)';
10782:                                     }
10783:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
10784:                                 } elsif (!@text) {
10785:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
10786:                                 }
10787:                                 $resulttext .= '</li>';
10788:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
10789:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
10790:                                     if ($loc eq 'b') {
10791:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
10792:                                     } elsif ($loc eq 's') {
10793:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
10794:                                     }
10795:                                 }
10796:                             }
10797:                             $resulttext .= '</li></ul></li>';
10798:                         } else {
10799:                             push(@deletions,$usertypeshash{$type});
10800:                         }
10801:                     }
10802:                 }
10803:                 if (@deletions) {
10804:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
10805:                                               join(', ',@deletions)).'</li>';
10806:                 }
10807:             }
10808:             my @offon = ('off','on');
10809:             if ($changes{'reporterrors'}) {
10810:                 $resulttext .= '<li>'.
10811:                                &mt('E-mail error reports to [_1] set to "'.
10812:                                    $offon[$env{'form.reporterrors'}].'".',
10813:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10814:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10815:                                '</li>';
10816:             }
10817:             if ($changes{'reportupdates'}) {
10818:                 $resulttext .= '<li>'.
10819:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10820:                                     $offon[$env{'form.reportupdates'}].'".',
10821:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10822:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10823:                                 '</li>';
10824:             }
10825:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10826:                 my (@optional,@required,@unused,$maxsizechg);
10827:                 foreach my $field (@{$changes{'helpform'}}) {
10828:                     if ($field eq 'maxsize') {
10829:                         $maxsizechg = 1;
10830:                         next;
10831:                     }
10832:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10833:                         push(@optional,$field);
10834:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10835:                         push(@unused,$field);
10836:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10837:                         push(@required,$field);
10838:                     }
10839:                 }
10840:                 if (@optional) {
10841:                     $resulttext .= '<li>'.
10842:                                    &mt('Help form fields changed to "Optional": [_1].',
10843:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10844:                                    '</li>';
10845:                 }
10846:                 if (@required) {
10847:                     $resulttext .= '<li>'.
10848:                                    &mt('Help form fields changed to "Required": [_1].',
10849:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10850:                                    '</li>';
10851:                 }
10852:                 if (@unused) {
10853:                     $resulttext .= '<li>'.
10854:                                    &mt('Help form fields changed to "Not shown": [_1].',
10855:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10856:                                    '</li>';
10857:                 }
10858:                 if ($maxsizechg) {
10859:                     $resulttext .= '<li>'.
10860:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10861:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10862:                                    '</li>';
10863:                 }
10864:             }
10865:             $resulttext .= '</ul>';
10866:         } else {
10867:             $resulttext = &mt('No changes made to contacts and form settings');
10868:         }
10869:     } else {
10870:         $resulttext = '<span class="LC_error">'.
10871:             &mt('An error occurred: [_1].',$putresult).'</span>';
10872:     }
10873:     return $resulttext;
10874: }
10875: 
10876: sub modify_passwords {
10877:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10878:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
10879:         $updatedefaults,$updateconf);
10880:     my $customfn = 'resetpw.html';
10881:     if (ref($domconfig{'passwords'}) eq 'HASH') {
10882:         %current = %{$domconfig{'passwords'}};
10883:     }
10884:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10885:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10886:     if (ref($types) eq 'ARRAY') {
10887:         @oktypes = @{$types};
10888:     }
10889:     push(@oktypes,'default');
10890: 
10891:     my %titles = &Apache::lonlocal::texthash (
10892:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
10893:         intauth_check  => 'Check bcrypt cost if authenticated',
10894:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
10895:         permanent      => 'Permanent e-mail address',
10896:         critical       => 'Critical notification address',
10897:         notify         => 'Notification address',
10898:         min            => 'Minimum password length',
10899:         max            => 'Maximum password length',
10900:         chars          => 'Required characters',
10901:         numsaved       => 'Number of previous passwords to save',
10902:         reset          => 'Resetting Forgotten Password',
10903:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
10904:         rules          => 'Rules for LON-CAPA Passwords',
10905:         crsownerchg    => 'Course Owner Changing Student Passwords',
10906:         username       => 'Username',
10907:         email          => 'E-mail address',
10908:     );
10909: 
10910: #
10911: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
10912: #
10913:     my (%curr_defaults,%save_defaults);
10914:     if (ref($domconfig{'defaults'}) eq 'HASH') {
10915:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
10916:             if ($key =~ /^intauth_(cost|check|switch)$/) {
10917:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
10918:             } else {
10919:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
10920:             }
10921:         }
10922:     }
10923:     my %staticdefaults = (
10924:         'resetlink'      => 2,
10925:         'resetcase'      => \@oktypes,
10926:         'resetprelink'   => 'both',
10927:         'resetemail'     => ['critical','notify','permanent'],
10928:         'intauth_cost'   => 10,
10929:         'intauth_check'  => 0,
10930:         'intauth_switch' => 0,
10931:     );
10932:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
10933:     foreach my $type (@oktypes) {
10934:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
10935:     }
10936:     my $linklife = $env{'form.passwords_link'};
10937:     $linklife =~ s/^\s+|\s+$//g;
10938:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
10939:         $newvalues{'resetlink'} = $linklife;
10940:         if ($current{'resetlink'}) {
10941:             if ($current{'resetlink'} ne $linklife) {
10942:                 $changes{'reset'} = 1;
10943:             }
10944:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
10945:             if ($staticdefaults{'resetlink'} ne $linklife) {
10946:                 $changes{'reset'} = 1;
10947:             }
10948:         }
10949:     } elsif ($current{'resetlink'}) {
10950:         $changes{'reset'} = 1;
10951:     }
10952:     my @casesens;
10953:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
10954:     foreach my $case (sort(@posscase)) {
10955:         if (grep(/^\Q$case\E$/,@oktypes)) {
10956:             push(@casesens,$case);
10957:         }
10958:     }
10959:     $newvalues{'resetcase'} = \@casesens;
10960:     if (ref($current{'resetcase'}) eq 'ARRAY') {
10961:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
10962:         if (@diffs > 0) {
10963:             $changes{'reset'} = 1;
10964:         }
10965:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
10966:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
10967:         if (@diffs > 0) {
10968:             $changes{'reset'} = 1;
10969:         }
10970:     }
10971:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
10972:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
10973:         if (exists($current{'resetprelink'})) {
10974:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
10975:                 $changes{'reset'} = 1;
10976:             }
10977:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
10978:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
10979:                 $changes{'reset'} = 1;
10980:             }
10981:         }
10982:     } elsif ($current{'resetprelink'}) {
10983:         $changes{'reset'} = 1;
10984:     }
10985:     foreach my $type (@oktypes) {
10986:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
10987:         my @postlink;
10988:         foreach my $item (sort(@possplink)) {
10989:             if ($item =~ /^(email|username)$/) {
10990:                 push(@postlink,$item);
10991:             }
10992:         }
10993:         $newvalues{'resetpostlink'}{$type} = \@postlink;
10994:         unless ($changes{'reset'}) {
10995:             if (ref($current{'resetpostlink'}) eq 'HASH') {
10996:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
10997:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
10998:                     if (@diffs > 0) {
10999:                         $changes{'reset'} = 1;
11000:                     }
11001:                 } else {
11002:                     $changes{'reset'} = 1;
11003:                 }
11004:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
11005:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
11006:                 if (@diffs > 0) {
11007:                     $changes{'reset'} = 1;
11008:                 }
11009:             }
11010:         }
11011:     }
11012:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
11013:     my @resetemail;
11014:     foreach my $item (sort(@possemailsrc)) {
11015:         if ($item =~ /^(permanent|critical|notify)$/) {
11016:             push(@resetemail,$item);
11017:         }
11018:     }
11019:     $newvalues{'resetemail'} = \@resetemail;
11020:     unless ($changes{'reset'}) {
11021:         if (ref($current{'resetemail'}) eq 'ARRAY') {
11022:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
11023:             if (@diffs > 0) {
11024:                 $changes{'reset'} = 1;
11025:             }
11026:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
11027:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
11028:             if (@diffs > 0) {
11029:                 $changes{'reset'} = 1;
11030:             }
11031:         }
11032:     }
11033:     if ($env{'form.passwords_stdtext'} == 0) {
11034:         $newvalues{'resetremove'} = 1;
11035:         unless ($current{'resetremove'}) {
11036:             $changes{'reset'} = 1;
11037:         }
11038:     } elsif ($current{'resetremove'}) {
11039:         $changes{'reset'} = 1;
11040:     }
11041:     if ($env{'form.passwords_customfile.filename'} ne '') {
11042:         my $servadm = $r->dir_config('lonAdmEMail');
11043:         my $servadm = $r->dir_config('lonAdmEMail');
11044:         my ($configuserok,$author_ok,$switchserver) =
11045:             &config_check($dom,$confname,$servadm);
11046:         my $error;
11047:         if ($configuserok eq 'ok') {
11048:             if ($switchserver) {
11049:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
11050:             } else {
11051:                 if ($author_ok eq 'ok') {
11052:                     my ($result,$customurl) =
11053:                         &publishlogo($r,'upload','passwords_customfile',$dom,
11054:                                      $confname,'customtext/resetpw','','',$customfn);
11055:                     if ($result eq 'ok') {
11056:                         $newvalues{'resetcustom'} = $customurl;
11057:                         $changes{'reset'} = 1;
11058:                     } else {
11059:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
11060:                     }
11061:                 } else {
11062:                     $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);
11063:                 }
11064:             }
11065:         } else {
11066:             $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);
11067:         }
11068:         if ($error) {
11069:             &Apache::lonnet::logthis($error);
11070:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11071:         }
11072:     } elsif ($current{'resetcustom'}) {
11073:         if ($env{'form.passwords_custom_del'}) {
11074:             $changes{'reset'} = 1;
11075:         } else {
11076:             $newvalues{'resetcustom'} = $current{'resetcustom'};
11077:         }
11078:     }
11079:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
11080:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
11081:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
11082:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
11083:             $changes{'intauth'} = 1;
11084:         }
11085:     } else {
11086:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
11087:     }
11088:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
11089:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
11090:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
11091:             $changes{'intauth'} = 1;
11092:         }
11093:     } else {
11094:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
11095:     }
11096:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
11097:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
11098:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
11099:             $changes{'intauth'} = 1;
11100:         }
11101:     } else {
11102:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
11103:     }
11104:     foreach my $item ('cost','check','switch') {
11105:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
11106:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
11107:             $updatedefaults = 1;
11108:         }
11109:     }
11110:     foreach my $rule ('min','max','numsaved') {
11111:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
11112:         my $ruleok;
11113:         if ($rule eq 'min') {
11114:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
11115:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
11116:                     $ruleok = 1;
11117:                 }
11118:             }
11119:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
11120:                  ($env{'form.passwords_'.$rule} ne '0')) {
11121:             $ruleok = 1;
11122:         }
11123:         if ($ruleok) {
11124:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
11125:             if (exists($current{$rule})) {
11126:                 if ($newvalues{$rule} ne $current{$rule}) {
11127:                     $changes{'rules'} = 1;
11128:                 }
11129:             } elsif ($rule eq 'min') {
11130:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
11131:                     $changes{'rules'} = 1;
11132:                 }
11133:             }
11134:         } elsif (exists($current{$rule})) {
11135:             $changes{'rules'} = 1;
11136:         }
11137:     }
11138:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
11139:     my @chars;
11140:     foreach my $item (sort(@posschars)) {
11141:         if ($item =~ /^(uc|lc|num|spec)$/) {
11142:             push(@chars,$item);
11143:         }
11144:     }
11145:     $newvalues{'chars'} = \@chars;
11146:     unless ($changes{'rules'}) {
11147:         if (ref($current{'chars'}) eq 'ARRAY') {
11148:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
11149:             if (@diffs > 0) {
11150:                 $changes{'rules'} = 1;
11151:             }
11152:         } else {
11153:             if (@chars > 0) {
11154:                 $changes{'rules'} = 1;
11155:             }
11156:         }
11157:     }
11158:     my %crsownerchg = (
11159:                         by => [],
11160:                         for => [],
11161:                       );
11162:     foreach my $item ('by','for') {
11163:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
11164:         foreach my $type (sort(@posstypes)) {
11165:             if (grep(/^\Q$type\E$/,@oktypes)) {
11166:                 push(@{$crsownerchg{$item}},$type);
11167:             }
11168:         }
11169:     }
11170:     $newvalues{'crsownerchg'} = \%crsownerchg;
11171:     if (ref($current{'crsownerchg'}) eq 'HASH') {
11172:         foreach my $item ('by','for') {
11173:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
11174:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
11175:                 if (@diffs > 0) {
11176:                     $changes{'crsownerchg'} = 1;
11177:                     last;
11178:                 }
11179:             }
11180:         }
11181:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
11182:         foreach my $item ('by','for') {
11183:             if (@{$crsownerchg{$item}} > 0) {
11184:                 $changes{'crsownerchg'} = 1;
11185:                 last;
11186:             }
11187:         }
11188:     }
11189: 
11190:     my %confighash = (
11191:                         defaults  => \%save_defaults,
11192:                         passwords => \%newvalues,
11193:                      );
11194:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
11195: 
11196:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
11197:     if ($putresult eq 'ok') {
11198:         if (keys(%changes) > 0) {
11199:             $resulttext = &mt('Changes made: ').'<ul>';
11200:             foreach my $key ('reset','intauth','rules','crsownerchg') {
11201:                 if ($changes{$key}) {
11202:                     unless ($key eq 'intauth') {
11203:                         $updateconf = 1;
11204:                     }
11205:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
11206:                     if ($key eq 'reset') {
11207:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
11208:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
11209:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
11210:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
11211:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />'.
11212:                                            &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchapub'}).'</br>'.
11213:                                            &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchapriv'}).'</li>';
11214:                         } else {
11215:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
11216:                         }
11217:                         if ($confighash{'passwords'}{'resetlink'}) {
11218:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
11219:                         } else {
11220:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
11221:                                                   &mt('Will default to 2 hours').'</li>';
11222:                         }
11223:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
11224:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
11225:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
11226:                             } else {
11227:                                 my $casesens;
11228:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
11229:                                     if ($type eq 'default') {
11230:                                         $casesens .= $othertitle.', ';
11231:                                     } elsif ($usertypes->{$type} ne '') {
11232:                                         $casesens .= $usertypes->{$type}.', ';
11233:                                     }
11234:                                 }
11235:                                 $casesens =~ s/\Q, \E$//;
11236:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
11237:                             }
11238:                         } else {
11239:                             $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>';
11240:                         }
11241:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
11242:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
11243:                         } else {
11244:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
11245:                         }
11246:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
11247:                             my $output;
11248:                             if (ref($types) eq 'ARRAY') {
11249:                                 foreach my $type (@{$types}) {
11250:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
11251:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
11252:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
11253:                                         } else {
11254:                                             $output .= $usertypes->{$type}.' -- '.
11255:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
11256:                                         }
11257:                                     }
11258:                                 }
11259:                             }
11260:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
11261:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
11262:                                     $output .= $othertitle.' -- '.&mt('none');
11263:                                 } else {
11264:                                     $output .= $othertitle.' -- '.
11265:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
11266:                                 }
11267:                             }
11268:                             if ($output) {
11269:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
11270:                             } else {
11271:                                 $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>';
11272:                             }
11273:                         } else {
11274:                             $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>';
11275:                         }
11276:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
11277:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
11278:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
11279:                             } else {
11280:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
11281:                             }
11282:                         } else {
11283:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA usedfor verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
11284:                         }
11285:                         if ($confighash{'passwords'}{'resetremove'}) {
11286:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
11287:                         } else {
11288:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
11289:                         }
11290:                         if ($confighash{'passwords'}{'resetcustom'}) {
11291:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
11292:                                                                             $titles{custom},600,500);
11293:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes [_1]',$customlink).'</li>';
11294:                         } else {
11295:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
11296:                         }
11297:                     } elsif ($key eq 'intauth') {
11298:                         foreach my $item ('cost','switch','check') {
11299:                             my $value = $save_defaults{$key.'_'.$item};
11300:                             if ($item eq 'switch') {
11301:                                 my %optiondesc = &Apache::lonlocal::texthash (
11302:                                                      0 => 'No',
11303:                                                      1 => 'Yes',
11304:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
11305:                                                  );
11306:                                 if ($value =~ /^(0|1|2)$/) {
11307:                                     $value = $optiondesc{$value};
11308:                                 } else {
11309:                                     $value = &mt('none -- defaults to No');
11310:                                 }
11311:                             } elsif ($item eq 'check') {
11312:                                 my %optiondesc = &Apache::lonlocal::texthash (
11313:                                                      0 => 'No',
11314:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
11315:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
11316:                                                  );
11317:                                 if ($value =~ /^(0|1|2)$/) {
11318:                                     $value = $optiondesc{$value};
11319:                                 } else {
11320:                                     $value = &mt('none -- defaults to No');
11321:                                 }
11322:                             }
11323:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
11324:                         }
11325:                     } elsif ($key eq 'rules') {
11326:                         foreach my $rule ('min','max','numsaved') {
11327:                             if ($confighash{'passwords'}{$rule} eq '') {
11328:                                 if ($rule eq 'min') {
11329:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
11330:                                                    ' '.&mt('Default of [_1] will be used',
11331:                                                            $Apache::lonnet::passwdmin).'</li>';
11332:                                 } else {
11333:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
11334:                                 }
11335:                             } else {
11336:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
11337:                             }
11338:                         }
11339:                     } elsif ($key eq 'crsownerchg') {
11340:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
11341:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
11342:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
11343:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
11344:                             } else {
11345:                                 my %crsownerstr;
11346:                                 foreach my $item ('by','for') {
11347:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
11348:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
11349:                                             if ($type eq 'default') {
11350:                                                 $crsownerstr{$item} .= $othertitle.', ';
11351:                                             } elsif ($usertypes->{$type} ne '') {
11352:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
11353:                                             }
11354:                                         }
11355:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
11356:                                     }
11357:                                 }
11358:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
11359:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
11360:                             }
11361:                         } else {
11362:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
11363:                         }
11364:                     }
11365:                     $resulttext .= '</ul></li>';
11366:                 }
11367:             }
11368:             $resulttext .= '</ul>';
11369:         } else {
11370:             $resulttext = &mt('No changes made to password settings');
11371:         }
11372:         my $cachetime = 24*60*60;
11373:         if ($updatedefaults) {
11374:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11375:             if (ref($lastactref) eq 'HASH') {
11376:                 $lastactref->{'domdefaults'} = 1;
11377:             }
11378:         }
11379:         if ($updateconf) {
11380:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
11381:             if (ref($lastactref) eq 'HASH') {
11382:                 $lastactref->{'passwdconf'} = 1;
11383:             }
11384:         }
11385:     } else {
11386:         $resulttext = '<span class="LC_error">'.
11387:             &mt('An error occurred: [_1]',$putresult).'</span>';
11388:     }
11389:     if ($errors) {
11390:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
11391:                        $errors.'</ul></p>';
11392:     }
11393:     return $resulttext;
11394: }
11395: 
11396: sub modify_usercreation {
11397:     my ($dom,%domconfig) = @_;
11398:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
11399:     my $warningmsg;
11400:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11401:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11402:             if ($key eq 'cancreate') {
11403:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11404:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11405:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
11406:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11407:                         } else {
11408:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11409:                         }
11410:                     }
11411:                 }
11412:             } elsif ($key eq 'email_rule') {
11413:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11414:             } else {
11415:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11416:             }
11417:         }
11418:     }
11419:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
11420:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
11421:     my @contexts = ('author','course','requestcrs');
11422:     foreach my $item(@contexts) {
11423:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
11424:     }
11425:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11426:         foreach my $item (@contexts) {
11427:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
11428:                 push(@{$changes{'cancreate'}},$item);
11429:             }
11430:         }
11431:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
11432:         foreach my $item (@contexts) {
11433:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
11434:                 if ($cancreate{$item} ne 'any') {
11435:                     push(@{$changes{'cancreate'}},$item);
11436:                 }
11437:             } else {
11438:                 if ($cancreate{$item} ne 'none') {
11439:                     push(@{$changes{'cancreate'}},$item);
11440:                 }
11441:             }
11442:         }
11443:     } else {
11444:         foreach my $item (@contexts)  {
11445:             push(@{$changes{'cancreate'}},$item);
11446:         }
11447:     }
11448: 
11449:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
11450:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
11451:             if (!grep(/^\Q$type\E$/,@username_rule)) {
11452:                 push(@{$changes{'username_rule'}},$type);
11453:             }
11454:         }
11455:         foreach my $type (@username_rule) {
11456:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
11457:                 push(@{$changes{'username_rule'}},$type);
11458:             }
11459:         }
11460:     } else {
11461:         push(@{$changes{'username_rule'}},@username_rule);
11462:     }
11463: 
11464:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
11465:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
11466:             if (!grep(/^\Q$type\E$/,@id_rule)) {
11467:                 push(@{$changes{'id_rule'}},$type);
11468:             }
11469:         }
11470:         foreach my $type (@id_rule) {
11471:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11472:                 push(@{$changes{'id_rule'}},$type);
11473:             }
11474:         }
11475:     } else {
11476:         push(@{$changes{'id_rule'}},@id_rule);
11477:     }
11478: 
11479:     my @authen_contexts = ('author','course','domain');
11480:     my @authtypes = ('int','krb4','krb5','loc');
11481:     my %authhash;
11482:     foreach my $item (@authen_contexts) {
11483:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11484:         foreach my $auth (@authtypes) {
11485:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11486:                 $authhash{$item}{$auth} = 1;
11487:             } else {
11488:                 $authhash{$item}{$auth} = 0;
11489:             }
11490:         }
11491:     }
11492:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11493:         foreach my $item (@authen_contexts) {
11494:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11495:                 foreach my $auth (@authtypes) {
11496:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11497:                         push(@{$changes{'authtypes'}},$item);
11498:                         last;
11499:                     }
11500:                 }
11501:             }
11502:         }
11503:     } else {
11504:         foreach my $item (@authen_contexts) {
11505:             push(@{$changes{'authtypes'}},$item);
11506:         }
11507:     }
11508: 
11509:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11510:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11511:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11512:     $save_usercreate{'id_rule'} = \@id_rule;
11513:     $save_usercreate{'username_rule'} = \@username_rule,
11514:     $save_usercreate{'authtypes'} = \%authhash;
11515: 
11516:     my %usercreation_hash =  (
11517:         usercreation     => \%save_usercreate,
11518:     );
11519: 
11520:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11521:                                              $dom);
11522: 
11523:     if ($putresult eq 'ok') {
11524:         if (keys(%changes) > 0) {
11525:             $resulttext = &mt('Changes made:').'<ul>';
11526:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11527:                 my %lt = &usercreation_types();
11528:                 foreach my $type (@{$changes{'cancreate'}}) {
11529:                     my $chgtext = $lt{$type}.', ';
11530:                     if ($cancreate{$type} eq 'none') {
11531:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11532:                     } elsif ($cancreate{$type} eq 'any') {
11533:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11534:                     } elsif ($cancreate{$type} eq 'official') {
11535:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11536:                     } elsif ($cancreate{$type} eq 'unofficial') {
11537:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11538:                     }
11539:                     $resulttext .= '<li>'.$chgtext.'</li>';
11540:                 }
11541:             }
11542:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11543:                 my ($rules,$ruleorder) = 
11544:                     &Apache::lonnet::inst_userrules($dom,'username');
11545:                 my $chgtext = '<ul>';
11546:                 foreach my $type (@username_rule) {
11547:                     if (ref($rules->{$type}) eq 'HASH') {
11548:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11549:                     }
11550:                 }
11551:                 $chgtext .= '</ul>';
11552:                 if (@username_rule > 0) {
11553:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11554:                 } else {
11555:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11556:                 }
11557:             }
11558:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11559:                 my ($idrules,$idruleorder) = 
11560:                     &Apache::lonnet::inst_userrules($dom,'id');
11561:                 my $chgtext = '<ul>';
11562:                 foreach my $type (@id_rule) {
11563:                     if (ref($idrules->{$type}) eq 'HASH') {
11564:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11565:                     }
11566:                 }
11567:                 $chgtext .= '</ul>';
11568:                 if (@id_rule > 0) {
11569:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11570:                 } else {
11571:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11572:                 }
11573:             }
11574:             my %authname = &authtype_names();
11575:             my %context_title = &context_names();
11576:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11577:                 my $chgtext = '<ul>';
11578:                 foreach my $type (@{$changes{'authtypes'}}) {
11579:                     my @allowed;
11580:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11581:                     foreach my $auth (@authtypes) {
11582:                         if ($authhash{$type}{$auth}) {
11583:                             push(@allowed,$authname{$auth});
11584:                         }
11585:                     }
11586:                     if (@allowed > 0) {
11587:                         $chgtext .= join(', ',@allowed).'</li>';
11588:                     } else {
11589:                         $chgtext .= &mt('none').'</li>';
11590:                     }
11591:                 }
11592:                 $chgtext .= '</ul>';
11593:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11594:                 $resulttext .= '</li>';
11595:             }
11596:             $resulttext .= '</ul>';
11597:         } else {
11598:             $resulttext = &mt('No changes made to user creation settings');
11599:         }
11600:     } else {
11601:         $resulttext = '<span class="LC_error">'.
11602:             &mt('An error occurred: [_1]',$putresult).'</span>';
11603:     }
11604:     if ($warningmsg ne '') {
11605:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11606:     }
11607:     return $resulttext;
11608: }
11609: 
11610: sub modify_selfcreation {
11611:     my ($dom,$lastactref,%domconfig) = @_;
11612:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
11613:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
11614:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11615:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
11616:     if (ref($typesref) eq 'ARRAY') {
11617:         @types = @{$typesref};
11618:     }
11619:     if (ref($usertypesref) eq 'HASH') {
11620:         %usertypes = %{$usertypesref};
11621:     }
11622:     $usertypes{'default'} = $othertitle;
11623: #
11624: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11625: #
11626:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11627:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11628:             if ($key eq 'cancreate') {
11629:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11630:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11631:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11632:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
11633:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
11634:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
11635:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
11636:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
11637:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11638:                         } else {
11639:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11640:                         }
11641:                     }
11642:                 }
11643:             } elsif ($key eq 'email_rule') {
11644:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11645:             } else {
11646:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11647:             }
11648:         }
11649:     }
11650: #
11651: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11652: #
11653:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11654:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11655:             if ($key eq 'selfcreate') {
11656:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11657:             } else {
11658:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11659:             }
11660:         }
11661:     }
11662: #
11663: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
11664: #
11665:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11666:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
11667:             if ($key eq 'inststatusguest') {
11668:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
11669:             } else {
11670:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
11671:             }
11672:         }
11673:     }
11674: 
11675:     my @contexts = ('selfcreate');
11676:     @{$cancreate{'selfcreate'}} = ();
11677:     %{$cancreate{'emailusername'}} = ();
11678:     if (@types) {
11679:         @{$cancreate{'statustocreate'}} = ();
11680:     }
11681:     %{$cancreate{'selfcreateprocessing'}} = ();
11682:     %{$cancreate{'shibenv'}} = ();
11683:     %{$cancreate{'emailverified'}} = ();
11684:     %{$cancreate{'emailoptions'}} = ();
11685:     %{$cancreate{'emaildomain'}} = ();
11686:     my %selfcreatetypes = (
11687:                              sso   => 'users authenticated by institutional single sign on',
11688:                              login => 'users authenticated by institutional log-in',
11689:                              email => 'users verified by e-mail',
11690:                           );
11691: #
11692: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11693: # is permitted.
11694: #
11695: 
11696:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
11697: 
11698:     my (@statuses,%email_rule);
11699:     foreach my $item ('login','sso','email') {
11700:         if ($item eq 'email') {
11701:             if ($env{'form.cancreate_email'}) {
11702:                 if (@types) {
11703:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
11704:                     foreach my $status (@poss_statuses) {
11705:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
11706:                             push(@statuses,$status);
11707:                         }
11708:                     }
11709:                     $save_inststatus{'inststatusguest'} = \@statuses;
11710:                 } else {
11711:                     push(@statuses,'default');
11712:                 }
11713:                 if (@statuses) {
11714:                     my %curr_rule;
11715:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11716:                         foreach my $type (@statuses) {
11717:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
11718:                         }
11719:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
11720:                         foreach my $type (@statuses) {
11721:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
11722:                         }
11723:                     }
11724:                     push(@{$cancreate{'selfcreate'}},'email');
11725:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
11726:                     my %curremaildom;
11727:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
11728:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
11729:                     }
11730:                     foreach my $type (@statuses) {
11731:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
11732:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11733:                         }
11734:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
11735:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
11736:                         }
11737:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
11738: #
11739: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
11740: #
11741:                             my $chosen = $1;
11742:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
11743:                                 my $emaildom;
11744:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
11745:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
11746:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
11747:                                     if (ref($curremaildom{$type}) eq 'HASH') {
11748:                                         if (exists($curremaildom{$type}{$chosen})) {
11749:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
11750:                                                 push(@{$changes{'cancreate'}},'emaildomain');
11751:                                             }
11752:                                         } elsif ($emaildom ne '') {
11753:                                             push(@{$changes{'cancreate'}},'emaildomain');
11754:                                         }
11755:                                     } elsif ($emaildom ne '') {
11756:                                         push(@{$changes{'cancreate'}},'emaildomain');
11757:                                     }
11758:                                 }
11759:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11760:                             } elsif ($chosen eq 'custom') {
11761:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
11762:                                 $email_rule{$type} = [];
11763:                                 if (ref($emailrules) eq 'HASH') {
11764:                                     foreach my $rule (@possemail_rules) {
11765:                                         if (exists($emailrules->{$rule})) {
11766:                                             push(@{$email_rule{$type}},$rule);
11767:                                         }
11768:                                     }
11769:                                 }
11770:                                 if (@{$email_rule{$type}}) {
11771:                                     $cancreate{'emailoptions'}{$type} = 'custom';
11772:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
11773:                                         if (@{$curr_rule{$type}} > 0) {
11774:                                             foreach my $rule (@{$curr_rule{$type}}) {
11775:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
11776:                                                     push(@{$changes{'email_rule'}},$type);
11777:                                                 }
11778:                                             }
11779:                                         }
11780:                                         foreach my $type (@{$email_rule{$type}}) {
11781:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
11782:                                                 push(@{$changes{'email_rule'}},$type);
11783:                                             }
11784:                                         }
11785:                                     } else {
11786:                                         push(@{$changes{'email_rule'}},$type);
11787:                                     }
11788:                                 }
11789:                             } else {
11790:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11791:                             }
11792:                         }
11793:                     }
11794:                     if (@types) {
11795:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11796:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
11797:                             if (@changed) {
11798:                                 push(@{$changes{'inststatus'}},'inststatusguest');
11799:                             }
11800:                         } else {
11801:                             push(@{$changes{'inststatus'}},'inststatusguest');
11802:                         }
11803:                     }
11804:                 } else {
11805:                     delete($env{'form.cancreate_email'});
11806:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11807:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11808:                             push(@{$changes{'inststatus'}},'inststatusguest');
11809:                         }
11810:                     }
11811:                 }
11812:             } else {
11813:                 $save_inststatus{'inststatusguest'} = [];
11814:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11815:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11816:                         push(@{$changes{'inststatus'}},'inststatusguest');
11817:                     }
11818:                 }
11819:             }
11820:         } else {
11821:             if ($env{'form.cancreate_'.$item}) {
11822:                 push(@{$cancreate{'selfcreate'}},$item);
11823:             }
11824:         }
11825:     }
11826:     my (%userinfo,%savecaptcha);
11827:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11828: #
11829: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11830: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11831: #
11832: 
11833:     if ($env{'form.cancreate_email'}) {
11834:         push(@contexts,'emailusername');
11835:         if (@statuses) {
11836:             foreach my $type (@statuses) {
11837:                 if (ref($infofields) eq 'ARRAY') {
11838:                     foreach my $field (@{$infofields}) {
11839:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11840:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11841:                         }
11842:                     }
11843:                 }
11844:             }
11845:         }
11846: #
11847: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11848: # queued requests for self-creation of account verified by e-mail.
11849: #
11850: 
11851:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11852:         @approvalnotify = sort(@approvalnotify);
11853:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11854:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11855:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11856:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11857:                     push(@{$changes{'cancreate'}},'notify');
11858:                 }
11859:             } else {
11860:                 if ($cancreate{'notify'}{'approval'}) {
11861:                     push(@{$changes{'cancreate'}},'notify');
11862:                 }
11863:             }
11864:         } elsif ($cancreate{'notify'}{'approval'}) {
11865:             push(@{$changes{'cancreate'}},'notify');
11866:         }
11867: 
11868:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11869:     }
11870: #  
11871: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11872: # institutional log-in.
11873: #
11874:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11875:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11876:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11877:             $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.').' '.
11878:                           &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.');
11879:         }
11880:     }
11881:     my @fields = ('lastname','firstname','middlename','generation',
11882:                   'permanentemail','id');
11883:     my @shibfields = (@fields,'inststatus');
11884:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11885: #
11886: # Where usernames may created for institutional log-in and/or institutional single sign on:
11887: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11888: # may self-create accounts 
11889: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11890: # which the user may supply, if institutional data is unavailable.
11891: #
11892:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11893:         if (@types) {
11894:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11895:             push(@contexts,'statustocreate');
11896:             foreach my $type (@types) {
11897:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11898:                 foreach my $field (@fields) {
11899:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11900:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11901:                     } else {
11902:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11903:                     }
11904:                 }
11905:             }
11906:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11907:                 foreach my $type (@types) {
11908:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11909:                         foreach my $field (@fields) {
11910:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11911:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11912:                                 push(@{$changes{'selfcreate'}},$type);
11913:                                 last;
11914:                             }
11915:                         }
11916:                     }
11917:                 }
11918:             } else {
11919:                 foreach my $type (@types) {
11920:                     push(@{$changes{'selfcreate'}},$type);
11921:                 }
11922:             }
11923:         }
11924:         foreach my $field (@shibfields) {
11925:             if ($env{'form.shibenv_'.$field} ne '') {
11926:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11927:             }
11928:         }
11929:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11930:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11931:                 foreach my $field (@shibfields) {
11932:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11933:                         push(@{$changes{'cancreate'}},'shibenv');
11934:                     }
11935:                 }
11936:             } else {
11937:                 foreach my $field (@shibfields) {
11938:                     if ($env{'form.shibenv_'.$field}) {
11939:                         push(@{$changes{'cancreate'}},'shibenv');
11940:                         last;
11941:                     }
11942:                 }
11943:             }
11944:         }
11945:     }
11946:     foreach my $item (@contexts) {
11947:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11948:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11949:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11950:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11951:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11952:                             push(@{$changes{'cancreate'}},$item);
11953:                         }
11954:                     }
11955:                 }
11956:             }
11957:             if (ref($cancreate{$item}) eq 'ARRAY') {
11958:                 foreach my $type (@{$cancreate{$item}}) {
11959:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11960:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11961:                             push(@{$changes{'cancreate'}},$item);
11962:                         }
11963:                     }
11964:                 }
11965:             }
11966:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11967:             if (ref($cancreate{$item}) eq 'HASH') {
11968:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11969:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
11970:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
11971:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
11972:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11973:                                     push(@{$changes{'cancreate'}},$item);
11974:                                 }
11975:                             }
11976:                         }
11977:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11978:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
11979:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11980:                                 push(@{$changes{'cancreate'}},$item);
11981:                             }
11982:                         }
11983:                     }
11984:                 }
11985:                 foreach my $type (keys(%{$cancreate{$item}})) {
11986:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11987:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11988:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
11989:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
11990:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11991:                                         push(@{$changes{'cancreate'}},$item);
11992:                                     }
11993:                                 }
11994:                             } else {
11995:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11996:                                     push(@{$changes{'cancreate'}},$item);
11997:                                 }
11998:                             }
11999:                         }
12000:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
12001:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
12002:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12003:                                 push(@{$changes{'cancreate'}},$item);
12004:                             }
12005:                         }
12006:                     }
12007:                 }
12008:             }
12009:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
12010:             if (ref($cancreate{$item}) eq 'ARRAY') {
12011:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
12012:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12013:                         push(@{$changes{'cancreate'}},$item);
12014:                     }
12015:                 }
12016:             }
12017:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
12018:             if (ref($cancreate{$item}) eq 'HASH') {
12019:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12020:                     push(@{$changes{'cancreate'}},$item);
12021:                 }
12022:             }
12023:         } elsif ($item eq 'emailusername') {
12024:             if (ref($cancreate{$item}) eq 'HASH') {
12025:                 foreach my $type (keys(%{$cancreate{$item}})) {
12026:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
12027:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
12028:                             if ($cancreate{$item}{$type}{$field}) {
12029:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12030:                                     push(@{$changes{'cancreate'}},$item);
12031:                                 }
12032:                                 last;
12033:                             }
12034:                         }
12035:                     }
12036:                 }
12037:             }
12038:         }
12039:     }
12040: #
12041: # Populate %save_usercreate hash with updates to self-creation configuration.
12042: #
12043:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
12044:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
12045:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
12046:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
12047:     if (ref($cancreate{'notify'}) eq 'HASH') {
12048:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
12049:     }
12050:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
12051:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
12052:     }
12053:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
12054:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
12055:     }
12056:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
12057:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
12058:     }
12059:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
12060:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
12061:     }
12062:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
12063:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
12064:     }
12065:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
12066:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
12067:     }
12068:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
12069:     $save_usercreate{'email_rule'} = \%email_rule;
12070: 
12071:     my %userconfig_hash = (
12072:             usercreation     => \%save_usercreate,
12073:             usermodification => \%save_usermodify,
12074:             inststatus       => \%save_inststatus,
12075:     );
12076: 
12077:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
12078:                                              $dom);
12079: #
12080: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
12081: #
12082:     if ($putresult eq 'ok') {
12083:         if (keys(%changes) > 0) {
12084:             $resulttext = &mt('Changes made:').'<ul>';
12085:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
12086:                 my %lt = &selfcreation_types();
12087:                 foreach my $type (@{$changes{'cancreate'}}) {
12088:                     my $chgtext = '';
12089:                     if ($type eq 'selfcreate') {
12090:                         if (@{$cancreate{$type}} == 0) {
12091:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
12092:                         } else {
12093:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
12094:                                         '<ul>';
12095:                             foreach my $case (@{$cancreate{$type}}) {
12096:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
12097:                             }
12098:                             $chgtext .= '</ul>';
12099:                             if (ref($cancreate{$type}) eq 'ARRAY') {
12100:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
12101:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
12102:                                         if (@{$cancreate{'statustocreate'}} == 0) {
12103:                                             $chgtext .= '<span class="LC_warning">'.
12104:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
12105:                                                         '</span><br />';
12106:                                         }
12107:                                     }
12108:                                 }
12109:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
12110:                                     if (!@statuses) {
12111:                                         $chgtext .= '<span class="LC_warning">'.
12112:                                                     &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.").
12113:                                                     '</span><br />';
12114: 
12115:                                     }
12116:                                 }
12117:                             }
12118:                         }
12119:                     } elsif ($type eq 'shibenv') {
12120:                         if (keys(%{$cancreate{$type}}) == 0) {
12121:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
12122:                         } else {
12123:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
12124:                                         '<ul>';
12125:                             foreach my $field (@shibfields) {
12126:                                 next if ($cancreate{$type}{$field} eq '');
12127:                                 if ($field eq 'inststatus') {
12128:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
12129:                                 } else {
12130:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
12131:                                 }
12132:                             }
12133:                             $chgtext .= '</ul>';
12134:                         }
12135:                     } elsif ($type eq 'statustocreate') {
12136:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
12137:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
12138:                             if (@{$cancreate{'selfcreate'}} > 0) {
12139:                                 if (@{$cancreate{'statustocreate'}} == 0) {
12140:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
12141:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
12142:                                         $chgtext .= '<br />'.
12143:                                                     '<span class="LC_warning">'.
12144:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
12145:                                                     '</span>';
12146:                                     }
12147:                                 } elsif (keys(%usertypes) > 0) {
12148:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12149:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
12150:                                     } else {
12151:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
12152:                                     }
12153:                                     $chgtext .= '<ul>';
12154:                                     foreach my $case (@{$cancreate{$type}}) {
12155:                                         if ($case eq 'default') {
12156:                                             $chgtext .= '<li>'.$othertitle.'</li>';
12157:                                         } else {
12158:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
12159:                                         }
12160:                                     }
12161:                                     $chgtext .= '</ul>';
12162:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12163:                                         $chgtext .= '<span class="LC_warning">'.
12164:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
12165:                                                     '</span>';
12166:                                     }
12167:                                 }
12168:                             } else {
12169:                                 if (@{$cancreate{$type}} == 0) {
12170:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
12171:                                 } else {
12172:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
12173:                                 }
12174:                             }
12175:                             $chgtext .= '<br />';
12176:                         }
12177:                     } elsif ($type eq 'selfcreateprocessing') {
12178:                         my %choices = &Apache::lonlocal::texthash (
12179:                                                                     automatic => 'Automatic approval',
12180:                                                                     approval  => 'Queued for approval',
12181:                                                                   );
12182:                         if (@types) {
12183:                             if (@statuses) {
12184:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
12185:                                             '<ul>';
12186:                                 foreach my $status (@statuses) {
12187:                                     if ($status eq 'default') {
12188:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12189:                                     } else {
12190:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12191:                                     }
12192:                                 }
12193:                                 $chgtext .= '</ul>';
12194:                             }
12195:                         } else {
12196:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
12197:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
12198:                         }
12199:                     } elsif ($type eq 'emailverified') {
12200:                         my %options = &Apache::lonlocal::texthash (
12201:                                                                     all   => 'Same as e-mail',
12202:                                                                     first => 'Omit @domain',
12203:                                                                     free  => 'Free to choose',
12204:                                                                   );
12205:                         if (@types) {
12206:                             if (@statuses) {
12207:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
12208:                                             '<ul>';
12209:                                 foreach my $status (@statuses) {
12210:                                     if ($status eq 'default') {
12211:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12212:                                     } else {
12213:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12214:                                     }
12215:                                 }
12216:                                 $chgtext .= '</ul>';
12217:                             }
12218:                         } else {
12219:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
12220:                                             $options{$cancreate{'emailverified'}{'default'}});
12221:                         }
12222:                     } elsif ($type eq 'emailoptions') {
12223:                         my %options = &Apache::lonlocal::texthash (
12224:                                                                     any     => 'Any e-mail',
12225:                                                                     inst    => 'Institutional only',
12226:                                                                     noninst => 'Non-institutional only',
12227:                                                                     custom  => 'Custom restrictions',
12228:                                                                   );
12229:                         if (@types) {
12230:                             if (@statuses) {
12231:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
12232:                                             '<ul>';
12233:                                 foreach my $status (@statuses) {
12234:                                     if ($type eq 'default') {
12235:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12236:                                     } else {
12237:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12238:                                     }
12239:                                 }
12240:                                 $chgtext .= '</ul>';
12241:                             }
12242:                         } else {
12243:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
12244:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
12245:                             } else {
12246:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
12247:                                                 $options{$cancreate{'emailoptions'}{'default'}});
12248:                             }
12249:                         }
12250:                     } elsif ($type eq 'emaildomain') {
12251:                         my $output;
12252:                         if (@statuses) {
12253:                             foreach my $type (@statuses) {
12254:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
12255:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
12256:                                         if ($type eq 'default') {
12257:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12258:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12259:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12260:                                             } else {
12261:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
12262:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
12263:                                             }
12264:                                         } else {
12265:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12266:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12267:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12268:                                             } else {
12269:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
12270:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
12271:                                             }
12272:                                         }
12273:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
12274:                                         if ($type eq 'default') {
12275:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12276:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12277:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12278:                                             } else {
12279:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
12280:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
12281:                                             }
12282:                                         } else {
12283:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12284:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12285:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12286:                                             } else {
12287:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
12288:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
12289:                                             }
12290:                                         }
12291:                                     }
12292:                                 }
12293:                             }
12294:                         }
12295:                         if ($output ne '') {
12296:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
12297:                                         '<ul>'.$output.'</ul>';
12298:                         }
12299:                     } elsif ($type eq 'captcha') {
12300:                         if ($savecaptcha{$type} eq 'notused') {
12301:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
12302:                         } else {
12303:                             my %captchas = &captcha_phrases();
12304:                             if ($captchas{$savecaptcha{$type}}) {
12305:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
12306:                             } else {
12307:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
12308:                             }
12309:                         }
12310:                     } elsif ($type eq 'recaptchakeys') {
12311:                         my ($privkey,$pubkey);
12312:                         if (ref($savecaptcha{$type}) eq 'HASH') {
12313:                             $pubkey = $savecaptcha{$type}{'public'};
12314:                             $privkey = $savecaptcha{$type}{'private'};
12315:                         }
12316:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
12317:                         if (!$pubkey) {
12318:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
12319:                         } else {
12320:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12321:                         }
12322:                         if (!$privkey) {
12323:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
12324:                         } else {
12325:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
12326:                         }
12327:                         $chgtext .= '</ul>';
12328:                     } elsif ($type eq 'recaptchaversion') {
12329:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
12330:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
12331:                         }
12332:                     } elsif ($type eq 'emailusername') {
12333:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
12334:                             if (@statuses) {
12335:                                 foreach my $type (@statuses) {
12336:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
12337:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
12338:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
12339:                                                     '<ul>';
12340:                                             foreach my $field (@{$infofields}) {
12341:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
12342:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
12343:                                                 }
12344:                                             }
12345:                                             $chgtext .= '</ul>';
12346:                                         } else {
12347:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12348:                                         }
12349:                                     } else {
12350:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12351:                                     }
12352:                                 }
12353:                             }
12354:                         }
12355:                     } elsif ($type eq 'notify') {
12356:                         my $numapprove = 0;
12357:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
12358:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
12359:                                 if ($cancreate{'notify'}{'approval'}) {
12360:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
12361:                                     $numapprove ++;
12362:                                 }
12363:                             }
12364:                         }
12365:                         unless ($numapprove) {
12366:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
12367:                         }
12368:                     }
12369:                     if ($chgtext) {
12370:                         $resulttext .= '<li>'.$chgtext.'</li>';
12371:                     }
12372:                 }
12373:             }
12374:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
12375:                 my ($emailrules,$emailruleorder) =
12376:                     &Apache::lonnet::inst_userrules($dom,'email');
12377:                 foreach my $type (@{$changes{'email_rule'}}) {
12378:                     if (ref($email_rule{$type}) eq 'ARRAY') {
12379:                         my $chgtext = '<ul>';
12380:                         foreach my $rule (@{$email_rule{$type}}) {
12381:                             if (ref($emailrules->{$rule}) eq 'HASH') {
12382:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
12383:                             }
12384:                         }
12385:                         $chgtext .= '</ul>';
12386:                         my $typename;
12387:                         if (@types) {
12388:                             if ($type eq 'default') {
12389:                                 $typename = $othertitle;
12390:                             } else {
12391:                                 $typename = $usertypes{$type};
12392:                             }
12393:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
12394:                         }
12395:                         if (@{$email_rule{$type}} > 0) {
12396:                             $resulttext .= '<li>'.
12397:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
12398:                                                $usertypes{$type}).
12399:                                            $chgtext.
12400:                                            '</li>';
12401:                         } else {
12402:                             $resulttext .= '<li>'.
12403:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
12404:                                            '</li>'.
12405:                                            &mt('(Affiliation: [_1])',$typename);
12406:                         }
12407:                     }
12408:                 }
12409:             }
12410:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
12411:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
12412:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
12413:                         my $chgtext = '<ul>';
12414:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
12415:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
12416:                         }
12417:                         $chgtext .= '</ul>';
12418:                         $resulttext .= '<li>'.
12419:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
12420:                                           $chgtext.
12421:                                        '</li>';
12422:                     } else {
12423:                         $resulttext .= '<li>'.
12424:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
12425:                                        '</li>';
12426:                     }
12427:                 }
12428:             }
12429:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
12430:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
12431:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12432:                 foreach my $type (@{$changes{'selfcreate'}}) {
12433:                     my $typename = $type;
12434:                     if (keys(%usertypes) > 0) {
12435:                         if ($usertypes{$type} ne '') {
12436:                             $typename = $usertypes{$type};
12437:                         }
12438:                     }
12439:                     my @modifiable;
12440:                     $resulttext .= '<li>'.
12441:                                     &mt('Self-creation of account by users with status: [_1]',
12442:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
12443:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
12444:                     foreach my $field (@fields) {
12445:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
12446:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
12447:                         }
12448:                     }
12449:                     if (@modifiable > 0) {
12450:                         $resulttext .= join(', ',@modifiable);
12451:                     } else {
12452:                         $resulttext .= &mt('none');
12453:                     }
12454:                     $resulttext .= '</li>';
12455:                 }
12456:                 $resulttext .= '</ul></li>';
12457:             }
12458:             $resulttext .= '</ul>';
12459:             my $cachetime = 24*60*60;
12460:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
12461:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12462:             if (ref($lastactref) eq 'HASH') {
12463:                 $lastactref->{'domdefaults'} = 1;
12464:             }
12465:         } else {
12466:             $resulttext = &mt('No changes made to self-creation settings');
12467:         }
12468:     } else {
12469:         $resulttext = '<span class="LC_error">'.
12470:             &mt('An error occurred: [_1]',$putresult).'</span>';
12471:     }
12472:     if ($warningmsg ne '') {
12473:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
12474:     }
12475:     return $resulttext;
12476: }
12477: 
12478: sub process_captcha {
12479:     my ($container,$changes,$newsettings,$current) = @_;
12480:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
12481:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
12482:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
12483:         $newsettings->{'captcha'} = 'original';
12484:     }
12485:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
12486:         if ($container eq 'cancreate') {
12487:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12488:                 push(@{$changes->{'cancreate'}},'captcha');
12489:             } elsif (!defined($changes->{'cancreate'})) {
12490:                 $changes->{'cancreate'} = ['captcha'];
12491:             }
12492:         } elsif ($container eq 'passwords') {
12493:             $changes->{'reset'} = 1;
12494:         } else {
12495:             $changes->{'captcha'} = 1;
12496:         }
12497:     }
12498:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
12499:     if ($newsettings->{'captcha'} eq 'recaptcha') {
12500:         $newpub = $env{'form.'.$container.'_recaptchapub'};
12501:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
12502:         $newpub =~ s/[^\w\-]//g;
12503:         $newpriv =~ s/[^\w\-]//g;
12504:         $newsettings->{'recaptchakeys'} = {
12505:                                              public  => $newpub,
12506:                                              private => $newpriv,
12507:                                           };
12508:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
12509:         $newversion =~ s/\D//g;
12510:         if ($newversion ne '2') {
12511:             $newversion = 1;
12512:         }
12513:         $newsettings->{'recaptchaversion'} = $newversion;
12514:     }
12515:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
12516:         $currpub = $current->{'recaptchakeys'}{'public'};
12517:         $currpriv = $current->{'recaptchakeys'}{'private'};
12518:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
12519:             $newsettings->{'recaptchakeys'} = {
12520:                                                  public  => '',
12521:                                                  private => '',
12522:                                               }
12523:         }
12524:     }
12525:     if ($current->{'captcha'} eq 'recaptcha') {
12526:         $currversion = $current->{'recaptchaversion'};
12527:         if ($currversion ne '2') {
12528:             $currversion = 1;
12529:         }
12530:     }
12531:     if ($currversion ne $newversion) {
12532:         if ($container eq 'cancreate') {
12533:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12534:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
12535:             } elsif (!defined($changes->{'cancreate'})) {
12536:                 $changes->{'cancreate'} = ['recaptchaversion'];
12537:             }
12538:         } elsif ($container eq 'passwords') {
12539:             $changes->{'reset'} = 1;
12540:         } else {
12541:             $changes->{'recaptchaversion'} = 1;
12542:         }
12543:     }
12544:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
12545:         if ($container eq 'cancreate') {
12546:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12547:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
12548:             } elsif (!defined($changes->{'cancreate'})) {
12549:                 $changes->{'cancreate'} = ['recaptchakeys'];
12550:             }
12551:         } elsif ($container eq 'passwords') {
12552:             $changes->{'reset'} = 1;
12553:         } else {
12554:             $changes->{'recaptchakeys'} = 1;
12555:         }
12556:     }
12557:     return;
12558: }
12559: 
12560: sub modify_usermodification {
12561:     my ($dom,%domconfig) = @_;
12562:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
12563:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
12564:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
12565:             if ($key eq 'selfcreate') {
12566:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
12567:             } else {  
12568:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
12569:             }
12570:         }
12571:     }
12572:     my @contexts = ('author','course');
12573:     my %context_title = (
12574:                            author => 'In author context',
12575:                            course => 'In course context',
12576:                         );
12577:     my @fields = ('lastname','firstname','middlename','generation',
12578:                   'permanentemail','id');
12579:     my %roles = (
12580:                   author => ['ca','aa'],
12581:                   course => ['st','ep','ta','in','cr'],
12582:                 );
12583:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12584:     foreach my $context (@contexts) {
12585:         foreach my $role (@{$roles{$context}}) {
12586:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
12587:             foreach my $item (@fields) {
12588:                 if (grep(/^\Q$item\E$/,@modifiable)) {
12589:                     $modifyhash{$context}{$role}{$item} = 1;
12590:                 } else {
12591:                     $modifyhash{$context}{$role}{$item} = 0;
12592:                 }
12593:             }
12594:         }
12595:         if (ref($curr_usermodification{$context}) eq 'HASH') {
12596:             foreach my $role (@{$roles{$context}}) {
12597:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
12598:                     foreach my $field (@fields) {
12599:                         if ($modifyhash{$context}{$role}{$field} ne 
12600:                                 $curr_usermodification{$context}{$role}{$field}) {
12601:                             push(@{$changes{$context}},$role);
12602:                             last;
12603:                         }
12604:                     }
12605:                 }
12606:             }
12607:         } else {
12608:             foreach my $context (@contexts) {
12609:                 foreach my $role (@{$roles{$context}}) {
12610:                     push(@{$changes{$context}},$role);
12611:                 }
12612:             }
12613:         }
12614:     }
12615:     my %usermodification_hash =  (
12616:                                    usermodification => \%modifyhash,
12617:                                  );
12618:     my $putresult = &Apache::lonnet::put_dom('configuration',
12619:                                              \%usermodification_hash,$dom);
12620:     if ($putresult eq 'ok') {
12621:         if (keys(%changes) > 0) {
12622:             $resulttext = &mt('Changes made: ').'<ul>';
12623:             foreach my $context (@contexts) {
12624:                 if (ref($changes{$context}) eq 'ARRAY') {
12625:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
12626:                     if (ref($changes{$context}) eq 'ARRAY') {
12627:                         foreach my $role (@{$changes{$context}}) {
12628:                             my $rolename;
12629:                             if ($role eq 'cr') {
12630:                                 $rolename = &mt('Custom');
12631:                             } else {
12632:                                 $rolename = &Apache::lonnet::plaintext($role);
12633:                             }
12634:                             my @modifiable;
12635:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
12636:                             foreach my $field (@fields) {
12637:                                 if ($modifyhash{$context}{$role}{$field}) {
12638:                                     push(@modifiable,$fieldtitles{$field});
12639:                                 }
12640:                             }
12641:                             if (@modifiable > 0) {
12642:                                 $resulttext .= join(', ',@modifiable);
12643:                             } else {
12644:                                 $resulttext .= &mt('none'); 
12645:                             }
12646:                             $resulttext .= '</li>';
12647:                         }
12648:                         $resulttext .= '</ul></li>';
12649:                     }
12650:                 }
12651:             }
12652:             $resulttext .= '</ul>';
12653:         } else {
12654:             $resulttext = &mt('No changes made to user modification settings');
12655:         }
12656:     } else {
12657:         $resulttext = '<span class="LC_error">'.
12658:             &mt('An error occurred: [_1]',$putresult).'</span>';
12659:     }
12660:     return $resulttext;
12661: }
12662: 
12663: sub modify_defaults {
12664:     my ($dom,$lastactref,%domconfig) = @_;
12665:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
12666:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12667:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
12668:                  'portal_def');
12669:     my @authtypes = ('internal','krb4','krb5','localauth');
12670:     foreach my $item (@items) {
12671:         $newvalues{$item} = $env{'form.'.$item};
12672:         if ($item eq 'auth_def') {
12673:             if ($newvalues{$item} ne '') {
12674:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
12675:                     push(@errors,$item);
12676:                 }
12677:             }
12678:         } elsif ($item eq 'lang_def') {
12679:             if ($newvalues{$item} ne '') {
12680:                 if ($newvalues{$item} =~ /^(\w+)/) {
12681:                     my $langcode = $1;
12682:                     if ($langcode ne 'x_chef') {
12683:                         if (code2language($langcode) eq '') {
12684:                             push(@errors,$item);
12685:                         }
12686:                     }
12687:                 } else {
12688:                     push(@errors,$item);
12689:                 }
12690:             }
12691:         } elsif ($item eq 'timezone_def') {
12692:             if ($newvalues{$item} ne '') {
12693:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
12694:                     push(@errors,$item);   
12695:                 }
12696:             }
12697:         } elsif ($item eq 'datelocale_def') {
12698:             if ($newvalues{$item} ne '') {
12699:                 my @datelocale_ids = DateTime::Locale->ids();
12700:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
12701:                     push(@errors,$item);
12702:                 }
12703:             }
12704:         } elsif ($item eq 'portal_def') {
12705:             if ($newvalues{$item} ne '') {
12706:                 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])\/?$/) {
12707:                     push(@errors,$item);
12708:                 }
12709:             }
12710:         }
12711:         if (grep(/^\Q$item\E$/,@errors)) {
12712:             $newvalues{$item} = $domdefaults{$item};
12713:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12714:             $changes{$item} = 1;
12715:         }
12716:         $domdefaults{$item} = $newvalues{$item};
12717:     }
12718:     my %staticdefaults = (
12719:                            'intauth_cost'   => 10,
12720:                            'intauth_check'  => 0,
12721:                            'intauth_switch' => 0,
12722:                          );
12723:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
12724:         if (exists($domdefaults{$item})) {
12725:             $newvalues{$item} = $domdefaults{$item};
12726:         } else {
12727:             $newvalues{$item} = $staticdefaults{$item};
12728:         }
12729:     }
12730:     my %defaults_hash = (
12731:                          defaults => \%newvalues,
12732:                         );
12733:     my $title = &defaults_titles();
12734: 
12735:     my $currinststatus;
12736:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12737:         $currinststatus = $domconfig{'inststatus'};
12738:     } else {
12739:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12740:         $currinststatus = {
12741:                              inststatustypes => $usertypes,
12742:                              inststatusorder => $types,
12743:                              inststatusguest => [],
12744:                           };
12745:     }
12746:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12747:     my @allpos;
12748:     my %alltypes;
12749:     my @inststatusguest;
12750:     if (ref($currinststatus) eq 'HASH') {
12751:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12752:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
12753:                 unless (grep(/^\Q$type\E$/,@todelete)) {
12754:                     push(@inststatusguest,$type);
12755:                 }
12756:             }
12757:         }
12758:     }
12759:     my ($currtitles,$currorder);
12760:     if (ref($currinststatus) eq 'HASH') {
12761:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12762:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12763:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12764:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12765:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12766:                     }
12767:                 }
12768:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12769:                     my $position = $env{'form.inststatus_pos_'.$type};
12770:                     $position =~ s/\D+//g;
12771:                     $allpos[$position] = $type;
12772:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12773:                     $alltypes{$type} =~ s/`//g;
12774:                 }
12775:             }
12776:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12777:             $currtitles =~ s/,$//;
12778:         }
12779:     }
12780:     if ($env{'form.addinststatus'}) {
12781:         my $newtype = $env{'form.addinststatus'};
12782:         $newtype =~ s/\W//g;
12783:         unless (exists($alltypes{$newtype})) {
12784:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12785:             $alltypes{$newtype} =~ s/`//g; 
12786:             my $position = $env{'form.addinststatus_pos'};
12787:             $position =~ s/\D+//g;
12788:             if ($position ne '') {
12789:                 $allpos[$position] = $newtype;
12790:             }
12791:         }
12792:     }
12793:     my @orderedstatus;
12794:     foreach my $type (@allpos) {
12795:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12796:             push(@orderedstatus,$type);
12797:         }
12798:     }
12799:     foreach my $type (keys(%alltypes)) {
12800:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12801:             delete($alltypes{$type});
12802:         }
12803:     }
12804:     $defaults_hash{'inststatus'} = {
12805:                                      inststatustypes => \%alltypes,
12806:                                      inststatusorder => \@orderedstatus,
12807:                                      inststatusguest => \@inststatusguest,
12808:                                    };
12809:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12810:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12811:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12812:         }
12813:     }
12814:     if ($currorder ne join(',',@orderedstatus)) {
12815:         $changes{'inststatus'}{'inststatusorder'} = 1;
12816:     }
12817:     my $newtitles;
12818:     foreach my $item (@orderedstatus) {
12819:         $newtitles .= $alltypes{$item}.',';
12820:     }
12821:     $newtitles =~ s/,$//;
12822:     if ($currtitles ne $newtitles) {
12823:         $changes{'inststatus'}{'inststatustypes'} = 1;
12824:     }
12825:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12826:                                              $dom);
12827:     if ($putresult eq 'ok') {
12828:         if (keys(%changes) > 0) {
12829:             $resulttext = &mt('Changes made:').'<ul>';
12830:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12831:             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";
12832:             foreach my $item (sort(keys(%changes))) {
12833:                 if ($item eq 'inststatus') {
12834:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12835:                         if (@orderedstatus) {
12836:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12837:                             foreach my $type (@orderedstatus) { 
12838:                                 $resulttext .= $alltypes{$type}.', ';
12839:                             }
12840:                             $resulttext =~ s/, $//;
12841:                             $resulttext .= '</li>';
12842:                         } else {
12843:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
12844:                         }
12845:                     }
12846:                 } else {
12847:                     my $value = $env{'form.'.$item};
12848:                     if ($value eq '') {
12849:                         $value = &mt('none');
12850:                     } elsif ($item eq 'auth_def') {
12851:                         my %authnames = &authtype_names();
12852:                         my %shortauth = (
12853:                                           internal   => 'int',
12854:                                           krb4       => 'krb4',
12855:                                           krb5       => 'krb5',
12856:                                           localauth  => 'loc',
12857:                         );
12858:                         $value = $authnames{$shortauth{$value}};
12859:                     }
12860:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12861:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12862:                 }
12863:             }
12864:             $resulttext .= '</ul>';
12865:             $mailmsgtext .= "\n";
12866:             my $cachetime = 24*60*60;
12867:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12868:             if (ref($lastactref) eq 'HASH') {
12869:                 $lastactref->{'domdefaults'} = 1;
12870:             }
12871:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12872:                 my $notify = 1;
12873:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12874:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12875:                         $notify = 0;
12876:                     }
12877:                 }
12878:                 if ($notify) {
12879:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12880:                                                "LON-CAPA Domain Settings Change - $dom",
12881:                                                $mailmsgtext);
12882:                 }
12883:             }
12884:         } else {
12885:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12886:         }
12887:     } else {
12888:         $resulttext = '<span class="LC_error">'.
12889:             &mt('An error occurred: [_1]',$putresult).'</span>';
12890:     }
12891:     if (@errors > 0) {
12892:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12893:         foreach my $item (@errors) {
12894:             $resulttext .= ' "'.$title->{$item}.'",';
12895:         }
12896:         $resulttext =~ s/,$//;
12897:     }
12898:     return $resulttext;
12899: }
12900: 
12901: sub modify_scantron {
12902:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12903:     my ($resulttext,%confhash,%changes,$errors);
12904:     my $custom = 'custom.tab';
12905:     my $default = 'default.tab';
12906:     my $servadm = $r->dir_config('lonAdmEMail');
12907:     my ($configuserok,$author_ok,$switchserver) =
12908:         &config_check($dom,$confname,$servadm);
12909:     if ($env{'form.scantronformat.filename'} ne '') {
12910:         my $error;
12911:         if ($configuserok eq 'ok') {
12912:             if ($switchserver) {
12913:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12914:             } else {
12915:                 if ($author_ok eq 'ok') {
12916:                     my ($result,$scantronurl) =
12917:                         &publishlogo($r,'upload','scantronformat',$dom,
12918:                                      $confname,'scantron','','',$custom);
12919:                     if ($result eq 'ok') {
12920:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12921:                         $changes{'scantronformat'} = 1;
12922:                     } else {
12923:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12924:                     }
12925:                 } else {
12926:                     $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);
12927:                 }
12928:             }
12929:         } else {
12930:             $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);
12931:         }
12932:         if ($error) {
12933:             &Apache::lonnet::logthis($error);
12934:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12935:         }
12936:     }
12937:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12938:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12939:             if ($env{'form.scantronformat_del'}) {
12940:                 $confhash{'scantron'}{'scantronformat'} = '';
12941:                 $changes{'scantronformat'} = 1;
12942:             } else {
12943:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
12944:             }
12945:         }
12946:     }
12947:     my @options = ('hdr','pad','rem');
12948:     my @fields = &scantroncsv_fields();
12949:     my %titles = &scantronconfig_titles();
12950:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
12951:     my ($newdat,$currdat,%newcol,%currcol);
12952:     if (grep(/^dat$/,@formats)) {
12953:         $confhash{'scantron'}{config}{dat} = 1;
12954:         $newdat = 1;
12955:     } else {
12956:         $newdat = 0;
12957:     }
12958:     if (grep(/^csv$/,@formats)) {
12959:         my %bynum;
12960:         foreach my $field (@fields) {
12961:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
12962:                 my $posscol = $1;
12963:                 if (($posscol < 20) && (!$bynum{$posscol})) {
12964:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
12965:                     $bynum{$posscol} = $field;
12966:                     $newcol{$field} = $posscol;
12967:                 }
12968:             }
12969:         }
12970:         if (keys(%newcol)) {
12971:             foreach my $option (@options) {
12972:                 if ($env{'form.scantroncsv_'.$option}) {
12973:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
12974:                 }
12975:             }
12976:         }
12977:     }
12978:     $currdat = 1;
12979:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12980:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
12981:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
12982:                 $currdat = 0;
12983:             }
12984:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
12985:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
12986:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
12987:                 }
12988:             }
12989:         }
12990:     }
12991:     if ($currdat != $newdat) {
12992:         $changes{'config'} = 1;
12993:     } else {
12994:         foreach my $field (@fields) {
12995:             if ($currcol{$field} ne '') {
12996:                 if ($currcol{$field} ne $newcol{$field}) {
12997:                     $changes{'config'} = 1;
12998:                     last;
12999:                 }
13000:             } elsif ($newcol{$field} ne '') {
13001:                 $changes{'config'} = 1;
13002:                 last;
13003:             }
13004:         }
13005:     }
13006:     if (keys(%confhash) > 0) {
13007:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
13008:                                                  $dom);
13009:         if ($putresult eq 'ok') {
13010:             if (keys(%changes) > 0) {
13011:                 if (ref($confhash{'scantron'}) eq 'HASH') {
13012:                     $resulttext = &mt('Changes made:').'<ul>';
13013:                     if ($changes{'scantronformat'}) {
13014:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
13015:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
13016:                         } else {
13017:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
13018:                         }
13019:                     }
13020:                     if ($changes{'config'}) {
13021:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
13022:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
13023:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
13024:                             }
13025:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
13026:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
13027:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
13028:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
13029:                                         foreach my $field (@fields) {
13030:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
13031:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
13032:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
13033:                                             }
13034:                                         }
13035:                                         $resulttext .= '</ul></li>';
13036:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
13037:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
13038:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
13039:                                                 foreach my $option (@options) {
13040:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
13041:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
13042:                                                     }
13043:                                                 }
13044:                                                 $resulttext .= '</ul></li>';
13045:                                             }
13046:                                         }
13047:                                     }
13048:                                 }
13049:                             }
13050:                         } else {
13051:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
13052:                         }
13053:                     }
13054:                     $resulttext .= '</ul>';
13055:                 } else {
13056:                     $resulttext = &mt('Changes made to bubblesheet format file.');
13057:                 }
13058:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
13059:                 if (ref($lastactref) eq 'HASH') {
13060:                     $lastactref->{'domainconfig'} = 1;
13061:                 }
13062:             } else {
13063:                 $resulttext = &mt('No changes made to bubblesheet format settings');
13064:             }
13065:         } else {
13066:             $resulttext = '<span class="LC_error">'.
13067:                 &mt('An error occurred: [_1]',$putresult).'</span>';
13068:         }
13069:     } else {
13070:         $resulttext = &mt('No changes made to bubblesheet format settings');
13071:     }
13072:     if ($errors) {
13073:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13074:                        $errors.'</ul>';
13075:     }
13076:     return $resulttext;
13077: }
13078: 
13079: sub modify_coursecategories {
13080:     my ($dom,$lastactref,%domconfig) = @_;
13081:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
13082:         $cathash);
13083:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
13084:     my @catitems = ('unauth','auth');
13085:     my @cattypes = ('std','domonly','codesrch','none');
13086:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
13087:         $cathash = $domconfig{'coursecategories'}{'cats'};
13088:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
13089:             $changes{'togglecats'} = 1;
13090:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
13091:         }
13092:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
13093:             $changes{'categorize'} = 1;
13094:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
13095:         }
13096:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
13097:             $changes{'togglecatscomm'} = 1;
13098:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
13099:         }
13100:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
13101:             $changes{'categorizecomm'} = 1;
13102:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
13103:         }
13104:         foreach my $item (@catitems) {
13105:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
13106:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
13107:                     $changes{$item} = 1;
13108:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
13109:                 }
13110:             }
13111:         }
13112:     } else {
13113:         $changes{'togglecats'} = 1;
13114:         $changes{'categorize'} = 1;
13115:         $changes{'togglecatscomm'} = 1;
13116:         $changes{'categorizecomm'} = 1;
13117:         $domconfig{'coursecategories'} = {
13118:                                              togglecats => $env{'form.togglecats'},
13119:                                              categorize => $env{'form.categorize'},
13120:                                              togglecatscomm => $env{'form.togglecatscomm'},
13121:                                              categorizecomm => $env{'form.categorizecomm'},
13122:                                          };
13123:         foreach my $item (@catitems) {
13124:             if ($env{'form.coursecat_'.$item} ne 'std') {
13125:                 $changes{$item} = 1;
13126:             }
13127:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
13128:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
13129:             }
13130:         }
13131:     }
13132:     if (ref($cathash) eq 'HASH') {
13133:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
13134:             push (@deletecategory,'instcode::0');
13135:         }
13136:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
13137:             push(@deletecategory,'communities::0');
13138:         }
13139:     }
13140:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
13141:     if (ref($cathash) eq 'HASH') {
13142:         if (@deletecategory > 0) {
13143:             #FIXME Need to remove category from all courses using a deleted category 
13144:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
13145:             foreach my $item (@deletecategory) {
13146:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
13147:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
13148:                     $deletions{$item} = 1;
13149:                     &recurse_cat_deletes($item,$cathash,\%deletions);
13150:                 }
13151:             }
13152:         }
13153:         foreach my $item (keys(%{$cathash})) {
13154:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
13155:             if ($cathash->{$item} ne $env{'form.'.$item}) {
13156:                 $reorderings{$item} = 1;
13157:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
13158:             }
13159:             if ($env{'form.addcategory_name_'.$item} ne '') {
13160:                 my $newcat = $env{'form.addcategory_name_'.$item};
13161:                 my $newdepth = $depth+1;
13162:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
13163:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
13164:                 $adds{$newitem} = 1; 
13165:             }
13166:             if ($env{'form.subcat_'.$item} ne '') {
13167:                 my $newcat = $env{'form.subcat_'.$item};
13168:                 my $newdepth = $depth+1;
13169:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
13170:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
13171:                 $adds{$newitem} = 1;
13172:             }
13173:         }
13174:     }
13175:     if ($env{'form.instcode'} eq '1') {
13176:         if (ref($cathash) eq 'HASH') {
13177:             my $newitem = 'instcode::0';
13178:             if ($cathash->{$newitem} eq '') {  
13179:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13180:                 $adds{$newitem} = 1;
13181:             }
13182:         } else {
13183:             my $newitem = 'instcode::0';
13184:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13185:             $adds{$newitem} = 1;
13186:         }
13187:     }
13188:     if ($env{'form.communities'} eq '1') {
13189:         if (ref($cathash) eq 'HASH') {
13190:             my $newitem = 'communities::0';
13191:             if ($cathash->{$newitem} eq '') {
13192:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13193:                 $adds{$newitem} = 1;
13194:             }
13195:         } else {
13196:             my $newitem = 'communities::0';
13197:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13198:             $adds{$newitem} = 1;
13199:         }
13200:     }
13201:     if ($env{'form.addcategory_name'} ne '') {
13202:         if (($env{'form.addcategory_name'} ne 'instcode') &&
13203:             ($env{'form.addcategory_name'} ne 'communities')) {
13204:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
13205:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
13206:             $adds{$newitem} = 1;
13207:         }
13208:     }
13209:     my $putresult;
13210:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13211:         if (keys(%deletions) > 0) {
13212:             foreach my $key (keys(%deletions)) {
13213:                 if ($predelallitems{$key} ne '') {
13214:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
13215:                 }
13216:             }
13217:         }
13218:         my (@chkcats,@chktrails,%chkallitems);
13219:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
13220:         if (ref($chkcats[0]) eq 'ARRAY') {
13221:             my $depth = 0;
13222:             my $chg = 0;
13223:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
13224:                 my $name = $chkcats[0][$i];
13225:                 my $item;
13226:                 if ($name eq '') {
13227:                     $chg ++;
13228:                 } else {
13229:                     $item = &escape($name).'::0';
13230:                     if ($chg) {
13231:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
13232:                     }
13233:                     $depth ++; 
13234:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
13235:                     $depth --;
13236:                 }
13237:             }
13238:         }
13239:     }
13240:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13241:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
13242:         if ($putresult eq 'ok') {
13243:             my %title = (
13244:                          togglecats     => 'Show/Hide a course in catalog',
13245:                          categorize     => 'Assign a category to a course',
13246:                          togglecatscomm => 'Show/Hide a community in catalog',
13247:                          categorizecomm => 'Assign a category to a community',
13248:                         );
13249:             my %level = (
13250:                          dom  => 'set in Domain ("Modify Course/Community")',
13251:                          crs  => 'set in Course ("Course Configuration")',
13252:                          comm => 'set in Community ("Community Configuration")',
13253:                          none     => 'No catalog',
13254:                          std      => 'Standard catalog',
13255:                          domonly  => 'Domain-only catalog',
13256:                          codesrch => 'Code search form',
13257:                         );
13258:             $resulttext = &mt('Changes made:').'<ul>';
13259:             if ($changes{'togglecats'}) {
13260:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
13261:             }
13262:             if ($changes{'categorize'}) {
13263:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
13264:             }
13265:             if ($changes{'togglecatscomm'}) {
13266:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
13267:             }
13268:             if ($changes{'categorizecomm'}) {
13269:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
13270:             }
13271:             if ($changes{'unauth'}) {
13272:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
13273:             }
13274:             if ($changes{'auth'}) {
13275:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
13276:             }
13277:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13278:                 my $cathash;
13279:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
13280:                     $cathash = $domconfig{'coursecategories'}{'cats'};
13281:                 } else {
13282:                     $cathash = {};
13283:                 } 
13284:                 my (@cats,@trails,%allitems);
13285:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
13286:                 if (keys(%deletions) > 0) {
13287:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
13288:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
13289:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
13290:                     }
13291:                     $resulttext .= '</ul></li>';
13292:                 }
13293:                 if (keys(%reorderings) > 0) {
13294:                     my %sort_by_trail;
13295:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
13296:                     foreach my $key (keys(%reorderings)) {
13297:                         if ($allitems{$key} ne '') {
13298:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13299:                         }
13300:                     }
13301:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13302:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13303:                     }
13304:                     $resulttext .= '</ul></li>';
13305:                 }
13306:                 if (keys(%adds) > 0) {
13307:                     my %sort_by_trail;
13308:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
13309:                     foreach my $key (keys(%adds)) {
13310:                         if ($allitems{$key} ne '') {
13311:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13312:                         }
13313:                     }
13314:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13315:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13316:                     }
13317:                     $resulttext .= '</ul></li>';
13318:                 }
13319:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
13320:                 if (ref($lastactref) eq 'HASH') {
13321:                     $lastactref->{'cats'} = 1;
13322:                 }
13323:             }
13324:             $resulttext .= '</ul>';
13325:             if ($changes{'unauth'} || $changes{'auth'}) {
13326:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13327:                 if ($changes{'auth'}) {
13328:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
13329:                 }
13330:                 if ($changes{'unauth'}) {
13331:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
13332:                 }
13333:                 my $cachetime = 24*60*60;
13334:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13335:                 if (ref($lastactref) eq 'HASH') {
13336:                     $lastactref->{'domdefaults'} = 1;
13337:                 }
13338:             }
13339:         } else {
13340:             $resulttext = '<span class="LC_error">'.
13341:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13342:         }
13343:     } else {
13344:         $resulttext = &mt('No changes made to course and community categories');
13345:     }
13346:     return $resulttext;
13347: }
13348: 
13349: sub modify_serverstatuses {
13350:     my ($dom,%domconfig) = @_;
13351:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
13352:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
13353:         %currserverstatus = %{$domconfig{'serverstatuses'}};
13354:     }
13355:     my @pages = &serverstatus_pages();
13356:     foreach my $type (@pages) {
13357:         $newserverstatus{$type}{'namedusers'} = '';
13358:         $newserverstatus{$type}{'machines'} = '';
13359:         if (defined($env{'form.'.$type.'_namedusers'})) {
13360:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
13361:             my @okusers;
13362:             foreach my $user (@users) {
13363:                 my ($uname,$udom) = split(/:/,$user);
13364:                 if (($udom =~ /^$match_domain$/) &&   
13365:                     (&Apache::lonnet::domain($udom)) &&
13366:                     ($uname =~ /^$match_username$/)) {
13367:                     if (!grep(/^\Q$user\E/,@okusers)) {
13368:                         push(@okusers,$user);
13369:                     }
13370:                 }
13371:             }
13372:             if (@okusers > 0) {
13373:                  @okusers = sort(@okusers);
13374:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
13375:             }
13376:         }
13377:         if (defined($env{'form.'.$type.'_machines'})) {
13378:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
13379:             my @okmachines;
13380:             foreach my $ip (@machines) {
13381:                 my @parts = split(/\./,$ip);
13382:                 next if (@parts < 4);
13383:                 my $badip = 0;
13384:                 for (my $i=0; $i<4; $i++) {
13385:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
13386:                         $badip = 1;
13387:                         last;
13388:                     }
13389:                 }
13390:                 if (!$badip) {
13391:                     push(@okmachines,$ip);     
13392:                 }
13393:             }
13394:             @okmachines = sort(@okmachines);
13395:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
13396:         }
13397:     }
13398:     my %serverstatushash =  (
13399:                                 serverstatuses => \%newserverstatus,
13400:                             );
13401:     foreach my $type (@pages) {
13402:         foreach my $setting ('namedusers','machines') {
13403:             my (@current,@new);
13404:             if (ref($currserverstatus{$type}) eq 'HASH') {
13405:                 if ($currserverstatus{$type}{$setting} ne '') { 
13406:                     @current = split(/,/,$currserverstatus{$type}{$setting});
13407:                 }
13408:             }
13409:             if ($newserverstatus{$type}{$setting} ne '') {
13410:                 @new = split(/,/,$newserverstatus{$type}{$setting});
13411:             }
13412:             if (@current > 0) {
13413:                 if (@new > 0) {
13414:                     foreach my $item (@current) {
13415:                         if (!grep(/^\Q$item\E$/,@new)) {
13416:                             $changes{$type}{$setting} = 1;
13417:                             last;
13418:                         }
13419:                     }
13420:                     foreach my $item (@new) {
13421:                         if (!grep(/^\Q$item\E$/,@current)) {
13422:                             $changes{$type}{$setting} = 1;
13423:                             last;
13424:                         }
13425:                     }
13426:                 } else {
13427:                     $changes{$type}{$setting} = 1;
13428:                 }
13429:             } elsif (@new > 0) {
13430:                 $changes{$type}{$setting} = 1;
13431:             }
13432:         }
13433:     }
13434:     if (keys(%changes) > 0) {
13435:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
13436:         my $putresult = &Apache::lonnet::put_dom('configuration',
13437:                                                  \%serverstatushash,$dom);
13438:         if ($putresult eq 'ok') {
13439:             $resulttext .= &mt('Changes made:').'<ul>';
13440:             foreach my $type (@pages) {
13441:                 if (ref($changes{$type}) eq 'HASH') {
13442:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
13443:                     if ($changes{$type}{'namedusers'}) {
13444:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
13445:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
13446:                         } else {
13447:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
13448:                         }
13449:                     }
13450:                     if ($changes{$type}{'machines'}) {
13451:                         if ($newserverstatus{$type}{'machines'} eq '') {
13452:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
13453:                         } else {
13454:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
13455:                         }
13456: 
13457:                     }
13458:                     $resulttext .= '</ul></li>';
13459:                 }
13460:             }
13461:             $resulttext .= '</ul>';
13462:         } else {
13463:             $resulttext = '<span class="LC_error">'.
13464:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
13465: 
13466:         }
13467:     } else {
13468:         $resulttext = &mt('No changes made to access to server status pages');
13469:     }
13470:     return $resulttext;
13471: }
13472: 
13473: sub modify_helpsettings {
13474:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13475:     my ($resulttext,$errors,%changes,%helphash);
13476:     my %defaultchecked = ('submitbugs' => 'on');
13477:     my @offon = ('off','on');
13478:     my @toggles = ('submitbugs');
13479:     my %current = ('submitbugs' => '',
13480:                    'adhoc'      => {},
13481:                   );
13482:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
13483:         %current = %{$domconfig{'helpsettings'}};
13484:     }
13485:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13486:     foreach my $item (@toggles) {
13487:         if ($defaultchecked{$item} eq 'on') { 
13488:             if ($current{$item} eq '') {
13489:                 if ($env{'form.'.$item} eq '0') {
13490:                     $changes{$item} = 1;
13491:                 }
13492:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13493:                 $changes{$item} = 1;
13494:             }
13495:         } elsif ($defaultchecked{$item} eq 'off') {
13496:             if ($current{$item} eq '') {
13497:                 if ($env{'form.'.$item} eq '1') {
13498:                     $changes{$item} = 1;
13499:                 }
13500:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13501:                 $changes{$item} = 1;
13502:             }
13503:         }
13504:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
13505:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
13506:         }
13507:     }
13508:     my $maxnum = $env{'form.helproles_maxnum'};
13509:     my $confname = $dom.'-domainconfig';
13510:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
13511:     my (@allpos,%newsettings,%changedprivs,$newrole);
13512:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13513:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
13514:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
13515:     my %lt = &Apache::lonlocal::texthash(
13516:                     s      => 'system',
13517:                     d      => 'domain',
13518:                     order  => 'Display order',
13519:                     access => 'Role usage',
13520:                     all    => 'All with domain helpdesk or helpdesk assistant role',
13521:                     dh     => 'All with domain helpdesk role',
13522:                     da     => 'All with domain helpdesk assistant role',
13523:                     none   => 'None',
13524:                     status => 'Determined based on institutional status',
13525:                     inc    => 'Include all, but exclude specific personnel',
13526:                     exc    => 'Exclude all, but include specific personnel',
13527:     );
13528:     for (my $num=0; $num<=$maxnum; $num++) {
13529:         my ($prefix,$identifier,$rolename,%curr);
13530:         if ($num == $maxnum) {
13531:             next unless ($env{'form.newcusthelp'} == $maxnum);
13532:             $identifier = 'custhelp'.$num;
13533:             $prefix = 'helproles_'.$num;
13534:             $rolename = $env{'form.custhelpname'.$num};
13535:             $rolename=~s/[^A-Za-z0-9]//gs;
13536:             next if ($rolename eq '');
13537:             next if (exists($existing{'rolesdef_'.$rolename}));
13538:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13539:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13540:                                                      $newprivs{'c'},$confname,$dom);
13541:             if ($result ne 'ok') {
13542:                 $errors .= '<li><span class="LC_error">'.
13543:                            &mt('An error occurred storing the new custom role: [_1]',
13544:                            $result).'</span></li>';
13545:                 next;
13546:             } else {
13547:                 $changedprivs{$rolename} = \%newprivs;
13548:                 $newrole = $rolename;
13549:             }
13550:         } else {
13551:             $prefix = 'helproles_'.$num;
13552:             $rolename = $env{'form.'.$prefix};
13553:             next if ($rolename eq '');
13554:             next unless (exists($existing{'rolesdef_'.$rolename}));
13555:             $identifier = 'custhelp'.$num;
13556:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13557:             my %currprivs;
13558:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
13559:                 split(/\_/,$existing{'rolesdef_'.$rolename});
13560:             foreach my $level ('c','d','s') {
13561:                 if ($newprivs{$level} ne $currprivs{$level}) {
13562:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13563:                                                              $newprivs{'c'},$confname,$dom);
13564:                     if ($result ne 'ok') {
13565:                         $errors .= '<li><span class="LC_error">'.
13566:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
13567:                                        $rolename,$result).'</span></li>';
13568:                     } else {
13569:                         $changedprivs{$rolename} = \%newprivs;
13570:                     }
13571:                     last;
13572:                 }
13573:             }
13574:             if (ref($current{'adhoc'}) eq 'HASH') {
13575:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13576:                     %curr = %{$current{'adhoc'}{$rolename}};
13577:                 }
13578:             }
13579:         }
13580:         my $newpos = $env{'form.'.$prefix.'_pos'};
13581:         $newpos =~ s/\D+//g;
13582:         $allpos[$newpos] = $rolename;
13583:         my $newdesc = $env{'form.'.$prefix.'_desc'};
13584:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
13585:         if ($curr{'desc'}) {
13586:             if ($curr{'desc'} ne $newdesc) {
13587:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
13588:                 $newsettings{$rolename}{'desc'} = $newdesc;
13589:             }
13590:         } elsif ($newdesc ne '') {
13591:             $changes{'customrole'}{$rolename}{'desc'} = 1;
13592:             $newsettings{$rolename}{'desc'} = $newdesc;
13593:         }
13594:         my $access = $env{'form.'.$prefix.'_access'};
13595:         if (grep(/^\Q$access\E$/,@accesstypes)) {
13596:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
13597:             if ($access eq 'status') {
13598:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
13599:                 if (scalar(@statuses) == 0) {
13600:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
13601:                 } else {
13602:                     my (@shownstatus,$numtypes);
13603:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13604:                     if (ref($types) eq 'ARRAY') {
13605:                         $numtypes = scalar(@{$types});
13606:                         foreach my $type (sort(@statuses)) {
13607:                             if ($type eq 'default') {
13608:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13609:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
13610:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13611:                                 push(@shownstatus,$usertypes->{$type});
13612:                             }
13613:                         }
13614:                     }
13615:                     if (grep(/^default$/,@statuses)) {
13616:                         push(@shownstatus,$othertitle);
13617:                     }
13618:                     if (scalar(@shownstatus) == 1+$numtypes) {
13619:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
13620:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
13621:                     } else {
13622:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
13623:                         if (ref($curr{'status'}) eq 'ARRAY') {
13624:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13625:                             if (@diffs) {
13626:                                 $changes{'customrole'}{$rolename}{$access} = 1;
13627:                             }
13628:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13629:                             $changes{'customrole'}{$rolename}{$access} = 1;
13630:                         }
13631:                     }
13632:                 }
13633:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
13634:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
13635:                 my @newspecstaff;
13636:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13637:                 foreach my $person (sort(@personnel)) {
13638:                     if ($domhelpdesk{$person}) {
13639:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
13640:                     }
13641:                 }
13642:                 if (ref($curr{$access}) eq 'ARRAY') {
13643:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13644:                     if (@diffs) {
13645:                         $changes{'customrole'}{$rolename}{$access} = 1;
13646:                     }
13647:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13648:                     $changes{'customrole'}{$rolename}{$access} = 1;
13649:                 }
13650:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13651:                     my ($uname,$udom) = split(/:/,$person);
13652:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
13653:                 }
13654:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
13655:             }
13656:         } else {
13657:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
13658:         }
13659:         unless ($curr{'access'} eq $access) {
13660:             $changes{'customrole'}{$rolename}{'access'} = 1;
13661:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
13662:         }
13663:     }
13664:     if (@allpos > 0) {
13665:         my $idx = 0;
13666:         foreach my $rolename (@allpos) {
13667:             if ($rolename ne '') {
13668:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
13669:                 if (ref($current{'adhoc'}) eq 'HASH') {
13670:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13671:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
13672:                             $changes{'customrole'}{$rolename}{'order'} = 1;
13673:                             $newsettings{$rolename}{'order'} = $idx+1;
13674:                         }
13675:                     }
13676:                 }
13677:                 $idx ++;
13678:             }
13679:         }
13680:     }
13681:     my $putresult;
13682:     if (keys(%changes) > 0) {
13683:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
13684:         if ($putresult eq 'ok') {
13685:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
13686:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
13687:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
13688:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
13689:                 }
13690:             }
13691:             my $cachetime = 24*60*60;
13692:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13693:             if (ref($lastactref) eq 'HASH') {
13694:                 $lastactref->{'domdefaults'} = 1;
13695:             }
13696:         } else {
13697:             $errors .= '<li><span class="LC_error">'.
13698:                        &mt('An error occurred storing the settings: [_1]',
13699:                            $putresult).'</span></li>';
13700:         }
13701:     }
13702:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
13703:         $resulttext = &mt('Changes made:').'<ul>';
13704:         my (%shownprivs,@levelorder);
13705:         @levelorder = ('c','d','s');
13706:         if ((keys(%changes)) && ($putresult eq 'ok')) {
13707:             foreach my $item (sort(keys(%changes))) {
13708:                 if ($item eq 'submitbugs') {
13709:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
13710:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
13711:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
13712:                 } elsif ($item eq 'customrole') {
13713:                     if (ref($changes{'customrole'}) eq 'HASH') {
13714:                         my @keyorder = ('order','desc','access','status','exc','inc');
13715:                         my %keytext = &Apache::lonlocal::texthash(
13716:                                                                    order  => 'Order',
13717:                                                                    desc   => 'Role description',
13718:                                                                    access => 'Role usage',
13719:                                                                    status => 'Allowed institutional types',
13720:                                                                    exc    => 'Allowed personnel',
13721:                                                                    inc    => 'Disallowed personnel',
13722:                         );
13723:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
13724:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
13725:                                 if ($role eq $newrole) {
13726:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
13727:                                                               $role).'<ul>';
13728:                                 } else {
13729:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13730:                                                               $role).'<ul>';
13731:                                 }
13732:                                 foreach my $key (@keyorder) {
13733:                                     if ($changes{'customrole'}{$role}{$key}) {
13734:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
13735:                                                                   $keytext{$key},$newsettings{$role}{$key}).
13736:                                                        '</li>';
13737:                                     }
13738:                                 }
13739:                                 if (ref($changedprivs{$role}) eq 'HASH') {
13740:                                     $shownprivs{$role} = 1;
13741:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
13742:                                     foreach my $level (@levelorder) {
13743:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13744:                                             next if ($item eq '');
13745:                                             my ($priv) = split(/\&/,$item,2);
13746:                                             if (&Apache::lonnet::plaintext($priv)) {
13747:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13748:                                                 unless ($level eq 'c') {
13749:                                                     $resulttext .= ' ('.$lt{$level}.')';
13750:                                                 }
13751:                                                 $resulttext .= '</li>';
13752:                                             }
13753:                                         }
13754:                                     }
13755:                                     $resulttext .= '</ul>';
13756:                                 }
13757:                                 $resulttext .= '</ul></li>';
13758:                             }
13759:                         }
13760:                     }
13761:                 }
13762:             }
13763:         }
13764:         if (keys(%changedprivs)) {
13765:             foreach my $role (sort(keys(%changedprivs))) {
13766:                 unless ($shownprivs{$role}) {
13767:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13768:                                               $role).'<ul>'.
13769:                                    '<li>'.&mt('Privileges set to :').'<ul>';
13770:                     foreach my $level (@levelorder) {
13771:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13772:                             next if ($item eq '');
13773:                             my ($priv) = split(/\&/,$item,2);
13774:                             if (&Apache::lonnet::plaintext($priv)) {
13775:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13776:                                 unless ($level eq 'c') {
13777:                                     $resulttext .= ' ('.$lt{$level}.')';
13778:                                 }
13779:                                 $resulttext .= '</li>';
13780:                             }
13781:                         }
13782:                     }
13783:                     $resulttext .= '</ul></li></ul></li>';
13784:                 }
13785:             }
13786:         }
13787:         $resulttext .= '</ul>';
13788:     } else {
13789:         $resulttext = &mt('No changes made to help settings');
13790:     }
13791:     if ($errors) {
13792:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13793:                                     $errors.'</ul>';
13794:     }
13795:     return $resulttext;
13796: }
13797: 
13798: sub modify_coursedefaults {
13799:     my ($dom,$lastactref,%domconfig) = @_;
13800:     my ($resulttext,$errors,%changes,%defaultshash);
13801:     my %defaultchecked = (
13802:                            'uselcmath'       => 'on',
13803:                            'usejsme'         => 'on'
13804:                          );
13805:     my @toggles = ('uselcmath','usejsme');
13806:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13807:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
13808:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
13809:     my @types = ('official','unofficial','community','textbook');
13810:     my %staticdefaults = (
13811:                            anonsurvey_threshold => 10,
13812:                            uploadquota          => 500,
13813:                            postsubmit           => 60,
13814:                            mysqltables          => 172800,
13815:                          );
13816:     my %texoptions = (
13817:                         MathJax  => 'MathJax',
13818:                         mimetex  => &mt('Convert to Images'),
13819:                         tth      => &mt('TeX to HTML'),
13820:                      );
13821:     $defaultshash{'coursedefaults'} = {};
13822: 
13823:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13824:         if ($domconfig{'coursedefaults'} eq '') {
13825:             $domconfig{'coursedefaults'} = {};
13826:         }
13827:     }
13828: 
13829:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13830:         foreach my $item (@toggles) {
13831:             if ($defaultchecked{$item} eq 'on') {
13832:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13833:                     ($env{'form.'.$item} eq '0')) {
13834:                     $changes{$item} = 1;
13835:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13836:                     $changes{$item} = 1;
13837:                 }
13838:             } elsif ($defaultchecked{$item} eq 'off') {
13839:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13840:                     ($env{'form.'.$item} eq '1')) {
13841:                     $changes{$item} = 1;
13842:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13843:                     $changes{$item} = 1;
13844:                 }
13845:             }
13846:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13847:         }
13848:         foreach my $item (@numbers) {
13849:             my ($currdef,$newdef);
13850:             $newdef = $env{'form.'.$item};
13851:             if ($item eq 'anonsurvey_threshold') {
13852:                 $currdef = $domconfig{'coursedefaults'}{$item};
13853:                 $newdef =~ s/\D//g;
13854:                 if ($newdef eq '' || $newdef < 1) {
13855:                     $newdef = 1;
13856:                 }
13857:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13858:             } else {
13859:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13860:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13861:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13862:                 }
13863:                 $newdef =~ s/[^\w.\-]//g;
13864:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13865:             }
13866:             if ($currdef ne $newdef) {
13867:                 if ($item eq 'anonsurvey_threshold') {
13868:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13869:                         $changes{$item} = 1;
13870:                     }
13871:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13872:                     my $setting = $1;
13873:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13874:                         $changes{$setting} = 1;
13875:                     }
13876:                 }
13877:             }
13878:         }
13879:         my $texengine;
13880:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
13881:             $texengine = $env{'form.texengine'};
13882:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
13883:             if ($currdef eq '') {
13884:                 unless ($texengine eq $Apache::lonnet::deftex) {
13885:                     $changes{'texengine'} = 1;
13886:                 }
13887:             } elsif ($currdef ne $texengine) {
13888:                 $changes{'texengine'} = 1;
13889:             }
13890:         }
13891:         if ($texengine ne '') {
13892:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
13893:         }
13894:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13895:         my @currclonecode;
13896:         if (ref($currclone) eq 'HASH') {
13897:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13898:                 @currclonecode = @{$currclone->{'instcode'}};
13899:             }
13900:         }
13901:         my $newclone;
13902:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13903:             $newclone = $env{'form.canclone'};
13904:         }
13905:         if ($newclone eq 'instcode') {
13906:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13907:             my (%codedefaults,@code_order,@clonecode);
13908:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13909:                                                     \@code_order);
13910:             foreach my $item (@code_order) {
13911:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13912:                     push(@clonecode,$item);
13913:                 }
13914:             }
13915:             if (@clonecode) {
13916:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13917:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13918:                 if (@diffs) {
13919:                     $changes{'canclone'} = 1;
13920:                 }
13921:             } else {
13922:                 $newclone eq '';
13923:             }
13924:         } elsif ($newclone ne '') {
13925:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13926:         }
13927:         if ($newclone ne $currclone) {
13928:             $changes{'canclone'} = 1;
13929:         }
13930:         my %credits;
13931:         foreach my $type (@types) {
13932:             unless ($type eq 'community') {
13933:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13934:                 $credits{$type} =~ s/[^\d.]+//g;
13935:             }
13936:         }
13937:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13938:             ($env{'form.coursecredits'} eq '1')) {
13939:             $changes{'coursecredits'} = 1;
13940:             foreach my $type (keys(%credits)) {
13941:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13942:             }
13943:         } else {
13944:             if ($env{'form.coursecredits'} eq '1') {
13945:                 foreach my $type (@types) {
13946:                     unless ($type eq 'community') {
13947:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13948:                             $changes{'coursecredits'} = 1;
13949:                         }
13950:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13951:                     }
13952:                 }
13953:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13954:                 foreach my $type (@types) {
13955:                     unless ($type eq 'community') {
13956:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13957:                             $changes{'coursecredits'} = 1;
13958:                             last;
13959:                         }
13960:                     }
13961:                 }
13962:             }
13963:         }
13964:         if ($env{'form.postsubmit'} eq '1') {
13965:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13966:             my %currtimeout;
13967:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13968:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13969:                     $changes{'postsubmit'} = 1;
13970:                 }
13971:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13972:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13973:                 }
13974:             } else {
13975:                 $changes{'postsubmit'} = 1;
13976:             }
13977:             foreach my $type (@types) {
13978:                 my $timeout = $env{'form.'.$type.'_timeout'};
13979:                 $timeout =~ s/\D//g;
13980:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13981:                     $timeout = '';
13982:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13983:                     $timeout = '0';
13984:                 }
13985:                 unless ($timeout eq '') {
13986:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13987:                 }
13988:                 if (exists($currtimeout{$type})) {
13989:                     if ($timeout ne $currtimeout{$type}) {
13990:                         $changes{'postsubmit'} = 1;
13991:                     }
13992:                 } elsif ($timeout ne '') {
13993:                     $changes{'postsubmit'} = 1;
13994:                 }
13995:             }
13996:         } else {
13997:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13998:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13999:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
14000:                     $changes{'postsubmit'} = 1;
14001:                 }
14002:             } else {
14003:                 $changes{'postsubmit'} = 1;
14004:             }
14005:         }
14006:     }
14007:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14008:                                              $dom);
14009:     if ($putresult eq 'ok') {
14010:         if (keys(%changes) > 0) {
14011:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14012:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
14013:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
14014:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
14015:                 foreach my $item ('uselcmath','usejsme','texengine') {
14016:                     if ($changes{$item}) {
14017:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
14018:                     }
14019:                 }
14020:                 if ($changes{'coursecredits'}) {
14021:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
14022:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
14023:                             $domdefaults{$type.'credits'} =
14024:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
14025:                         }
14026:                     }
14027:                 }
14028:                 if ($changes{'postsubmit'}) {
14029:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14030:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
14031:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
14032:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
14033:                                 $domdefaults{$type.'postsubtimeout'} =
14034:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
14035:                             }
14036:                         }
14037:                     }
14038:                 }
14039:                 if ($changes{'uploadquota'}) {
14040:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
14041:                         foreach my $type (@types) {
14042:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
14043:                         }
14044:                     }
14045:                 }
14046:                 if ($changes{'canclone'}) {
14047:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
14048:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
14049:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
14050:                             if (@clonecodes) {
14051:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
14052:                             }
14053:                         }
14054:                     } else {
14055:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
14056:                     }
14057:                 }
14058:                 my $cachetime = 24*60*60;
14059:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14060:                 if (ref($lastactref) eq 'HASH') {
14061:                     $lastactref->{'domdefaults'} = 1;
14062:                 }
14063:             }
14064:             $resulttext = &mt('Changes made:').'<ul>';
14065:             foreach my $item (sort(keys(%changes))) {
14066:                 if ($item eq 'uselcmath') {
14067:                     if ($env{'form.'.$item} eq '1') {
14068:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
14069:                     } else {
14070:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
14071:                     }
14072:                 } elsif ($item eq 'usejsme') {
14073:                     if ($env{'form.'.$item} eq '1') {
14074:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
14075:                     } else {
14076:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
14077:                     }
14078:                 } elsif ($item eq 'texengine') {
14079:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
14080:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
14081:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
14082:                     }
14083:                 } elsif ($item eq 'anonsurvey_threshold') {
14084:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
14085:                 } elsif ($item eq 'uploadquota') {
14086:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
14087:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
14088:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
14089:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
14090:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
14091: 
14092:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
14093:                                        '</ul>'.
14094:                                        '</li>';
14095:                     } else {
14096:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
14097:                     }
14098:                 } elsif ($item eq 'mysqltables') {
14099:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
14100:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
14101:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
14102:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
14103:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
14104:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
14105:                                        '</ul>'.
14106:                                        '</li>';
14107:                     } else {
14108:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
14109:                     }
14110:                 } elsif ($item eq 'postsubmit') {
14111:                     if ($domdefaults{'postsubmit'} eq 'off') {
14112:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
14113:                     } else {
14114:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
14115:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14116:                             $resulttext .= &mt('durations:').'<ul>';
14117:                             foreach my $type (@types) {
14118:                                 $resulttext .= '<li>';
14119:                                 my $timeout;
14120:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
14121:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
14122:                                 }
14123:                                 my $display;
14124:                                 if ($timeout eq '0') {
14125:                                     $display = &mt('unlimited');
14126:                                 } elsif ($timeout eq '') {
14127:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
14128:                                 } else {
14129:                                     $display = &mt('[quant,_1,second]',$timeout);
14130:                                 }
14131:                                 if ($type eq 'community') {
14132:                                     $resulttext .= &mt('Communities');
14133:                                 } elsif ($type eq 'official') {
14134:                                     $resulttext .= &mt('Official courses');
14135:                                 } elsif ($type eq 'unofficial') {
14136:                                     $resulttext .= &mt('Unofficial courses');
14137:                                 } elsif ($type eq 'textbook') {
14138:                                     $resulttext .= &mt('Textbook courses');
14139:                                 }
14140:                                 $resulttext .= ' -- '.$display.'</li>';
14141:                             }
14142:                             $resulttext .= '</ul>';
14143:                         }
14144:                         $resulttext .= '</li>';
14145:                     }
14146:                 } elsif ($item eq 'coursecredits') {
14147:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
14148:                         if (($domdefaults{'officialcredits'} eq '') &&
14149:                             ($domdefaults{'unofficialcredits'} eq '') &&
14150:                             ($domdefaults{'textbookcredits'} eq '')) {
14151:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
14152:                         } else {
14153:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
14154:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
14155:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
14156:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
14157:                                            '</ul>'.
14158:                                            '</li>';
14159:                         }
14160:                     } else {
14161:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
14162:                     }
14163:                 } elsif ($item eq 'canclone') {
14164:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
14165:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
14166:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
14167:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
14168:                         }
14169:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
14170:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
14171:                     } else {
14172:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
14173:                     }
14174:                 }
14175:             }
14176:             $resulttext .= '</ul>';
14177:         } else {
14178:             $resulttext = &mt('No changes made to course defaults');
14179:         }
14180:     } else {
14181:         $resulttext = '<span class="LC_error">'.
14182:             &mt('An error occurred: [_1]',$putresult).'</span>';
14183:     }
14184:     return $resulttext;
14185: }
14186: 
14187: sub modify_selfenrollment {
14188:     my ($dom,$lastactref,%domconfig) = @_;
14189:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
14190:     my @types = ('official','unofficial','community','textbook');
14191:     my %titles = &tool_titles();
14192:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
14193:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
14194:     $ordered{'default'} = ['types','registered','approval','limit'];
14195: 
14196:     my (%roles,%shown,%toplevel);
14197:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
14198: 
14199:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
14200:         if ($domconfig{'selfenrollment'} eq '') {
14201:             $domconfig{'selfenrollment'} = {};
14202:         }
14203:     }
14204:     %toplevel = (
14205:                   admin      => 'Configuration Rights',
14206:                   default    => 'Default settings',
14207:                   validation => 'Validation of self-enrollment requests',
14208:                 );
14209:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
14210: 
14211:     if (ref($ordered{'admin'}) eq 'ARRAY') {
14212:         foreach my $item (@{$ordered{'admin'}}) {
14213:             foreach my $type (@types) {
14214:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
14215:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
14216:                 } else {
14217:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
14218:                 }
14219:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
14220:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
14221:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
14222:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
14223:                             push(@{$changes{'admin'}{$type}},$item);
14224:                         }
14225:                     } else {
14226:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
14227:                             push(@{$changes{'admin'}{$type}},$item);
14228:                         }
14229:                     }
14230:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
14231:                     push(@{$changes{'admin'}{$type}},$item);
14232:                 }
14233:             }
14234:         }
14235:     }
14236: 
14237:     foreach my $item (@{$ordered{'default'}}) {
14238:         foreach my $type (@types) {
14239:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
14240:             if ($item eq 'types') {
14241:                 unless (($value eq 'all') || ($value eq 'dom')) {
14242:                     $value = '';
14243:                 }
14244:             } elsif ($item eq 'registered') {
14245:                 unless ($value eq '1') {
14246:                     $value = 0;
14247:                 }
14248:             } elsif ($item eq 'approval') {
14249:                 unless ($value =~ /^[012]$/) {
14250:                     $value = 0;
14251:                 }
14252:             } else {
14253:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14254:                     $value = 'none';
14255:                 }
14256:             }
14257:             $selfenrollhash{'default'}{$type}{$item} = $value;
14258:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
14259:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14260:                     if ($selfenrollhash{'default'}{$type}{$item} ne
14261:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
14262:                          push(@{$changes{'default'}{$type}},$item);
14263:                     }
14264:                 } else {
14265:                     push(@{$changes{'default'}{$type}},$item);
14266:                 }
14267:             } else {
14268:                 push(@{$changes{'default'}{$type}},$item);
14269:             }
14270:             if ($item eq 'limit') {
14271:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14272:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
14273:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
14274:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
14275:                     }
14276:                 } else {
14277:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
14278:                 }
14279:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14280:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
14281:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
14282:                          push(@{$changes{'default'}{$type}},'cap');
14283:                     }
14284:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
14285:                     push(@{$changes{'default'}{$type}},'cap');
14286:                 }
14287:             }
14288:         }
14289:     }
14290: 
14291:     foreach my $item (@{$itemsref}) {
14292:         if ($item eq 'fields') {
14293:             my @changed;
14294:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
14295:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
14296:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
14297:             }
14298:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14299:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
14300:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
14301:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
14302:                 } else {
14303:                     @changed = @{$selfenrollhash{'validation'}{$item}};
14304:                 }
14305:             } else {
14306:                 @changed = @{$selfenrollhash{'validation'}{$item}};
14307:             }
14308:             if (@changed) {
14309:                 if ($selfenrollhash{'validation'}{$item}) { 
14310:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
14311:                 } else {
14312:                     $changes{'validation'}{$item} = &mt('None');
14313:                 }
14314:             }
14315:         } else {
14316:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
14317:             if ($item eq 'markup') {
14318:                if ($env{'form.selfenroll_validation_'.$item}) {
14319:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14320:                }
14321:             }
14322:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14323:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
14324:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
14325:                 }
14326:             }
14327:         }
14328:     }
14329: 
14330:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
14331:                                              $dom);
14332:     if ($putresult eq 'ok') {
14333:         if (keys(%changes) > 0) {
14334:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14335:             $resulttext = &mt('Changes made:').'<ul>';
14336:             foreach my $key ('admin','default','validation') {
14337:                 if (ref($changes{$key}) eq 'HASH') {
14338:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
14339:                     if ($key eq 'validation') {
14340:                         foreach my $item (@{$itemsref}) {
14341:                             if (exists($changes{$key}{$item})) {
14342:                                 if ($item eq 'markup') {
14343:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14344:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
14345:                                 } else {  
14346:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14347:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
14348:                                 }
14349:                             }
14350:                         }
14351:                     } else {
14352:                         foreach my $type (@types) {
14353:                             if ($type eq 'community') {
14354:                                 $roles{'1'} = &mt('Community personnel');
14355:                             } else {
14356:                                 $roles{'1'} = &mt('Course personnel');
14357:                             }
14358:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
14359:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
14360:                                     if ($key eq 'admin') {
14361:                                         my @mgrdc = ();
14362:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14363:                                             foreach my $item (@{$ordered{'admin'}}) {
14364:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
14365:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
14366:                                                         push(@mgrdc,$item);
14367:                                                     }
14368:                                                 }
14369:                                             }
14370:                                             if (@mgrdc) {
14371:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
14372:                                             } else {
14373:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
14374:                                             }
14375:                                         }
14376:                                     } else {
14377:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14378:                                             foreach my $item (@{$ordered{$key}}) {
14379:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14380:                                                     $domdefaults{$type.'selfenroll'.$item} =
14381:                                                         $selfenrollhash{$key}{$type}{$item};
14382:                                                 }
14383:                                             }
14384:                                         }
14385:                                     }
14386:                                 }
14387:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
14388:                                 foreach my $item (@{$ordered{$key}}) {
14389:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14390:                                         $resulttext .= '<li>';
14391:                                         if ($key eq 'admin') {
14392:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
14393:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
14394:                                         } else {
14395:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
14396:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
14397:                                         }
14398:                                         $resulttext .= '</li>';
14399:                                     }
14400:                                 }
14401:                                 $resulttext .= '</ul></li>';
14402:                             }
14403:                         }
14404:                         $resulttext .= '</ul></li>'; 
14405:                     }
14406:                 }
14407:             }
14408:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
14409:                 my $cachetime = 24*60*60;
14410:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14411:                 if (ref($lastactref) eq 'HASH') {
14412:                     $lastactref->{'domdefaults'} = 1;
14413:                 }
14414:             }
14415:             $resulttext .= '</ul>';
14416:         } else {
14417:             $resulttext = &mt('No changes made to self-enrollment settings');
14418:         }
14419:     } else {
14420:         $resulttext = '<span class="LC_error">'.
14421:             &mt('An error occurred: [_1]',$putresult).'</span>';
14422:     }
14423:     return $resulttext;
14424: }
14425: 
14426: sub modify_usersessions {
14427:     my ($dom,$lastactref,%domconfig) = @_;
14428:     my @hostingtypes = ('version','excludedomain','includedomain');
14429:     my @offloadtypes = ('primary','default');
14430:     my %types = (
14431:                   remote => \@hostingtypes,
14432:                   hosted => \@hostingtypes,
14433:                   spares => \@offloadtypes,
14434:                 );
14435:     my @prefixes = ('remote','hosted','spares');
14436:     my @lcversions = &Apache::lonnet::all_loncaparevs();
14437:     my (%by_ip,%by_location,@intdoms);
14438:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
14439:     my @locations = sort(keys(%by_location));
14440:     my (%defaultshash,%changes);
14441:     foreach my $prefix (@prefixes) {
14442:         $defaultshash{'usersessions'}{$prefix} = {};
14443:     }
14444:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14445:     my $resulttext;
14446:     my %iphost = &Apache::lonnet::get_iphost();
14447:     foreach my $prefix (@prefixes) {
14448:         next if ($prefix eq 'spares');
14449:         foreach my $type (@{$types{$prefix}}) {
14450:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14451:             if ($type eq 'version') {
14452:                 my $value = $env{'form.'.$prefix.'_'.$type};
14453:                 my $okvalue;
14454:                 if ($value ne '') {
14455:                     if (grep(/^\Q$value\E$/,@lcversions)) {
14456:                         $okvalue = $value;
14457:                     }
14458:                 }
14459:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14460:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14461:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
14462:                             if ($inuse == 0) {
14463:                                 $changes{$prefix}{$type} = 1;
14464:                             } else {
14465:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
14466:                                     $changes{$prefix}{$type} = 1;
14467:                                 }
14468:                                 if ($okvalue ne '') {
14469:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14470:                                 } 
14471:                             }
14472:                         } else {
14473:                             if (($inuse == 1) && ($okvalue ne '')) {
14474:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14475:                                 $changes{$prefix}{$type} = 1;
14476:                             }
14477:                         }
14478:                     } else {
14479:                         if (($inuse == 1) && ($okvalue ne '')) {
14480:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14481:                             $changes{$prefix}{$type} = 1;
14482:                         }
14483:                     }
14484:                 } else {
14485:                     if (($inuse == 1) && ($okvalue ne '')) {
14486:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14487:                         $changes{$prefix}{$type} = 1;
14488:                     }
14489:                 }
14490:             } else {
14491:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14492:                 my @okvals;
14493:                 foreach my $val (@vals) {
14494:                     if ($val =~ /:/) {
14495:                         my @items = split(/:/,$val);
14496:                         foreach my $item (@items) {
14497:                             if (ref($by_location{$item}) eq 'ARRAY') {
14498:                                 push(@okvals,$item);
14499:                             }
14500:                         }
14501:                     } else {
14502:                         if (ref($by_location{$val}) eq 'ARRAY') {
14503:                             push(@okvals,$val);
14504:                         }
14505:                     }
14506:                 }
14507:                 @okvals = sort(@okvals);
14508:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14509:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14510:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14511:                             if ($inuse == 0) {
14512:                                 $changes{$prefix}{$type} = 1; 
14513:                             } else {
14514:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14515:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
14516:                                 if (@changed > 0) {
14517:                                     $changes{$prefix}{$type} = 1;
14518:                                 }
14519:                             }
14520:                         } else {
14521:                             if ($inuse == 1) {
14522:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14523:                                 $changes{$prefix}{$type} = 1;
14524:                             }
14525:                         } 
14526:                     } else {
14527:                         if ($inuse == 1) {
14528:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14529:                             $changes{$prefix}{$type} = 1;
14530:                         }
14531:                     }
14532:                 } else {
14533:                     if ($inuse == 1) {
14534:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14535:                         $changes{$prefix}{$type} = 1;
14536:                     }
14537:                 }
14538:             }
14539:         }
14540:     }
14541: 
14542:     my @alldoms = &Apache::lonnet::all_domains();
14543:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14544:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
14545:     my $savespares;
14546: 
14547:     foreach my $lonhost (sort(keys(%servers))) {
14548:         my $serverhomeID =
14549:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
14550:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
14551:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
14552:         my %spareschg;
14553:         foreach my $type (@{$types{'spares'}}) {
14554:             my @okspares;
14555:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
14556:             foreach my $server (@checked) {
14557:                 if (&Apache::lonnet::hostname($server) ne '') {
14558:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
14559:                         unless (grep(/^\Q$server\E$/,@okspares)) {
14560:                             push(@okspares,$server);
14561:                         }
14562:                     }
14563:                 }
14564:             }
14565:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
14566:             my $newspare;
14567:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
14568:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
14569:                     $newspare = $new;
14570:                 }
14571:             }
14572:             my @spares;
14573:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
14574:                 @spares = sort(@okspares,$newspare);
14575:             } else {
14576:                 @spares = sort(@okspares);
14577:             }
14578:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
14579:             if (ref($spareid{$lonhost}) eq 'HASH') {
14580:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
14581:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
14582:                     if (@diffs > 0) {
14583:                         $spareschg{$type} = 1;
14584:                     }
14585:                 }
14586:             }
14587:         }
14588:         if (keys(%spareschg) > 0) {
14589:             $changes{'spares'}{$lonhost} = \%spareschg;
14590:         }
14591:     }
14592:     $defaultshash{'usersessions'}{'offloadnow'} = {};
14593:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
14594:     my @okoffload;
14595:     if (@offloadnow) {
14596:         foreach my $server (@offloadnow) {
14597:             if (&Apache::lonnet::hostname($server) ne '') {
14598:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
14599:                     push(@okoffload,$server);
14600:                 }
14601:             }
14602:         }
14603:         if (@okoffload) {
14604:             foreach my $lonhost (@okoffload) {
14605:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
14606:             }
14607:         }
14608:     }
14609:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
14610:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
14611:             if (ref($changes{'spares'}) eq 'HASH') {
14612:                 if (keys(%{$changes{'spares'}}) > 0) {
14613:                     $savespares = 1;
14614:                 }
14615:             }
14616:         } else {
14617:             $savespares = 1;
14618:         }
14619:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
14620:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
14621:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
14622:                     $changes{'offloadnow'} = 1;
14623:                     last;
14624:                 }
14625:             }
14626:             unless ($changes{'offloadnow'}) {
14627:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
14628:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
14629:                         $changes{'offloadnow'} = 1;
14630:                         last;
14631:                     }
14632:                 }
14633:             }
14634:         } elsif (@okoffload) {
14635:             $changes{'offloadnow'} = 1;
14636:         }
14637:     } elsif (@okoffload) {
14638:         $changes{'offloadnow'} = 1;
14639:     }
14640:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
14641:     if ((keys(%changes) > 0) || ($savespares)) {
14642:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14643:                                                  $dom);
14644:         if ($putresult eq 'ok') {
14645:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14646:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
14647:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
14648:                 }
14649:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
14650:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
14651:                 }
14652:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14653:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
14654:                 }
14655:             }
14656:             my $cachetime = 24*60*60;
14657:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14658:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
14659:             if (ref($lastactref) eq 'HASH') {
14660:                 $lastactref->{'domdefaults'} = 1;
14661:                 $lastactref->{'usersessions'} = 1;
14662:             }
14663:             if (keys(%changes) > 0) {
14664:                 my %lt = &usersession_titles();
14665:                 $resulttext = &mt('Changes made:').'<ul>';
14666:                 foreach my $prefix (@prefixes) {
14667:                     if (ref($changes{$prefix}) eq 'HASH') {
14668:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14669:                         if ($prefix eq 'spares') {
14670:                             if (ref($changes{$prefix}) eq 'HASH') {
14671:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
14672:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
14673:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
14674:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
14675:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14676:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
14677:                                         foreach my $type (@{$types{$prefix}}) {
14678:                                             if ($changes{$prefix}{$lonhost}{$type}) {
14679:                                                 my $offloadto = &mt('None');
14680:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
14681:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
14682:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
14683:                                                     }
14684:                                                 }
14685:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
14686:                                             }
14687:                                         }
14688:                                     }
14689:                                     $resulttext .= '</li>';
14690:                                 }
14691:                             }
14692:                         } else {
14693:                             foreach my $type (@{$types{$prefix}}) {
14694:                                 if (defined($changes{$prefix}{$type})) {
14695:                                     my $newvalue;
14696:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14697:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
14698:                                             if ($type eq 'version') {
14699:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
14700:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14701:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
14702:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
14703:                                                 }
14704:                                             }
14705:                                         }
14706:                                     }
14707:                                     if ($newvalue eq '') {
14708:                                         if ($type eq 'version') {
14709:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
14710:                                         } else {
14711:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14712:                                         }
14713:                                     } else {
14714:                                         if ($type eq 'version') {
14715:                                             $newvalue .= ' '.&mt('(or later)'); 
14716:                                         }
14717:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14718:                                     }
14719:                                 }
14720:                             }
14721:                         }
14722:                         $resulttext .= '</ul>';
14723:                     }
14724:                 }
14725:                 if ($changes{'offloadnow'}) {
14726:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14727:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
14728:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
14729:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
14730:                                 $resulttext .= '<li>'.$lonhost.'</li>';
14731:                             }
14732:                             $resulttext .= '</ul>';
14733:                         } else {
14734:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
14735:                         }
14736:                     } else {
14737:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
14738:                     }
14739:                 }
14740:                 $resulttext .= '</ul>';
14741:             } else {
14742:                 $resulttext = $nochgmsg;
14743:             }
14744:         } else {
14745:             $resulttext = '<span class="LC_error">'.
14746:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14747:         }
14748:     } else {
14749:         $resulttext = $nochgmsg;
14750:     }
14751:     return $resulttext;
14752: }
14753: 
14754: sub modify_loadbalancing {
14755:     my ($dom,%domconfig) = @_;
14756:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
14757:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
14758:     my ($othertitle,$usertypes,$types) =
14759:         &Apache::loncommon::sorted_inst_types($dom);
14760:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14761:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
14762:     my @sparestypes = ('primary','default');
14763:     my %typetitles = &sparestype_titles();
14764:     my $resulttext;
14765:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
14766:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14767:         %existing = %{$domconfig{'loadbalancing'}};
14768:     }
14769:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
14770:                               \%currtargets,\%currrules,\%currcookies);
14771:     my ($saveloadbalancing,%defaultshash,%changes);
14772:     my ($alltypes,$othertypes,$titles) =
14773:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
14774:     my %ruletitles = &offloadtype_text();
14775:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
14776:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
14777:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
14778:         if ($balancer eq '') {
14779:             next;
14780:         }
14781:         if (!exists($servers{$balancer})) {
14782:             if (exists($currbalancer{$balancer})) {
14783:                 push(@{$changes{'delete'}},$balancer);
14784:             }
14785:             next;
14786:         }
14787:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14788:             push(@{$changes{'delete'}},$balancer);
14789:             next;
14790:         }
14791:         if (!exists($currbalancer{$balancer})) {
14792:             push(@{$changes{'add'}},$balancer);
14793:         }
14794:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14795:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14796:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14797:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14798:             $saveloadbalancing = 1;
14799:         }
14800:         foreach my $sparetype (@sparestypes) {
14801:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14802:             my @offloadto;
14803:             foreach my $target (@targets) {
14804:                 if (($servers{$target}) && ($target ne $balancer)) {
14805:                     if ($sparetype eq 'default') {
14806:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14807:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14808:                         }
14809:                     }
14810:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14811:                         push(@offloadto,$target);
14812:                     }
14813:                 }
14814:             }
14815:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14816:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14817:                     push(@offloadto,$balancer);
14818:                 }
14819:             }
14820:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14821:         }
14822:         if ($env{'form.loadbalancing_cookie_'.$i}) {
14823:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
14824:             if (exists($currbalancer{$balancer})) {
14825:                 unless ($currcookies{$balancer}) {
14826:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
14827:                 }
14828:             }
14829:         } elsif (exists($currbalancer{$balancer})) {
14830:             if ($currcookies{$balancer}) {
14831:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
14832:             }
14833:         }
14834:         if (ref($currtargets{$balancer}) eq 'HASH') {
14835:             foreach my $sparetype (@sparestypes) {
14836:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14837:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14838:                     if (@targetdiffs > 0) {
14839:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14840:                     }
14841:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14842:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14843:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14844:                     }
14845:                 }
14846:             }
14847:         } else {
14848:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14849:                 foreach my $sparetype (@sparestypes) {
14850:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14851:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14852:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14853:                         }
14854:                     }
14855:                 }
14856:             }
14857:         }
14858:         my $ishomedom;
14859:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14860:             $ishomedom = 1;
14861:         }
14862:         if (ref($alltypes) eq 'ARRAY') {
14863:             foreach my $type (@{$alltypes}) {
14864:                 my $rule;
14865:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14866:                          (!$ishomedom)) {
14867:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14868:                 }
14869:                 if ($rule eq 'specific') {
14870:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14871:                     if (exists($servers{$specifiedhost})) {
14872:                         $rule = $specifiedhost;
14873:                     }
14874:                 }
14875:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14876:                 if (ref($currrules{$balancer}) eq 'HASH') {
14877:                     if ($rule ne $currrules{$balancer}{$type}) {
14878:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14879:                     }
14880:                 } elsif ($rule ne '') {
14881:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14882:                 }
14883:             }
14884:         }
14885:     }
14886:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14887:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14888:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14889:             $defaultshash{'loadbalancing'} = {};
14890:         }
14891:         my $putresult = &Apache::lonnet::put_dom('configuration',
14892:                                                  \%defaultshash,$dom);
14893:         if ($putresult eq 'ok') {
14894:             if (keys(%changes) > 0) {
14895:                 my %toupdate;
14896:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14897:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14898:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14899:                         $toupdate{$balancer} = 1;
14900:                     }
14901:                 }
14902:                 if (ref($changes{'add'}) eq 'ARRAY') {
14903:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14904:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14905:                         $toupdate{$balancer} = 1;
14906:                     }
14907:                 }
14908:                 if (ref($changes{'curr'}) eq 'HASH') {
14909:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14910:                         $toupdate{$balancer} = 1;
14911:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14912:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14913:                                 my %offloadstr;
14914:                                 foreach my $sparetype (@sparestypes) {
14915:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14916:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14917:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14918:                                         }
14919:                                     }
14920:                                 }
14921:                                 if (keys(%offloadstr) == 0) {
14922:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14923:                                 } else {
14924:                                     my $showoffload;
14925:                                     foreach my $sparetype (@sparestypes) {
14926:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14927:                                         if (defined($offloadstr{$sparetype})) {
14928:                                             $showoffload .= $offloadstr{$sparetype};
14929:                                         } else {
14930:                                             $showoffload .= &mt('None');
14931:                                         }
14932:                                         $showoffload .= ('&nbsp;'x3);
14933:                                     }
14934:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14935:                                 }
14936:                             }
14937:                         }
14938:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14939:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14940:                                 foreach my $type (@{$alltypes}) {
14941:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14942:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14943:                                         my $balancetext;
14944:                                         if ($rule eq '') {
14945:                                             $balancetext =  $ruletitles{'default'};
14946:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14947:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14948:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14949:                                                 foreach my $sparetype (@sparestypes) {
14950:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14951:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14952:                                                     }
14953:                                                 }
14954:                                                 foreach my $item (@{$alltypes}) {
14955:                                                     next if ($item =~  /^_LC_ipchange/);
14956:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14957:                                                     if ($hasrule eq 'homeserver') {
14958:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14959:                                                     } else {
14960:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14961:                                                             if ($servers{$hasrule}) {
14962:                                                                 $toupdate{$hasrule} = 1;
14963:                                                             }
14964:                                                         }
14965:                                                     }
14966:                                                 }
14967:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14968:                                                     $balancetext =  $ruletitles{$rule};
14969:                                                 } else {
14970:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14971:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14972:                                                     if ($receiver) {
14973:                                                         $toupdate{$receiver};
14974:                                                     }
14975:                                                 }
14976:                                             } else {
14977:                                                 $balancetext =  $ruletitles{$rule};
14978:                                             }
14979:                                         } else {
14980:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14981:                                         }
14982:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14983:                                     }
14984:                                 }
14985:                             }
14986:                         }
14987:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
14988:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
14989:                                                       $balancer).'</li>';
14990:                         }
14991:                         if (keys(%toupdate)) {
14992:                             my %thismachine;
14993:                             my $updatedhere;
14994:                             my $cachetime = 60*60*24;
14995:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14996:                             foreach my $lonhost (keys(%toupdate)) {
14997:                                 if ($thismachine{$lonhost}) {
14998:                                     unless ($updatedhere) {
14999:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
15000:                                                                       $defaultshash{'loadbalancing'},
15001:                                                                       $cachetime);
15002:                                         $updatedhere = 1;
15003:                                     }
15004:                                 } else {
15005:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
15006:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
15007:                                 }
15008:                             }
15009:                         }
15010:                     }
15011:                 }
15012:                 if ($resulttext ne '') {
15013:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
15014:                 } else {
15015:                     $resulttext = $nochgmsg;
15016:                 }
15017:             } else {
15018:                 $resulttext = $nochgmsg;
15019:             }
15020:         } else {
15021:             $resulttext = '<span class="LC_error">'.
15022:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15023:         }
15024:     } else {
15025:         $resulttext = $nochgmsg;
15026:     }
15027:     return $resulttext;
15028: }
15029: 
15030: sub recurse_check {
15031:     my ($chkcats,$categories,$depth,$name) = @_;
15032:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
15033:         my $chg = 0;
15034:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
15035:             my $category = $chkcats->[$depth]{$name}[$j];
15036:             my $item;
15037:             if ($category eq '') {
15038:                 $chg ++;
15039:             } else {
15040:                 my $deeper = $depth + 1;
15041:                 $item = &escape($category).':'.&escape($name).':'.$depth;
15042:                 if ($chg) {
15043:                     $categories->{$item} -= $chg;
15044:                 }
15045:                 &recurse_check($chkcats,$categories,$deeper,$category);
15046:                 $deeper --;
15047:             }
15048:         }
15049:     }
15050:     return;
15051: }
15052: 
15053: sub recurse_cat_deletes {
15054:     my ($item,$coursecategories,$deletions) = @_;
15055:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
15056:     my $subdepth = $depth + 1;
15057:     if (ref($coursecategories) eq 'HASH') {
15058:         foreach my $subitem (keys(%{$coursecategories})) {
15059:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
15060:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
15061:                 delete($coursecategories->{$subitem});
15062:                 $deletions->{$subitem} = 1;
15063:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
15064:             }
15065:         }
15066:     }
15067:     return;
15068: }
15069: 
15070: sub active_dc_picker {
15071:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
15072:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
15073:     my @domcoord = keys(%domcoords);
15074:     if (keys(%currhash)) {
15075:         foreach my $dc (keys(%currhash)) {
15076:             unless (exists($domcoords{$dc})) {
15077:                 push(@domcoord,$dc);
15078:             }
15079:         }
15080:     }
15081:     @domcoord = sort(@domcoord);
15082:     my $numdcs = scalar(@domcoord);
15083:     my $rows = 0;
15084:     my $table;
15085:     if ($numdcs > 1) {
15086:         $table = '<table>';
15087:         for (my $i=0; $i<@domcoord; $i++) {
15088:             my $rem = $i%($numinrow);
15089:             if ($rem == 0) {
15090:                 if ($i > 0) {
15091:                     $table .= '</tr>';
15092:                 }
15093:                 $table .= '<tr>';
15094:                 $rows ++;
15095:             }
15096:             my $check = '';
15097:             if ($inputtype eq 'radio') {
15098:                 if (keys(%currhash) == 0) {
15099:                     if (!$i) {
15100:                         $check = ' checked="checked"';
15101:                     }
15102:                 } elsif (exists($currhash{$domcoord[$i]})) {
15103:                     $check = ' checked="checked"';
15104:                 }
15105:             } else {
15106:                 if (exists($currhash{$domcoord[$i]})) {
15107:                     $check = ' checked="checked"';
15108:                 }
15109:             }
15110:             if ($i == @domcoord - 1) {
15111:                 my $colsleft = $numinrow - $rem;
15112:                 if ($colsleft > 1) {
15113:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
15114:                 } else {
15115:                     $table .= '<td class="LC_left_item">';
15116:                 }
15117:             } else {
15118:                 $table .= '<td class="LC_left_item">';
15119:             }
15120:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
15121:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15122:             $table .= '<span class="LC_nobreak"><label>'.
15123:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
15124:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
15125:             if ($user ne $dcname.':'.$dcdom) {
15126:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15127:             }
15128:             $table .= '</label></span></td>';
15129:         }
15130:         $table .= '</tr></table>';
15131:     } elsif ($numdcs == 1) {
15132:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
15133:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15134:         if ($inputtype eq 'radio') {
15135:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
15136:             if ($user ne $dcname.':'.$dcdom) {
15137:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15138:             }
15139:         } else {
15140:             my $check;
15141:             if (exists($currhash{$domcoord[0]})) {
15142:                 $check = ' checked="checked"';
15143:             }
15144:             $table = '<span class="LC_nobreak"><label>'.
15145:                      '<input type="checkbox" name="'.$name.'" '.
15146:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
15147:             if ($user ne $dcname.':'.$dcdom) {
15148:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15149:             }
15150:             $table .= '</label></span>';
15151:             $rows ++;
15152:         }
15153:     }
15154:     return ($numdcs,$table,$rows);
15155: }
15156: 
15157: sub usersession_titles {
15158:     return &Apache::lonlocal::texthash(
15159:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
15160:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
15161:                spares => 'Servers offloaded to, when busy',
15162:                version => 'LON-CAPA version requirement',
15163:                excludedomain => 'Allow all, but exclude specific domains',
15164:                includedomain => 'Deny all, but include specific domains',
15165:                primary => 'Primary (checked first)',
15166:                default => 'Default',
15167:            );
15168: }
15169: 
15170: sub id_for_thisdom {
15171:     my (%servers) = @_;
15172:     my %altids;
15173:     foreach my $server (keys(%servers)) {
15174:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
15175:         if ($serverhome ne $server) {
15176:             $altids{$serverhome} = $server;
15177:         }
15178:     }
15179:     return %altids;
15180: }
15181: 
15182: sub count_servers {
15183:     my ($currbalancer,%servers) = @_;
15184:     my (@spares,$numspares);
15185:     foreach my $lonhost (sort(keys(%servers))) {
15186:         next if ($currbalancer eq $lonhost);
15187:         push(@spares,$lonhost);
15188:     }
15189:     if ($currbalancer) {
15190:         $numspares = scalar(@spares);
15191:     } else {
15192:         $numspares = scalar(@spares) - 1;
15193:     }
15194:     return ($numspares,@spares);
15195: }
15196: 
15197: sub lonbalance_targets_js {
15198:     my ($dom,$types,$servers,$settings) = @_;
15199:     my $select = &mt('Select');
15200:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
15201:     if (ref($servers) eq 'HASH') {
15202:         $alltargets = join("','",sort(keys(%{$servers})));
15203:         my @homedoms;
15204:         foreach my $server (sort(keys(%{$servers}))) {
15205:             if (&Apache::lonnet::host_domain($server) eq $dom) {
15206:                 push(@homedoms,'1');
15207:             } else {
15208:                 push(@homedoms,'0');
15209:             }
15210:         }
15211:         $allishome = join("','",@homedoms);
15212:     }
15213:     if (ref($types) eq 'ARRAY') {
15214:         if (@{$types} > 0) {
15215:             @alltypes = @{$types};
15216:         }
15217:     }
15218:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
15219:     $allinsttypes = join("','",@alltypes);
15220:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
15221:     if (ref($settings) eq 'HASH') {
15222:         %existing = %{$settings};
15223:     }
15224:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
15225:                               \%currtargets,\%currrules,\%currcookies);
15226:     my $balancers = join("','",sort(keys(%currbalancer)));
15227:     return <<"END";
15228: 
15229: <script type="text/javascript">
15230: // <![CDATA[
15231: 
15232: currBalancers = new Array('$balancers');
15233: 
15234: function toggleTargets(balnum) {
15235:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15236:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
15237:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
15238:     var prevbalancer = prevhostitem.value;
15239:     var baltotal = document.getElementById('loadbalancing_total').value;
15240:     prevhostitem.value = balancer;
15241:     if (prevbalancer != '') {
15242:         var prevIdx = currBalancers.indexOf(prevbalancer);
15243:         if (prevIdx != -1) {
15244:             currBalancers.splice(prevIdx,1);
15245:         }
15246:     }
15247:     if (balancer == '') {
15248:         hideSpares(balnum);
15249:     } else {
15250:         var currIdx = currBalancers.indexOf(balancer);
15251:         if (currIdx == -1) {
15252:             currBalancers.push(balancer);
15253:         }
15254:         var homedoms = new Array('$allishome');
15255:         var ishomedom = homedoms[lonhostitem.selectedIndex];
15256:         showSpares(balancer,ishomedom,balnum);
15257:     }
15258:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
15259:     return;
15260: }
15261: 
15262: function showSpares(balancer,ishomedom,balnum) {
15263:     var alltargets = new Array('$alltargets');
15264:     var insttypes = new Array('$allinsttypes');
15265:     var offloadtypes = new Array('primary','default');
15266: 
15267:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
15268:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
15269:  
15270:     for (var i=0; i<offloadtypes.length; i++) {
15271:         var count = 0;
15272:         for (var j=0; j<alltargets.length; j++) {
15273:             if (alltargets[j] != balancer) {
15274:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
15275:                 item.value = alltargets[j];
15276:                 item.style.textAlign='left';
15277:                 item.style.textFace='normal';
15278:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
15279:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
15280:                     item.disabled = '';
15281:                 } else {
15282:                     item.disabled = 'disabled';
15283:                     item.checked = false;
15284:                 }
15285:                 count ++;
15286:             }
15287:         }
15288:     }
15289:     for (var k=0; k<insttypes.length; k++) {
15290:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
15291:             if (ishomedom == 1) {
15292:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15293:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15294:             } else {
15295:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15296:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15297:             }
15298:         } else {
15299:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15300:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15301:         }
15302:         if ((insttypes[k] != '_LC_external') && 
15303:             ((insttypes[k] != '_LC_internetdom') ||
15304:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
15305:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
15306:             item.options.length = 0;
15307:             item.options[0] = new Option("","",true,true);
15308:             var idx = 0;
15309:             for (var m=0; m<alltargets.length; m++) {
15310:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
15311:                     idx ++;
15312:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15313:                 }
15314:             }
15315:         }
15316:     }
15317:     return;
15318: }
15319: 
15320: function hideSpares(balnum) {
15321:     var alltargets = new Array('$alltargets');
15322:     var insttypes = new Array('$allinsttypes');
15323:     var offloadtypes = new Array('primary','default');
15324: 
15325:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
15326:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
15327: 
15328:     var total = alltargets.length - 1;
15329:     for (var i=0; i<offloadtypes; i++) {
15330:         for (var j=0; j<total; j++) {
15331:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
15332:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
15333:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
15334:         }
15335:     }
15336:     for (var k=0; k<insttypes.length; k++) {
15337:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15338:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15339:         if (insttypes[k] != '_LC_external') {
15340:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
15341:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
15342:         }
15343:     }
15344:     return;
15345: }
15346: 
15347: function checkOffloads(item,balnum,type) {
15348:     var alltargets = new Array('$alltargets');
15349:     var offloadtypes = new Array('primary','default');
15350:     if (item.checked) {
15351:         var total = alltargets.length - 1;
15352:         var other;
15353:         if (type == offloadtypes[0]) {
15354:             other = offloadtypes[1];
15355:         } else {
15356:             other = offloadtypes[0];
15357:         }
15358:         for (var i=0; i<total; i++) {
15359:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
15360:             if (server == item.value) {
15361:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
15362:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
15363:                 }
15364:             }
15365:         }
15366:     }
15367:     return;
15368: }
15369: 
15370: function singleServerToggle(balnum,type) {
15371:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
15372:     if (offloadtoSelIdx == 0) {
15373:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
15374:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15375: 
15376:     } else {
15377:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
15378:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15379:     }
15380:     return;
15381: }
15382: 
15383: function balanceruleChange(formname,balnum,type) {
15384:     if (type == '_LC_external') {
15385:         return;
15386:     }
15387:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
15388:     for (var i=0; i<typesRules.length; i++) {
15389:         if (formname.elements[typesRules[i]].checked) {
15390:             if (formname.elements[typesRules[i]].value != 'specific') {
15391:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
15392:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15393:             } else {
15394:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15395:             }
15396:         }
15397:     }
15398:     return;
15399: }
15400: 
15401: function balancerDeleteChange(balnum) {
15402:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15403:     var baltotal = document.getElementById('loadbalancing_total').value;
15404:     var addtarget;
15405:     var removetarget;
15406:     var action = 'delete';
15407:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
15408:         var lonhost = hostitem.value;
15409:         var currIdx = currBalancers.indexOf(lonhost);
15410:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
15411:             if (currIdx != -1) {
15412:                 currBalancers.splice(currIdx,1);
15413:             }
15414:             addtarget = lonhost;
15415:         } else {
15416:             if (currIdx == -1) {
15417:                 currBalancers.push(lonhost);
15418:             }
15419:             removetarget = lonhost;
15420:             action = 'undelete';
15421:         }
15422:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
15423:     }
15424:     return;
15425: }
15426: 
15427: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
15428:     if (baltotal > 1) {
15429:         var offloadtypes = new Array('primary','default');
15430:         var alltargets = new Array('$alltargets');
15431:         var insttypes = new Array('$allinsttypes');
15432:         for (var i=0; i<baltotal; i++) {
15433:             if (i != balnum) {
15434:                 for (var j=0; j<offloadtypes.length; j++) {
15435:                     var total = alltargets.length - 1;
15436:                     for (var k=0; k<total; k++) {
15437:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
15438:                         var server = serveritem.value;
15439:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
15440:                             if (server == addtarget) {
15441:                                 serveritem.disabled = '';
15442:                             }
15443:                         }
15444:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15445:                             if (server == removetarget) {
15446:                                 serveritem.disabled = 'disabled';
15447:                                 serveritem.checked = false;
15448:                             }
15449:                         }
15450:                     }
15451:                 }
15452:                 for (var j=0; j<insttypes.length; j++) {
15453:                     if (insttypes[j] != '_LC_external') {
15454:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
15455:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
15456:                             var currSel = singleserver.selectedIndex;
15457:                             var currVal = singleserver.options[currSel].value;
15458:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
15459:                                 var numoptions = singleserver.options.length;
15460:                                 var needsnew = 1;
15461:                                 for (var k=0; k<numoptions; k++) {
15462:                                     if (singleserver.options[k] == addtarget) {
15463:                                         needsnew = 0;
15464:                                         break;
15465:                                     }
15466:                                 }
15467:                                 if (needsnew == 1) {
15468:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
15469:                                 }
15470:                             }
15471:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15472:                                 singleserver.options.length = 0;
15473:                                 if ((currVal) && (currVal != removetarget)) {
15474:                                     singleserver.options[0] = new Option("","",false,false);
15475:                                 } else {
15476:                                     singleserver.options[0] = new Option("","",true,true);
15477:                                 }
15478:                                 var idx = 0;
15479:                                 for (var m=0; m<alltargets.length; m++) {
15480:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
15481:                                         idx ++;
15482:                                         if (currVal == alltargets[m]) {
15483:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
15484:                                         } else {
15485:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15486:                                         }
15487:                                     }
15488:                                 }
15489:                             }
15490:                         }
15491:                     }
15492:                 }
15493:             }
15494:         }
15495:     }
15496:     return;
15497: }
15498: 
15499: // ]]>
15500: </script>
15501: 
15502: END
15503: }
15504: 
15505: sub new_spares_js {
15506:     my @sparestypes = ('primary','default');
15507:     my $types = join("','",@sparestypes);
15508:     my $select = &mt('Select');
15509:     return <<"END";
15510: 
15511: <script type="text/javascript">
15512: // <![CDATA[
15513: 
15514: function updateNewSpares(formname,lonhost) {
15515:     var types = new Array('$types');
15516:     var include = new Array();
15517:     var exclude = new Array();
15518:     for (var i=0; i<types.length; i++) {
15519:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
15520:         for (var j=0; j<spareboxes.length; j++) {
15521:             if (formname.elements[spareboxes[j]].checked) {
15522:                 exclude.push(formname.elements[spareboxes[j]].value);
15523:             } else {
15524:                 include.push(formname.elements[spareboxes[j]].value);
15525:             }
15526:         }
15527:     }
15528:     for (var i=0; i<types.length; i++) {
15529:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
15530:         var selIdx = newSpare.selectedIndex;
15531:         var currnew = newSpare.options[selIdx].value;
15532:         var okSpares = new Array();
15533:         for (var j=0; j<newSpare.options.length; j++) {
15534:             var possible = newSpare.options[j].value;
15535:             if (possible != '') {
15536:                 if (exclude.indexOf(possible) == -1) {
15537:                     okSpares.push(possible);
15538:                 } else {
15539:                     if (currnew == possible) {
15540:                         selIdx = 0;
15541:                     }
15542:                 }
15543:             }
15544:         }
15545:         for (var k=0; k<include.length; k++) {
15546:             if (okSpares.indexOf(include[k]) == -1) {
15547:                 okSpares.push(include[k]);
15548:             }
15549:         }
15550:         okSpares.sort();
15551:         newSpare.options.length = 0;
15552:         if (selIdx == 0) {
15553:             newSpare.options[0] = new Option("$select","",true,true);
15554:         } else {
15555:             newSpare.options[0] = new Option("$select","",false,false);
15556:         }
15557:         for (var m=0; m<okSpares.length; m++) {
15558:             var idx = m+1;
15559:             var selThis = 0;
15560:             if (selIdx != 0) {
15561:                 if (okSpares[m] == currnew) {
15562:                     selThis = 1;
15563:                 }
15564:             }
15565:             if (selThis == 1) {
15566:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
15567:             } else {
15568:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
15569:             }
15570:         }
15571:     }
15572:     return;
15573: }
15574: 
15575: function checkNewSpares(lonhost,type) {
15576:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
15577:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
15578:     if (chosen != '') { 
15579:         var othertype;
15580:         var othernewSpare;
15581:         if (type == 'primary') {
15582:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
15583:         }
15584:         if (type == 'default') {
15585:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
15586:         }
15587:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
15588:             othernewSpare.selectedIndex = 0;
15589:         }
15590:     }
15591:     return;
15592: }
15593: 
15594: // ]]>
15595: </script>
15596: 
15597: END
15598: 
15599: }
15600: 
15601: sub common_domprefs_js {
15602:     return <<"END";
15603: 
15604: <script type="text/javascript">
15605: // <![CDATA[
15606: 
15607: function getIndicesByName(formname,item) {
15608:     var group = new Array();
15609:     for (var i=0;i<formname.elements.length;i++) {
15610:         if (formname.elements[i].name == item) {
15611:             group.push(formname.elements[i].id);
15612:         }
15613:     }
15614:     return group;
15615: }
15616: 
15617: // ]]>
15618: </script>
15619: 
15620: END
15621: 
15622: }
15623: 
15624: sub recaptcha_js {
15625:     my %lt = &captcha_phrases();
15626:     return <<"END";
15627: 
15628: <script type="text/javascript">
15629: // <![CDATA[
15630: 
15631: function updateCaptcha(caller,context) {
15632:     var privitem;
15633:     var pubitem;
15634:     var privtext;
15635:     var pubtext;
15636:     var versionitem;
15637:     var versiontext;
15638:     if (document.getElementById(context+'_recaptchapub')) {
15639:         pubitem = document.getElementById(context+'_recaptchapub');
15640:     } else {
15641:         return;
15642:     }
15643:     if (document.getElementById(context+'_recaptchapriv')) {
15644:         privitem = document.getElementById(context+'_recaptchapriv');
15645:     } else {
15646:         return;
15647:     }
15648:     if (document.getElementById(context+'_recaptchapubtxt')) {
15649:         pubtext = document.getElementById(context+'_recaptchapubtxt');
15650:     } else {
15651:         return;
15652:     }
15653:     if (document.getElementById(context+'_recaptchaprivtxt')) {
15654:         privtext = document.getElementById(context+'_recaptchaprivtxt');
15655:     } else {
15656:         return;
15657:     }
15658:     if (document.getElementById(context+'_recaptchaversion')) {
15659:         versionitem = document.getElementById(context+'_recaptchaversion');
15660:     } else {
15661:         return;
15662:     }
15663:     if (document.getElementById(context+'_recaptchavertxt')) {
15664:         versiontext = document.getElementById(context+'_recaptchavertxt');
15665:     } else {
15666:         return;
15667:     }
15668:     if (caller.checked) {
15669:         if (caller.value == 'recaptcha') {
15670:             pubitem.type = 'text';
15671:             privitem.type = 'text';
15672:             pubitem.size = '40';
15673:             privitem.size = '40';
15674:             pubtext.innerHTML = "$lt{'pub'}";
15675:             privtext.innerHTML = "$lt{'priv'}";
15676:             versionitem.type = 'text';
15677:             versionitem.size = '3';
15678:             versiontext.innerHTML = "$lt{'ver'}";
15679:         } else {
15680:             pubitem.type = 'hidden';
15681:             privitem.type = 'hidden';
15682:             versionitem.type = 'hidden';
15683:             pubtext.innerHTML = '';
15684:             privtext.innerHTML = '';
15685:             versiontext.innerHTML = '';
15686:         }
15687:     }
15688:     return;
15689: }
15690: 
15691: // ]]>
15692: </script>
15693: 
15694: END
15695: 
15696: }
15697: 
15698: sub toggle_display_js {
15699:     return <<"END";
15700: 
15701: <script type="text/javascript">
15702: // <![CDATA[
15703: 
15704: function toggleDisplay(domForm,caller) {
15705:     if (document.getElementById(caller)) {
15706:         var divitem = document.getElementById(caller);
15707:         var optionsElement = domForm.coursecredits;
15708:         var checkval = 1;
15709:         var dispval = 'block';
15710:         var selfcreateRegExp = /^cancreate_emailverified/;
15711:         if (caller == 'emailoptions') {
15712:             optionsElement = domForm.cancreate_email; 
15713:         }
15714:         if (caller == 'studentsubmission') {
15715:             optionsElement = domForm.postsubmit;
15716:         }
15717:         if (caller == 'cloneinstcode') {
15718:             optionsElement = domForm.canclone;
15719:             checkval = 'instcode';
15720:         }
15721:         if (selfcreateRegExp.test(caller)) {
15722:             optionsElement = domForm.elements[caller];
15723:             checkval = 'other';
15724:             dispval = 'inline'
15725:         }
15726:         if (optionsElement.length) {
15727:             var currval;
15728:             for (var i=0; i<optionsElement.length; i++) {
15729:                 if (optionsElement[i].checked) {
15730:                    currval = optionsElement[i].value;
15731:                 }
15732:             }
15733:             if (currval == checkval) {
15734:                 divitem.style.display = dispval;
15735:             } else {
15736:                 divitem.style.display = 'none';
15737:             }
15738:         }
15739:     }
15740:     return;
15741: }
15742: 
15743: // ]]>
15744: </script>
15745: 
15746: END
15747: 
15748: }
15749: 
15750: sub captcha_phrases {
15751:     return &Apache::lonlocal::texthash (
15752:                  priv => 'Private key',
15753:                  pub  => 'Public key',
15754:                  original  => 'original (CAPTCHA)',
15755:                  recaptcha => 'successor (ReCAPTCHA)',
15756:                  notused   => 'unused',
15757:                  ver => 'ReCAPTCHA version (1 or 2)',
15758:     );
15759: }
15760: 
15761: sub devalidate_remote_domconfs {
15762:     my ($dom,$cachekeys) = @_;
15763:     return unless (ref($cachekeys) eq 'HASH');
15764:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15765:     my %thismachine;
15766:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15767:     my @posscached = ('domainconfig','domdefaults','usersessions',
15768:                       'directorysrch','passwdconf','cats');
15769:     if (keys(%servers)) {
15770:         foreach my $server (keys(%servers)) {
15771:             next if ($thismachine{$server});
15772:             my @cached;
15773:             foreach my $name (@posscached) {
15774:                 if ($cachekeys->{$name}) {
15775:                     push(@cached,&escape($name).':'.&escape($dom));
15776:                 }
15777:             }
15778:             if (@cached) {
15779:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
15780:             }
15781:         }
15782:     }
15783:     return;
15784: }
15785: 
15786: 1;

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