File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.99: download - view: text, annotated - select for diffs
Sun Aug 25 14:35:21 2019 UTC (4 years, 10 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  Backport 1.365, 1.366, 1.367 (except password expiration parts).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.99 2019/08/25 14:35:21 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   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:     } else {
 2972:         foreach my $type (@mailings) {
 2973:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 2974:             $datatable .= '<tr'.$css_class.'>'.
 2975:                           '<td><span class="LC_nobreak">'.
 2976:                           $titles->{$type}.': </span></td>'.
 2977:                           '<td class="LC_left_item">';
 2978:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2979:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 2980:             }
 2981:             $datatable .= '<span class="LC_nobreak">';
 2982:             foreach my $item (@contacts) {
 2983:                 $datatable .= '<label>'.
 2984:                               '<input type="checkbox" name="'.$type.'"'.
 2985:                               $checked{$type}{$item}.
 2986:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 2987:                               '</label>&nbsp;';
 2988:             }
 2989:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2990:                           '<input type="text" name="'.$type.'_others" '.
 2991:                           'value="'.$otheremails{$type}.'"  />';
 2992:             my %locchecked;
 2993:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2994:                 foreach my $loc ('s','b') {
 2995:                     if ($includeloc{$type} eq $loc) {
 2996:                         $locchecked{$loc} = ' checked="checked"';
 2997:                         last;
 2998:                     }
 2999:                 }
 3000:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3001:                               '<input type="text" name="'.$type.'_bcc" '.
 3002:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3003:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3004:                               &mt('Text automatically added to e-mail:').' '.
 3005:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3006:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3007:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3008:                               ('&nbsp;'x2).
 3009:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3010:                               '</span></fieldset>';
 3011:             }
 3012:             $datatable .= '</td></tr>'."\n";
 3013:             $rownum ++;
 3014:         }
 3015:     }
 3016:     if ($position eq 'middle') {
 3017:         my %choices;
 3018:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3019:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3020:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3021:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3022:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3023:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3024:         my @toggles = ('reporterrors','reportupdates');
 3025:         my %defaultchecked = ('reporterrors'  => 'on',
 3026:                               'reportupdates' => 'on');
 3027:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3028:                                                    \%choices,$rownum);
 3029:         $datatable .= $reports;
 3030:     } elsif ($position eq 'bottom') {
 3031:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3032:         $datatable .= '<tr'.$css_class.'>'.
 3033:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3034:                       &mt('(e-mail, subject, and description always shown)').
 3035:                       '</td><td class="LC_left_item">';
 3036:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3037:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3038:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3039:             foreach my $field (@{$fields}) {
 3040:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3041:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3042:                     $datatable .= ' '.&mt('(logged-in users)');
 3043:                 }
 3044:                 $datatable .='</td><td>';
 3045:                 my $clickaction;
 3046:                 if ($field eq 'screenshot') {
 3047:                     $clickaction = ' onclick="screenshotSize(this);"';
 3048:                 }
 3049:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3050:                     foreach my $option (@{$possoptions->{$field}}) {
 3051:                         my $checked;
 3052:                         if ($currfield{$field} eq $option) {
 3053:                             $checked = ' checked="checked"';
 3054:                         }
 3055:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3056:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3057:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3058:                                       '</label></span>'.('&nbsp;'x2);
 3059:                     }
 3060:                 }
 3061:                 if ($field eq 'screenshot') {
 3062:                     my $display;
 3063:                     if ($currfield{$field} eq 'no') {
 3064:                         $display = ' style="display:none"';
 3065:                     }
 3066:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3067:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3068:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3069:                 }
 3070:                 $datatable .= '</td></tr>';
 3071:             }
 3072:             $datatable .= '</table>';
 3073:         }
 3074:         $datatable .= '</td></tr>'."\n";
 3075:         $rownum ++;
 3076:     }
 3077:     $$rowtotal += $rownum;
 3078:     return $datatable;
 3079: }
 3080: 
 3081: sub contacts_javascript {
 3082:     return <<"ENDSCRIPT";
 3083: 
 3084: <script type="text/javascript">
 3085: // <![CDATA[
 3086: 
 3087: function screenshotSize(field) {
 3088:     if (document.getElementById('help_screenshotsize')) {
 3089:         if (field.value == 'no') {
 3090:             document.getElementById('help_screenshotsize').style.display="none";
 3091:         } else {
 3092:             document.getElementById('help_screenshotsize').style.display="";
 3093:         }
 3094:     }
 3095:     return;
 3096: }
 3097: 
 3098: // ]]>
 3099: </script>
 3100: 
 3101: ENDSCRIPT
 3102: }
 3103: 
 3104: sub print_helpsettings {
 3105:     my ($position,$dom,$settings,$rowtotal) = @_;
 3106:     my $confname = $dom.'-domainconfig';
 3107:     my $formname = 'display';
 3108:     my ($datatable,$itemcount);
 3109:     if ($position eq 'top') {
 3110:         $itemcount = 1;
 3111:         my (%choices,%defaultchecked,@toggles);
 3112:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3113:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3114:                                      &mt('LON-CAPA bug tracker'),600,500));
 3115:         %defaultchecked = ('submitbugs' => 'on');
 3116:         @toggles = ('submitbugs');
 3117:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3118:                                                      \%choices,$itemcount);
 3119:         $$rowtotal ++;
 3120:     } else {
 3121:         my $css_class;
 3122:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3123:         my (%customroles,%ordered,%current);
 3124:         if (ref($settings) eq 'HASH') {
 3125:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3126:                 %current = %{$settings->{'adhoc'}};
 3127:             }
 3128:         }
 3129:         my $count = 0;
 3130:         foreach my $key (sort(keys(%existing))) {
 3131:             if ($key=~/^rolesdef\_(\w+)$/) {
 3132:                 my $rolename = $1;
 3133:                 my (%privs,$order);
 3134:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3135:                 $customroles{$rolename} = \%privs;
 3136:                 if (ref($current{$rolename}) eq 'HASH') {
 3137:                     $order = $current{$rolename}{'order'};
 3138:                 }
 3139:                 if ($order eq '') {
 3140:                     $order = $count;
 3141:                 }
 3142:                 $ordered{$order} = $rolename;
 3143:                 $count++;
 3144:             }
 3145:         }
 3146:         my $maxnum = scalar(keys(%ordered));
 3147:         my @roles_by_num = ();
 3148:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3149:             push(@roles_by_num,$item);
 3150:         }
 3151:         my $context = 'domprefs';
 3152:         my $crstype = 'Course';
 3153:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3154:         my @accesstypes = ('all','dh','da','none');
 3155:         my ($numstatustypes,@jsarray);
 3156:         if (ref($types) eq 'ARRAY') {
 3157:             if (@{$types} > 0) {
 3158:                 $numstatustypes = scalar(@{$types});
 3159:                 push(@accesstypes,'status');
 3160:                 @jsarray = ('bystatus');
 3161:             }
 3162:         }
 3163:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3164:         if (keys(%domhelpdesk)) {
 3165:             push(@accesstypes,('inc','exc'));
 3166:             push(@jsarray,('notinc','notexc'));
 3167:         }
 3168:         my $hiddenstr = join("','",@jsarray);
 3169:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3170:         my $context = 'domprefs';
 3171:         my $crstype = 'Course';
 3172:         my $prefix = 'helproles_';
 3173:         my $add_class = 'LC_hidden';
 3174:         foreach my $num (@roles_by_num) {
 3175:             my $role = $ordered{$num};
 3176:             my ($desc,$access,@statuses);
 3177:             if (ref($current{$role}) eq 'HASH') {
 3178:                 $desc = $current{$role}{'desc'};
 3179:                 $access = $current{$role}{'access'};
 3180:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3181:                     @statuses = @{$current{$role}{'insttypes'}};
 3182:                 }
 3183:             }
 3184:             if ($desc eq '') {
 3185:                 $desc = $role;
 3186:             }
 3187:             my $identifier = 'custhelp'.$num;
 3188:             my %full=();
 3189:             my %levels= (
 3190:                          course => {},
 3191:                          domain => {},
 3192:                          system => {},
 3193:                         );
 3194:             my %levelscurrent=(
 3195:                                course => {},
 3196:                                domain => {},
 3197:                                system => {},
 3198:                               );
 3199:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3200:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3201:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3202:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3203:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3204:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3205:             for (my $k=0; $k<=$maxnum; $k++) {
 3206:                 my $vpos = $k+1;
 3207:                 my $selstr;
 3208:                 if ($k == $num) {
 3209:                     $selstr = ' selected="selected" ';
 3210:                 }
 3211:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3212:             }
 3213:             $datatable .= '</select>'.('&nbsp;'x2).
 3214:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3215:                           '</td>'.
 3216:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3217:                           &mt('Name shown to users:').
 3218:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3219:                           '</fieldset>'.
 3220:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3221:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3222:                           '<fieldset>'.
 3223:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3224:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3225:                                                                    \%levelscurrent,$identifier,
 3226:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3227:                           '</fieldset></td>';
 3228:             $itemcount ++;
 3229:         }
 3230:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3231:         my $newcust = 'custhelp'.$count;
 3232:         my (%privs,%levelscurrent);
 3233:         my %full=();
 3234:         my %levels= (
 3235:                      course => {},
 3236:                      domain => {},
 3237:                      system => {},
 3238:                     );
 3239:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3240:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3241:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3242:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3243:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3244:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3245:         for (my $k=0; $k<$maxnum+1; $k++) {
 3246:             my $vpos = $k+1;
 3247:             my $selstr;
 3248:             if ($k == $maxnum) {
 3249:                 $selstr = ' selected="selected" ';
 3250:             }
 3251:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3252:         }
 3253:         $datatable .= '</select>&nbsp;'."\n".
 3254:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3255:                       '</label></span></td>'.
 3256:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3257:                       '<span class="LC_nobreak">'.
 3258:                       &mt('Internal name:').
 3259:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3260:                       '</span>'.('&nbsp;'x4).
 3261:                       '<span class="LC_nobreak">'.
 3262:                       &mt('Name shown to users:').
 3263:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3264:                       '</span></fieldset>'.
 3265:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3266:                                              $usertypes,$types,\%domhelpdesk).
 3267:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3268:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3269:                                                                 \@templateroles,$newcust).
 3270:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3271:                                                                \%levelscurrent,$newcust).
 3272:                       '</fieldset>'.
 3273:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 3274:                       '</td></tr>';
 3275:         $count ++;
 3276:         $$rowtotal += $count;
 3277:     }
 3278:     return $datatable;
 3279: }
 3280: 
 3281: sub adhocbutton {
 3282:     my ($prefix,$num,$field,$visibility) = @_;
 3283:     my %lt = &Apache::lonlocal::texthash(
 3284:                                           show => 'Show details',
 3285:                                           hide => 'Hide details',
 3286:                                         );
 3287:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3288:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3289:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3290:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3291: }
 3292: 
 3293: sub helpsettings_javascript {
 3294:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3295:     return unless(ref($roles_by_num) eq 'ARRAY');
 3296:     my %html_js_lt = &Apache::lonlocal::texthash(
 3297:                                           show => 'Show details',
 3298:                                           hide => 'Hide details',
 3299:                                         );
 3300:     &html_escape(\%html_js_lt);
 3301:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3302:     return <<"ENDSCRIPT";
 3303: <script type="text/javascript">
 3304: // <![CDATA[
 3305: 
 3306: function reorderHelpRoles(form,item) {
 3307:     var changedVal;
 3308: $jstext
 3309:     var newpos = 'helproles_${total}_pos';
 3310:     var maxh = 1 + $total;
 3311:     var current = new Array();
 3312:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3313:     if (item == newpos) {
 3314:         changedVal = newitemVal;
 3315:     } else {
 3316:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3317:         current[newitemVal] = newpos;
 3318:     }
 3319:     for (var i=0; i<helproles.length; i++) {
 3320:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3321:         if (elementName != item) {
 3322:             if (form.elements[elementName]) {
 3323:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3324:                 current[currVal] = elementName;
 3325:             }
 3326:         }
 3327:     }
 3328:     var oldVal;
 3329:     for (var j=0; j<maxh; j++) {
 3330:         if (current[j] == undefined) {
 3331:             oldVal = j;
 3332:         }
 3333:     }
 3334:     if (oldVal < changedVal) {
 3335:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3336:            var elementName = current[k];
 3337:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3338:         }
 3339:     } else {
 3340:         for (var k=changedVal; k<oldVal; k++) {
 3341:             var elementName = current[k];
 3342:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3343:         }
 3344:     }
 3345:     return;
 3346: }
 3347: 
 3348: function helpdeskAccess(num) {
 3349:     var curraccess = null;
 3350:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3351:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3352:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3353:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3354:             }
 3355:         }
 3356:     }
 3357:     var shown = Array();
 3358:     var hidden = Array();
 3359:     if (curraccess == 'none') {
 3360:         hidden = Array('$hiddenstr');
 3361:     } else {
 3362:         if (curraccess == 'status') {
 3363:             shown = Array('bystatus');
 3364:             hidden = Array('notinc','notexc');
 3365:         } else {
 3366:             if (curraccess == 'exc') {
 3367:                 shown = Array('notexc');
 3368:                 hidden = Array('notinc','bystatus');
 3369:             }
 3370:             if (curraccess == 'inc') {
 3371:                 shown = Array('notinc');
 3372:                 hidden = Array('notexc','bystatus');
 3373:             }
 3374:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3375:                 hidden = Array('notinc','notexc','bystatus');
 3376:             }
 3377:         }
 3378:     }
 3379:     if (hidden.length > 0) {
 3380:         for (var i=0; i<hidden.length; i++) {
 3381:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3382:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3383:             }
 3384:         }
 3385:     }
 3386:     if (shown.length > 0) {
 3387:         for (var i=0; i<shown.length; i++) {
 3388:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3389:                 if (shown[i] == 'privs') {
 3390:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3391:                 } else {
 3392:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3393:                 }
 3394:             }
 3395:         }
 3396:     }
 3397:     return;
 3398: }
 3399: 
 3400: function toggleHelpdeskItem(num,field) {
 3401:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3402:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3403:             document.getElementById('helproles_'+num+'_'+field).className =
 3404:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3405:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3406:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3407:             }
 3408:         } else {
 3409:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3410:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3411:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3412:             }
 3413:         }
 3414:     }
 3415:     return;
 3416: }
 3417: 
 3418: // ]]>
 3419: </script>
 3420: 
 3421: ENDSCRIPT
 3422: }
 3423: 
 3424: sub helpdeskroles_access {
 3425:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3426:         $usertypes,$types,$domhelpdesk) = @_;
 3427:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3428:     my %lt = &Apache::lonlocal::texthash(
 3429:                     'rou'    => 'Role usage',
 3430:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3431:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3432:                     'dh'     => 'All with domain helpdesk role',
 3433:                     'da'     => 'All with domain helpdesk assistant role',
 3434:                     'none'   => 'None',
 3435:                     'status' => 'Determined based on institutional status',
 3436:                     'inc'    => 'Include all, but exclude specific personnel',
 3437:                     'exc'    => 'Exclude all, but include specific personnel',
 3438:                   );
 3439:     my %usecheck = (
 3440:                      all => ' checked="checked"',
 3441:                    );
 3442:     my %displaydiv = (
 3443:                       status => 'none',
 3444:                       inc    => 'none',
 3445:                       exc    => 'none',
 3446:                       priv   => 'block',
 3447:                      );
 3448:     my $output;
 3449:     if (ref($current) eq 'HASH') {
 3450:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3451:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3452:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3453:                 delete($usecheck{'all'});
 3454:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3455:                     my $access = $1;
 3456:                     $displaydiv{$access} = 'inline';
 3457:                 } elsif ($current->{access} eq 'none') {
 3458:                     $displaydiv{'priv'} = 'none';
 3459:                 }
 3460:             }
 3461:         }
 3462:     }
 3463:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3464:               '<p>'.$lt{'whi'}.'</p>';
 3465:     foreach my $access (@{$accesstypes}) {
 3466:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3467:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3468:                    $lt{$access}.'</label>';
 3469:         if ($access eq 'status') {
 3470:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3471:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3472:                                                                  $othertitle,$usertypes,$types).
 3473:                        '</div>';
 3474:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3475:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3476:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3477:                        '</div>';
 3478:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3479:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3480:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3481:                        '</div>';
 3482:         }
 3483:         $output .= '</p>';
 3484:     }
 3485:     $output .= '</fieldset>';
 3486:     return $output;
 3487: }
 3488: 
 3489: sub radiobutton_prefs {
 3490:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3491:         $additional,$align) = @_;
 3492:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3493:                    (ref($choices) eq 'HASH'));
 3494: 
 3495:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3496: 
 3497:     foreach my $item (@{$toggles}) {
 3498:         if ($defaultchecked->{$item} eq 'on') {
 3499:             $checkedon{$item} = ' checked="checked" ';
 3500:             $checkedoff{$item} = ' ';
 3501:         } elsif ($defaultchecked->{$item} eq 'off') {
 3502:             $checkedoff{$item} = ' checked="checked" ';
 3503:             $checkedon{$item} = ' ';
 3504:         }
 3505:     }
 3506:     if (ref($settings) eq 'HASH') {
 3507:         foreach my $item (@{$toggles}) {
 3508:             if ($settings->{$item} eq '1') {
 3509:                 $checkedon{$item} =  ' checked="checked" ';
 3510:                 $checkedoff{$item} = ' ';
 3511:             } elsif ($settings->{$item} eq '0') {
 3512:                 $checkedoff{$item} =  ' checked="checked" ';
 3513:                 $checkedon{$item} = ' ';
 3514:             }
 3515:         }
 3516:     }
 3517:     if ($onclick) {
 3518:         $onclick = ' onclick="'.$onclick.'"';
 3519:     }
 3520:     foreach my $item (@{$toggles}) {
 3521:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3522:         $datatable .=
 3523:             '<tr'.$css_class.'><td valign="top">'.
 3524:             '<span class="LC_nobreak">'.$choices->{$item}.
 3525:             '</span></td>';
 3526:         if ($align eq 'left') {
 3527:             $datatable .= '<td class="LC_left_item">';
 3528:         } else {
 3529:             $datatable .= '<td class="LC_right_item">';
 3530:         }
 3531:         $datatable .=
 3532:             '<span class="LC_nobreak">'.
 3533:             '<label><input type="radio" name="'.
 3534:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3535:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3536:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3537:             '</span>'.$additional.
 3538:             '</td>'.
 3539:             '</tr>';
 3540:         $itemcount ++;
 3541:     }
 3542:     return ($datatable,$itemcount);
 3543: }
 3544: 
 3545: sub print_coursedefaults {
 3546:     my ($position,$dom,$settings,$rowtotal) = @_;
 3547:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 3548:     my $itemcount = 1;
 3549:     my %choices =  &Apache::lonlocal::texthash (
 3550:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 3551:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 3552:         coursecredits        => 'Credits can be specified for courses',
 3553:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 3554:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 3555:         texengine            => 'Default method to display mathematics',
 3556:         postsubmit           => 'Disable submit button/keypress following student submission',
 3557:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 3558:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 3559:     );
 3560:     my %staticdefaults = (
 3561:                            anonsurvey_threshold => 10,
 3562:                            uploadquota          => 500,
 3563:                            postsubmit           => 60,
 3564:                            mysqltables          => 172800,
 3565:                          );
 3566:     if ($position eq 'top') {
 3567:         %defaultchecked = (
 3568:                             'uselcmath'       => 'on',
 3569:                             'usejsme'         => 'on',
 3570:                             'canclone'        => 'none',
 3571:                           );
 3572:         @toggles = ('uselcmath','usejsme');
 3573:         my $deftex = $Apache::lonnet::deftex;
 3574:         if (ref($settings) eq 'HASH') {
 3575:             if ($settings->{'texengine'}) {
 3576:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 3577:                     $deftex = $settings->{'texengine'};
 3578:                 }
 3579:             }
 3580:         }
 3581:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3582:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 3583:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 3584:                        '</span></td><td class="LC_right_item">'.
 3585:                        '<select name="texengine">'."\n";
 3586:         my %texoptions = (
 3587:                             MathJax  => 'MathJax',
 3588:                             mimetex  => &mt('Convert to Images'),
 3589:                             tth      => &mt('TeX to HTML'),
 3590:                          );
 3591:         foreach my $renderer ('MathJax','mimetex','tth') {
 3592:             my $selected = '';
 3593:             if ($renderer eq $deftex) {
 3594:                 $selected = ' selected="selected"';
 3595:             }
 3596:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 3597:         }
 3598:         $mathdisp .= '</select></td></tr>'."\n";
 3599:         $itemcount ++;
 3600:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3601:                                                      \%choices,$itemcount);
 3602:         $datatable = $mathdisp.$datatable;
 3603:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3604:         $datatable .=
 3605:             '<tr'.$css_class.'><td valign="top">'.
 3606:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 3607:             '</span></td><td class="LC_left_item">';
 3608:         my $currcanclone = 'none';
 3609:         my $onclick;
 3610:         my @cloneoptions = ('none','domain');
 3611:         my %clonetitles = (
 3612:                              none     => 'No additional course requesters',
 3613:                              domain   => "Any course requester in course's domain",
 3614:                              instcode => 'Course requests for official courses ...',
 3615:                           );
 3616:         my (%codedefaults,@code_order,@posscodes);
 3617:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 3618:                                                     \@code_order) eq 'ok') {
 3619:             if (@code_order > 0) {
 3620:                 push(@cloneoptions,'instcode');
 3621:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 3622:             }
 3623:         }
 3624:         if (ref($settings) eq 'HASH') {
 3625:             if ($settings->{'canclone'}) {
 3626:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 3627:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 3628:                         if (@code_order > 0) {
 3629:                             $currcanclone = 'instcode';
 3630:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 3631:                         }
 3632:                     }
 3633:                 } elsif ($settings->{'canclone'} eq 'domain') {
 3634:                     $currcanclone = $settings->{'canclone'};
 3635:                 }
 3636:             }
 3637:         }
 3638:         foreach my $option (@cloneoptions) {
 3639:             my ($checked,$additional);
 3640:             if ($currcanclone eq $option) {
 3641:                 $checked = ' checked="checked"';
 3642:             }
 3643:             if ($option eq 'instcode') {
 3644:                 if (@code_order) {
 3645:                     my $show = 'none';
 3646:                     if ($checked) {
 3647:                         $show = 'block';
 3648:                     }
 3649:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 3650:                                   &mt('Institutional codes for new and cloned course have identical:').
 3651:                                   '<br />';
 3652:                     foreach my $item (@code_order) {
 3653:                         my $codechk;
 3654:                         if ($checked) {
 3655:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 3656:                                 $codechk = ' checked="checked"';
 3657:                             }
 3658:                         }
 3659:                         $additional .= '<label>'.
 3660:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 3661:                                        $item.'</label>';
 3662:                     }
 3663:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 3664:                 }
 3665:             }
 3666:             $datatable .=
 3667:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 3668:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 3669:                 '</label>&nbsp;'.$additional.'</span><br />';
 3670:         }
 3671:         $datatable .= '</td>'.
 3672:                       '</tr>';
 3673:         $itemcount ++;
 3674:     } else {
 3675:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3676:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 3677:         my $currusecredits = 0;
 3678:         my $postsubmitclient = 1;
 3679:         my @types = ('official','unofficial','community','textbook');
 3680:         if (ref($settings) eq 'HASH') {
 3681:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 3682:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 3683:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3684:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3685:                 }
 3686:             }
 3687:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3688:                 foreach my $type (@types) {
 3689:                     next if ($type eq 'community');
 3690:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3691:                     if ($defcredits{$type} ne '') {
 3692:                         $currusecredits = 1;
 3693:                     }
 3694:                 }
 3695:             }
 3696:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3697:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3698:                     $postsubmitclient = 0;
 3699:                     foreach my $type (@types) {
 3700:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3701:                     }
 3702:                 } else {
 3703:                     foreach my $type (@types) {
 3704:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3705:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3706:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 3707:                             } else {
 3708:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3709:                             }
 3710:                         } else {
 3711:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3712:                         }
 3713:                     }
 3714:                 }
 3715:             } else {
 3716:                 foreach my $type (@types) {
 3717:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3718:                 }
 3719:             }
 3720:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 3721:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 3722:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 3723:                 }
 3724:             } else {
 3725:                 foreach my $type (@types) {
 3726:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 3727:                 }
 3728:             }
 3729:         } else {
 3730:             foreach my $type (@types) {
 3731:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3732:             }
 3733:         }
 3734:         if (!$currdefresponder) {
 3735:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3736:         } elsif ($currdefresponder < 1) {
 3737:             $currdefresponder = 1;
 3738:         }
 3739:         foreach my $type (@types) {
 3740:             if ($curruploadquota{$type} eq '') {
 3741:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3742:             }
 3743:         }
 3744:         $datatable .=
 3745:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3746:                 $choices{'anonsurvey_threshold'}.
 3747:                 '</span></td>'.
 3748:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3749:                 '<input type="text" name="anonsurvey_threshold"'.
 3750:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3751:                 '</td></tr>'."\n";
 3752:         $itemcount ++;
 3753:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3754:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3755:                       $choices{'uploadquota'}.
 3756:                       '</span></td>'.
 3757:                       '<td align="right" class="LC_right_item">'.
 3758:                       '<table><tr>';
 3759:         foreach my $type (@types) {
 3760:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3761:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3762:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3763:         }
 3764:         $datatable .= '</tr></table></td></tr>'."\n";
 3765:         $itemcount ++;
 3766:         my $onclick = "toggleDisplay(this.form,'credits');";
 3767:         my $display = 'none';
 3768:         if ($currusecredits) {
 3769:             $display = 'block';
 3770:         }
 3771:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3772:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3773:         foreach my $type (@types) {
 3774:             next if ($type eq 'community');
 3775:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3776:                            '<input type="text" name="'.$type.'_credits"'.
 3777:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3778:         }
 3779:         $additional .= '</tr></table></div>'."\n";
 3780:         %defaultchecked = ('coursecredits' => 'off');
 3781:         @toggles = ('coursecredits');
 3782:         my $current = {
 3783:                         'coursecredits' => $currusecredits,
 3784:                       };
 3785:         (my $table,$itemcount) =
 3786:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3787:                                \%choices,$itemcount,$onclick,$additional,'left');
 3788:         $datatable .= $table;
 3789:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3790:         my $display = 'none';
 3791:         if ($postsubmitclient) {
 3792:             $display = 'block';
 3793:         }
 3794:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3795:                       &mt('Number of seconds submit is disabled').'<br />'.
 3796:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3797:                       '<table><tr>';
 3798:         foreach my $type (@types) {
 3799:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3800:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3801:                            $deftimeout{$type}.'" size="5" /></td>';
 3802:         }
 3803:         $additional .= '</tr></table></div>'."\n";
 3804:         %defaultchecked = ('postsubmit' => 'on');
 3805:         @toggles = ('postsubmit');
 3806:         $current = {
 3807:                        'postsubmit' => $postsubmitclient,
 3808:                    };
 3809:         ($table,$itemcount) =
 3810:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3811:                                \%choices,$itemcount,$onclick,$additional,'left');
 3812:         $datatable .= $table;
 3813:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3814:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3815:                       $choices{'mysqltables'}.
 3816:                       '</span></td>'.
 3817:                       '<td align="right" class="LC_right_item">'.
 3818:                       '<table><tr>';
 3819:         foreach my $type (@types) {
 3820:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3821:                            '<input type="text" name="mysqltables_'.$type.'"'.
 3822:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 3823:         }
 3824:         $datatable .= '</tr></table></td></tr>'."\n";
 3825:         $itemcount ++;
 3826: 
 3827:     }
 3828:     $$rowtotal += $itemcount;
 3829:     return $datatable;
 3830: }
 3831: 
 3832: sub print_selfenrollment {
 3833:     my ($position,$dom,$settings,$rowtotal) = @_;
 3834:     my ($css_class,$datatable);
 3835:     my $itemcount = 1;
 3836:     my @types = ('official','unofficial','community','textbook');
 3837:     if (($position eq 'top') || ($position eq 'middle')) {
 3838:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 3839:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 3840:         my @rows;
 3841:         my $key;
 3842:         if ($position eq 'top') {
 3843:             $key = 'admin'; 
 3844:             if (ref($rowsref) eq 'ARRAY') {
 3845:                 @rows = @{$rowsref};
 3846:             }
 3847:         } elsif ($position eq 'middle') {
 3848:             $key = 'default';
 3849:             @rows = ('types','registered','approval','limit');
 3850:         }
 3851:         foreach my $row (@rows) {
 3852:             if (defined($titlesref->{$row})) {
 3853:                 $itemcount ++;
 3854:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3855:                 $datatable .= '<tr'.$css_class.'>'.
 3856:                               '<td>'.$titlesref->{$row}.'</td>'.
 3857:                               '<td class="LC_left_item">'.
 3858:                               '<table><tr>';
 3859:                 my (%current,%currentcap);
 3860:                 if (ref($settings) eq 'HASH') {
 3861:                     if (ref($settings->{$key}) eq 'HASH') {
 3862:                         foreach my $type (@types) {
 3863:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3864:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 3865:                             }
 3866:                             if (($row eq 'limit') && ($key eq 'default')) {
 3867:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3868:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 3869:                                 }
 3870:                             }
 3871:                         }
 3872:                     }
 3873:                 }
 3874:                 my %roles = (
 3875:                              '0' => &Apache::lonnet::plaintext('dc'),
 3876:                             ); 
 3877:             
 3878:                 foreach my $type (@types) {
 3879:                     unless (($row eq 'registered') && ($key eq 'default')) {
 3880:                         $datatable .= '<th>'.&mt($type).'</th>';
 3881:                     }
 3882:                 }
 3883:                 unless (($row eq 'registered') && ($key eq 'default')) {
 3884:                     $datatable .= '</tr><tr>';
 3885:                 }
 3886:                 foreach my $type (@types) {
 3887:                     if ($type eq 'community') {
 3888:                         $roles{'1'} = &mt('Community personnel');
 3889:                     } else {
 3890:                         $roles{'1'} = &mt('Course personnel');
 3891:                     }
 3892:                     $datatable .= '<td style="vertical-align: top">';
 3893:                     if ($position eq 'top') {
 3894:                         my %checked;
 3895:                         if ($current{$type} eq '0') {
 3896:                             $checked{'0'} = ' checked="checked"';
 3897:                         } else {
 3898:                             $checked{'1'} = ' checked="checked"';
 3899:                         }
 3900:                         foreach my $role ('1','0') {
 3901:                             $datatable .= '<span class="LC_nobreak"><label>'.
 3902:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 3903:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 3904:                                           $roles{$role}.'</label></span> ';
 3905:                         }
 3906:                     } else {
 3907:                         if ($row eq 'types') {
 3908:                             my %checked;
 3909:                             if ($current{$type} =~ /^(all|dom)$/) {
 3910:                                 $checked{$1} = ' checked="checked"';
 3911:                             } else {
 3912:                                 $checked{''} = ' checked="checked"';
 3913:                             }
 3914:                             foreach my $val ('','dom','all') {
 3915:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3916:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3917:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3918:                             }
 3919:                         } elsif ($row eq 'registered') {
 3920:                             my %checked;
 3921:                             if ($current{$type} eq '1') {
 3922:                                 $checked{'1'} = ' checked="checked"';
 3923:                             } else {
 3924:                                 $checked{'0'} = ' checked="checked"';
 3925:                             }
 3926:                             foreach my $val ('0','1') {
 3927:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3928:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3929:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3930:                             }
 3931:                         } elsif ($row eq 'approval') {
 3932:                             my %checked;
 3933:                             if ($current{$type} =~ /^([12])$/) {
 3934:                                 $checked{$1} = ' checked="checked"';
 3935:                             } else {
 3936:                                 $checked{'0'} = ' checked="checked"';
 3937:                             }
 3938:                             for my $val (0..2) {
 3939:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3940:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3941:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3942:                             }
 3943:                         } elsif ($row eq 'limit') {
 3944:                             my %checked;
 3945:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3946:                                 $checked{$1} = ' checked="checked"';
 3947:                             } else {
 3948:                                 $checked{'none'} = ' checked="checked"';
 3949:                             }
 3950:                             my $cap;
 3951:                             if ($currentcap{$type} =~ /^\d+$/) {
 3952:                                 $cap = $currentcap{$type};
 3953:                             }
 3954:                             foreach my $val ('none','allstudents','selfenrolled') {
 3955:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3956:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3957:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3958:                             }
 3959:                             $datatable .= '<br />'.
 3960:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3961:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3962:                                           '</span>'; 
 3963:                         }
 3964:                     }
 3965:                     $datatable .= '</td>';
 3966:                 }
 3967:                 $datatable .= '</tr>';
 3968:             }
 3969:             $datatable .= '</table></td></tr>';
 3970:         }
 3971:     } elsif ($position eq 'bottom') {
 3972:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3973:     }
 3974:     $$rowtotal += $itemcount;
 3975:     return $datatable;
 3976: }
 3977: 
 3978: sub print_validation_rows {
 3979:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3980:     my ($itemsref,$namesref,$fieldsref);
 3981:     if ($caller eq 'selfenroll') { 
 3982:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3983:     } elsif ($caller eq 'requestcourses') {
 3984:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3985:     }
 3986:     my %currvalidation;
 3987:     if (ref($settings) eq 'HASH') {
 3988:         if (ref($settings->{'validation'}) eq 'HASH') {
 3989:             %currvalidation = %{$settings->{'validation'}};
 3990:         }
 3991:     }
 3992:     my $datatable;
 3993:     my $itemcount = 0;
 3994:     foreach my $item (@{$itemsref}) {
 3995:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3996:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3997:                       $namesref->{$item}.
 3998:                       '</span></td>'.
 3999:                       '<td class="LC_left_item">';
 4000:         if (($item eq 'url') || ($item eq 'button')) {
 4001:             $datatable .= '<span class="LC_nobreak">'.
 4002:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4003:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4004:         } elsif ($item eq 'fields') {
 4005:             my @currfields;
 4006:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4007:                 @currfields = @{$currvalidation{$item}};
 4008:             }
 4009:             foreach my $field (@{$fieldsref}) {
 4010:                 my $check = '';
 4011:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4012:                     $check = ' checked="checked"';
 4013:                 }
 4014:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4015:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4016:                               ' value="'.$field.'"'.$check.' />'.$field.
 4017:                               '</label></span> ';
 4018:             }
 4019:         } elsif ($item eq 'markup') {
 4020:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 4021:                            $currvalidation{$item}.
 4022:                               '</textarea>';
 4023:         }
 4024:         $datatable .= '</td></tr>'."\n";
 4025:         if (ref($rowtotal)) {
 4026:             $itemcount ++;
 4027:         }
 4028:     }
 4029:     if ($caller eq 'requestcourses') {
 4030:         my %currhash;
 4031:         if (ref($settings) eq 'HASH') {
 4032:             if (ref($settings->{'validation'}) eq 'HASH') {
 4033:                 if ($settings->{'validation'}{'dc'} ne '') {
 4034:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4035:                 }
 4036:             }
 4037:         }
 4038:         my $numinrow = 2;
 4039:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4040:                                                        'validationdc',%currhash);
 4041:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4042:         $datatable .= '<tr'.$css_class.'><td>';
 4043:         if ($numdc > 1) {
 4044:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4045:         } else {
 4046:             $datatable .=  &mt('Course creation processed as: ');
 4047:         }
 4048:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4049:         $itemcount ++;
 4050:     }
 4051:     if (ref($rowtotal)) {
 4052:         $$rowtotal += $itemcount;
 4053:     }
 4054:     return $datatable;
 4055: }
 4056: 
 4057: sub print_passwords {
 4058:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 4059:     my ($datatable,$css_class);
 4060:     my $itemcount = 0;
 4061:     my %titles = &Apache::lonlocal::texthash (
 4062:         captcha        => '"Forgot Password" CAPTCHA validation',
 4063:         link           => 'Reset link expiration (hours)',
 4064:         case           => 'Case-sensitive usernames/e-mail',
 4065:         prelink        => 'Information required (form 1)',
 4066:         postlink       => 'Information required (form 2)',
 4067:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 4068:         customtext     => 'Domain specific text (HTML)',
 4069:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 4070:         intauth_check  => 'Check bcrypt cost if authenticated',
 4071:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 4072:         permanent      => 'Permanent e-mail address',
 4073:         critical       => 'Critical notification address',
 4074:         notify         => 'Notification address',
 4075:         min            => 'Minimum password length',
 4076:         max            => 'Maximum password length',
 4077:         chars          => 'Required characters',
 4078:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 4079:     );
 4080:     if ($position eq 'top') {
 4081:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4082:         my $shownlinklife = 2;
 4083:         my $prelink = 'both';
 4084:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 4085:         if (ref($settings) eq 'HASH') {
 4086:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 4087:                 $shownlinklife = $settings->{resetlink};
 4088:             }
 4089:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 4090:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 4091:             }
 4092:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 4093:                 $prelink = $settings->{resetprelink};
 4094:             }
 4095:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 4096:                 %postlink = %{$settings->{resetpostlink}};
 4097:             }
 4098:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 4099:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 4100:             }
 4101:             if ($settings->{resetremove}) {
 4102:                 $nostdtext = 1;
 4103:             }
 4104:             if ($settings->{resetcustom}) {
 4105:                 $customurl = $settings->{resetcustom};
 4106:             }
 4107:         } else {
 4108:             if (ref($types) eq 'ARRAY') {
 4109:                 foreach my $item (@{$types}) {
 4110:                     $casesens{$item} = 1;
 4111:                     $postlink{$item} = ['username','email'];
 4112:                 }
 4113:                 }
 4114:             }
 4115:             $casesens{'default'} = 1;
 4116:             $postlink{'default'} = ['username','email'];
 4117:             $prelink = 'both';
 4118:             %emailsrc = (
 4119:                           permanent => 1,
 4120:                           critical  => 1,
 4121:                           notify    => 1,
 4122:             );
 4123:         }
 4124:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 4125:         $itemcount ++;
 4126:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4127:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 4128:                       '<td class="LC_left_item">'.
 4129:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 4130:                       'name="passwords_link" size="3" /></td></tr>';
 4131:         $itemcount ++;
 4132:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4133:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 4134:                       '<td class="LC_left_item">';
 4135:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4136:             foreach my $item (@{$types}) {
 4137:                 my $checkedcase;
 4138:                 if ($casesens{$item}) {
 4139:                     $checkedcase = ' checked="checked"';
 4140:                 }
 4141:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4142:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 4143:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 4144:                               '<span>&nbsp;&nbsp; ';
 4145:             }
 4146:         }
 4147:         my $checkedcase;
 4148:         if ($casesens{'default'}) {
 4149:             $checkedcase = ' checked="checked"';
 4150:         }
 4151:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 4152:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 4153:                       $othertitle.'</label></span></td>';
 4154:         $itemcount ++;
 4155:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4156:         my %checkedpre = (
 4157:                              both => ' checked="checked"',
 4158:                              either => '',
 4159:                          );
 4160:         if ($prelink eq 'either') {
 4161:             $checkedpre{either} = ' checked="checked"';
 4162:             $checkedpre{both} = '';
 4163:         }
 4164:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 4165:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4166:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 4167:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 4168:                       '<span class="LC_nobreak"><label>'.
 4169:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 4170:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 4171:         $itemcount ++;
 4172:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4173:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 4174:                       '<td class="LC_left_item">';
 4175:         my %postlinked;
 4176:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4177:             foreach my $item (@{$types}) {
 4178:                 undef(%postlinked);
 4179:                 $datatable .= '<fieldset style="display: inline-block;">'.
 4180:                               '<legend>'.$usertypes->{$item}.'</legend>';
 4181:                 if (ref($postlink{$item}) eq 'ARRAY') {
 4182:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 4183:                 }
 4184:                 foreach my $field ('email','username') {
 4185:                     my $checked;
 4186:                     if ($postlinked{$field}) {
 4187:                         $checked = ' checked="checked"';
 4188:                     }
 4189:                     $datatable .= '<span class="LC_nobreak"><label>'.
 4190:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 4191:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 4192:                                   '<span>&nbsp;&nbsp; ';
 4193:                 }
 4194:                 $datatable .= '</fieldset>';
 4195:             }
 4196:         }
 4197:         if (ref($postlink{'default'}) eq 'ARRAY') {
 4198:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 4199:         }
 4200:         $datatable .= '<fieldset style="display: inline-block;">'.
 4201:                       '<legend>'.$othertitle.'</legend>';
 4202:         foreach my $field ('email','username') {
 4203:             my $checked;
 4204:             if ($postlinked{$field}) {
 4205:                 $checked = ' checked="checked"';
 4206:             }
 4207:             $datatable .= '<span class="LC_nobreak"><label>'.
 4208:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 4209:                           $field.'"'.$checked.' />'.$field.'</label>'.
 4210:                           '<span>&nbsp;&nbsp; ';
 4211:         }
 4212:         $datatable .= '</fieldset></td></tr>';
 4213:         $itemcount ++;
 4214:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4215:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 4216:                       '<td class="LC_left_item">';
 4217:         foreach my $type ('permanent','critical','notify') {
 4218:             my $checkedemail;
 4219:             if ($emailsrc{$type}) {
 4220:                 $checkedemail = ' checked="checked"';
 4221:             }
 4222:             $datatable .= '<span class="LC_nobreak"><label>'.
 4223:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 4224:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 4225:                           '<span>&nbsp;&nbsp; ';
 4226:         }
 4227:         $datatable .= '</td></tr>';
 4228:         $itemcount ++;
 4229:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4230:         my $switchserver = &check_switchserver($dom,$confname);
 4231:         my ($showstd,$noshowstd);
 4232:         if ($nostdtext) {
 4233:             $noshowstd = ' checked="checked"';
 4234:         } else {
 4235:             $showstd = ' checked="checked"';
 4236:         }
 4237:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 4238:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4239:                       &mt('Retain standard text:').
 4240:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 4241:                       &mt('Yes').'</label>'.'&nbsp;'.
 4242:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 4243:                       &mt('No').'</label></span><br />'.
 4244:                       '<span class="LC_fontsize_small">'.
 4245:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 4246:                       &mt('Include custom text:');
 4247:         if ($customurl) {
 4248:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('Custom text file'),600,500,
 4249:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4250:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 4251:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 4252:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 4253:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 4254:         }
 4255:         if ($switchserver) {
 4256:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 4257:         } else {
 4258:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4259:                          '<input type="file" name="passwords_customfile" /></span>';
 4260:         }
 4261:         $datatable .= '</td></tr>';
 4262:     } elsif ($position eq 'middle') {
 4263:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 4264:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 4265:         my %defaults;
 4266:         if (ref($domconf{'defaults'}) eq 'HASH') {
 4267:             %defaults = %{$domconf{'defaults'}};
 4268:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 4269:                 $defaults{'intauth_cost'} = 10;
 4270:             }
 4271:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 4272:                 $defaults{'intauth_check'} = 0;
 4273:             }
 4274:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 4275:                 $defaults{'intauth_switch'} = 0;
 4276:             }
 4277:         } else {
 4278:             %defaults = (
 4279:                           'intauth_cost'   => 10,
 4280:                           'intauth_check'  => 0,
 4281:                           'intauth_switch' => 0,
 4282:                         );
 4283:         }
 4284:         foreach my $item (@items) {
 4285:             if ($itemcount%2) {
 4286:                 $css_class = '';
 4287:             } else {
 4288:                 $css_class = ' class="LC_odd_row" ';
 4289:             }
 4290:             $datatable .= '<tr'.$css_class.'>'.
 4291:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 4292:                           '</span></td><td class="LC_left_item" colspan="3">';
 4293:             if ($item eq 'intauth_switch') {
 4294:                 my @options = (0,1,2);
 4295:                 my %optiondesc = &Apache::lonlocal::texthash (
 4296:                                    0 => 'No',
 4297:                                    1 => 'Yes',
 4298:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 4299:                                  );
 4300:                 $datatable .= '<table width="100%">';
 4301:                 foreach my $option (@options) {
 4302:                     my $checked = ' ';
 4303:                     if ($defaults{$item} eq $option) {
 4304:                         $checked = ' checked="checked"';
 4305:                     }
 4306:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4307:                                   '<label><input type="radio" name="'.$item.
 4308:                                   '" value="'.$option.'"'.$checked.' />'.
 4309:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4310:                 }
 4311:                 $datatable .= '</table>';
 4312:             } elsif ($item eq 'intauth_check') {
 4313:                 my @options = (0,1,2);
 4314:                 my %optiondesc = &Apache::lonlocal::texthash (
 4315:                                    0 => 'No',
 4316:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 4317:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 4318:                                  );
 4319:                 $datatable .= '<table width="100%">';
 4320:                 foreach my $option (@options) {
 4321:                     my $checked = ' ';
 4322:                     my $onclick;
 4323:                     if ($defaults{$item} eq $option) {
 4324:                         $checked = ' checked="checked"';
 4325:                     }
 4326:                     if ($option == 2) {
 4327:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 4328:                     }
 4329:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4330:                                   '<label><input type="radio" name="'.$item.
 4331:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 4332:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4333:                 }
 4334:                 $datatable .= '</table>';
 4335:             } else {
 4336:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4337:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 4338:             }
 4339:             $datatable .= '</td></tr>';
 4340:             $itemcount ++;
 4341:         }
 4342:     } elsif ($position eq 'lower') {
 4343:         my ($min,$max,%chars,$numsaved);
 4344:         $min = $Apache::lonnet::passwdmin;
 4345:         if (ref($settings) eq 'HASH') {
 4346:             if ($settings->{min}) {
 4347:                 $min = $settings->{min};
 4348:             }
 4349:             if ($settings->{max}) {
 4350:                 $max = $settings->{max};
 4351:             }
 4352:             if (ref($settings->{chars}) eq 'ARRAY') {
 4353:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 4354:             }
 4355:             if ($settings->{numsaved}) {
 4356:                 $numsaved = $settings->{numsaved};
 4357:             }
 4358:         }
 4359:         my %rulenames = &Apache::lonlocal::texthash(
 4360:                                                      uc => 'At least one upper case letter',
 4361:                                                      lc => 'At least one lower case letter',
 4362:                                                      num => 'At least one number',
 4363:                                                      spec => 'At least one non-alphanumeric',
 4364:                                                    );
 4365:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4366:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 4367:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4368:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 4369:                       'onblur="javascript:warnIntPass(this);" />'.
 4370:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 4371:                       '</span></td></tr>';
 4372:         $itemcount ++;
 4373:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4374:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 4375:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4376:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 4377:                       'onblur="javascript:warnIntPass(this);" />'.
 4378:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 4379:                       '</span></td></tr>';
 4380:         $itemcount ++;
 4381:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4382:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 4383:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 4384:                       '</span></td>';
 4385:         my $numinrow = 2;
 4386:         my @possrules = ('uc','lc','num','spec');
 4387:         $datatable .= '<td class="LC_left_item"><table>';
 4388:         for (my $i=0; $i<@possrules; $i++) {
 4389:             my ($rem,$checked);
 4390:             if ($chars{$possrules[$i]}) {
 4391:                 $checked = ' checked="checked"';
 4392:             }
 4393:             $rem = $i%($numinrow);
 4394:             if ($rem == 0) {
 4395:                 if ($i > 0) {
 4396:                     $datatable .= '</tr>';
 4397:                 }
 4398:                 $datatable .= '<tr>';
 4399:             }
 4400:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 4401:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 4402:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 4403:         }
 4404:         my $rem = @possrules%($numinrow);
 4405:         my $colsleft = $numinrow - $rem;
 4406:         if ($colsleft > 1 ) {
 4407:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4408:                           '&nbsp;</td>';
 4409:         } elsif ($colsleft == 1) {
 4410:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4411:         }
 4412:         $datatable .='</table></td></tr>';
 4413:         $itemcount ++;
 4414:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4415:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 4416:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4417:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" .
 4418:                       'onblur="javascript:warnIntPass(this);" />'.
 4419:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 4420:                       '</span></td></tr>';
 4421:     } else {
 4422:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4423:         my %ownerchg = (
 4424:                           by  => {},
 4425:                           for => {},
 4426:                        );
 4427:         my %ownertitles = &Apache::lonlocal::texthash (
 4428:                             by  => 'Course owner status(es) allowed',
 4429:                             for => 'Student status(es) allowed',
 4430:                           );
 4431:         if (ref($settings) eq 'HASH') {
 4432:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 4433:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 4434:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 4435:                 }
 4436:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 4437:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 4438:                 }
 4439:             }
 4440:         }
 4441:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4442:         $datatable .= '<tr '.$css_class.'>'.
 4443:                       '<td>'.
 4444:                       &mt('Requirements').'<ul>'.
 4445:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 4446:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 4447:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 4448:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 4449:                       '</ul>'.
 4450:                       '</td>'.
 4451:                       '<td class="LC_left_item">';
 4452:         foreach my $item ('by','for') {
 4453:             $datatable .= '<fieldset style="display: inline-block;">'.
 4454:                           '<legend>'.$ownertitles{$item}.'</legend>';
 4455:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4456:                 foreach my $type (@{$types}) {
 4457:                     my $checked;
 4458:                     if ($ownerchg{$item}{$type}) {
 4459:                         $checked = ' checked="checked"';
 4460:                     }
 4461:                     $datatable .= '<span class="LC_nobreak"><label>'.
 4462:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 4463:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 4464:                                   '<span>&nbsp;&nbsp; ';
 4465:                 }
 4466:             }
 4467:             my $checked;
 4468:             if ($ownerchg{$item}{'default'}) {
 4469:                 $checked = ' checked="checked"';
 4470:             }
 4471:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 4472:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 4473:                           $othertitle.'</label></span></fieldset>';
 4474:         }
 4475:         $datatable .= '</td></tr>';
 4476:     }
 4477:     return $datatable;
 4478: }
 4479: 
 4480: sub print_usersessions {
 4481:     my ($position,$dom,$settings,$rowtotal) = @_;
 4482:     my ($css_class,$datatable,%checked,%choices);
 4483:     my (%by_ip,%by_location,@intdoms);
 4484:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 4485: 
 4486:     my @alldoms = &Apache::lonnet::all_domains();
 4487:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4488:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4489:     my %altids = &id_for_thisdom(%servers);
 4490:     my $itemcount = 1;
 4491:     if ($position eq 'top') {
 4492:         if (keys(%serverhomes) > 1) {
 4493:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4494:             my $curroffloadnow;
 4495:             if (ref($settings) eq 'HASH') {
 4496:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4497:                     $curroffloadnow = $settings->{'offloadnow'};
 4498:                 }
 4499:             }
 4500:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4501:         } else {
 4502:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4503:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 4504:         }
 4505:     } else {
 4506:         if (keys(%by_location) == 0) {
 4507:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4508:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 4509:         } else {
 4510:             my %lt = &usersession_titles();
 4511:             my $numinrow = 5;
 4512:             my $prefix;
 4513:             my @types;
 4514:             if ($position eq 'bottom') {
 4515:                 $prefix = 'remote';
 4516:                 @types = ('version','excludedomain','includedomain');
 4517:             } else {
 4518:                 $prefix = 'hosted';
 4519:                 @types = ('excludedomain','includedomain');
 4520:             }
 4521:             my (%current,%checkedon,%checkedoff);
 4522:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 4523:             my @locations = sort(keys(%by_location));
 4524:             foreach my $type (@types) {
 4525:                 $checkedon{$type} = '';
 4526:                 $checkedoff{$type} = ' checked="checked"';
 4527:             }
 4528:             if (ref($settings) eq 'HASH') {
 4529:                 if (ref($settings->{$prefix}) eq 'HASH') {
 4530:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 4531:                         $current{$key} = $settings->{$prefix}{$key};
 4532:                         if ($key eq 'version') {
 4533:                             if ($current{$key} ne '') {
 4534:                                 $checkedon{$key} = ' checked="checked"';
 4535:                                 $checkedoff{$key} = '';
 4536:                             }
 4537:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 4538:                             $checkedon{$key} = ' checked="checked"';
 4539:                             $checkedoff{$key} = '';
 4540:                         }
 4541:                     }
 4542:                 }
 4543:             }
 4544:             foreach my $type (@types) {
 4545:                 next if ($type ne 'version' && !@locations);
 4546:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4547:                 $datatable .= '<tr'.$css_class.'>
 4548:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 4549:                                <span class="LC_nobreak">&nbsp;
 4550:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4551:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4552:                 if ($type eq 'version') {
 4553:                     my $selector = '<select name="'.$prefix.'_version">';
 4554:                     foreach my $version (@lcversions) {
 4555:                         my $selected = '';
 4556:                         if ($current{'version'} eq $version) {
 4557:                             $selected = ' selected="selected"';
 4558:                         }
 4559:                         $selector .= ' <option value="'.$version.'"'.
 4560:                                      $selected.'>'.$version.'</option>';
 4561:                     }
 4562:                     $selector .= '</select> ';
 4563:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4564:                 } else {
 4565:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4566:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4567:                                  ' />'.('&nbsp;'x2).
 4568:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 4569:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4570:                                  "\n".
 4571:                                  '</div><div><table>';
 4572:                     my $rem;
 4573:                     for (my $i=0; $i<@locations; $i++) {
 4574:                         my ($showloc,$value,$checkedtype);
 4575:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 4576:                             my $ip = $by_location{$locations[$i]}->[0];
 4577:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 4578:                                  $value = join(':',@{$by_ip{$ip}});
 4579:                                 $showloc = join(', ',@{$by_ip{$ip}});
 4580:                                 if (ref($current{$type}) eq 'ARRAY') {
 4581:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 4582:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4583:                                             $checkedtype = ' checked="checked"';
 4584:                                             last;
 4585:                                         }
 4586:                                     }
 4587:                                 }
 4588:                             }
 4589:                         }
 4590:                         $rem = $i%($numinrow);
 4591:                         if ($rem == 0) {
 4592:                             if ($i > 0) {
 4593:                                 $datatable .= '</tr>';
 4594:                             }
 4595:                             $datatable .= '<tr>';
 4596:                         }
 4597:                         $datatable .= '<td class="LC_left_item">'.
 4598:                                       '<span class="LC_nobreak"><label>'.
 4599:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 4600:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4601:                                       '</label></span></td>';
 4602:                     }
 4603:                     $rem = @locations%($numinrow);
 4604:                     my $colsleft = $numinrow - $rem;
 4605:                     if ($colsleft > 1 ) {
 4606:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4607:                                       '&nbsp;</td>';
 4608:                     } elsif ($colsleft == 1) {
 4609:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4610:                     }
 4611:                     $datatable .= '</tr></table>';
 4612:                 }
 4613:                 $datatable .= '</td></tr>';
 4614:                 $itemcount ++;
 4615:             }
 4616:         }
 4617:     }
 4618:     $$rowtotal += $itemcount;
 4619:     return $datatable;
 4620: }
 4621: 
 4622: sub build_location_hashes {
 4623:     my ($intdoms,$by_ip,$by_location) = @_;
 4624:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4625:                   (ref($by_location) eq 'HASH')); 
 4626:     my %iphost = &Apache::lonnet::get_iphost();
 4627:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4628:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4629:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4630:         foreach my $id (@{$iphost{$primary_ip}}) {
 4631:             my $intdom = &Apache::lonnet::internet_dom($id);
 4632:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4633:                 push(@{$intdoms},$intdom);
 4634:             }
 4635:         }
 4636:     }
 4637:     foreach my $ip (keys(%iphost)) {
 4638:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4639:             foreach my $id (@{$iphost{$ip}}) {
 4640:                 my $location = &Apache::lonnet::internet_dom($id);
 4641:                 if ($location) {
 4642:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 4643:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4644:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4645:                             push(@{$by_ip->{$ip}},$location);
 4646:                         }
 4647:                     } else {
 4648:                         $by_ip->{$ip} = [$location];
 4649:                     }
 4650:                 }
 4651:             }
 4652:         }
 4653:     }
 4654:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4655:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4656:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4657:             my $first = $by_ip->{$ip}->[0];
 4658:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4659:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4660:                     push(@{$by_location->{$first}},$ip);
 4661:                 }
 4662:             } else {
 4663:                 $by_location->{$first} = [$ip];
 4664:             }
 4665:         }
 4666:     }
 4667:     return;
 4668: }
 4669: 
 4670: sub current_offloads_to {
 4671:     my ($dom,$settings,$servers) = @_;
 4672:     my (%spareid,%otherdomconfigs);
 4673:     if (ref($servers) eq 'HASH') {
 4674:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4675:             my $gotspares;
 4676:             if (ref($settings) eq 'HASH') {
 4677:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4678:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4679:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4680:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4681:                         $gotspares = 1;
 4682:                     }
 4683:                 }
 4684:             }
 4685:             unless ($gotspares) {
 4686:                 my $gotspares;
 4687:                 my $serverhomeID =
 4688:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4689:                 my $serverhomedom =
 4690:                     &Apache::lonnet::host_domain($serverhomeID);
 4691:                 if ($serverhomedom ne $dom) {
 4692:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4693:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4694:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4695:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4696:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4697:                                 $gotspares = 1;
 4698:                             }
 4699:                         }
 4700:                     } else {
 4701:                         $otherdomconfigs{$serverhomedom} =
 4702:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4703:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4704:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4705:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4706:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4707:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4708:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4709:                                         $gotspares = 1;
 4710:                                     }
 4711:                                 }
 4712:                             }
 4713:                         }
 4714:                     }
 4715:                 }
 4716:             }
 4717:             unless ($gotspares) {
 4718:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4719:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4720:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4721:                } else {
 4722:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4723:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4724:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4725:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4726:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4727:                     } else {
 4728:                         my %what = (
 4729:                              spareid => 1,
 4730:                         );
 4731:                         my ($result,$returnhash) = 
 4732:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4733:                         if ($result eq 'ok') { 
 4734:                             if (ref($returnhash) eq 'HASH') {
 4735:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4736:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4737:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4738:                                 }
 4739:                             }
 4740:                         }
 4741:                     }
 4742:                 }
 4743:             }
 4744:         }
 4745:     }
 4746:     return %spareid;
 4747: }
 4748: 
 4749: sub spares_row {
 4750:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4751:     my $css_class;
 4752:     my $numinrow = 4;
 4753:     my $itemcount = 1;
 4754:     my $datatable;
 4755:     my %typetitles = &sparestype_titles();
 4756:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4757:         foreach my $server (sort(keys(%{$servers}))) {
 4758:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4759:             my ($othercontrol,$serverdom);
 4760:             if ($serverhome ne $server) {
 4761:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4762:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4763:             } else {
 4764:                 $serverdom = &Apache::lonnet::host_domain($server);
 4765:                 if ($serverdom ne $dom) {
 4766:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4767:                 }
 4768:             }
 4769:             next unless (ref($spareid->{$server}) eq 'HASH');
 4770:             my $checkednow;
 4771:             if (ref($curroffloadnow) eq 'HASH') {
 4772:                 if ($curroffloadnow->{$server}) {
 4773:                     $checkednow = ' checked="checked"';
 4774:                 }
 4775:             }
 4776:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4777:             $datatable .= '<tr'.$css_class.'>
 4778:                            <td rowspan="2">
 4779:                             <span class="LC_nobreak">'.
 4780:                           &mt('[_1] when busy, offloads to:'
 4781:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4782:                           '<span class="LC_nobreak">'."\n".
 4783:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4784:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4785:                           "\n";
 4786:             my (%current,%canselect);
 4787:             my @choices = 
 4788:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4789:             foreach my $type ('primary','default') {
 4790:                 if (ref($spareid->{$server}) eq 'HASH') {
 4791:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4792:                         my @spares = @{$spareid->{$server}{$type}};
 4793:                         if (@spares > 0) {
 4794:                             if ($othercontrol) {
 4795:                                 $current{$type} = join(', ',@spares);
 4796:                             } else {
 4797:                                 $current{$type} .= '<table>';
 4798:                                 my $numspares = scalar(@spares);
 4799:                                 for (my $i=0;  $i<@spares; $i++) {
 4800:                                     my $rem = $i%($numinrow);
 4801:                                     if ($rem == 0) {
 4802:                                         if ($i > 0) {
 4803:                                             $current{$type} .= '</tr>';
 4804:                                         }
 4805:                                         $current{$type} .= '<tr>';
 4806:                                     }
 4807:                                     $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;'.
 4808:                                                        $spareid->{$server}{$type}[$i].
 4809:                                                        '</label></td>'."\n";
 4810:                                 }
 4811:                                 my $rem = @spares%($numinrow);
 4812:                                 my $colsleft = $numinrow - $rem;
 4813:                                 if ($colsleft > 1 ) {
 4814:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4815:                                                        '" class="LC_left_item">'.
 4816:                                                        '&nbsp;</td>';
 4817:                                 } elsif ($colsleft == 1) {
 4818:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4819:                                 }
 4820:                                 $current{$type} .= '</tr></table>';
 4821:                             }
 4822:                         }
 4823:                     }
 4824:                     if ($current{$type} eq '') {
 4825:                         $current{$type} = &mt('None specified');
 4826:                     }
 4827:                     if ($othercontrol) {
 4828:                         if ($type eq 'primary') {
 4829:                             $canselect{$type} = $othercontrol;
 4830:                         }
 4831:                     } else {
 4832:                         $canselect{$type} = 
 4833:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4834:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4835:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4836:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4837:                         if (@choices > 0) {
 4838:                             foreach my $lonhost (@choices) {
 4839:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4840:                             }
 4841:                         }
 4842:                         $canselect{$type} .= '</select>'."\n";
 4843:                     }
 4844:                 } else {
 4845:                     $current{$type} = &mt('Could not be determined');
 4846:                     if ($type eq 'primary') {
 4847:                         $canselect{$type} =  $othercontrol;
 4848:                     }
 4849:                 }
 4850:                 if ($type eq 'default') {
 4851:                     $datatable .= '<tr'.$css_class.'>';
 4852:                 }
 4853:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 4854:                               '<td>'.$current{$type}.'</td>'."\n".
 4855:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 4856:             }
 4857:             $itemcount ++;
 4858:         }
 4859:     }
 4860:     $$rowtotal += $itemcount;
 4861:     return $datatable;
 4862: }
 4863: 
 4864: sub possible_newspares {
 4865:     my ($server,$currspares,$serverhomes,$altids) = @_;
 4866:     my $serverhostname = &Apache::lonnet::hostname($server);
 4867:     my %excluded;
 4868:     if ($serverhostname ne '') {
 4869:         %excluded = (
 4870:                        $serverhostname => 1,
 4871:                     );
 4872:     }
 4873:     if (ref($currspares) eq 'HASH') {
 4874:         foreach my $type (keys(%{$currspares})) {
 4875:             if (ref($currspares->{$type}) eq 'ARRAY') {
 4876:                 if (@{$currspares->{$type}} > 0) {
 4877:                     foreach my $curr (@{$currspares->{$type}}) {
 4878:                         my $hostname = &Apache::lonnet::hostname($curr);
 4879:                         $excluded{$hostname} = 1;
 4880:                     }
 4881:                 }
 4882:             }
 4883:         }
 4884:     }
 4885:     my @choices;
 4886:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4887:         if (keys(%{$serverhomes}) > 1) {
 4888:             foreach my $name (sort(keys(%{$serverhomes}))) {
 4889:                 unless ($excluded{$name}) {
 4890:                     if (exists($altids->{$serverhomes->{$name}})) {
 4891:                         push(@choices,$altids->{$serverhomes->{$name}});
 4892:                     } else {
 4893:                         push(@choices,$serverhomes->{$name});
 4894:                     }
 4895:                 }
 4896:             }
 4897:         }
 4898:     }
 4899:     return sort(@choices);
 4900: }
 4901: 
 4902: sub print_loadbalancing {
 4903:     my ($dom,$settings,$rowtotal) = @_;
 4904:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4905:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4906:     my $numinrow = 1;
 4907:     my $datatable;
 4908:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4909:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 4910:     if (ref($settings) eq 'HASH') {
 4911:         %existing = %{$settings};
 4912:     }
 4913:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 4914:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 4915:                                   \%currtargets,\%currrules,\%currcookies);
 4916:     } else {
 4917:         return;
 4918:     }
 4919:     my ($othertitle,$usertypes,$types) =
 4920:         &Apache::loncommon::sorted_inst_types($dom);
 4921:     my $rownum = 8;
 4922:     if (ref($types) eq 'ARRAY') {
 4923:         $rownum += scalar(@{$types});
 4924:     }
 4925:     my @css_class = ('LC_odd_row','LC_even_row');
 4926:     my $balnum = 0;
 4927:     my $islast;
 4928:     my (@toshow,$disabledtext);
 4929:     if (keys(%currbalancer) > 0) {
 4930:         @toshow = sort(keys(%currbalancer));
 4931:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 4932:             push(@toshow,'');
 4933:         }
 4934:     } else {
 4935:         @toshow = ('');
 4936:         $disabledtext = &mt('No existing load balancer');
 4937:     }
 4938:     foreach my $lonhost (@toshow) {
 4939:         if ($balnum == scalar(@toshow)-1) {
 4940:             $islast = 1;
 4941:         } else {
 4942:             $islast = 0;
 4943:         }
 4944:         my $cssidx = $balnum%2;
 4945:         my $targets_div_style = 'display: none';
 4946:         my $disabled_div_style = 'display: block';
 4947:         my $homedom_div_style = 'display: none';
 4948:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 4949:                       '<td rowspan="'.$rownum.'" valign="top">'.
 4950:                       '<p>';
 4951:         if ($lonhost eq '') {
 4952:             $datatable .= '<span class="LC_nobreak">';
 4953:             if (keys(%currbalancer) > 0) {
 4954:                 $datatable .= &mt('Add balancer:');
 4955:             } else {
 4956:                 $datatable .= &mt('Enable balancer:');
 4957:             }
 4958:             $datatable .= '&nbsp;'.
 4959:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 4960:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 4961:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 4962:                           '<option value="" selected="selected">'.&mt('None').
 4963:                           '</option>'."\n";
 4964:             foreach my $server (sort(keys(%servers))) {
 4965:                 next if ($currbalancer{$server});
 4966:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 4967:             }
 4968:             $datatable .=
 4969:                 '</select>'."\n".
 4970:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 4971:         } else {
 4972:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 4973:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 4974:                            &mt('Stop balancing').'</label>'.
 4975:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 4976:             $targets_div_style = 'display: block';
 4977:             $disabled_div_style = 'display: none';
 4978:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 4979:                 $homedom_div_style = 'display: block';
 4980:             }
 4981:         }
 4982:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 4983:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 4984:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 4985:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 4986:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 4987:         my @sparestypes = ('primary','default');
 4988:         my %typetitles = &sparestype_titles();
 4989:         my %hostherechecked = (
 4990:                                   no => ' checked="checked"',
 4991:                               );
 4992:         my %balcookiechecked = (
 4993:                                   no => ' checked="checked"',
 4994:                                );
 4995:         foreach my $sparetype (@sparestypes) {
 4996:             my $targettable;
 4997:             for (my $i=0; $i<$numspares; $i++) {
 4998:                 my $checked;
 4999:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5000:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5001:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5002:                             $checked = ' checked="checked"';
 5003:                         }
 5004:                     }
 5005:                 }
 5006:                 my ($chkboxval,$disabled);
 5007:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5008:                     $chkboxval = $spares[$i];
 5009:                 }
 5010:                 if (exists($currbalancer{$spares[$i]})) {
 5011:                     $disabled = ' disabled="disabled"';
 5012:                 }
 5013:                 $targettable .=
 5014:                     '<td><span class="LC_nobreak"><label>'.
 5015:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5016:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5017:                     '</span></label></span></td>';
 5018:                 my $rem = $i%($numinrow);
 5019:                 if ($rem == 0) {
 5020:                     if (($i > 0) && ($i < $numspares-1)) {
 5021:                         $targettable .= '</tr>';
 5022:                     }
 5023:                     if ($i < $numspares-1) {
 5024:                         $targettable .= '<tr>';
 5025:                     }
 5026:                 }
 5027:             }
 5028:             if ($targettable ne '') {
 5029:                 my $rem = $numspares%($numinrow);
 5030:                 my $colsleft = $numinrow - $rem;
 5031:                 if ($colsleft > 1 ) {
 5032:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5033:                                     '&nbsp;</td>';
 5034:                 } elsif ($colsleft == 1) {
 5035:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5036:                 }
 5037:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5038:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5039:             }
 5040:             $hostherechecked{$sparetype} = '';
 5041:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5042:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5043:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5044:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5045:                         $hostherechecked{'no'} = '';
 5046:                     }
 5047:                 }
 5048:             }
 5049:         }
 5050:         if ($currcookies{$lonhost}) {
 5051:             %balcookiechecked = (
 5052:                                     yes => ' checked="checked"',
 5053:                                 );
 5054:         }
 5055:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5056:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5057:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5058:         foreach my $sparetype (@sparestypes) {
 5059:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5060:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5061:                           '</i></label><br />';
 5062:         }
 5063:         $datatable .= &mt('Use balancer cookie').'<br />'.
 5064:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 5065:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 5066:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 5067:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 5068:                       '</div></td></tr>'.
 5069:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5070:                                            $othertitle,$usertypes,$types,\%servers,
 5071:                                            \%currbalancer,$lonhost,
 5072:                                            $targets_div_style,$homedom_div_style,
 5073:                                            $css_class[$cssidx],$balnum,$islast);
 5074:         $$rowtotal += $rownum;
 5075:         $balnum ++;
 5076:     }
 5077:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5078:     return $datatable;
 5079: }
 5080: 
 5081: sub get_loadbalancers_config {
 5082:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 5083:     return unless ((ref($servers) eq 'HASH') &&
 5084:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5085:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 5086:                    (ref($currcookies) eq 'HASH'));
 5087:     if (keys(%{$existing}) > 0) {
 5088:         my $oldlonhost;
 5089:         foreach my $key (sort(keys(%{$existing}))) {
 5090:             if ($key eq 'lonhost') {
 5091:                 $oldlonhost = $existing->{'lonhost'};
 5092:                 $currbalancer->{$oldlonhost} = 1;
 5093:             } elsif ($key eq 'targets') {
 5094:                 if ($oldlonhost) {
 5095:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5096:                 }
 5097:             } elsif ($key eq 'rules') {
 5098:                 if ($oldlonhost) {
 5099:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5100:                 }
 5101:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5102:                 $currbalancer->{$key} = 1;
 5103:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5104:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5105:                 if ($existing->{$key}{'cookie'}) {
 5106:                     $currcookies->{$key} = 1;
 5107:                 }
 5108:             }
 5109:         }
 5110:     } else {
 5111:         my ($balancerref,$targetsref) =
 5112:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5113:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5114:             foreach my $server (sort(keys(%{$balancerref}))) {
 5115:                 $currbalancer->{$server} = 1;
 5116:                 $currtargets->{$server} = $targetsref->{$server};
 5117:             }
 5118:         }
 5119:     }
 5120:     return;
 5121: }
 5122: 
 5123: sub loadbalancing_rules {
 5124:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5125:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5126:         $css_class,$balnum,$islast) = @_;
 5127:     my $output;
 5128:     my $num = 0;
 5129:     my ($alltypes,$othertypes,$titles) =
 5130:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5131:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5132:         foreach my $type (@{$alltypes}) {
 5133:             $num ++;
 5134:             my $current;
 5135:             if (ref($currrules) eq 'HASH') {
 5136:                 $current = $currrules->{$type};
 5137:             }
 5138:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5139:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5140:                     $current = '';
 5141:                 }
 5142:             }
 5143:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5144:                                              $servers,$currbalancer,$lonhost,$dom,
 5145:                                              $targets_div_style,$homedom_div_style,
 5146:                                              $css_class,$balnum,$num,$islast);
 5147:         }
 5148:     }
 5149:     return $output;
 5150: }
 5151: 
 5152: sub loadbalancing_titles {
 5153:     my ($dom,$intdom,$usertypes,$types) = @_;
 5154:     my %othertypes = (
 5155:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5156:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5157:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5158:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5159:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5160:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5161:                      );
 5162:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5163:     my @available;
 5164:     if (ref($types) eq 'ARRAY') {
 5165:         @available = @{$types};
 5166:     }
 5167:     unless (grep(/^default$/,@available)) {
 5168:         push(@available,'default');
 5169:     }
 5170:     unshift(@alltypes,@available);
 5171:     my %titles;
 5172:     foreach my $type (@alltypes) {
 5173:         if ($type =~ /^_LC_/) {
 5174:             $titles{$type} = $othertypes{$type};
 5175:         } elsif ($type eq 'default') {
 5176:             $titles{$type} = &mt('All users from [_1]',$dom);
 5177:             if (ref($types) eq 'ARRAY') {
 5178:                 if (@{$types} > 0) {
 5179:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5180:                 }
 5181:             }
 5182:         } elsif (ref($usertypes) eq 'HASH') {
 5183:             $titles{$type} = $usertypes->{$type};
 5184:         }
 5185:     }
 5186:     return (\@alltypes,\%othertypes,\%titles);
 5187: }
 5188: 
 5189: sub loadbalance_rule_row {
 5190:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5191:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5192:     my @rulenames;
 5193:     my %ruletitles = &offloadtype_text();
 5194:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5195:         @rulenames = ('balancer','offloadedto','specific');
 5196:     } else {
 5197:         @rulenames = ('default','homeserver');
 5198:         if ($type eq '_LC_external') {
 5199:             push(@rulenames,'externalbalancer');
 5200:         } else {
 5201:             push(@rulenames,'specific');
 5202:         }
 5203:         push(@rulenames,'none');
 5204:     }
 5205:     my $style = $targets_div_style;
 5206:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5207:         $style = $homedom_div_style;
 5208:     }
 5209:     my $space;
 5210:     if ($islast && $num == 1) {
 5211:         $space = '<div display="inline-block">&nbsp;</div>';
 5212:     }
 5213:     my $output =
 5214:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5215:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5216:         '<td valaign="top">'.$space.
 5217:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5218:     for (my $i=0; $i<@rulenames; $i++) {
 5219:         my $rule = $rulenames[$i];
 5220:         my ($checked,$extra);
 5221:         if ($rulenames[$i] eq 'default') {
 5222:             $rule = '';
 5223:         }
 5224:         if ($rulenames[$i] eq 'specific') {
 5225:             if (ref($servers) eq 'HASH') {
 5226:                 my $default;
 5227:                 if (($current ne '') && (exists($servers->{$current}))) {
 5228:                     $checked = ' checked="checked"';
 5229:                 }
 5230:                 unless ($checked) {
 5231:                     $default = ' selected="selected"';
 5232:                 }
 5233:                 $extra =
 5234:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5235:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5236:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5237:                     '<option value=""'.$default.'></option>'."\n";
 5238:                 foreach my $server (sort(keys(%{$servers}))) {
 5239:                     if (ref($currbalancer) eq 'HASH') {
 5240:                         next if (exists($currbalancer->{$server}));
 5241:                     }
 5242:                     my $selected;
 5243:                     if ($server eq $current) {
 5244:                         $selected = ' selected="selected"';
 5245:                     }
 5246:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5247:                 }
 5248:                 $extra .= '</select>';
 5249:             }
 5250:         } elsif ($rule eq $current) {
 5251:             $checked = ' checked="checked"';
 5252:         }
 5253:         $output .= '<span class="LC_nobreak"><label>'.
 5254:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5255:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5256:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5257:                    ')"'.$checked.' />&nbsp;';
 5258:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5259:             $output .= $ruletitles{'particular'};
 5260:         } else {
 5261:             $output .= $ruletitles{$rulenames[$i]};
 5262:         }
 5263:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5264:     }
 5265:     $output .= '</div></td></tr>'."\n";
 5266:     return $output;
 5267: }
 5268: 
 5269: sub offloadtype_text {
 5270:     my %ruletitles = &Apache::lonlocal::texthash (
 5271:            'default'          => 'Offloads to default destinations',
 5272:            'homeserver'       => "Offloads to user's home server",
 5273:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5274:            'specific'         => 'Offloads to specific server',
 5275:            'none'             => 'No offload',
 5276:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5277:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5278:            'particular'       => 'Session hosted (after re-auth) on server:',
 5279:     );
 5280:     return %ruletitles;
 5281: }
 5282: 
 5283: sub sparestype_titles {
 5284:     my %typestitles = &Apache::lonlocal::texthash (
 5285:                           'primary' => 'primary',
 5286:                           'default' => 'default',
 5287:                       );
 5288:     return %typestitles;
 5289: }
 5290: 
 5291: sub contact_titles {
 5292:     my %titles = &Apache::lonlocal::texthash (
 5293:                    'supportemail'    => 'Support E-mail address',
 5294:                    'adminemail'      => 'Default Server Admin E-mail address',
 5295:                    'errormail'       => 'Error reports to be e-mailed to',
 5296:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5297:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 5298:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 5299:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5300:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5301:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5302:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5303:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 5304:                  );
 5305:     my %short_titles = &Apache::lonlocal::texthash (
 5306:                            adminemail   => 'Admin E-mail address',
 5307:                            supportemail => 'Support E-mail',
 5308:                        );   
 5309:     return (\%titles,\%short_titles);
 5310: }
 5311: 
 5312: sub helpform_fields {
 5313:     my %titles =  &Apache::lonlocal::texthash (
 5314:                        'username'   => 'Name',
 5315:                        'user'       => 'Username/domain',
 5316:                        'phone'      => 'Phone',
 5317:                        'cc'         => 'Cc e-mail',
 5318:                        'course'     => 'Course Details',
 5319:                        'section'    => 'Sections',
 5320:                        'screenshot' => 'File upload',
 5321:     );
 5322:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5323:     my %possoptions = (
 5324:                         username     => ['yes','no','req'],
 5325:                         phone        => ['yes','no','req'],
 5326:                         user         => ['yes','no'],
 5327:                         cc           => ['yes','no'],
 5328:                         course       => ['yes','no'],
 5329:                         section      => ['yes','no'],
 5330:                         screenshot   => ['yes','no'],
 5331:                       );
 5332:     my %fieldoptions = &Apache::lonlocal::texthash (
 5333:                          'yes'  => 'Optional',
 5334:                          'req'  => 'Required',
 5335:                          'no'   => "Not shown",
 5336:     );
 5337:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5338: }
 5339: 
 5340: sub tool_titles {
 5341:     my %titles = &Apache::lonlocal::texthash (
 5342:                      aboutme    => 'Personal web page',
 5343:                      blog       => 'Blog',
 5344:                      webdav     => 'WebDAV',
 5345:                      portfolio  => 'Portfolio',
 5346:                      official   => 'Official courses (with institutional codes)',
 5347:                      unofficial => 'Unofficial courses',
 5348:                      community  => 'Communities',
 5349:                      textbook   => 'Textbook courses',
 5350:                  );
 5351:     return %titles;
 5352: }
 5353: 
 5354: sub courserequest_titles {
 5355:     my %titles = &Apache::lonlocal::texthash (
 5356:                                    official   => 'Official',
 5357:                                    unofficial => 'Unofficial',
 5358:                                    community  => 'Communities',
 5359:                                    textbook   => 'Textbook',
 5360:                                    norequest  => 'Not allowed',
 5361:                                    approval   => 'Approval by Dom. Coord.',
 5362:                                    validate   => 'With validation',
 5363:                                    autolimit  => 'Numerical limit',
 5364:                                    unlimited  => '(blank for unlimited)',
 5365:                  );
 5366:     return %titles;
 5367: }
 5368: 
 5369: sub authorrequest_titles {
 5370:     my %titles = &Apache::lonlocal::texthash (
 5371:                                    norequest  => 'Not allowed',
 5372:                                    approval   => 'Approval by Dom. Coord.',
 5373:                                    automatic  => 'Automatic approval',
 5374:                  );
 5375:     return %titles;
 5376: }
 5377: 
 5378: sub courserequest_conditions {
 5379:     my %conditions = &Apache::lonlocal::texthash (
 5380:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5381:        validate   => '(Processing of request subject to institutional validation).',
 5382:                  );
 5383:     return %conditions;
 5384: }
 5385: 
 5386: 
 5387: sub print_usercreation {
 5388:     my ($position,$dom,$settings,$rowtotal) = @_;
 5389:     my $numinrow = 4;
 5390:     my $datatable;
 5391:     if ($position eq 'top') {
 5392:         $$rowtotal ++;
 5393:         my $rowcount = 0;
 5394:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5395:         if (ref($rules) eq 'HASH') {
 5396:             if (keys(%{$rules}) > 0) {
 5397:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5398:                                                 $ruleorder,$numinrow,$rowcount);
 5399:                 $$rowtotal ++;
 5400:                 $rowcount ++;
 5401:             }
 5402:         }
 5403:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5404:         if (ref($idrules) eq 'HASH') {
 5405:             if (keys(%{$idrules}) > 0) {
 5406:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5407:                                                 $idruleorder,$numinrow,$rowcount);
 5408:                 $$rowtotal ++;
 5409:                 $rowcount ++;
 5410:             }
 5411:         }
 5412:         if ($rowcount == 0) {
 5413:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5414:             $$rowtotal ++;
 5415:             $rowcount ++;
 5416:         }
 5417:     } elsif ($position eq 'middle') {
 5418:         my @creators = ('author','course','requestcrs');
 5419:         my ($rules,$ruleorder) =
 5420:             &Apache::lonnet::inst_userrules($dom,'username');
 5421:         my %lt = &usercreation_types();
 5422:         my %checked;
 5423:         if (ref($settings) eq 'HASH') {
 5424:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5425:                 foreach my $item (@creators) {
 5426:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5427:                 }
 5428:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5429:                 foreach my $item (@creators) {
 5430:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5431:                         $checked{$item} = 'none';
 5432:                     }
 5433:                 }
 5434:             }
 5435:         }
 5436:         my $rownum = 0;
 5437:         foreach my $item (@creators) {
 5438:             $rownum ++;
 5439:             if ($checked{$item} eq '') {
 5440:                 $checked{$item} = 'any';
 5441:             }
 5442:             my $css_class;
 5443:             if ($rownum%2) {
 5444:                 $css_class = '';
 5445:             } else {
 5446:                 $css_class = ' class="LC_odd_row" ';
 5447:             }
 5448:             $datatable .= '<tr'.$css_class.'>'.
 5449:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5450:                          '</span></td><td align="right">';
 5451:             my @options = ('any');
 5452:             if (ref($rules) eq 'HASH') {
 5453:                 if (keys(%{$rules}) > 0) {
 5454:                     push(@options,('official','unofficial'));
 5455:                 }
 5456:             }
 5457:             push(@options,'none');
 5458:             foreach my $option (@options) {
 5459:                 my $type = 'radio';
 5460:                 my $check = ' ';
 5461:                 if ($checked{$item} eq $option) {
 5462:                     $check = ' checked="checked" ';
 5463:                 } 
 5464:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5465:                               '<input type="'.$type.'" name="can_createuser_'.
 5466:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5467:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5468:             }
 5469:             $datatable .= '</td></tr>';
 5470:         }
 5471:     } else {
 5472:         my @contexts = ('author','course','domain');
 5473:         my @authtypes = ('int','krb4','krb5','loc');
 5474:         my %checked;
 5475:         if (ref($settings) eq 'HASH') {
 5476:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5477:                 foreach my $item (@contexts) {
 5478:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5479:                         foreach my $auth (@authtypes) {
 5480:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5481:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5482:                             }
 5483:                         }
 5484:                     }
 5485:                 }
 5486:             }
 5487:         } else {
 5488:             foreach my $item (@contexts) {
 5489:                 foreach my $auth (@authtypes) {
 5490:                     $checked{$item}{$auth} = ' checked="checked" ';
 5491:                 }
 5492:             }
 5493:         }
 5494:         my %title = &context_names();
 5495:         my %authname = &authtype_names();
 5496:         my $rownum = 0;
 5497:         my $css_class; 
 5498:         foreach my $item (@contexts) {
 5499:             if ($rownum%2) {
 5500:                 $css_class = '';
 5501:             } else {
 5502:                 $css_class = ' class="LC_odd_row" ';
 5503:             }
 5504:             $datatable .=   '<tr'.$css_class.'>'.
 5505:                             '<td>'.$title{$item}.
 5506:                             '</td><td class="LC_left_item">'.
 5507:                             '<span class="LC_nobreak">';
 5508:             foreach my $auth (@authtypes) {
 5509:                 $datatable .= '<label>'. 
 5510:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5511:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5512:                               $authname{$auth}.'</label>&nbsp;';
 5513:             }
 5514:             $datatable .= '</span></td></tr>';
 5515:             $rownum ++;
 5516:         }
 5517:         $$rowtotal += $rownum;
 5518:     }
 5519:     return $datatable;
 5520: }
 5521: 
 5522: sub print_selfcreation {
 5523:     my ($position,$dom,$settings,$rowtotal) = @_;
 5524:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 5525:         $emaildomain,$datatable);
 5526:     if (ref($settings) eq 'HASH') {
 5527:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5528:             $createsettings = $settings->{'cancreate'};
 5529:             if (ref($createsettings) eq 'HASH') {
 5530:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5531:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5532:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5533:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5534:                         @selfcreate = ('email','login','sso');
 5535:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5536:                         @selfcreate = ($createsettings->{'selfcreate'});
 5537:                     }
 5538:                 }
 5539:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5540:                     $processing = $createsettings->{'selfcreateprocessing'};
 5541:                 }
 5542:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 5543:                     $emailoptions = $createsettings->{'emailoptions'};
 5544:                 }
 5545:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 5546:                     $emailverified = $createsettings->{'emailverified'};
 5547:                 }
 5548:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 5549:                     $emaildomain = $createsettings->{'emaildomain'};
 5550:                 }
 5551:             }
 5552:         }
 5553:     }
 5554:     my %radiohash;
 5555:     my $numinrow = 4;
 5556:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5557:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5558:     if ($position eq 'top') {
 5559:         my %choices = &Apache::lonlocal::texthash (
 5560:                                                       cancreate_login      => 'Institutional Login',
 5561:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5562:                                                   );
 5563:         my @toggles = sort(keys(%choices));
 5564:         my %defaultchecked = (
 5565:                                'cancreate_login' => 'off',
 5566:                                'cancreate_sso'   => 'off',
 5567:                              );
 5568:         my ($onclick,$itemcount);
 5569:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5570:                                                      \%choices,$itemcount,$onclick);
 5571:         $$rowtotal += $itemcount;
 5572: 
 5573:         if (ref($usertypes) eq 'HASH') {
 5574:             if (keys(%{$usertypes}) > 0) {
 5575:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5576:                                              $dom,$numinrow,$othertitle,
 5577:                                              'statustocreate',$rowtotal);
 5578:                 $$rowtotal ++;
 5579:             }
 5580:         }
 5581:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5582:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5583:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5584:         my $rem;
 5585:         my $numperrow = 2;
 5586:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5587:         $datatable .= '<tr'.$css_class.'>'.
 5588:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5589:                      '<td class="LC_left_item">'."\n".
 5590:                      '<table>'."\n";
 5591:         for (my $i=0; $i<@fields; $i++) {
 5592:             $rem = $i%($numperrow);
 5593:             if ($rem == 0) {
 5594:                 if ($i > 0) {
 5595:                     $datatable .= '</tr>';
 5596:                 }
 5597:                 $datatable .= '<tr>';
 5598:             }
 5599:             my $currval;
 5600:             if (ref($createsettings) eq 'HASH') {
 5601:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5602:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5603:                 }
 5604:             }
 5605:             $datatable .= '<td class="LC_left_item">'.
 5606:                           '<span class="LC_nobreak">'.
 5607:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5608:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5609:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5610:         }
 5611:         my $colsleft = $numperrow - $rem;
 5612:         if ($colsleft > 1 ) {
 5613:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5614:                          '&nbsp;</td>';
 5615:         } elsif ($colsleft == 1) {
 5616:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5617:         }
 5618:         $datatable .= '</tr></table></td></tr>';
 5619:         $$rowtotal ++;
 5620:     } elsif ($position eq 'middle') {
 5621:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5622:         my @posstypes;
 5623:         if (ref($types) eq 'ARRAY') {
 5624:             @posstypes = @{$types};
 5625:         }
 5626:         unless (grep(/^default$/,@posstypes)) {
 5627:             push(@posstypes,'default');
 5628:         }
 5629:         my %usertypeshash;
 5630:         if (ref($usertypes) eq 'HASH') {
 5631:             %usertypeshash = %{$usertypes};
 5632:         }
 5633:         $usertypeshash{'default'} = $othertitle;
 5634:         foreach my $status (@posstypes) {
 5635:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5636:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 5637:             $$rowtotal ++;
 5638:         }
 5639:     } else {
 5640:         my %choices = &Apache::lonlocal::texthash (
 5641:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 5642:                                                   );
 5643:         my @toggles = sort(keys(%choices));
 5644:         my %defaultchecked = (
 5645:                                'cancreate_email' => 'off',
 5646:                              );
 5647:         my $customclass = 'LC_selfcreate_email';
 5648:         my $classprefix = 'LC_canmodify_emailusername_';
 5649:         my $optionsprefix = 'LC_options_emailusername_';
 5650:         my $display = 'none';
 5651:         my $rowstyle = 'display:none';
 5652:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5653:             $display = 'block';
 5654:             $rowstyle = 'display:table-row';
 5655:         }
 5656:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 5657:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5658:                                                      \%choices,$$rowtotal,$onclick);
 5659:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 5660:                                          $rowstyle);
 5661:         $$rowtotal ++;
 5662:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 5663:                                       $rowstyle);
 5664:         $$rowtotal ++;
 5665:         my (@ordered,@posstypes,%usertypeshash);
 5666:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5667:         my ($emailrules,$emailruleorder) =
 5668:             &Apache::lonnet::inst_userrules($dom,'email');
 5669:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5670:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5671:         if (ref($types) eq 'ARRAY') {
 5672:             @posstypes = @{$types};
 5673:         }
 5674:         if (@posstypes) {
 5675:             unless (grep(/^default$/,@posstypes)) {
 5676:                 push(@posstypes,'default');
 5677:             }
 5678:             if (ref($usertypes) eq 'HASH') {
 5679:                 %usertypeshash = %{$usertypes};
 5680:             }
 5681:             my $currassign;
 5682:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 5683:                 $currassign = {
 5684:                                   selfassign => $domdefaults{'inststatusguest'},
 5685:                               };
 5686:                 @ordered = @{$domdefaults{'inststatusguest'}};
 5687:             } else {
 5688:                 $currassign = { selfassign => [] };
 5689:             }
 5690:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 5691:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 5692:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 5693:                                          $numinrow,$othertitle,'selfassign',
 5694:                                          $rowtotal,$onclicktypes,$customclass,
 5695:                                          $rowstyle);
 5696:             $$rowtotal ++;
 5697:             $usertypeshash{'default'} = $othertitle;
 5698:             foreach my $status (@posstypes) {
 5699:                 my $css_class;
 5700:                 if ($$rowtotal%2) {
 5701:                     $css_class = 'LC_odd_row ';
 5702:                 }
 5703:                 $css_class .= $customclass;
 5704:                 my $rowid = $optionsprefix.$status;
 5705:                 my $hidden = 1;
 5706:                 my $currstyle = 'display:none';
 5707:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5708:                     $currstyle = $rowstyle;
 5709:                     $hidden = 0;
 5710:                 }
 5711:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5712:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 5713:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 5714:                 unless ($hidden) {
 5715:                     $$rowtotal ++;
 5716:                 }
 5717:             }
 5718:         } else {
 5719:             my $css_class;
 5720:             if ($$rowtotal%2) {
 5721:                 $css_class = 'LC_odd_row ';
 5722:             }
 5723:             $css_class .= $customclass;
 5724:             $usertypeshash{'default'} = $othertitle;
 5725:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5726:                                          $emailrules,$emailruleorder,$settings,'default','',
 5727:                                          $othertitle,$css_class,$rowstyle,$intdom);
 5728:             $$rowtotal ++;
 5729:         }
 5730:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5731:         $numinrow = 1;
 5732:         if (@posstypes) {
 5733:             foreach my $status (@posstypes) {
 5734:                 my $rowid = $classprefix.$status;
 5735:                 my $datarowstyle = 'display:none';
 5736:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5737:                     $datarowstyle = $rowstyle;
 5738:                 }
 5739:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5740:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5741:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 5742:                 unless ($datarowstyle eq 'display:none') {
 5743:                     $$rowtotal ++;
 5744:                 }
 5745:             }
 5746:         } else {
 5747:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 5748:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5749:                                                    $infotitles,'',$customclass,$rowstyle);
 5750:         }
 5751:     }
 5752:     return $datatable;
 5753: }
 5754: 
 5755: sub selfcreate_javascript {
 5756:     return <<"ENDSCRIPT";
 5757: 
 5758: <script type="text/javascript">
 5759: // <![CDATA[
 5760: 
 5761: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 5762:     var x = document.getElementsByClassName(target);
 5763:     var insttypes = 0;
 5764:     var insttypeRegExp = new RegExp(prefix);
 5765:     if ((x.length != undefined) && (x.length > 0)) {
 5766:         if (form.elements[radio].length != undefined) {
 5767:             for (var i=0; i<form.elements[radio].length; i++) {
 5768:                 if (form.elements[radio][i].checked) {
 5769:                     if (form.elements[radio][i].value == 1) {
 5770:                         for (var j=0; j<x.length; j++) {
 5771:                             if (x[j].id == 'undefined') {
 5772:                                 x[j].style.display = 'table-row';
 5773:                             } else if (insttypeRegExp.test(x[j].id)) {
 5774:                                 insttypes ++;
 5775:                             } else {
 5776:                                 x[j].style.display = 'table-row';
 5777:                             }
 5778:                         }
 5779:                     } else {
 5780:                         for (var j=0; j<x.length; j++) {
 5781:                             x[j].style.display = 'none';
 5782:                         }
 5783:                     }
 5784:                     break;
 5785:                 }
 5786:             }
 5787:             if (insttypes > 0) {
 5788:                 toggleDataRow(form,checkbox,target,altprefix);
 5789:                 toggleDataRow(form,checkbox,target,prefix,1);
 5790:             }
 5791:         }
 5792:     }
 5793:     return;
 5794: }
 5795: 
 5796: function toggleDataRow(form,checkbox,target,prefix,docount) {
 5797:     if (form.elements[checkbox].length != undefined) {
 5798:         var count = 0;
 5799:         if (docount) {
 5800:             for (var i=0; i<form.elements[checkbox].length; i++) {
 5801:                 if (form.elements[checkbox][i].checked) {
 5802:                     count ++;
 5803:                 }
 5804:             }
 5805:         }
 5806:         for (var i=0; i<form.elements[checkbox].length; i++) {
 5807:             var type = form.elements[checkbox][i].value;
 5808:             if (document.getElementById(prefix+type)) {
 5809:                 if (form.elements[checkbox][i].checked) {
 5810:                     document.getElementById(prefix+type).style.display = 'table-row';
 5811:                     if (count % 2 == 1) {
 5812:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 5813:                     } else {
 5814:                         document.getElementById(prefix+type).className = target;
 5815:                     }
 5816:                     count ++;
 5817:                 } else {
 5818:                     document.getElementById(prefix+type).style.display = 'none';
 5819:                 }
 5820:             }
 5821:         }
 5822:     }
 5823:     return;
 5824: }
 5825: 
 5826: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 5827:     var caller = radio+'_'+status;
 5828:     if (form.elements[caller].length != undefined) {
 5829:         for (var i=0; i<form.elements[caller].length; i++) {
 5830:             if (form.elements[caller][i].checked) {
 5831:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 5832:                     var curr = form.elements[caller][i].value;
 5833:                     if (prefix) {
 5834:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 5835:                     }
 5836:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 5837:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 5838:                     if (curr == 'custom') {
 5839:                         if (prefix) {
 5840:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 5841:                         }
 5842:                     } else if (curr == 'inst') {
 5843:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 5844:                     } else if (curr == 'noninst') {
 5845:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 5846:                     }
 5847:                     break;
 5848:                 }
 5849:             }
 5850:         }
 5851:     }
 5852: }
 5853: 
 5854: // ]]>
 5855: </script>
 5856: 
 5857: ENDSCRIPT
 5858: }
 5859: 
 5860: sub noninst_users {
 5861:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 5862:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 5863:     my $class = 'LC_left_item';
 5864:     if ($css_class) {
 5865:         $css_class = ' class="'.$css_class.'"';
 5866:     }
 5867:     if ($rowid) {
 5868:         $rowid = ' id="'.$rowid.'"';
 5869:     }
 5870:     if ($rowstyle) {
 5871:         $rowstyle = ' style="'.$rowstyle.'"';
 5872:     }
 5873:     my ($output,$description);
 5874:     if ($type eq 'default') {
 5875:         $description = &mt('Requests for: [_1]',$typetitle);
 5876:     } else {
 5877:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 5878:     }
 5879:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 5880:               "<td>$description</td>\n".
 5881:               '<td class="'.$class.'" colspan="2">'.
 5882:               '<table><tr>';
 5883:     my %headers = &Apache::lonlocal::texthash(
 5884:               approve  => 'Processing',
 5885:               email    => 'E-mail',
 5886:               username => 'Username',
 5887:     );
 5888:     foreach my $item ('approve','email','username') {
 5889:         $output .= '<th>'.$headers{$item}.'</th>';
 5890:     }
 5891:     $output .= '</tr><tr>';
 5892:     foreach my $item ('approve','email','username') {
 5893:         $output .= '<td valign="top">';
 5894:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 5895:         if ($item eq 'approve') {
 5896:             %choices = &Apache::lonlocal::texthash (
 5897:                                                      automatic => 'Automatically approved',
 5898:                                                      approval  => 'Queued for approval',
 5899:                                                    );
 5900:             @options = ('automatic','approval');
 5901:             $hashref = $processing;
 5902:             $defoption = 'automatic';
 5903:             $name = 'cancreate_emailprocess_'.$type;
 5904:         } elsif ($item eq 'email') {
 5905:             %choices = &Apache::lonlocal::texthash (
 5906:                                                      any     => 'Any e-mail',
 5907:                                                      inst    => 'Institutional only',
 5908:                                                      noninst => 'Non-institutional only',
 5909:                                                      custom  => 'Custom restrictions',
 5910:                                                    );
 5911:             @options = ('any','inst','noninst');
 5912:             my $showcustom;
 5913:             if (ref($emailrules) eq 'HASH') {
 5914:                 if (keys(%{$emailrules}) > 0) {
 5915:                     push(@options,'custom');
 5916:                     $showcustom = 'cancreate_emailrule';
 5917:                     if (ref($settings) eq 'HASH') {
 5918:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 5919:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 5920:                                 if (exists($emailrules->{$rule})) {
 5921:                                     $hascustom ++;
 5922:                                 }
 5923:                             }
 5924:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 5925:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 5926:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 5927:                                     if (exists($emailrules->{$rule})) {
 5928:                                         $hascustom ++;
 5929:                                     }
 5930:                                 }
 5931:                             }
 5932:                         }
 5933:                     }
 5934:                 }
 5935:             }
 5936:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 5937:                                                      "'cancreate_emaildomain','$type'".');"';
 5938:             $hashref = $emailoptions;
 5939:             $defoption = 'any';
 5940:             $name = 'cancreate_emailoptions_'.$type;
 5941:         } elsif ($item eq 'username') {
 5942:             %choices = &Apache::lonlocal::texthash (
 5943:                                                      all    => 'Same as e-mail',
 5944:                                                      first  => 'Omit @domain',
 5945:                                                      free   => 'Free to choose',
 5946:                                                    );
 5947:             @options = ('all','first','free');
 5948:             $hashref = $emailverified;
 5949:             $defoption = 'all';
 5950:             $name = 'cancreate_usernameoptions_'.$type;
 5951:         }
 5952:         foreach my $option (@options) {
 5953:             my $checked;
 5954:             if (ref($hashref) eq 'HASH') {
 5955:                 if ($type eq '') {
 5956:                     if (!exists($hashref->{'default'})) {
 5957:                         if ($option eq $defoption) {
 5958:                             $checked = ' checked="checked"';
 5959:                         }
 5960:                     } else {
 5961:                         if ($hashref->{'default'} eq $option) {
 5962:                             $checked = ' checked="checked"';
 5963:                         }
 5964:                     }
 5965:                 } else {
 5966:                     if (!exists($hashref->{$type})) {
 5967:                         if ($option eq $defoption) {
 5968:                             $checked = ' checked="checked"';
 5969:                         }
 5970:                     } else {
 5971:                         if ($hashref->{$type} eq $option) {
 5972:                             $checked = ' checked="checked"';
 5973:                         }
 5974:                     }
 5975:                 }
 5976:             } elsif (($item eq 'email') && ($hascustom)) {
 5977:                 if ($option eq 'custom') {
 5978:                     $checked = ' checked="checked"';
 5979:                 }
 5980:             } elsif ($option eq $defoption) {
 5981:                 $checked = ' checked="checked"';
 5982:             }
 5983:             $output .= '<span class="LC_nobreak"><label>'.
 5984:                        '<input type="radio" name="'.$name.'"'.
 5985:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 5986:                        $choices{$option}.'</label></span><br />';
 5987:             if ($item eq 'email') {
 5988:                 if ($option eq 'custom') {
 5989:                     my $id = 'cancreate_emailrule_'.$type;
 5990:                     my $display = 'none';
 5991:                     if ($checked) {
 5992:                         $display = 'inline';
 5993:                     }
 5994:                     my $numinrow = 2;
 5995:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 5996:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 5997:                                &user_formats_row('email',$settings,$emailrules,
 5998:                                                  $emailruleorder,$numinrow,'',$type);
 5999:                               '</table></fieldset>';
 6000:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 6001:                     my %text = &Apache::lonlocal::texthash (
 6002:                                                              inst    => 'must end:',
 6003:                                                              noninst => 'cannot end:',
 6004:                                                            );
 6005:                     my $value;
 6006:                     if (ref($emaildomain) eq 'HASH') {
 6007:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 6008:                             $value = $emaildomain->{$type}->{$option};
 6009:                         }
 6010:                     }
 6011:                     if ($value eq '') {
 6012:                         $value = '@'.$intdom;
 6013:                     }
 6014:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 6015:                     my $display = 'none';
 6016:                     if ($checked) {
 6017:                         $display = 'inline';
 6018:                     }
 6019:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 6020:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 6021:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 6022:                                '</div>';
 6023:                 }
 6024:             }
 6025:         }
 6026:         $output .= '</td>'."\n";
 6027:     }
 6028:     $output .= "</tr></table></td></tr>\n";
 6029:     return $output;
 6030: }
 6031: 
 6032: sub captcha_choice {
 6033:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 6034:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 6035:         $vertext,$currver); 
 6036:     my %lt = &captcha_phrases();
 6037:     $keyentry = 'hidden';
 6038:     my $colspan=2;
 6039:     if ($context eq 'cancreate') {
 6040:         $rowname = &mt('CAPTCHA validation');
 6041:     } elsif ($context eq 'login') {
 6042:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 6043:     } elsif ($context eq 'passwords') {
 6044:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 6045:         $colspan=1;
 6046:     }
 6047:     if (ref($settings) eq 'HASH') {
 6048:         if ($settings->{'captcha'}) {
 6049:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6050:         } else {
 6051:             $checked{'original'} = ' checked="checked"';
 6052:         }
 6053:         if ($settings->{'captcha'} eq 'recaptcha') {
 6054:             $pubtext = $lt{'pub'};
 6055:             $privtext = $lt{'priv'};
 6056:             $keyentry = 'text';
 6057:             $vertext = $lt{'ver'};
 6058:             $currver = $settings->{'recaptchaversion'};
 6059:             if ($currver ne '2') {
 6060:                 $currver = 1;
 6061:             }
 6062:         }
 6063:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6064:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6065:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6066:         }
 6067:     } else {
 6068:         $checked{'original'} = ' checked="checked"';
 6069:     }
 6070:     my $css_class;
 6071:     if ($itemcount%2) {
 6072:         $css_class = 'LC_odd_row';
 6073:     }
 6074:     if ($customcss) {
 6075:         $css_class .= " $customcss";
 6076:     }
 6077:     $css_class =~ s/^\s+//;
 6078:     if ($css_class) {
 6079:         $css_class = ' class="'.$css_class.'"';
 6080:     }
 6081:     if ($rowstyle) {
 6082:         $css_class .= ' style="'.$rowstyle.'"';
 6083:     }
 6084:     my $output = '<tr'.$css_class.'>'.
 6085:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 6086:                  '<table><tr><td>'."\n";
 6087:     foreach my $option ('original','recaptcha','notused') {
 6088:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6089:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6090:                    $lt{$option}.'</label></span>';
 6091:         unless ($option eq 'notused') {
 6092:             $output .= ('&nbsp;'x2)."\n";
 6093:         }
 6094:     }
 6095: #
 6096: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6097: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6098: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6099: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6100: #
 6101:     $output .= '</td></tr>'."\n".
 6102:                '<tr><td class="LC_zero_height">'."\n".
 6103:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6104:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6105:                $currpub.'" size="40" /></span><br />'."\n".
 6106:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6107:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6108:                $currpriv.'" size="40" /></span><br />'.
 6109:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6110:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6111:                $currver.'" size="3" /></span><br />'.
 6112:                '</td></tr></table>'."\n".
 6113:                '</td></tr>';
 6114:     return $output;
 6115: }
 6116: 
 6117: sub user_formats_row {
 6118:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 6119:     my $output;
 6120:     my %text = (
 6121:                    'username' => 'new usernames',
 6122:                    'id'       => 'IDs',
 6123:                );
 6124:     unless ($type eq 'email') {
 6125:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6126:         $output = '<tr '.$css_class.'>'.
 6127:                   '<td><span class="LC_nobreak">'.
 6128:                   &mt("Format rules to check for $text{$type}: ").
 6129:                   '</td><td class="LC_left_item" colspan="2"><table>';
 6130:     }
 6131:     my $rem;
 6132:     if (ref($ruleorder) eq 'ARRAY') {
 6133:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6134:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6135:                 my $rem = $i%($numinrow);
 6136:                 if ($rem == 0) {
 6137:                     if ($i > 0) {
 6138:                         $output .= '</tr>';
 6139:                     }
 6140:                     $output .= '<tr>';
 6141:                 }
 6142:                 my $check = ' ';
 6143:                 if (ref($settings) eq 'HASH') {
 6144:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6145:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6146:                             $check = ' checked="checked" ';
 6147:                         }
 6148:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 6149:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 6150:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 6151:                                 $check = ' checked="checked" ';
 6152:                             }
 6153:                         }
 6154:                     }
 6155:                 }
 6156:                 my $name = $type.'_rule';
 6157:                 if ($type eq 'email') {
 6158:                     $name .= '_'.$status;
 6159:                 }
 6160:                 $output .= '<td class="LC_left_item">'.
 6161:                            '<span class="LC_nobreak"><label>'.
 6162:                            '<input type="checkbox" name="'.$name.'" '.
 6163:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6164:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6165:             }
 6166:         }
 6167:         $rem = @{$ruleorder}%($numinrow);
 6168:     }
 6169:     my $colsleft;
 6170:     if ($rem) {
 6171:         $colsleft = $numinrow - $rem;
 6172:     }
 6173:     if ($colsleft > 1 ) {
 6174:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6175:                    '&nbsp;</td>';
 6176:     } elsif ($colsleft == 1) {
 6177:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6178:     }
 6179:     $output .= '</tr></table>';
 6180:     unless ($type eq 'email') {
 6181:         $output .= '</td></tr>';
 6182:     }
 6183:     return $output;
 6184: }
 6185: 
 6186: sub usercreation_types {
 6187:     my %lt = &Apache::lonlocal::texthash (
 6188:                     author     => 'When adding a co-author',
 6189:                     course     => 'When adding a user to a course',
 6190:                     requestcrs => 'When requesting a course',
 6191:                     any        => 'Any',
 6192:                     official   => 'Institutional only ',
 6193:                     unofficial => 'Non-institutional only',
 6194:                     none       => 'None',
 6195:     );
 6196:     return %lt;
 6197: }
 6198: 
 6199: sub selfcreation_types {
 6200:     my %lt = &Apache::lonlocal::texthash (
 6201:                     selfcreate => 'User creates own account',
 6202:                     any        => 'Any',
 6203:                     official   => 'Institutional only ',
 6204:                     unofficial => 'Non-institutional only',
 6205:                     email      => 'E-mail address',
 6206:                     login      => 'Institutional Login',
 6207:                     sso        => 'SSO',
 6208:              );
 6209: }
 6210: 
 6211: sub authtype_names {
 6212:     my %lt = &Apache::lonlocal::texthash(
 6213:                       int    => 'Internal',
 6214:                       krb4   => 'Kerberos 4',
 6215:                       krb5   => 'Kerberos 5',
 6216:                       loc    => 'Local',
 6217:                   );
 6218:     return %lt;
 6219: }
 6220: 
 6221: sub context_names {
 6222:     my %context_title = &Apache::lonlocal::texthash(
 6223:        author => 'Creating users when an Author',
 6224:        course => 'Creating users when in a course',
 6225:        domain => 'Creating users when a Domain Coordinator',
 6226:     );
 6227:     return %context_title;
 6228: }
 6229: 
 6230: sub print_usermodification {
 6231:     my ($position,$dom,$settings,$rowtotal) = @_;
 6232:     my $numinrow = 4;
 6233:     my ($context,$datatable,$rowcount);
 6234:     if ($position eq 'top') {
 6235:         $rowcount = 0;
 6236:         $context = 'author'; 
 6237:         foreach my $role ('ca','aa') {
 6238:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6239:                                                    $numinrow,$rowcount);
 6240:             $$rowtotal ++;
 6241:             $rowcount ++;
 6242:         }
 6243:     } elsif ($position eq 'bottom') {
 6244:         $context = 'course';
 6245:         $rowcount = 0;
 6246:         foreach my $role ('st','ep','ta','in','cr') {
 6247:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6248:                                                    $numinrow,$rowcount);
 6249:             $$rowtotal ++;
 6250:             $rowcount ++;
 6251:         }
 6252:     }
 6253:     return $datatable;
 6254: }
 6255: 
 6256: sub print_defaults {
 6257:     my ($position,$dom,$settings,$rowtotal) = @_;
 6258:     my $rownum = 0;
 6259:     my ($datatable,$css_class,$titles);
 6260:     unless ($position eq 'bottom') {
 6261:         $titles = &defaults_titles($dom);
 6262:     }
 6263:     if ($position eq 'top') {
 6264:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6265:                      'datelocale_def','portal_def');
 6266:         my %defaults;
 6267:         if (ref($settings) eq 'HASH') {
 6268:             %defaults = %{$settings};
 6269:         } else {
 6270:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6271:             foreach my $item (@items) {
 6272:                 $defaults{$item} = $domdefaults{$item};
 6273:             }
 6274:         }
 6275:         foreach my $item (@items) {
 6276:             if ($rownum%2) {
 6277:                 $css_class = '';
 6278:             } else {
 6279:                 $css_class = ' class="LC_odd_row" ';
 6280:             }
 6281:             $datatable .= '<tr'.$css_class.'>'.
 6282:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6283:                           '</span></td><td class="LC_right_item" colspan="3">';
 6284:             if ($item eq 'auth_def') {
 6285:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6286:                 my %shortauth = (
 6287:                                  internal => 'int',
 6288:                                  krb4 => 'krb4',
 6289:                                  krb5 => 'krb5',
 6290:                                  localauth  => 'loc'
 6291:                                 );
 6292:                 my %authnames = &authtype_names();
 6293:                 foreach my $auth (@authtypes) {
 6294:                     my $checked = ' ';
 6295:                     if ($defaults{$item} eq $auth) {
 6296:                         $checked = ' checked="checked" ';
 6297:                     }
 6298:                     $datatable .= '<label><input type="radio" name="'.$item.
 6299:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6300:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6301:                 }
 6302:             } elsif ($item eq 'timezone_def') {
 6303:                 my $includeempty = 1;
 6304:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6305:             } elsif ($item eq 'datelocale_def') {
 6306:                 my $includeempty = 1;
 6307:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6308:             } elsif ($item eq 'lang_def') {
 6309:                 my $includeempty = 1;
 6310:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6311:             } else {
 6312:                 my $size;
 6313:                 if ($item eq 'portal_def') {
 6314:                     $size = ' size="25"';
 6315:                 }
 6316:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6317:                               $defaults{$item}.'"'.$size.' />';
 6318:             }
 6319:             $datatable .= '</td></tr>';
 6320:             $rownum ++;
 6321:         }
 6322:     } else {
 6323:         my %defaults;
 6324:         if (ref($settings) eq 'HASH') {
 6325:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6326:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6327:                 for (my $i=0; $i<$maxnum; $i++) {
 6328:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6329:                     my $item = $settings->{'inststatusorder'}->[$i];
 6330:                     my $title = $settings->{'inststatustypes'}->{$item};
 6331:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6332:                     $datatable .= '<tr'.$css_class.'>'.
 6333:                                   '<td><span class="LC_nobreak">'.
 6334:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6335:                     for (my $k=0; $k<=$maxnum; $k++) {
 6336:                         my $vpos = $k+1;
 6337:                         my $selstr;
 6338:                         if ($k == $i) {
 6339:                             $selstr = ' selected="selected" ';
 6340:                         }
 6341:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6342:                     }
 6343:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6344:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6345:                                   &mt('delete').'</span></td>'.
 6346:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6347:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6348:                                   '</span></td></tr>';
 6349:                 }
 6350:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6351:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6352:                 $datatable .= '<tr '.$css_class.'>'.
 6353:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6354:                 for (my $k=0; $k<=$maxnum; $k++) {
 6355:                     my $vpos = $k+1;
 6356:                     my $selstr;
 6357:                     if ($k == $maxnum) {
 6358:                         $selstr = ' selected="selected" ';
 6359:                     }
 6360:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6361:                 }
 6362:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6363:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6364:                               '&nbsp;'.&mt('(new)').
 6365:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6366:                               &mt('Name displayed:').
 6367:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6368:                               '</tr>'."\n";
 6369:                 $rownum ++;
 6370:             }
 6371:         }
 6372:     }
 6373:     $$rowtotal += $rownum;
 6374:     return $datatable;
 6375: }
 6376: 
 6377: sub get_languages_hash {
 6378:     my %langchoices;
 6379:     foreach my $id (&Apache::loncommon::languageids()) {
 6380:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6381:         if ($code ne '') {
 6382:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6383:         }
 6384:     }
 6385:     return %langchoices;
 6386: }
 6387: 
 6388: sub defaults_titles {
 6389:     my ($dom) = @_;
 6390:     my %titles = &Apache::lonlocal::texthash (
 6391:                    'auth_def'      => 'Default authentication type',
 6392:                    'auth_arg_def'  => 'Default authentication argument',
 6393:                    'lang_def'      => 'Default language',
 6394:                    'timezone_def'  => 'Default timezone',
 6395:                    'datelocale_def' => 'Default locale for dates',
 6396:                    'portal_def'     => 'Portal/Default URL',
 6397:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6398:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6399:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6400:                  );
 6401:     if ($dom) {
 6402:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6403:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6404:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6405:         $protocol = 'http' if ($protocol ne 'https');
 6406:         if ($uint_dom) {
 6407:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6408:                                          $uint_dom);
 6409:         }
 6410:     }
 6411:     return (\%titles);
 6412: }
 6413: 
 6414: sub print_scantron {
 6415:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 6416:     if ($position eq 'top') {
 6417:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 6418:     } else {
 6419:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 6420:     }
 6421: }
 6422: 
 6423: sub scantron_javascript {
 6424:     return <<"ENDSCRIPT";
 6425: 
 6426: <script type="text/javascript">
 6427: // <![CDATA[
 6428: 
 6429: function toggleScantron(form) {
 6430:     var csvfieldset = new Array();
 6431:     if (document.getElementById('scantroncsv_cols')) {
 6432:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 6433:     }
 6434:     if (document.getElementById('scantroncsv_options')) {
 6435:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 6436:     }
 6437:     if (csvfieldset.length) {
 6438:         if (document.getElementById('scantronconfcsv')) {
 6439:             var scantroncsv = document.getElementById('scantronconfcsv');
 6440:             if (scantroncsv.checked) {
 6441:                 for (var i=0; i<csvfieldset.length; i++) {
 6442:                     csvfieldset[i].style.display = 'block';
 6443:                 }
 6444:             } else {
 6445:                 for (var i=0; i<csvfieldset.length; i++) {
 6446:                     csvfieldset[i].style.display = 'none';
 6447:                 }
 6448:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 6449:                 if (csvselects.length) {
 6450:                     for (var j=0; j<csvselects.length; j++) {
 6451:                         csvselects[j].selectedIndex = 0;
 6452:                     }
 6453:                 }
 6454:             }
 6455:         }
 6456:     }
 6457:     return;
 6458: }
 6459: // ]]>
 6460: </script>
 6461: 
 6462: ENDSCRIPT
 6463: 
 6464: }
 6465: 
 6466: sub print_scantronformat {
 6467:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6468:     my $itemcount = 1;
 6469:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6470:         %confhash);
 6471:     my $switchserver = &check_switchserver($dom,$confname);
 6472:     my %lt = &Apache::lonlocal::texthash (
 6473:                 default => 'Default bubblesheet format file error',
 6474:                 custom  => 'Custom bubblesheet format file error',
 6475:              );
 6476:     my %scantronfiles = (
 6477:         default => 'default.tab',
 6478:         custom => 'custom.tab',
 6479:     );
 6480:     foreach my $key (keys(%scantronfiles)) {
 6481:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6482:                               .$scantronfiles{$key};
 6483:     }
 6484:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6485:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6486:         if (!$switchserver) {
 6487:             my $servadm = $r->dir_config('lonAdmEMail');
 6488:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6489:             if ($configuserok eq 'ok') {
 6490:                 if ($author_ok eq 'ok') {
 6491:                     my %legacyfile = (
 6492:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 6493:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 6494:                     );
 6495:                     my %md5chk;
 6496:                     foreach my $type (keys(%legacyfile)) {
 6497:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6498:                         chomp($md5chk{$type});
 6499:                     }
 6500:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6501:                         foreach my $type (keys(%legacyfile)) {
 6502:                             ($scantronurls{$type},my $error) =
 6503:                                 &legacy_scantronformat($r,$dom,$confname,
 6504:                                                  $type,$legacyfile{$type},
 6505:                                                  $scantronurls{$type},
 6506:                                                  $scantronfiles{$type});
 6507:                             if ($error ne '') {
 6508:                                 $error{$type} = $error;
 6509:                             }
 6510:                         }
 6511:                         if (keys(%error) == 0) {
 6512:                             $is_custom = 1;
 6513:                             $confhash{'scantron'}{'scantronformat'} =
 6514:                                 $scantronurls{'custom'};
 6515:                             my $putresult =
 6516:                                 &Apache::lonnet::put_dom('configuration',
 6517:                                                          \%confhash,$dom);
 6518:                             if ($putresult ne 'ok') {
 6519:                                 $error{'custom'} =
 6520:                                     '<span class="LC_error">'.
 6521:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6522:                             }
 6523:                         }
 6524:                     } else {
 6525:                         ($scantronurls{'default'},my $error) =
 6526:                             &legacy_scantronformat($r,$dom,$confname,
 6527:                                           'default',$legacyfile{'default'},
 6528:                                           $scantronurls{'default'},
 6529:                                           $scantronfiles{'default'});
 6530:                         if ($error eq '') {
 6531:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6532:                             my $putresult =
 6533:                                 &Apache::lonnet::put_dom('configuration',
 6534:                                                          \%confhash,$dom);
 6535:                             if ($putresult ne 'ok') {
 6536:                                 $error{'default'} =
 6537:                                     '<span class="LC_error">'.
 6538:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6539:                             }
 6540:                         } else {
 6541:                             $error{'default'} = $error;
 6542:                         }
 6543:                     }
 6544:                 }
 6545:             }
 6546:         } else {
 6547:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6548:         }
 6549:     }
 6550:     if (ref($settings) eq 'HASH') {
 6551:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6552:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6553:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6554:                 $scantronurl = '';
 6555:             } else {
 6556:                 $scantronurl = $settings->{'scantronformat'};
 6557:             }
 6558:             $is_custom = 1;
 6559:         } else {
 6560:             $scantronurl = $scantronurls{'default'};
 6561:         }
 6562:     } else {
 6563:         if ($is_custom) {
 6564:             $scantronurl = $scantronurls{'custom'};
 6565:         } else {
 6566:             $scantronurl = $scantronurls{'default'};
 6567:         }
 6568:     }
 6569:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6570:     $datatable .= '<tr'.$css_class.'>';
 6571:     if (!$is_custom) {
 6572:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6573:                       '<span class="LC_nobreak">';
 6574:         if ($scantronurl) {
 6575:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6576:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6577:         } else {
 6578:             $datatable = &mt('File unavailable for display');
 6579:         }
 6580:         $datatable .= '</span></td>';
 6581:         if (keys(%error) == 0) { 
 6582:             $datatable .= '<td valign="bottom">';
 6583:             if (!$switchserver) {
 6584:                 $datatable .= &mt('Upload:').'<br />';
 6585:             }
 6586:         } else {
 6587:             my $errorstr;
 6588:             foreach my $key (sort(keys(%error))) {
 6589:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6590:             }
 6591:             $datatable .= '<td>'.$errorstr;
 6592:         }
 6593:     } else {
 6594:         if (keys(%error) > 0) {
 6595:             my $errorstr;
 6596:             foreach my $key (sort(keys(%error))) {
 6597:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6598:             } 
 6599:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6600:         } elsif ($scantronurl) {
 6601:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6602:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6603:             $datatable .= '<td><span class="LC_nobreak">'.
 6604:                           $link.
 6605:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6606:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6607:                           '<td><span class="LC_nobreak">&nbsp;'.
 6608:                           &mt('Replace:').'</span><br />';
 6609:         }
 6610:     }
 6611:     if (keys(%error) == 0) {
 6612:         if ($switchserver) {
 6613:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6614:         } else {
 6615:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6616:                          '<input type="file" name="scantronformat" /></span>';
 6617:         }
 6618:     }
 6619:     $datatable .= '</td></tr>';
 6620:     $$rowtotal ++;
 6621:     return $datatable;
 6622: }
 6623: 
 6624: sub legacy_scantronformat {
 6625:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6626:     my ($url,$error);
 6627:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6628:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6629:         (my $result,$url) =
 6630:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6631:                          '','',$newfile);
 6632:         if ($result ne 'ok') {
 6633:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6634:         }
 6635:     }
 6636:     return ($url,$error);
 6637: }
 6638: 
 6639: sub print_scantronconfig {
 6640:     my ($dom,$settings,$rowtotal) = @_;
 6641:     my $itemcount = 2;
 6642:     my $is_checked = ' checked="checked"';
 6643:     my %optionson = (
 6644:                      hdr => ' checked="checked"',
 6645:                      pad => ' checked="checked"',
 6646:                      rem => ' checked="checked"',
 6647:                     );
 6648:     my %optionsoff = (
 6649:                       hdr => '',
 6650:                       pad => '',
 6651:                       rem => '',
 6652:                      );
 6653:     my $currcsvsty = 'none';
 6654:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 6655:     my @fields = &scantroncsv_fields();
 6656:     my %titles = &scantronconfig_titles();
 6657:     if (ref($settings) eq 'HASH') {
 6658:         if (ref($settings->{config}) eq 'HASH') {
 6659:             if ($settings->{config}->{dat}) {
 6660:                 $checked{'dat'} = $is_checked;
 6661:             }
 6662:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 6663:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 6664:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 6665:                     if (keys(%csvfields) > 0) {
 6666:                         $checked{'csv'} = $is_checked;
 6667:                         $currcsvsty = 'block';
 6668:                     }
 6669:                 }
 6670:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 6671:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 6672:                     foreach my $option (keys(%optionson)) {
 6673:                         unless ($csvoptions{$option}) {
 6674:                             $optionsoff{$option} = $optionson{$option};
 6675:                             $optionson{$option} = '';
 6676:                         }
 6677:                     }
 6678:                 }
 6679:             }
 6680:         } else {
 6681:             $checked{'dat'} = $is_checked;
 6682:         }
 6683:     } else {
 6684:         $checked{'dat'} = $is_checked;
 6685:     }
 6686:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 6687:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6688:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 6689:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 6690:     foreach my $item ('dat','csv') {
 6691:         my $id;
 6692:         if ($item eq 'csv') {
 6693:             $id = 'id="scantronconfcsv" ';
 6694:         }
 6695:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 6696:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 6697:         if ($item eq 'csv') {
 6698:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 6699:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 6700:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 6701:             foreach my $col (@fields) {
 6702:                 my $selnone;
 6703:                 if ($csvfields{$col} eq '') {
 6704:                     $selnone = ' selected="selected"';
 6705:                 }
 6706:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 6707:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 6708:                               '<option value=""'.$selnone.'></option>';
 6709:                 for (my $i=0; $i<20; $i++) {
 6710:                     my $shown = $i+1;
 6711:                     my $sel;
 6712:                     unless ($selnone) {
 6713:                         if (exists($csvfields{$col})) {
 6714:                             if ($csvfields{$col} == $i) {
 6715:                                 $sel = ' selected="selected"';
 6716:                             }
 6717:                         }
 6718:                     }
 6719:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 6720:                 }
 6721:                 $datatable .= '</select></td></tr>';
 6722:            }
 6723:            $datatable .= '</table></fieldset>'.
 6724:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 6725:                          '<legend>'.&mt('CSV Options').'</legend>';
 6726:            foreach my $option ('hdr','pad','rem') {
 6727:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 6728:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 6729:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 6730:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 6731:            }
 6732:            $datatable .= '</fieldset>';
 6733:            $itemcount ++;
 6734:         }
 6735:     }
 6736:     $datatable .= '</td></tr>';
 6737:     $$rowtotal ++;
 6738:     return $datatable;
 6739: }
 6740: 
 6741: sub scantronconfig_titles {
 6742:     return &Apache::lonlocal::texthash(
 6743:                                           dat => 'Standard format (.dat)',
 6744:                                           csv => 'Comma separated values (.csv)',
 6745:                                           hdr => 'Remove first line in file (contains column titles)',
 6746:                                           pad => 'Prepend 0s to PaperID',
 6747:                                           rem => 'Remove leading spaces (except Question Response columns)',
 6748:                                           CODE => 'CODE',
 6749:                                           ID   => 'Student ID',
 6750:                                           PaperID => 'Paper ID',
 6751:                                           FirstName => 'First Name',
 6752:                                           LastName => 'Last Name',
 6753:                                           FirstQuestion => 'First Question Response',
 6754:                                           Section => 'Section',
 6755:     );
 6756: }
 6757: 
 6758: sub scantroncsv_fields {
 6759:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 6760: }
 6761: 
 6762: sub print_coursecategories {
 6763:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6764:     my $datatable;
 6765:     if ($position eq 'top') {
 6766:         my (%checked);
 6767:         my @catitems = ('unauth','auth');
 6768:         my @cattypes = ('std','domonly','codesrch','none');
 6769:         $checked{'unauth'} = 'std';
 6770:         $checked{'auth'} = 'std';
 6771:         if (ref($settings) eq 'HASH') {
 6772:             foreach my $type (@cattypes) {
 6773:                 if ($type eq $settings->{'unauth'}) {
 6774:                     $checked{'unauth'} = $type;
 6775:                 }
 6776:                 if ($type eq $settings->{'auth'}) {
 6777:                     $checked{'auth'} = $type;
 6778:                 }
 6779:             }
 6780:         }
 6781:         my %lt = &Apache::lonlocal::texthash (
 6782:                                                unauth   => 'Catalog type for unauthenticated users',
 6783:                                                auth     => 'Catalog type for authenticated users',
 6784:                                                none     => 'No catalog',
 6785:                                                std      => 'Standard catalog',
 6786:                                                domonly  => 'Domain-only catalog',
 6787:                                                codesrch => "Code search form",
 6788:                                              );
 6789:        my $itemcount = 0;
 6790:        foreach my $item (@catitems) {
 6791:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6792:            $datatable .= '<tr '.$css_class.'>'.
 6793:                          '<td>'.$lt{$item}.'</td>'.
 6794:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6795:            foreach my $type (@cattypes) {
 6796:                my $ischecked;
 6797:                if ($checked{$item} eq $type) {
 6798:                    $ischecked=' checked="checked"';
 6799:                }
 6800:                $datatable .= '<label>'.
 6801:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6802:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6803:            }
 6804:            $datatable .= '</span></td></tr>';
 6805:            $itemcount ++;
 6806:         }
 6807:         $$rowtotal += $itemcount;
 6808:     } elsif ($position eq 'middle') {
 6809:         my $toggle_cats_crs = ' ';
 6810:         my $toggle_cats_dom = ' checked="checked" ';
 6811:         my $can_cat_crs = ' ';
 6812:         my $can_cat_dom = ' checked="checked" ';
 6813:         my $toggle_catscomm_comm = ' ';
 6814:         my $toggle_catscomm_dom = ' checked="checked" ';
 6815:         my $can_catcomm_comm = ' ';
 6816:         my $can_catcomm_dom = ' checked="checked" ';
 6817: 
 6818:         if (ref($settings) eq 'HASH') {
 6819:             if ($settings->{'togglecats'} eq 'crs') {
 6820:                 $toggle_cats_crs = $toggle_cats_dom;
 6821:                 $toggle_cats_dom = ' ';
 6822:             }
 6823:             if ($settings->{'categorize'} eq 'crs') {
 6824:                 $can_cat_crs = $can_cat_dom;
 6825:                 $can_cat_dom = ' ';
 6826:             }
 6827:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6828:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6829:                 $toggle_catscomm_dom = ' ';
 6830:             }
 6831:             if ($settings->{'categorizecomm'} eq 'comm') {
 6832:                 $can_catcomm_comm = $can_catcomm_dom;
 6833:                 $can_catcomm_dom = ' ';
 6834:             }
 6835:         }
 6836:         my %title = &Apache::lonlocal::texthash (
 6837:                      togglecats     => 'Show/Hide a course in catalog',
 6838:                      togglecatscomm => 'Show/Hide a community in catalog',
 6839:                      categorize     => 'Assign a category to a course',
 6840:                      categorizecomm => 'Assign a category to a community',
 6841:                     );
 6842:         my %level = &Apache::lonlocal::texthash (
 6843:                      dom  => 'Set in Domain',
 6844:                      crs  => 'Set in Course',
 6845:                      comm => 'Set in Community',
 6846:                     );
 6847:         $datatable = '<tr class="LC_odd_row">'.
 6848:                   '<td>'.$title{'togglecats'}.'</td>'.
 6849:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6850:                   '<input type="radio" name="togglecats"'.
 6851:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6852:                   '<label><input type="radio" name="togglecats"'.
 6853:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6854:                   '</tr><tr>'.
 6855:                   '<td>'.$title{'categorize'}.'</td>'.
 6856:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6857:                   '<label><input type="radio" name="categorize"'.
 6858:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6859:                   '<label><input type="radio" name="categorize"'.
 6860:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6861:                   '</tr><tr class="LC_odd_row">'.
 6862:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6863:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6864:                   '<input type="radio" name="togglecatscomm"'.
 6865:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6866:                   '<label><input type="radio" name="togglecatscomm"'.
 6867:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6868:                   '</tr><tr>'.
 6869:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6870:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6871:                   '<label><input type="radio" name="categorizecomm"'.
 6872:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6873:                   '<label><input type="radio" name="categorizecomm"'.
 6874:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6875:                   '</tr>';
 6876:         $$rowtotal += 4;
 6877:     } else {
 6878:         my $css_class;
 6879:         my $itemcount = 1;
 6880:         my $cathash; 
 6881:         if (ref($settings) eq 'HASH') {
 6882:             $cathash = $settings->{'cats'};
 6883:         }
 6884:         if (ref($cathash) eq 'HASH') {
 6885:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6886:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6887:                                                    \%allitems,\%idx,\@jsarray);
 6888:             my $maxdepth = scalar(@cats);
 6889:             my $colattrib = '';
 6890:             if ($maxdepth > 2) {
 6891:                 $colattrib = ' colspan="2" ';
 6892:             }
 6893:             my @path;
 6894:             if (@cats > 0) {
 6895:                 if (ref($cats[0]) eq 'ARRAY') {
 6896:                     my $numtop = @{$cats[0]};
 6897:                     my $maxnum = $numtop;
 6898:                     my %default_names = (
 6899:                           instcode    => &mt('Official courses'),
 6900:                           communities => &mt('Communities'),
 6901:                     );
 6902: 
 6903:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6904:                         ($cathash->{'instcode::0'} eq '') ||
 6905:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6906:                         ($cathash->{'communities::0'} eq '')) {
 6907:                         $maxnum ++;
 6908:                     }
 6909:                     my $lastidx;
 6910:                     for (my $i=0; $i<$numtop; $i++) {
 6911:                         my $parent = $cats[0][$i];
 6912:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6913:                         my $item = &escape($parent).'::0';
 6914:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6915:                         $lastidx = $idx{$item};
 6916:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6917:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6918:                         for (my $k=0; $k<=$maxnum; $k++) {
 6919:                             my $vpos = $k+1;
 6920:                             my $selstr;
 6921:                             if ($k == $i) {
 6922:                                 $selstr = ' selected="selected" ';
 6923:                             }
 6924:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6925:                         }
 6926:                         $datatable .= '</select></span></td><td>';
 6927:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 6928:                             $datatable .=  '<span class="LC_nobreak">'
 6929:                                            .$default_names{$parent}.'</span>';
 6930:                             if ($parent eq 'instcode') {
 6931:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6932:                                               .&mt('with institutional codes')
 6933:                                               .')</span></td><td'.$colattrib.'>';
 6934:                             } else {
 6935:                                 $datatable .= '<table><tr><td>';
 6936:                             }
 6937:                             $datatable .= '<span class="LC_nobreak">'
 6938:                                           .'<label><input type="radio" name="'
 6939:                                           .$parent.'" value="1" checked="checked" />'
 6940:                                           .&mt('Display').'</label>';
 6941:                             if ($parent eq 'instcode') {
 6942:                                 $datatable .= '&nbsp;';
 6943:                             } else {
 6944:                                 $datatable .= '</span></td></tr><tr><td>'
 6945:                                               .'<span class="LC_nobreak">';
 6946:                             }
 6947:                             $datatable .= '<label><input type="radio" name="'
 6948:                                           .$parent.'" value="0" />'
 6949:                                           .&mt('Do not display').'</label></span>';
 6950:                             if ($parent eq 'communities') {
 6951:                                 $datatable .= '</td></tr></table>';
 6952:                             }
 6953:                             $datatable .= '</td>';
 6954:                         } else {
 6955:                             $datatable .= $parent
 6956:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6957:                                           .'<input type="checkbox" name="deletecategory" '
 6958:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6959:                         }
 6960:                         my $depth = 1;
 6961:                         push(@path,$parent);
 6962:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6963:                         pop(@path);
 6964:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6965:                         $itemcount ++;
 6966:                     }
 6967:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6968:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6969:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6970:                     for (my $k=0; $k<=$maxnum; $k++) {
 6971:                         my $vpos = $k+1;
 6972:                         my $selstr;
 6973:                         if ($k == $numtop) {
 6974:                             $selstr = ' selected="selected" ';
 6975:                         }
 6976:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6977:                     }
 6978:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6979:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6980:                                   .'</tr>'."\n";
 6981:                     $itemcount ++;
 6982:                     foreach my $default ('instcode','communities') {
 6983:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6984:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6985:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6986:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6987:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6988:                             for (my $k=0; $k<=$maxnum; $k++) {
 6989:                                 my $vpos = $k+1;
 6990:                                 my $selstr;
 6991:                                 if ($k == $maxnum) {
 6992:                                     $selstr = ' selected="selected" ';
 6993:                                 }
 6994:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6995:                             }
 6996:                             $datatable .= '</select></span></td>'.
 6997:                                           '<td><span class="LC_nobreak">'.
 6998:                                           $default_names{$default}.'</span>';
 6999:                             if ($default eq 'instcode') {
 7000:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 7001:                                               .&mt('with institutional codes').')</span>';
 7002:                             }
 7003:                             $datatable .= '</td>'
 7004:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 7005:                                           .&mt('Display').'</label>&nbsp;'
 7006:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 7007:                                           .&mt('Do not display').'</label></span></td></tr>';
 7008:                         }
 7009:                     }
 7010:                 }
 7011:             } else {
 7012:                 $datatable .= &initialize_categories($itemcount);
 7013:             }
 7014:         } else {
 7015:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 7016:                           .&initialize_categories($itemcount);
 7017:         }
 7018:         $$rowtotal += $itemcount;
 7019:     }
 7020:     return $datatable;
 7021: }
 7022: 
 7023: sub print_serverstatuses {
 7024:     my ($dom,$settings,$rowtotal) = @_;
 7025:     my $datatable;
 7026:     my @pages = &serverstatus_pages();
 7027:     my (%namedaccess,%machineaccess);
 7028:     foreach my $type (@pages) {
 7029:         $namedaccess{$type} = '';
 7030:         $machineaccess{$type}= '';
 7031:     }
 7032:     if (ref($settings) eq 'HASH') {
 7033:         foreach my $type (@pages) {
 7034:             if (exists($settings->{$type})) {
 7035:                 if (ref($settings->{$type}) eq 'HASH') {
 7036:                     foreach my $key (keys(%{$settings->{$type}})) {
 7037:                         if ($key eq 'namedusers') {
 7038:                             $namedaccess{$type} = $settings->{$type}->{$key};
 7039:                         } elsif ($key eq 'machines') {
 7040:                             $machineaccess{$type} = $settings->{$type}->{$key};
 7041:                         }
 7042:                     }
 7043:                 }
 7044:             }
 7045:         }
 7046:     }
 7047:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7048:     my $rownum = 0;
 7049:     my $css_class;
 7050:     foreach my $type (@pages) {
 7051:         $rownum ++;
 7052:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 7053:         $datatable .= '<tr'.$css_class.'>'.
 7054:                       '<td><span class="LC_nobreak">'.
 7055:                       $titles->{$type}.'</span></td>'.
 7056:                       '<td class="LC_left_item">'.
 7057:                       '<input type="text" name="'.$type.'_namedusers" '.
 7058:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 7059:                       '<td class="LC_right_item">'.
 7060:                       '<span class="LC_nobreak">'.
 7061:                       '<input type="text" name="'.$type.'_machines" '.
 7062:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 7063:                       '</span></td></tr>'."\n";
 7064:     }
 7065:     $$rowtotal += $rownum;
 7066:     return $datatable;
 7067: }
 7068: 
 7069: sub serverstatus_pages {
 7070:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 7071:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 7072:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 7073:             'uniquecodes','diskusage','coursecatalog');
 7074: }
 7075: 
 7076: sub defaults_javascript {
 7077:     my ($settings) = @_;
 7078:     return unless (ref($settings) eq 'HASH');
 7079:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7080:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 7081:         if ($maxnum eq '') {
 7082:             $maxnum = 0;
 7083:         }
 7084:         $maxnum ++;
 7085:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 7086:         return <<"ENDSCRIPT";
 7087: <script type="text/javascript">
 7088: // <![CDATA[
 7089: function reorderTypes(form,caller) {
 7090:     var changedVal;
 7091: $jstext 
 7092:     var newpos = 'addinststatus_pos';
 7093:     var current = new Array;
 7094:     var maxh = $maxnum;
 7095:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7096:     var oldVal;
 7097:     if (caller == newpos) {
 7098:         changedVal = newitemVal;
 7099:     } else {
 7100:         var curritem = 'inststatus_pos_'+caller;
 7101:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7102:         current[newitemVal] = newpos;
 7103:     }
 7104:     for (var i=0; i<inststatuses.length; i++) {
 7105:         if (inststatuses[i] != caller) {
 7106:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7107:             if (form.elements[elementName]) {
 7108:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7109:                 current[currVal] = elementName;
 7110:             }
 7111:         }
 7112:     }
 7113:     for (var j=0; j<maxh; j++) {
 7114:         if (current[j] == undefined) {
 7115:             oldVal = j;
 7116:         }
 7117:     }
 7118:     if (oldVal < changedVal) {
 7119:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7120:            var elementName = current[k];
 7121:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7122:         }
 7123:     } else {
 7124:         for (var k=changedVal; k<oldVal; k++) {
 7125:             var elementName = current[k];
 7126:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7127:         }
 7128:     }
 7129:     return;
 7130: }
 7131: 
 7132: // ]]>
 7133: </script>
 7134: 
 7135: ENDSCRIPT
 7136:     }
 7137: }
 7138: 
 7139: sub passwords_javascript {
 7140:     my %intalert = &Apache::lonlocal::texthash (
 7141:         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.',
 7142:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
 7143:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
 7144:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
 7145:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
 7146:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
 7147:     );
 7148:     &js_escape(\%intalert);
 7149:     my $defmin = $Apache::lonnet::passwdmin;
 7150:     my $intauthjs = <<"ENDSCRIPT";
 7151: 
 7152: function warnIntAuth(field) {
 7153:     if (field.name == 'intauth_check') {
 7154:         if (field.value == '2') {
 7155:             alert('$intalert{authcheck}');
 7156:         }
 7157:     }
 7158:     if (field.name == 'intauth_cost') {
 7159:         field.value.replace(/\s/g,'');
 7160:         if (field.value != '') {
 7161:             var regexdigit=/^\\d+\$/;
 7162:             if (!regexdigit.test(field.value)) {
 7163:                 alert('$intalert{authcost}');
 7164:             }
 7165:         }
 7166:     }
 7167:     return;
 7168: }
 7169: 
 7170: function warnIntPass(field) {
 7171:     field.value.replace(/^\s+/,'');
 7172:     field.value.replace(/\s+\$/,'');
 7173:     var regexdigit=/^\\d+\$/;
 7174:     if (field.name == 'passwords_min') {
 7175:         if (field.value == '') {
 7176:             alert('$intalert{passmin}');
 7177:             field.value = '$defmin';
 7178:         } else {
 7179:             if (!regexdigit.test(field.value)) {
 7180:                 alert('$intalert{passmin}');
 7181:                 field.value = '$defmin';
 7182:             }
 7183:             var minval = parseInt(field.value,10);
 7184:             if (minval < $defmin) {
 7185:                 alert('$intalert{passmin}');
 7186:                 field.value = '$defmin';
 7187:             }
 7188:         }
 7189:     } else {
 7190:         if (field.value == '0') {
 7191:             field.value = '';
 7192:         }
 7193:         if (field.value != '') {
 7194:             if (field.name == 'passwords_expire') {
 7195:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/;
 7196:                 if (!regexpposnum.test(field.value)) {
 7197:                     alert('$intalert{passexp}');
 7198:                     field.value = '';
 7199:                 } else {
 7200:                     var expval = parseFloat(field.value);
 7201:                     if (expval == 0) {
 7202:                         alert('$intalert{passexp}');
 7203:                         field.value = '';
 7204:                     }
 7205:                 }
 7206:             } else {
 7207:                 if (!regexdigit.test(field.value)) {
 7208:                     if (field.name == 'passwords_max') {
 7209:                         alert('$intalert{passmax}');
 7210:                     } else {
 7211:                         if (field.name == 'passwords_numsaved') {
 7212:                             alert('$intalert{passnum}');
 7213:                         }
 7214:                     }
 7215:                 }
 7216:                 field.value = '';
 7217:             }
 7218:         }
 7219:     }
 7220:     return;
 7221: }
 7222: 
 7223: ENDSCRIPT
 7224:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7225: }
 7226: 
 7227: sub coursecategories_javascript {
 7228:     my ($settings) = @_;
 7229:     my ($output,$jstext,$cathash);
 7230:     if (ref($settings) eq 'HASH') {
 7231:         $cathash = $settings->{'cats'};
 7232:     }
 7233:     if (ref($cathash) eq 'HASH') {
 7234:         my (@cats,@jsarray,%idx);
 7235:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7236:         if (@jsarray > 0) {
 7237:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7238:             for (my $i=0; $i<@jsarray; $i++) {
 7239:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7240:                     my $catstr = join('","',@{$jsarray[$i]});
 7241:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7242:                 }
 7243:             }
 7244:         }
 7245:     } else {
 7246:         $jstext  = '    var categories = Array(1);'."\n".
 7247:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7248:     }
 7249:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7250:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7251:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7252:     &js_escape(\$instcode_reserved);
 7253:     &js_escape(\$communities_reserved);
 7254:     &js_escape(\$choose_again);
 7255:     $output = <<"ENDSCRIPT";
 7256: <script type="text/javascript">
 7257: // <![CDATA[
 7258: function reorderCats(form,parent,item,idx) {
 7259:     var changedVal;
 7260: $jstext
 7261:     var newpos = 'addcategory_pos';
 7262:     if (parent == '') {
 7263:         var has_instcode = 0;
 7264:         var maxtop = categories[idx].length;
 7265:         for (var j=0; j<maxtop; j++) {
 7266:             if (categories[idx][j] == 'instcode::0') {
 7267:                 has_instcode == 1;
 7268:             }
 7269:         }
 7270:         if (has_instcode == 0) {
 7271:             categories[idx][maxtop] = 'instcode_pos';
 7272:         }
 7273:     } else {
 7274:         newpos += '_'+parent;
 7275:     }
 7276:     var maxh = 1 + categories[idx].length;
 7277:     var current = new Array;
 7278:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7279:     if (item == newpos) {
 7280:         changedVal = newitemVal;
 7281:     } else {
 7282:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7283:         current[newitemVal] = newpos;
 7284:     }
 7285:     for (var i=0; i<categories[idx].length; i++) {
 7286:         var elementName = categories[idx][i];
 7287:         if (elementName != item) {
 7288:             if (form.elements[elementName]) {
 7289:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7290:                 current[currVal] = elementName;
 7291:             }
 7292:         }
 7293:     }
 7294:     var oldVal;
 7295:     for (var j=0; j<maxh; j++) {
 7296:         if (current[j] == undefined) {
 7297:             oldVal = j;
 7298:         }
 7299:     }
 7300:     if (oldVal < changedVal) {
 7301:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7302:            var elementName = current[k];
 7303:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7304:         }
 7305:     } else {
 7306:         for (var k=changedVal; k<oldVal; k++) {
 7307:             var elementName = current[k];
 7308:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7309:         }
 7310:     }
 7311:     return;
 7312: }
 7313: 
 7314: function categoryCheck(form) {
 7315:     if (form.elements['addcategory_name'].value == 'instcode') {
 7316:         alert('$instcode_reserved\\n$choose_again');
 7317:         return false;
 7318:     }
 7319:     if (form.elements['addcategory_name'].value == 'communities') {
 7320:         alert('$communities_reserved\\n$choose_again');
 7321:         return false;
 7322:     }
 7323:     return true;
 7324: }
 7325: 
 7326: // ]]>
 7327: </script>
 7328: 
 7329: ENDSCRIPT
 7330:     return $output;
 7331: }
 7332: 
 7333: sub initialize_categories {
 7334:     my ($itemcount) = @_;
 7335:     my ($datatable,$css_class,$chgstr);
 7336:     my %default_names = (
 7337:                       instcode    => 'Official courses (with institutional codes)',
 7338:                       communities => 'Communities',
 7339:                         );
 7340:     my $select0 = ' selected="selected"';
 7341:     my $select1 = '';
 7342:     foreach my $default ('instcode','communities') {
 7343:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7344:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 7345:         if ($default eq 'communities') {
 7346:             $select1 = $select0;
 7347:             $select0 = '';
 7348:         }
 7349:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7350:                      .'<select name="'.$default.'_pos">'
 7351:                      .'<option value="0"'.$select0.'>1</option>'
 7352:                      .'<option value="1"'.$select1.'>2</option>'
 7353:                      .'<option value="2">3</option></select>&nbsp;'
 7354:                      .$default_names{$default}
 7355:                      .'</span></td><td><span class="LC_nobreak">'
 7356:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7357:                      .&mt('Display').'</label>&nbsp;<label>'
 7358:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7359:                  .'</label></span></td></tr>';
 7360:         $itemcount ++;
 7361:     }
 7362:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7363:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7364:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7365:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7366:                   .'<option value="0">1</option>'
 7367:                   .'<option value="1">2</option>'
 7368:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7369:                   .&mt('Add category').'</span></td><tda<span class="LC_nobreak">>'.&mt('Name:')
 7370:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 7371:                   .'</td></tr>';
 7372:     return $datatable;
 7373: }
 7374: 
 7375: sub build_category_rows {
 7376:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7377:     my ($text,$name,$item,$chgstr);
 7378:     if (ref($cats) eq 'ARRAY') {
 7379:         my $maxdepth = scalar(@{$cats});
 7380:         if (ref($cats->[$depth]) eq 'HASH') {
 7381:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7382:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7383:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7384:                 $text .= '<td><table class="LC_data_table">';
 7385:                 my ($idxnum,$parent_name,$parent_item);
 7386:                 my $higher = $depth - 1;
 7387:                 if ($higher == 0) {
 7388:                     $parent_name = &escape($parent).'::'.$higher;
 7389:                 } else {
 7390:                     if (ref($path) eq 'ARRAY') {
 7391:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7392:                     }
 7393:                 }
 7394:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7395:                 for (my $j=0; $j<=$numchildren; $j++) {
 7396:                     if ($j < $numchildren) {
 7397:                         $name = $cats->[$depth]{$parent}[$j];
 7398:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7399:                         $idxnum = $idx->{$item};
 7400:                     } else {
 7401:                         $name = $parent_name;
 7402:                         $item = $parent_item;
 7403:                     }
 7404:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7405:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7406:                     for (my $i=0; $i<=$numchildren; $i++) {
 7407:                         my $vpos = $i+1;
 7408:                         my $selstr;
 7409:                         if ($j == $i) {
 7410:                             $selstr = ' selected="selected" ';
 7411:                         }
 7412:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7413:                     }
 7414:                     $text .= '</select>&nbsp;';
 7415:                     if ($j < $numchildren) {
 7416:                         my $deeper = $depth+1;
 7417:                         $text .= $name.'&nbsp;'
 7418:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7419:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7420:                         if(ref($path) eq 'ARRAY') {
 7421:                             push(@{$path},$name);
 7422:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7423:                             pop(@{$path});
 7424:                         }
 7425:                     } else {
 7426:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 7427:                         if ($j == $numchildren) {
 7428:                             $text .= $name;
 7429:                         } else {
 7430:                             $text .= $item;
 7431:                         }
 7432:                         $text .= '" value="" />';
 7433:                     }
 7434:                     $text .= '</td></tr>';
 7435:                 }
 7436:                 $text .= '</table></td>';
 7437:             } else {
 7438:                 my $higher = $depth-1;
 7439:                 if ($higher == 0) {
 7440:                     $name = &escape($parent).'::'.$higher;
 7441:                 } else {
 7442:                     if (ref($path) eq 'ARRAY') {
 7443:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7444:                     }
 7445:                 }
 7446:                 my $colspan;
 7447:                 if ($parent ne 'instcode') {
 7448:                     $colspan = $maxdepth - $depth - 1;
 7449:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 7450:                 }
 7451:             }
 7452:         }
 7453:     }
 7454:     return $text;
 7455: }
 7456: 
 7457: sub modifiable_userdata_row {
 7458:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 7459:         $rowid,$customcss,$rowstyle) = @_;
 7460:     my ($role,$rolename,$statustype);
 7461:     $role = $item;
 7462:     if ($context eq 'cancreate') {
 7463:         if ($item =~ /^(emailusername)_(.+)$/) {
 7464:             $role = $1;
 7465:             $statustype = $2;
 7466:             if (ref($usertypes) eq 'HASH') {
 7467:                 if ($usertypes->{$statustype}) {
 7468:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7469:                 } else {
 7470:                     $rolename = &mt('Data provided by user');
 7471:                 }
 7472:             }
 7473:         }
 7474:     } elsif ($context eq 'selfcreate') {
 7475:         if (ref($usertypes) eq 'HASH') {
 7476:             $rolename = $usertypes->{$role};
 7477:         } else {
 7478:             $rolename = $role;
 7479:         }
 7480:     } else {
 7481:         if ($role eq 'cr') {
 7482:             $rolename = &mt('Custom role');
 7483:         } else {
 7484:             $rolename = &Apache::lonnet::plaintext($role);
 7485:         }
 7486:     }
 7487:     my (@fields,%fieldtitles);
 7488:     if (ref($fieldsref) eq 'ARRAY') {
 7489:         @fields = @{$fieldsref};
 7490:     } else {
 7491:         @fields = ('lastname','firstname','middlename','generation',
 7492:                    'permanentemail','id');
 7493:     }
 7494:     if ((ref($titlesref) eq 'HASH')) {
 7495:         %fieldtitles = %{$titlesref};
 7496:     } else {
 7497:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7498:     }
 7499:     my $output;
 7500:     my $css_class;
 7501:     if ($rowcount%2) {
 7502:         $css_class = 'LC_odd_row';
 7503:     }
 7504:     if ($customcss) {
 7505:         $css_class .= " $customcss";
 7506:     }
 7507:     $css_class =~ s/^\s+//;
 7508:     if ($css_class) {
 7509:         $css_class = ' class="'.$css_class.'"';
 7510:     }
 7511:     if ($rowstyle) {
 7512:         $css_class .= ' style="'.$rowstyle.'"';
 7513:     }
 7514:     if ($rowid) {
 7515:         $rowid = ' id="'.$rowid.'"';
 7516:     }
 7517: 
 7518:     $output = '<tr '.$css_class.$rowid.'>'.
 7519:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7520:               '<td class="LC_left_item" colspan="2"><table>';
 7521:     my $rem;
 7522:     my %checks;
 7523:     if (ref($settings) eq 'HASH') {
 7524:         if (ref($settings->{$context}) eq 'HASH') {
 7525:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7526:                 my $hashref = $settings->{$context}->{$role};
 7527:                 if ($role eq 'emailusername') {
 7528:                     if ($statustype) {
 7529:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7530:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7531:                             if (ref($hashref) eq 'HASH') { 
 7532:                                 foreach my $field (@fields) {
 7533:                                     if ($hashref->{$field}) {
 7534:                                         $checks{$field} = $hashref->{$field};
 7535:                                     }
 7536:                                 }
 7537:                             }
 7538:                         }
 7539:                     }
 7540:                 } else {
 7541:                     if (ref($hashref) eq 'HASH') {
 7542:                         foreach my $field (@fields) {
 7543:                             if ($hashref->{$field}) {
 7544:                                 $checks{$field} = ' checked="checked" ';
 7545:                             }
 7546:                         }
 7547:                     }
 7548:                 }
 7549:             }
 7550:         }
 7551:     }
 7552: 
 7553:     my $total = scalar(@fields);
 7554:     for (my $i=0; $i<$total; $i++) {
 7555:         $rem = $i%($numinrow);
 7556:         if ($rem == 0) {
 7557:             if ($i > 0) {
 7558:                 $output .= '</tr>';
 7559:             }
 7560:             $output .= '<tr>';
 7561:         }
 7562:         my $check = ' ';
 7563:         unless ($role eq 'emailusername') {
 7564:             if (exists($checks{$fields[$i]})) {
 7565:                 $check = $checks{$fields[$i]};
 7566:             } else {
 7567:                 if ($role eq 'st') {
 7568:                     if (ref($settings) ne 'HASH') {
 7569:                         $check = ' checked="checked" '; 
 7570:                     }
 7571:                 }
 7572:             }
 7573:         }
 7574:         $output .= '<td class="LC_left_item">'.
 7575:                    '<span class="LC_nobreak">';
 7576:         if ($role eq 'emailusername') {
 7577:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7578:                 $checks{$fields[$i]} = 'omit';
 7579:             }
 7580:             foreach my $option ('required','optional','omit') {
 7581:                 my $checked='';
 7582:                 if ($checks{$fields[$i]} eq $option) {
 7583:                     $checked='checked="checked" ';
 7584:                 }
 7585:                 $output .= '<label>'.
 7586:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7587:                            &mt($option).'</label>'.('&nbsp;' x2);
 7588:             }
 7589:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7590:         } else {
 7591:             $output .= '<label>'.
 7592:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7593:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7594:                        '</label>';
 7595:         }
 7596:         $output .= '</span></td>';
 7597:     }
 7598:     $rem = $total%$numinrow;
 7599:     my $colsleft;
 7600:     if ($rem) {
 7601:         $colsleft = $numinrow - $rem;
 7602:     }
 7603:     if ($colsleft > 1) {
 7604:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7605:                    '&nbsp;</td>';
 7606:     } elsif ($colsleft == 1) {
 7607:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7608:     }
 7609:     $output .= '</tr></table></td></tr>';
 7610:     return $output;
 7611: }
 7612: 
 7613: sub insttypes_row {
 7614:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 7615:         $customcss,$rowstyle) = @_;
 7616:     my %lt = &Apache::lonlocal::texthash (
 7617:                       cansearch => 'Users allowed to search',
 7618:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7619:                       lockablenames => 'User preference to lock name',
 7620:                       selfassign    => 'Self-reportable affiliations',
 7621:              );
 7622:     my $showdom;
 7623:     if ($context eq 'cansearch') {
 7624:         $showdom = ' ('.$dom.')';
 7625:     }
 7626:     my $class = 'LC_left_item';
 7627:     if ($context eq 'statustocreate') {
 7628:         $class = 'LC_right_item';
 7629:     }
 7630:     my $css_class;
 7631:     if ($$rowtotal%2) {
 7632:         $css_class = 'LC_odd_row';
 7633:     }
 7634:     if ($customcss) {
 7635:         $css_class .= ' '.$customcss;
 7636:     }
 7637:     $css_class =~ s/^\s+//;
 7638:     if ($css_class) {
 7639:         $css_class = ' class="'.$css_class.'"';
 7640:     }
 7641:     if ($rowstyle) {
 7642:         $css_class .= ' style="'.$rowstyle.'"';
 7643:     }
 7644:     if ($onclick) {
 7645:         $onclick = 'onclick="'.$onclick.'" ';
 7646:     }
 7647:     my $output = '<tr'.$css_class.'>'.
 7648:                  '<td>'.$lt{$context}.$showdom.
 7649:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7650:     my $rem;
 7651:     if (ref($types) eq 'ARRAY') {
 7652:         for (my $i=0; $i<@{$types}; $i++) {
 7653:             if (defined($usertypes->{$types->[$i]})) {
 7654:                 my $rem = $i%($numinrow);
 7655:                 if ($rem == 0) {
 7656:                     if ($i > 0) {
 7657:                         $output .= '</tr>';
 7658:                     }
 7659:                     $output .= '<tr>';
 7660:                 }
 7661:                 my $check = ' ';
 7662:                 if (ref($settings) eq 'HASH') {
 7663:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7664:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7665:                             $check = ' checked="checked" ';
 7666:                         }
 7667:                     } elsif ($context eq 'statustocreate') {
 7668:                         $check = ' checked="checked" ';
 7669:                     }
 7670:                 }
 7671:                 $output .= '<td class="LC_left_item">'.
 7672:                            '<span class="LC_nobreak"><label>'.
 7673:                            '<input type="checkbox" name="'.$context.'" '.
 7674:                            'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
 7675:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7676:             }
 7677:         }
 7678:         $rem = @{$types}%($numinrow);
 7679:     }
 7680:     my $colsleft = $numinrow - $rem;
 7681:     if ($rem == 0) {
 7682:         $output .= '<tr>';
 7683:     }
 7684:     if ($colsleft > 1) {
 7685:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7686:     } else {
 7687:         $output .= '<td class="LC_left_item">';
 7688:     }
 7689:     my $defcheck = ' ';
 7690:     if (ref($settings) eq 'HASH') {  
 7691:         if (ref($settings->{$context}) eq 'ARRAY') {
 7692:             if (grep(/^default$/,@{$settings->{$context}})) {
 7693:                 $defcheck = ' checked="checked" ';
 7694:             }
 7695:         } elsif ($context eq 'statustocreate') {
 7696:             $defcheck = ' checked="checked" ';
 7697:         }
 7698:     }
 7699:     $output .= '<span class="LC_nobreak"><label>'.
 7700:                '<input type="checkbox" name="'.$context.'" '.
 7701:                'value="default"'.$defcheck.$onclick.'/>'.
 7702:                $othertitle.'</label></span></td>'.
 7703:                '</tr></table></td></tr>';
 7704:     return $output;
 7705: }
 7706: 
 7707: sub sorted_searchtitles {
 7708:     my %searchtitles = &Apache::lonlocal::texthash(
 7709:                          'uname' => 'username',
 7710:                          'lastname' => 'last name',
 7711:                          'lastfirst' => 'last name, first name',
 7712:                      );
 7713:     my @titleorder = ('uname','lastname','lastfirst');
 7714:     return (\%searchtitles,\@titleorder);
 7715: }
 7716: 
 7717: sub sorted_searchtypes {
 7718:     my %srchtypes_desc = (
 7719:                            exact    => 'is exact match',
 7720:                            contains => 'contains ..',
 7721:                            begins   => 'begins with ..',
 7722:                          );
 7723:     my @srchtypeorder = ('exact','begins','contains');
 7724:     return (\%srchtypes_desc,\@srchtypeorder);
 7725: }
 7726: 
 7727: sub usertype_update_row {
 7728:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7729:     my $datatable;
 7730:     my $numinrow = 4;
 7731:     foreach my $type (@{$types}) {
 7732:         if (defined($usertypes->{$type})) {
 7733:             $$rownums ++;
 7734:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7735:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7736:                           '</td><td class="LC_left_item"><table>';
 7737:             for (my $i=0; $i<@{$fields}; $i++) {
 7738:                 my $rem = $i%($numinrow);
 7739:                 if ($rem == 0) {
 7740:                     if ($i > 0) {
 7741:                         $datatable .= '</tr>';
 7742:                     }
 7743:                     $datatable .= '<tr>';
 7744:                 }
 7745:                 my $check = ' ';
 7746:                 if (ref($settings) eq 'HASH') {
 7747:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7748:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7749:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7750:                                 $check = ' checked="checked" ';
 7751:                             }
 7752:                         }
 7753:                     }
 7754:                 }
 7755: 
 7756:                 if ($i == @{$fields}-1) {
 7757:                     my $colsleft = $numinrow - $rem;
 7758:                     if ($colsleft > 1) {
 7759:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7760:                     } else {
 7761:                         $datatable .= '<td>';
 7762:                     }
 7763:                 } else {
 7764:                     $datatable .= '<td>';
 7765:                 }
 7766:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7767:                               '<input type="checkbox" name="updateable_'.$type.
 7768:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7769:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7770:             }
 7771:             $datatable .= '</tr></table></td></tr>';
 7772:         }
 7773:     }
 7774:     return $datatable;
 7775: }
 7776: 
 7777: sub modify_login {
 7778:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7779:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7780:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7781:     %title = ( coursecatalog => 'Display course catalog',
 7782:                adminmail => 'Display administrator E-mail address',
 7783:                helpdesk  => 'Display "Contact Helpdesk" link',
 7784:                newuser => 'Link for visitors to create a user account',
 7785:                loginheader => 'Log-in box header');
 7786:     @offon = ('off','on');
 7787:     if (ref($domconfig{login}) eq 'HASH') {
 7788:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7789:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7790:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7791:             }
 7792:         }
 7793:     }
 7794:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7795:                                            \%domconfig,\%loginhash);
 7796:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7797:     foreach my $item (@toggles) {
 7798:         $loginhash{login}{$item} = $env{'form.'.$item};
 7799:     }
 7800:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7801:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7802:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7803:                                          \%loginhash);
 7804:     }
 7805: 
 7806:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7807:     my %domservers = &Apache::lonnet::get_servers($dom);
 7808:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7809:     if (keys(%servers) > 1) {
 7810:         foreach my $lonhost (keys(%servers)) {
 7811:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7812:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7813:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7814:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7815:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7816:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7817:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7818:                         $changes{'loginvia'}{$lonhost} = 1;
 7819:                     } else {
 7820:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7821:                         $changes{'loginvia'}{$lonhost} = 1;
 7822:                     }
 7823:                 } else {
 7824:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7825:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7826:                         $changes{'loginvia'}{$lonhost} = 1;
 7827:                     }
 7828:                 }
 7829:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7830:                     foreach my $item (@loginvia_attribs) {
 7831:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7832:                     }
 7833:                 } else {
 7834:                     foreach my $item (@loginvia_attribs) {
 7835:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7836:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7837:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7838:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7839:                                 $new = '/';
 7840:                             }
 7841:                         }
 7842:                         if (($item eq 'custompath') && 
 7843:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7844:                             $new = '';
 7845:                         }
 7846:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7847:                             $changes{'loginvia'}{$lonhost} = 1;
 7848:                         }
 7849:                         if ($item eq 'exempt') {
 7850:                             $new = &check_exempt_addresses($new);
 7851:                         }
 7852:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7853:                     }
 7854:                 }
 7855:             } else {
 7856:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7857:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7858:                     $changes{'loginvia'}{$lonhost} = 1;
 7859:                     foreach my $item (@loginvia_attribs) {
 7860:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7861:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7862:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7863:                                 $new = '/';
 7864:                             }
 7865:                         }
 7866:                         if (($item eq 'custompath') && 
 7867:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7868:                             $new = '';
 7869:                         }
 7870:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7871:                     }
 7872:                 }
 7873:             }
 7874:         }
 7875:     }
 7876: 
 7877:     my $servadm = $r->dir_config('lonAdmEMail');
 7878:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7879:     if (ref($domconfig{'login'}) eq 'HASH') {
 7880:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7881:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7882:                 if ($lang eq 'nolang') {
 7883:                     push(@currlangs,$lang);
 7884:                 } elsif (defined($langchoices{$lang})) {
 7885:                     push(@currlangs,$lang);
 7886:                 } else {
 7887:                     next;
 7888:                 }
 7889:             }
 7890:         }
 7891:     }
 7892:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7893:     if (@currlangs > 0) {
 7894:         foreach my $lang (@currlangs) {
 7895:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7896:                 $changes{'helpurl'}{$lang} = 1;
 7897:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7898:                 $changes{'helpurl'}{$lang} = 1;
 7899:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7900:                 push(@newlangs,$lang);
 7901:             } else {
 7902:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7903:             }
 7904:         }
 7905:     }
 7906:     unless (grep(/^nolang$/,@currlangs)) {
 7907:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7908:             $changes{'helpurl'}{'nolang'} = 1;
 7909:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7910:             push(@newlangs,'nolang');
 7911:         }
 7912:     }
 7913:     if ($env{'form.loginhelpurl_add_lang'}) {
 7914:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7915:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7916:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7917:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7918:         }
 7919:     }
 7920:     if ((@newlangs > 0) || ($addedfile)) {
 7921:         my $error;
 7922:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7923:         if ($configuserok eq 'ok') {
 7924:             if ($switchserver) {
 7925:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7926:             } elsif ($author_ok eq 'ok') {
 7927:                 my @allnew = @newlangs;
 7928:                 if ($addedfile ne '') {
 7929:                     push(@allnew,$addedfile);
 7930:                 }
 7931:                 foreach my $lang (@allnew) {
 7932:                     my $formelem = 'loginhelpurl_'.$lang;
 7933:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7934:                         $formelem = 'loginhelpurl_add_file';
 7935:                     }
 7936:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7937:                                                                "help/$lang",'','',$newfile{$lang});
 7938:                     if ($result eq 'ok') {
 7939:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7940:                         $changes{'helpurl'}{$lang} = 1;
 7941:                     } else {
 7942:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7943:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7944:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7945:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7946:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7947:                         }
 7948:                     }
 7949:                 }
 7950:             } else {
 7951:                 $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);
 7952:             }
 7953:         } else {
 7954:             $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);
 7955:         }
 7956:         if ($error) {
 7957:             &Apache::lonnet::logthis($error);
 7958:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7959:         }
 7960:     }
 7961: 
 7962:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7963:     if (ref($domconfig{'login'}) eq 'HASH') {
 7964:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7965:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7966:                 if ($domservers{$lonhost}) {
 7967:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7968:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7969:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7970:                     }
 7971:                 }
 7972:             }
 7973:         }
 7974:     }
 7975:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7976:     foreach my $lonhost (sort(keys(%domservers))) {
 7977:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7978:             $changes{'headtag'}{$lonhost} = 1;
 7979:         } else {
 7980:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7981:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7982:             }
 7983:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7984:                 push(@newhosts,$lonhost);
 7985:             } elsif ($currheadtagurls{$lonhost}) {
 7986:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7987:                 if ($currexempt{$lonhost}) {
 7988:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7989:                         $changes{'headtag'}{$lonhost} = 1;
 7990:                     }
 7991:                 } elsif ($possexempt{$lonhost}) {
 7992:                     $changes{'headtag'}{$lonhost} = 1;
 7993:                 }
 7994:                 if ($possexempt{$lonhost}) {
 7995:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7996:                 }
 7997:             }
 7998:         }
 7999:     }
 8000:     if (@newhosts) {
 8001:         my $error;
 8002:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8003:         if ($configuserok eq 'ok') {
 8004:             if ($switchserver) {
 8005:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8006:             } elsif ($author_ok eq 'ok') {
 8007:                 foreach my $lonhost (@newhosts) {
 8008:                     my $formelem = 'loginheadtag_'.$lonhost;
 8009:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8010:                                                                           "login/headtag/$lonhost",'','',
 8011:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8012:                     if ($result eq 'ok') {
 8013:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8014:                         $changes{'headtag'}{$lonhost} = 1;
 8015:                         if ($possexempt{$lonhost}) {
 8016:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8017:                         }
 8018:                     } else {
 8019:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 8020:                                            $newheadtagurls{$lonhost},$result);
 8021:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8022:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 8023:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 8024:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 8025:                         }
 8026:                     }
 8027:                 }
 8028:             } else {
 8029:                 $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);
 8030:             }
 8031:         } else {
 8032:             $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);
 8033:         }
 8034:         if ($error) {
 8035:             &Apache::lonnet::logthis($error);
 8036:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8037:         }
 8038:     }
 8039:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 8040: 
 8041:     my $defaulthelpfile = '/adm/loginproblems.html';
 8042:     my $defaulttext = &mt('Default in use');
 8043: 
 8044:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 8045:                                              $dom);
 8046:     if ($putresult eq 'ok') {
 8047:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8048:         my %defaultchecked = (
 8049:                     'coursecatalog' => 'on',
 8050:                     'helpdesk'      => 'on',
 8051:                     'adminmail'     => 'off',
 8052:                     'newuser'       => 'off',
 8053:         );
 8054:         if (ref($domconfig{'login'}) eq 'HASH') {
 8055:             foreach my $item (@toggles) {
 8056:                 if ($defaultchecked{$item} eq 'on') { 
 8057:                     if (($domconfig{'login'}{$item} eq '0') &&
 8058:                         ($env{'form.'.$item} eq '1')) {
 8059:                         $changes{$item} = 1;
 8060:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8061:                               $domconfig{'login'}{$item} eq '1') &&
 8062:                              ($env{'form.'.$item} eq '0')) {
 8063:                         $changes{$item} = 1;
 8064:                     }
 8065:                 } elsif ($defaultchecked{$item} eq 'off') {
 8066:                     if (($domconfig{'login'}{$item} eq '1') &&
 8067:                         ($env{'form.'.$item} eq '0')) {
 8068:                         $changes{$item} = 1;
 8069:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8070:                               $domconfig{'login'}{$item} eq '0') &&
 8071:                              ($env{'form.'.$item} eq '1')) {
 8072:                         $changes{$item} = 1;
 8073:                     }
 8074:                 }
 8075:             }
 8076:         }
 8077:         if (keys(%changes) > 0 || $colchgtext) {
 8078:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8079:             if (ref($lastactref) eq 'HASH') {
 8080:                 $lastactref->{'domainconfig'} = 1;
 8081:             }
 8082:             $resulttext = &mt('Changes made:').'<ul>';
 8083:             foreach my $item (sort(keys(%changes))) {
 8084:                 if ($item eq 'loginvia') {
 8085:                     if (ref($changes{$item}) eq 'HASH') {
 8086:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 8087:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8088:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 8089:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 8090:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 8091:                                     $protocol = 'http' if ($protocol ne 'https');
 8092:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 8093: 
 8094:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 8095:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 8096:                                     } else {
 8097:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 8098:                                     }
 8099:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 8100:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 8101:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 8102:                                     }
 8103:                                     $resulttext .= '</li>';
 8104:                                 } else {
 8105:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 8106:                                 }
 8107:                             } else {
 8108:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 8109:                             }
 8110:                         }
 8111:                         $resulttext .= '</ul></li>';
 8112:                     }
 8113:                 } elsif ($item eq 'helpurl') {
 8114:                     if (ref($changes{$item}) eq 'HASH') {
 8115:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 8116:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 8117:                                 my ($chg,$link);
 8118:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 8119:                                 if ($lang eq 'nolang') {
 8120:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 8121:                                 } else {
 8122:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 8123:                                 }
 8124:                                 $resulttext .= '<li>'.$chg.'</li>';
 8125:                             } else {
 8126:                                 my $chg;
 8127:                                 if ($lang eq 'nolang') {
 8128:                                     $chg = &mt('custom log-in help file for no preferred language');
 8129:                                 } else {
 8130:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 8131:                                 }
 8132:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 8133:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 8134:                                                       '?inhibitmenu=yes',$chg,600,500).
 8135:                                                '</li>';
 8136:                             }
 8137:                         }
 8138:                     }
 8139:                 } elsif ($item eq 'headtag') {
 8140:                     if (ref($changes{$item}) eq 'HASH') {
 8141:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8142:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8143:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 8144:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8145:                                 $resulttext .= '<li><a href="'.
 8146:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 8147:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 8148:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 8149:                                 if ($possexempt{$lonhost}) {
 8150:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 8151:                                 } else {
 8152:                                     $resulttext .= &mt('included for any client IP');
 8153:                                 }
 8154:                                 $resulttext .= '</li>';
 8155:                             }
 8156:                         }
 8157:                     }
 8158:                 } elsif ($item eq 'captcha') {
 8159:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8160:                         my $chgtxt;
 8161:                         if ($loginhash{'login'}{$item} eq 'notused') {
 8162:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 8163:                         } else {
 8164:                             my %captchas = &captcha_phrases();
 8165:                             if ($captchas{$loginhash{'login'}{$item}}) {
 8166:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 8167:                             } else {
 8168:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 8169:                             }
 8170:                         }
 8171:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8172:                     }
 8173:                 } elsif ($item eq 'recaptchakeys') {
 8174:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8175:                         my ($privkey,$pubkey);
 8176:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 8177:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 8178:                             $privkey = $loginhash{'login'}{$item}{'private'};
 8179:                         }
 8180:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 8181:                         if (!$pubkey) {
 8182:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 8183:                         } else {
 8184:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8185:                         }
 8186:                         if (!$privkey) {
 8187:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 8188:                         } else {
 8189:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 8190:                         }
 8191:                         $chgtxt .= '</ul>';
 8192:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8193:                     }
 8194:                 } elsif ($item eq 'recaptchaversion') {
 8195:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8196:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 8197:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 8198:                                            '</li>';
 8199:                         }
 8200:                     }
 8201:                 } else {
 8202:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 8203:                 }
 8204:             }
 8205:             $resulttext .= $colchgtext.'</ul>';
 8206:         } else {
 8207:             $resulttext = &mt('No changes made to log-in page settings');
 8208:         }
 8209:     } else {
 8210:         $resulttext = '<span class="LC_error">'.
 8211: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8212:     }
 8213:     if ($errors) {
 8214:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8215:                        $errors.'</ul>';
 8216:     }
 8217:     return $resulttext;
 8218: }
 8219: 
 8220: sub check_exempt_addresses {
 8221:     my ($iplist) = @_;
 8222:     $iplist =~ s/^\s+//;
 8223:     $iplist =~ s/\s+$//;
 8224:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8225:     my (@okips,$new);
 8226:     foreach my $ip (@poss_ips) {
 8227:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8228:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8229:                 push(@okips,$ip);
 8230:             }
 8231:         }
 8232:     }
 8233:     if (@okips > 0) {
 8234:         $new = join(',',@okips);
 8235:     } else {
 8236:         $new = '';
 8237:     }
 8238:     return $new;
 8239: }
 8240: 
 8241: sub color_font_choices {
 8242:     my %choices =
 8243:         &Apache::lonlocal::texthash (
 8244:             img => "Header",
 8245:             bgs => "Background colors",
 8246:             links => "Link colors",
 8247:             images => "Images",
 8248:             font => "Font color",
 8249:             fontmenu => "Font menu",
 8250:             pgbg => "Page",
 8251:             tabbg => "Header",
 8252:             sidebg => "Border",
 8253:             link => "Link",
 8254:             alink => "Active link",
 8255:             vlink => "Visited link",
 8256:         );
 8257:     return %choices;
 8258: }
 8259: 
 8260: sub modify_rolecolors {
 8261:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8262:     my ($resulttext,%rolehash);
 8263:     $rolehash{'rolecolors'} = {};
 8264:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8265:         if ($domconfig{'rolecolors'} eq '') {
 8266:             $domconfig{'rolecolors'} = {};
 8267:         }
 8268:     }
 8269:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8270:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8271:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8272:                                              $dom);
 8273:     if ($putresult eq 'ok') {
 8274:         if (keys(%changes) > 0) {
 8275:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8276:             if (ref($lastactref) eq 'HASH') {
 8277:                 $lastactref->{'domainconfig'} = 1;
 8278:             }
 8279:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8280:                                              $rolehash{'rolecolors'});
 8281:         } else {
 8282:             $resulttext = &mt('No changes made to default color schemes');
 8283:         }
 8284:     } else {
 8285:         $resulttext = '<span class="LC_error">'.
 8286: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8287:     }
 8288:     if ($errors) {
 8289:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8290:                        $errors.'</ul>';
 8291:     }
 8292:     return $resulttext;
 8293: }
 8294: 
 8295: sub modify_colors {
 8296:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8297:     my (%changes,%choices);
 8298:     my @bgs;
 8299:     my @links = ('link','alink','vlink');
 8300:     my @logintext;
 8301:     my @images;
 8302:     my $servadm = $r->dir_config('lonAdmEMail');
 8303:     my $errors;
 8304:     my %defaults;
 8305:     foreach my $role (@{$roles}) {
 8306:         if ($role eq 'login') {
 8307:             %choices = &login_choices();
 8308:             @logintext = ('textcol','bgcol');
 8309:         } else {
 8310:             %choices = &color_font_choices();
 8311:         }
 8312:         if ($role eq 'login') {
 8313:             @images = ('img','logo','domlogo','login');
 8314:             @bgs = ('pgbg','mainbg','sidebg');
 8315:         } else {
 8316:             @images = ('img');
 8317:             @bgs = ('pgbg','tabbg','sidebg');
 8318:         }
 8319:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8320:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8321:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8322:         }
 8323:         if ($role eq 'login') {
 8324:             foreach my $item (@logintext) {
 8325:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8326:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8327:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8328:                 }
 8329:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8330:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8331:                 }
 8332:             }
 8333:         } else {
 8334:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8335:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8336:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8337:             }
 8338:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8339:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8340:             }
 8341:         }
 8342:         foreach my $item (@bgs) {
 8343:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8344:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8345:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8346:             }
 8347:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8348:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8349:             }
 8350:         }
 8351:         foreach my $item (@links) {
 8352:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8353:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8354:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8355:             }
 8356:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8357:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8358:             }
 8359:         }
 8360:         my ($configuserok,$author_ok,$switchserver) = 
 8361:             &config_check($dom,$confname,$servadm);
 8362:         my ($width,$height) = &thumb_dimensions();
 8363:         if (ref($domconfig->{$role}) ne 'HASH') {
 8364:             $domconfig->{$role} = {};
 8365:         }
 8366:         foreach my $img (@images) {
 8367:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8368:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8369:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8370:                 } else { 
 8371:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8372:                 }
 8373:             } 
 8374: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8375: 		 && !defined($domconfig->{$role}{$img})
 8376: 		 && !$env{'form.'.$role.'_del_'.$img}
 8377: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8378: 		# import the old configured image from the .tab setting
 8379: 		# if they haven't provided a new one 
 8380: 		$domconfig->{$role}{$img} = 
 8381: 		    $env{'form.'.$role.'_import_'.$img};
 8382: 	    }
 8383:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8384:                 my $error;
 8385:                 if ($configuserok eq 'ok') {
 8386:                     if ($switchserver) {
 8387:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8388:                     } else {
 8389:                         if ($author_ok eq 'ok') {
 8390:                             my ($result,$logourl) = 
 8391:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8392:                                            $dom,$confname,$img,$width,$height);
 8393:                             if ($result eq 'ok') {
 8394:                                 $confhash->{$role}{$img} = $logourl;
 8395:                                 $changes{$role}{'images'}{$img} = 1;
 8396:                             } else {
 8397:                                 $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);
 8398:                             }
 8399:                         } else {
 8400:                             $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);
 8401:                         }
 8402:                     }
 8403:                 } else {
 8404:                     $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);
 8405:                 }
 8406:                 if ($error) {
 8407:                     &Apache::lonnet::logthis($error);
 8408:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8409:                 }
 8410:             } elsif ($domconfig->{$role}{$img} ne '') {
 8411:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8412:                     my $error;
 8413:                     if ($configuserok eq 'ok') {
 8414: # is confname an author?
 8415:                         if ($switchserver eq '') {
 8416:                             if ($author_ok eq 'ok') {
 8417:                                 my ($result,$logourl) = 
 8418:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8419:                                             $dom,$confname,$img,$width,$height);
 8420:                                 if ($result eq 'ok') {
 8421:                                     $confhash->{$role}{$img} = $logourl;
 8422: 				    $changes{$role}{'images'}{$img} = 1;
 8423:                                 }
 8424:                             }
 8425:                         }
 8426:                     }
 8427:                 }
 8428:             }
 8429:         }
 8430:         if (ref($domconfig) eq 'HASH') {
 8431:             if (ref($domconfig->{$role}) eq 'HASH') {
 8432:                 foreach my $img (@images) {
 8433:                     if ($domconfig->{$role}{$img} ne '') {
 8434:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8435:                             $confhash->{$role}{$img} = '';
 8436:                             $changes{$role}{'images'}{$img} = 1;
 8437:                         } else {
 8438:                             if ($confhash->{$role}{$img} eq '') {
 8439:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8440:                             }
 8441:                         }
 8442:                     } else {
 8443:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8444:                             $confhash->{$role}{$img} = '';
 8445:                             $changes{$role}{'images'}{$img} = 1;
 8446:                         } 
 8447:                     }
 8448:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8449:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8450:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8451:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8452:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8453:                             }
 8454:                         } else {
 8455:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8456:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8457:                             }
 8458:                         }
 8459:                     }
 8460:                 }
 8461:                 if ($domconfig->{$role}{'font'} ne '') {
 8462:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8463:                         $changes{$role}{'font'} = 1;
 8464:                     }
 8465:                 } else {
 8466:                     if ($confhash->{$role}{'font'}) {
 8467:                         $changes{$role}{'font'} = 1;
 8468:                     }
 8469:                 }
 8470:                 if ($role ne 'login') {
 8471:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8472:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8473:                             $changes{$role}{'fontmenu'} = 1;
 8474:                         }
 8475:                     } else {
 8476:                         if ($confhash->{$role}{'fontmenu'}) {
 8477:                             $changes{$role}{'fontmenu'} = 1;
 8478:                         }
 8479:                     }
 8480:                 }
 8481:                 foreach my $item (@bgs) {
 8482:                     if ($domconfig->{$role}{$item} ne '') {
 8483:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8484:                             $changes{$role}{'bgs'}{$item} = 1;
 8485:                         } 
 8486:                     } else {
 8487:                         if ($confhash->{$role}{$item}) {
 8488:                             $changes{$role}{'bgs'}{$item} = 1;
 8489:                         }
 8490:                     }
 8491:                 }
 8492:                 foreach my $item (@links) {
 8493:                     if ($domconfig->{$role}{$item} ne '') {
 8494:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8495:                             $changes{$role}{'links'}{$item} = 1;
 8496:                         }
 8497:                     } else {
 8498:                         if ($confhash->{$role}{$item}) {
 8499:                             $changes{$role}{'links'}{$item} = 1;
 8500:                         }
 8501:                     }
 8502:                 }
 8503:                 foreach my $item (@logintext) {
 8504:                     if ($domconfig->{$role}{$item} ne '') {
 8505:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8506:                             $changes{$role}{'logintext'}{$item} = 1;
 8507:                         }
 8508:                     } else {
 8509:                         if ($confhash->{$role}{$item}) {
 8510:                             $changes{$role}{'logintext'}{$item} = 1;
 8511:                         }
 8512:                     }
 8513:                 }
 8514:             } else {
 8515:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8516:                                         \@logintext,$confhash,\%changes); 
 8517:             }
 8518:         } else {
 8519:             &default_change_checker($role,\@images,\@links,\@bgs,
 8520:                                     \@logintext,$confhash,\%changes); 
 8521:         }
 8522:     }
 8523:     return ($errors,%changes);
 8524: }
 8525: 
 8526: sub config_check {
 8527:     my ($dom,$confname,$servadm) = @_;
 8528:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8529:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8530:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8531:                                                    $confname,$servadm);
 8532:     if ($configuserok eq 'ok') {
 8533:         $switchserver = &check_switchserver($dom,$confname);
 8534:         if ($switchserver eq '') {
 8535:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8536:         }
 8537:     }
 8538:     return ($configuserok,$author_ok,$switchserver);
 8539: }
 8540: 
 8541: sub default_change_checker {
 8542:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8543:     foreach my $item (@{$links}) {
 8544:         if ($confhash->{$role}{$item}) {
 8545:             $changes->{$role}{'links'}{$item} = 1;
 8546:         }
 8547:     }
 8548:     foreach my $item (@{$bgs}) {
 8549:         if ($confhash->{$role}{$item}) {
 8550:             $changes->{$role}{'bgs'}{$item} = 1;
 8551:         }
 8552:     }
 8553:     foreach my $item (@{$logintext}) {
 8554:         if ($confhash->{$role}{$item}) {
 8555:             $changes->{$role}{'logintext'}{$item} = 1;
 8556:         }
 8557:     }
 8558:     foreach my $img (@{$images}) {
 8559:         if ($env{'form.'.$role.'_del_'.$img}) {
 8560:             $confhash->{$role}{$img} = '';
 8561:             $changes->{$role}{'images'}{$img} = 1;
 8562:         }
 8563:         if ($role eq 'login') {
 8564:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8565:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8566:             }
 8567:         }
 8568:     }
 8569:     if ($confhash->{$role}{'font'}) {
 8570:         $changes->{$role}{'font'} = 1;
 8571:     }
 8572: }
 8573: 
 8574: sub display_colorchgs {
 8575:     my ($dom,$changes,$roles,$confhash) = @_;
 8576:     my (%choices,$resulttext);
 8577:     if (!grep(/^login$/,@{$roles})) {
 8578:         $resulttext = &mt('Changes made:').'<br />';
 8579:     }
 8580:     foreach my $role (@{$roles}) {
 8581:         if ($role eq 'login') {
 8582:             %choices = &login_choices();
 8583:         } else {
 8584:             %choices = &color_font_choices();
 8585:         }
 8586:         if (ref($changes->{$role}) eq 'HASH') {
 8587:             if ($role ne 'login') {
 8588:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8589:             }
 8590:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8591:                 if ($role ne 'login') {
 8592:                     $resulttext .= '<ul>';
 8593:                 }
 8594:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8595:                     if ($role ne 'login') {
 8596:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8597:                     }
 8598:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8599:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8600:                             if ($confhash->{$role}{$key}{$item}) {
 8601:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8602:                             } else {
 8603:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8604:                             }
 8605:                         } elsif ($confhash->{$role}{$item} eq '') {
 8606:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8607:                         } else {
 8608:                             my $newitem = $confhash->{$role}{$item};
 8609:                             if ($key eq 'images') {
 8610:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8611:                             }
 8612:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8613:                         }
 8614:                     }
 8615:                     if ($role ne 'login') {
 8616:                         $resulttext .= '</ul></li>';
 8617:                     }
 8618:                 } else {
 8619:                     if ($confhash->{$role}{$key} eq '') {
 8620:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8621:                     } else {
 8622:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8623:                     }
 8624:                 }
 8625:                 if ($role ne 'login') {
 8626:                     $resulttext .= '</ul>';
 8627:                 }
 8628:             }
 8629:         }
 8630:     }
 8631:     return $resulttext;
 8632: }
 8633: 
 8634: sub thumb_dimensions {
 8635:     return ('200','50');
 8636: }
 8637: 
 8638: sub check_dimensions {
 8639:     my ($inputfile) = @_;
 8640:     my ($fullwidth,$fullheight);
 8641:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8642:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8643:             my $imageinfo = <PIPE>;
 8644:             if (!close(PIPE)) {
 8645:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8646:             }
 8647:             chomp($imageinfo);
 8648:             my ($fullsize) = 
 8649:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8650:             if ($fullsize) {
 8651:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8652:             }
 8653:         }
 8654:     }
 8655:     return ($fullwidth,$fullheight);
 8656: }
 8657: 
 8658: sub check_configuser {
 8659:     my ($uhome,$dom,$confname,$servadm) = @_;
 8660:     my ($configuserok,%currroles);
 8661:     if ($uhome eq 'no_host') {
 8662:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8663:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
 8664:         $configuserok = 
 8665:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8666:                              $configpass,'','','','','',undef,$servadm);
 8667:     } else {
 8668:         $configuserok = 'ok';
 8669:         %currroles = 
 8670:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8671:     }
 8672:     return ($configuserok,%currroles);
 8673: }
 8674: 
 8675: sub check_authorstatus {
 8676:     my ($dom,$confname,%currroles) = @_;
 8677:     my $author_ok;
 8678:     if (!$currroles{':'.$dom.':au'}) {
 8679:         my $start = time;
 8680:         my $end = 0;
 8681:         $author_ok = 
 8682:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8683:                                         'au',$end,$start,'','','domconfig');
 8684:     } else {
 8685:         $author_ok = 'ok';
 8686:     }
 8687:     return $author_ok;
 8688: }
 8689: 
 8690: sub publishlogo {
 8691:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8692:     my ($output,$fname,$logourl);
 8693:     if ($action eq 'upload') {
 8694:         $fname=$env{'form.'.$formname.'.filename'};
 8695:         chop($env{'form.'.$formname});
 8696:     } else {
 8697:         ($fname) = ($formname =~ /([^\/]+)$/);
 8698:     }
 8699:     if ($savefileas ne '') {
 8700:         $fname = $savefileas;
 8701:     }
 8702:     $fname=&Apache::lonnet::clean_filename($fname);
 8703: # See if there is anything left
 8704:     unless ($fname) { return ('error: no uploaded file'); }
 8705:     $fname="$subdir/$fname";
 8706:     my $docroot=$r->dir_config('lonDocRoot');
 8707:     my $filepath="$docroot/priv";
 8708:     my $relpath = "$dom/$confname";
 8709:     my ($fnamepath,$file,$fetchthumb);
 8710:     $file=$fname;
 8711:     if ($fname=~m|/|) {
 8712:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8713:     }
 8714:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8715:     my $count;
 8716:     for ($count=5;$count<=$#parts;$count++) {
 8717:         $filepath.="/$parts[$count]";
 8718:         if ((-e $filepath)!=1) {
 8719:             mkdir($filepath,02770);
 8720:         }
 8721:     }
 8722:     # Check for bad extension and disallow upload
 8723:     if ($file=~/\.(\w+)$/ &&
 8724:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8725:         $output = 
 8726:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8727:     } elsif ($file=~/\.(\w+)$/ &&
 8728:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8729:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8730:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8731:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8732:     } elsif (-d "$filepath/$file") {
 8733:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8734:     } else {
 8735:         my $source = $filepath.'/'.$file;
 8736:         my $logfile;
 8737:         if (!open($logfile,">>",$source.'.log')) {
 8738:             return (&mt('No write permission to Authoring Space'));
 8739:         }
 8740:         print $logfile
 8741: "\n================= Publish ".localtime()." ================\n".
 8742: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8743: # Save the file
 8744:         if (!open(FH,">",$source)) {
 8745:             &Apache::lonnet::logthis('Failed to create '.$source);
 8746:             return (&mt('Failed to create file'));
 8747:         }
 8748:         if ($action eq 'upload') {
 8749:             if (!print FH ($env{'form.'.$formname})) {
 8750:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8751:                 return (&mt('Failed to write file'));
 8752:             }
 8753:         } else {
 8754:             my $original = &Apache::lonnet::filelocation('',$formname);
 8755:             if(!copy($original,$source)) {
 8756:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8757:                 return (&mt('Failed to write file'));
 8758:             }
 8759:         }
 8760:         close(FH);
 8761:         chmod(0660, $source); # Permissions to rw-rw---.
 8762: 
 8763:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8764:         my $copyfile=$targetdir.'/'.$file;
 8765: 
 8766:         my @parts=split(/\//,$targetdir);
 8767:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8768:         for (my $count=5;$count<=$#parts;$count++) {
 8769:             $path.="/$parts[$count]";
 8770:             if (!-e $path) {
 8771:                 print $logfile "\nCreating directory ".$path;
 8772:                 mkdir($path,02770);
 8773:             }
 8774:         }
 8775:         my $versionresult;
 8776:         if (-e $copyfile) {
 8777:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8778:         } else {
 8779:             $versionresult = 'ok';
 8780:         }
 8781:         if ($versionresult eq 'ok') {
 8782:             if (copy($source,$copyfile)) {
 8783:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8784:                 $output = 'ok';
 8785:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8786:                 push(@{$modified_urls},[$copyfile,$source]);
 8787:                 my $metaoutput = 
 8788:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8789:                 unless ($registered_cleanup) {
 8790:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8791:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8792:                     $registered_cleanup=1;
 8793:                 }
 8794:             } else {
 8795:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8796:                 $output = &mt('Failed to copy file to RES space').", $!";
 8797:             }
 8798:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8799:                 my $inputfile = $filepath.'/'.$file;
 8800:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8801:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8802:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8803:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8804:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8805:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 8806:                         system({$args[0]} @args);
 8807:                         chmod(0660, $filepath.'/tn-'.$file);
 8808:                         if (-e $outfile) {
 8809:                             my $copyfile=$targetdir.'/tn-'.$file;
 8810:                             if (copy($outfile,$copyfile)) {
 8811:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8812:                                 my $thumb_metaoutput = 
 8813:                                     &write_metadata($dom,$confname,$formname,
 8814:                                                     $targetdir,'tn-'.$file,$logfile);
 8815:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8816:                                 unless ($registered_cleanup) {
 8817:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8818:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8819:                                     $registered_cleanup=1;
 8820:                                 }
 8821:                             } else {
 8822:                                 print $logfile "\nUnable to write ".$copyfile.
 8823:                                                ':'.$!."\n";
 8824:                             }
 8825:                         }
 8826:                     }
 8827:                 }
 8828:             }
 8829:         } else {
 8830:             $output = $versionresult;
 8831:         }
 8832:     }
 8833:     return ($output,$logourl);
 8834: }
 8835: 
 8836: sub logo_versioning {
 8837:     my ($targetdir,$file,$logfile) = @_;
 8838:     my $target = $targetdir.'/'.$file;
 8839:     my ($maxversion,$fn,$extn,$output);
 8840:     $maxversion = 0;
 8841:     if ($file =~ /^(.+)\.(\w+)$/) {
 8842:         $fn=$1;
 8843:         $extn=$2;
 8844:     }
 8845:     opendir(DIR,$targetdir);
 8846:     while (my $filename=readdir(DIR)) {
 8847:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8848:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8849:         }
 8850:     }
 8851:     $maxversion++;
 8852:     print $logfile "\nCreating old version ".$maxversion."\n";
 8853:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8854:     if (copy($target,$copyfile)) {
 8855:         print $logfile "Copied old target to ".$copyfile."\n";
 8856:         $copyfile=$copyfile.'.meta';
 8857:         if (copy($target.'.meta',$copyfile)) {
 8858:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8859:             $output = 'ok';
 8860:         } else {
 8861:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8862:             $output = &mt('Failed to copy old meta').", $!, ";
 8863:         }
 8864:     } else {
 8865:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8866:         $output = &mt('Failed to copy old target').", $!, ";
 8867:     }
 8868:     return $output;
 8869: }
 8870: 
 8871: sub write_metadata {
 8872:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8873:     my (%metadatafields,%metadatakeys,$output);
 8874:     $metadatafields{'title'}=$formname;
 8875:     $metadatafields{'creationdate'}=time;
 8876:     $metadatafields{'lastrevisiondate'}=time;
 8877:     $metadatafields{'copyright'}='public';
 8878:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8879:                                          $env{'user.domain'};
 8880:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8881:     $metadatafields{'domain'}=$dom;
 8882:     {
 8883:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8884:         my $mfh;
 8885:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 8886:             foreach (sort(keys(%metadatafields))) {
 8887:                 unless ($_=~/\./) {
 8888:                     my $unikey=$_;
 8889:                     $unikey=~/^([A-Za-z]+)/;
 8890:                     my $tag=$1;
 8891:                     $tag=~tr/A-Z/a-z/;
 8892:                     print $mfh "\n\<$tag";
 8893:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8894:                         my $value=$metadatafields{$unikey.'.'.$_};
 8895:                         $value=~s/\"/\'\'/g;
 8896:                         print $mfh ' '.$_.'="'.$value.'"';
 8897:                     }
 8898:                     print $mfh '>'.
 8899:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8900:                             .'</'.$tag.'>';
 8901:                 }
 8902:             }
 8903:             $output = 'ok';
 8904:             print $logfile "\nWrote metadata";
 8905:             close($mfh);
 8906:         } else {
 8907:             print $logfile "\nFailed to open metadata file";
 8908:             $output = &mt('Could not write metadata');
 8909:         }
 8910:     }
 8911:     return $output;
 8912: }
 8913: 
 8914: sub notifysubscribed {
 8915:     foreach my $targetsource (@{$modified_urls}){
 8916:         next unless (ref($targetsource) eq 'ARRAY');
 8917:         my ($target,$source)=@{$targetsource};
 8918:         if ($source ne '') {
 8919:             if (open(my $logfh,">>",$source.'.log')) {
 8920:                 print $logfh "\nCleanup phase: Notifications\n";
 8921:                 my @subscribed=&subscribed_hosts($target);
 8922:                 foreach my $subhost (@subscribed) {
 8923:                     print $logfh "\nNotifying host ".$subhost.':';
 8924:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8925:                     print $logfh $reply;
 8926:                 }
 8927:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8928:                 foreach my $subhost (@subscribedmeta) {
 8929:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8930:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8931:                                                         $subhost);
 8932:                     print $logfh $reply;
 8933:                 }
 8934:                 print $logfh "\n============ Done ============\n";
 8935:                 close($logfh);
 8936:             }
 8937:         }
 8938:     }
 8939:     return OK;
 8940: }
 8941: 
 8942: sub subscribed_hosts {
 8943:     my ($target) = @_;
 8944:     my @subscribed;
 8945:     if (open(my $fh,"<","$target.subscription")) {
 8946:         while (my $subline=<$fh>) {
 8947:             if ($subline =~ /^($match_lonid):/) {
 8948:                 my $host = $1;
 8949:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8950:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8951:                         push(@subscribed,$host);
 8952:                     }
 8953:                 }
 8954:             }
 8955:         }
 8956:     }
 8957:     return @subscribed;
 8958: }
 8959: 
 8960: sub check_switchserver {
 8961:     my ($dom,$confname) = @_;
 8962:     my ($allowed,$switchserver);
 8963:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8964:     if ($home eq 'no_host') {
 8965:         $home = &Apache::lonnet::domain($dom,'primary');
 8966:     }
 8967:     my @ids=&Apache::lonnet::current_machine_ids();
 8968:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8969:     if (!$allowed) {
 8970: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8971:     }
 8972:     return $switchserver;
 8973: }
 8974: 
 8975: sub modify_quotas {
 8976:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8977:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8978:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8979:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8980:         $validationfieldsref);
 8981:     if ($action eq 'quotas') {
 8982:         $context = 'tools'; 
 8983:     } else {
 8984:         $context = $action;
 8985:     }
 8986:     if ($context eq 'requestcourses') {
 8987:         @usertools = ('official','unofficial','community','textbook');
 8988:         @options =('norequest','approval','validate','autolimit');
 8989:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8990:         %titles = &courserequest_titles();
 8991:         $toolregexp = join('|',@usertools);
 8992:         %conditions = &courserequest_conditions();
 8993:         $confname = $dom.'-domainconfig';
 8994:         my $servadm = $r->dir_config('lonAdmEMail');
 8995:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8996:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8997:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8998:     } elsif ($context eq 'requestauthor') {
 8999:         @usertools = ('author');
 9000:         %titles = &authorrequest_titles();
 9001:     } else {
 9002:         @usertools = ('aboutme','blog','webdav','portfolio');
 9003:         %titles = &tool_titles();
 9004:     }
 9005:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9006:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9007:     foreach my $key (keys(%env)) {
 9008:         if ($context eq 'requestcourses') {
 9009:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9010:                 my $item = $1;
 9011:                 my $type = $2;
 9012:                 if ($type =~ /^limit_(.+)/) {
 9013:                     $limithash{$item}{$1} = $env{$key};
 9014:                 } else {
 9015:                     $confhash{$item}{$type} = $env{$key};
 9016:                 }
 9017:             }
 9018:         } elsif ($context eq 'requestauthor') {
 9019:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 9020:                 $confhash{$1} = $env{$key};
 9021:             }
 9022:         } else {
 9023:             if ($key =~ /^form\.quota_(.+)$/) {
 9024:                 $confhash{'defaultquota'}{$1} = $env{$key};
 9025:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 9026:                 $confhash{'authorquota'}{$1} = $env{$key};
 9027:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 9028:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 9029:             }
 9030:         }
 9031:     }
 9032:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9033:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 9034:         @approvalnotify = sort(@approvalnotify);
 9035:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 9036:         my @crstypes = ('official','unofficial','community','textbook');
 9037:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 9038:         foreach my $type (@hasuniquecode) {
 9039:             if (grep(/^\Q$type\E$/,@crstypes)) {
 9040:                 $confhash{'uniquecode'}{$type} = 1;
 9041:             }
 9042:         }
 9043:         my (%newbook,%allpos);
 9044:         if ($context eq 'requestcourses') {
 9045:             foreach my $type ('textbooks','templates') {
 9046:                 @{$allpos{$type}} = (); 
 9047:                 my $invalid;
 9048:                 if ($type eq 'textbooks') {
 9049:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 9050:                 } else {
 9051:                     $invalid = &mt('Invalid LON-CAPA course for template');
 9052:                 }
 9053:                 if ($env{'form.'.$type.'_addbook'}) {
 9054:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 9055:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 9056:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 9057:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 9058:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9059:                         } else {
 9060:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 9061:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 9062:                             $position =~ s/\D+//g;
 9063:                             if ($position ne '') {
 9064:                                 $allpos{$type}[$position] = $newbook{$type};
 9065:                             }
 9066:                         }
 9067:                     } else {
 9068:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9069:                     }
 9070:                 }
 9071:             } 
 9072:         }
 9073:         if (ref($domconfig{$action}) eq 'HASH') {
 9074:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 9075:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 9076:                     $changes{'notify'}{'approval'} = 1;
 9077:                 }
 9078:             } else {
 9079:                 if ($confhash{'notify'}{'approval'}) {
 9080:                     $changes{'notify'}{'approval'} = 1;
 9081:                 }
 9082:             }
 9083:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 9084:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9085:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 9086:                         unless ($confhash{'uniquecode'}{$crstype}) {
 9087:                             $changes{'uniquecode'} = 1;
 9088:                         }
 9089:                     }
 9090:                     unless ($changes{'uniquecode'}) {
 9091:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 9092:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 9093:                                 $changes{'uniquecode'} = 1;
 9094:                             }
 9095:                         }
 9096:                     }
 9097:                } else {
 9098:                    $changes{'uniquecode'} = 1;
 9099:                }
 9100:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 9101:                 $changes{'uniquecode'} = 1;
 9102:             }
 9103:             if ($context eq 'requestcourses') {
 9104:                 foreach my $type ('textbooks','templates') {
 9105:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9106:                         my %deletions;
 9107:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 9108:                         if (@todelete) {
 9109:                             map { $deletions{$_} = 1; } @todelete;
 9110:                         }
 9111:                         my %imgdeletions;
 9112:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 9113:                         if (@todeleteimages) {
 9114:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9115:                         }
 9116:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 9117:                         for (my $i=0; $i<=$maxnum; $i++) {
 9118:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 9119:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 9120:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 9121:                                 if ($deletions{$key}) {
 9122:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 9123:                                         #FIXME need to obsolete item in RES space
 9124:                                     }
 9125:                                     next;
 9126:                                 } else {
 9127:                                     my $newpos = $env{'form.'.$itemid};
 9128:                                     $newpos =~ s/\D+//g;
 9129:                                     foreach my $item ('subject','title','publisher','author') {
 9130:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9131:                                                  ($type eq 'templates'));
 9132:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 9133:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 9134:                                             $changes{$type}{$key} = 1;
 9135:                                         }
 9136:                                     }
 9137:                                     $allpos{$type}[$newpos] = $key;
 9138:                                 }
 9139:                                 if ($imgdeletions{$key}) {
 9140:                                     $changes{$type}{$key} = 1;
 9141:                                     #FIXME need to obsolete item in RES space
 9142:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 9143:                                     my ($cdom,$cnum) = split(/_/,$key);
 9144:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9145:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9146:                                     } else {
 9147:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 9148:                                                                                       $cdom,$cnum,$type,$configuserok,
 9149:                                                                                       $switchserver,$author_ok);
 9150:                                         if ($imgurl) {
 9151:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 9152:                                             $changes{$type}{$key} = 1; 
 9153:                                         }
 9154:                                         if ($error) {
 9155:                                             &Apache::lonnet::logthis($error);
 9156:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9157:                                         }
 9158:                                     } 
 9159:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 9160:                                     $confhash{$type}{$key}{'image'} = 
 9161:                                         $domconfig{$action}{$type}{$key}{'image'};
 9162:                                 }
 9163:                             }
 9164:                         }
 9165:                     }
 9166:                 }
 9167:             }
 9168:         } else {
 9169:             if ($confhash{'notify'}{'approval'}) {
 9170:                 $changes{'notify'}{'approval'} = 1;
 9171:             }
 9172:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 9173:                 $changes{'uniquecode'} = 1;
 9174:             }
 9175:         }
 9176:         if ($context eq 'requestcourses') {
 9177:             foreach my $type ('textbooks','templates') {
 9178:                 if ($newbook{$type}) {
 9179:                     $changes{$type}{$newbook{$type}} = 1;
 9180:                     foreach my $item ('subject','title','publisher','author') {
 9181:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9182:                                  ($type eq 'template'));
 9183:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 9184:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 9185:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 9186:                         }
 9187:                     }
 9188:                     if ($type eq 'textbooks') {
 9189:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 9190:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 9191:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9192:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9193:                             } else {
 9194:                                 my ($imageurl,$error) =
 9195:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 9196:                                                             $configuserok,$switchserver,$author_ok);
 9197:                                 if ($imageurl) {
 9198:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 9199:                                 }
 9200:                                 if ($error) {
 9201:                                     &Apache::lonnet::logthis($error);
 9202:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9203:                                 }
 9204:                             }
 9205:                         }
 9206:                     }
 9207:                 }
 9208:                 if (@{$allpos{$type}} > 0) {
 9209:                     my $idx = 0;
 9210:                     foreach my $item (@{$allpos{$type}}) {
 9211:                         if ($item ne '') {
 9212:                             $confhash{$type}{$item}{'order'} = $idx;
 9213:                             if (ref($domconfig{$action}) eq 'HASH') {
 9214:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9215:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 9216:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 9217:                                             $changes{$type}{$item} = 1;
 9218:                                         }
 9219:                                     }
 9220:                                 }
 9221:                             }
 9222:                             $idx ++;
 9223:                         }
 9224:                     }
 9225:                 }
 9226:             }
 9227:             if (ref($validationitemsref) eq 'ARRAY') {
 9228:                 foreach my $item (@{$validationitemsref}) {
 9229:                     if ($item eq 'fields') {
 9230:                         my @changed;
 9231:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9232:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9233:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9234:                         }
 9235:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9236:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9237:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9238:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9239:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9240:                                 } else {
 9241:                                     @changed = @{$confhash{'validation'}{$item}};
 9242:                                 }
 9243:                             } else {
 9244:                                 @changed = @{$confhash{'validation'}{$item}};
 9245:                             }
 9246:                         } else {
 9247:                             @changed = @{$confhash{'validation'}{$item}};
 9248:                         }
 9249:                         if (@changed) {
 9250:                             if ($confhash{'validation'}{$item}) {
 9251:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9252:                             } else {
 9253:                                 $changes{'validation'}{$item} = &mt('None');
 9254:                             }
 9255:                         }
 9256:                     } else {
 9257:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9258:                         if ($item eq 'markup') {
 9259:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9260:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9261:                             }
 9262:                         }
 9263:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9264:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9265:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9266:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9267:                                 }
 9268:                             } else {
 9269:                                 if ($confhash{'validation'}{$item} ne '') {
 9270:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9271:                                 }
 9272:                             }
 9273:                         } else {
 9274:                             if ($confhash{'validation'}{$item} ne '') {
 9275:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9276:                             }
 9277:                         }
 9278:                     }
 9279:                 }
 9280:             }
 9281:             if ($env{'form.validationdc'}) {
 9282:                 my $newval = $env{'form.validationdc'};
 9283:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9284:                 if (exists($domcoords{$newval})) {
 9285:                     $confhash{'validation'}{'dc'} = $newval;
 9286:                 }
 9287:             }
 9288:             if (ref($confhash{'validation'}) eq 'HASH') {
 9289:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9290:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9291:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9292:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9293:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9294:                                     $changes{'validation'}{'dc'} = &mt('None');
 9295:                                 } else {
 9296:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9297:                                 }
 9298:                             }
 9299:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9300:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9301:                         }
 9302:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9303:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9304:                     }
 9305:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9306:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9307:                 }
 9308:             } else {
 9309:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9310:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9311:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9312:                             $changes{'validation'}{'dc'} = &mt('None');
 9313:                         }
 9314:                     }
 9315:                 }
 9316:             }
 9317:         }
 9318:     } else {
 9319:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9320:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9321:     }
 9322:     foreach my $item (@usertools) {
 9323:         foreach my $type (@{$types},'default','_LC_adv') {
 9324:             my $unset; 
 9325:             if ($context eq 'requestcourses') {
 9326:                 $unset = '0';
 9327:                 if ($type eq '_LC_adv') {
 9328:                     $unset = '';
 9329:                 }
 9330:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9331:                     $confhash{$item}{$type} .= '=';
 9332:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9333:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9334:                     }
 9335:                 }
 9336:             } elsif ($context eq 'requestauthor') {
 9337:                 $unset = '0';
 9338:                 if ($type eq '_LC_adv') {
 9339:                     $unset = '';
 9340:                 }
 9341:             } else {
 9342:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9343:                     $confhash{$item}{$type} = 1;
 9344:                 } else {
 9345:                     $confhash{$item}{$type} = 0;
 9346:                 }
 9347:             }
 9348:             if (ref($domconfig{$action}) eq 'HASH') {
 9349:                 if ($action eq 'requestauthor') {
 9350:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9351:                         $changes{$type} = 1;
 9352:                     }
 9353:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9354:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9355:                         $changes{$item}{$type} = 1;
 9356:                     }
 9357:                 } else {
 9358:                     if ($context eq 'requestcourses') {
 9359:                         if ($confhash{$item}{$type} ne $unset) {
 9360:                             $changes{$item}{$type} = 1;
 9361:                         }
 9362:                     } else {
 9363:                         if (!$confhash{$item}{$type}) {
 9364:                             $changes{$item}{$type} = 1;
 9365:                         }
 9366:                     }
 9367:                 }
 9368:             } else {
 9369:                 if ($context eq 'requestcourses') {
 9370:                     if ($confhash{$item}{$type} ne $unset) {
 9371:                         $changes{$item}{$type} = 1;
 9372:                     }
 9373:                 } elsif ($context eq 'requestauthor') {
 9374:                     if ($confhash{$type} ne $unset) {
 9375:                         $changes{$type} = 1;
 9376:                     }
 9377:                 } else {
 9378:                     if (!$confhash{$item}{$type}) {
 9379:                         $changes{$item}{$type} = 1;
 9380:                     }
 9381:                 }
 9382:             }
 9383:         }
 9384:     }
 9385:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9386:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9387:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9388:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9389:                     if (exists($confhash{'defaultquota'}{$key})) {
 9390:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9391:                             $changes{'defaultquota'}{$key} = 1;
 9392:                         }
 9393:                     } else {
 9394:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9395:                     }
 9396:                 }
 9397:             } else {
 9398:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9399:                     if (exists($confhash{'defaultquota'}{$key})) {
 9400:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9401:                             $changes{'defaultquota'}{$key} = 1;
 9402:                         }
 9403:                     } else {
 9404:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9405:                     }
 9406:                 }
 9407:             }
 9408:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9409:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9410:                     if (exists($confhash{'authorquota'}{$key})) {
 9411:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9412:                             $changes{'authorquota'}{$key} = 1;
 9413:                         }
 9414:                     } else {
 9415:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9416:                     }
 9417:                 }
 9418:             }
 9419:         }
 9420:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9421:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9422:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9423:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9424:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9425:                             $changes{'defaultquota'}{$key} = 1;
 9426:                         }
 9427:                     } else {
 9428:                         if (!exists($domconfig{'quotas'}{$key})) {
 9429:                             $changes{'defaultquota'}{$key} = 1;
 9430:                         }
 9431:                     }
 9432:                 } else {
 9433:                     $changes{'defaultquota'}{$key} = 1;
 9434:                 }
 9435:             }
 9436:         }
 9437:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9438:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9439:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9440:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9441:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9442:                             $changes{'authorquota'}{$key} = 1;
 9443:                         }
 9444:                     } else {
 9445:                         $changes{'authorquota'}{$key} = 1;
 9446:                     }
 9447:                 } else {
 9448:                     $changes{'authorquota'}{$key} = 1;
 9449:                 }
 9450:             }
 9451:         }
 9452:     }
 9453: 
 9454:     if ($context eq 'requestauthor') {
 9455:         $domdefaults{'requestauthor'} = \%confhash;
 9456:     } else {
 9457:         foreach my $key (keys(%confhash)) {
 9458:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9459:                 $domdefaults{$key} = $confhash{$key};
 9460:             }
 9461:         }
 9462:     }
 9463: 
 9464:     my %quotahash = (
 9465:                       $action => { %confhash }
 9466:                     );
 9467:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9468:                                              $dom);
 9469:     if ($putresult eq 'ok') {
 9470:         if (keys(%changes) > 0) {
 9471:             my $cachetime = 24*60*60;
 9472:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9473:             if (ref($lastactref) eq 'HASH') {
 9474:                 $lastactref->{'domdefaults'} = 1;
 9475:             }
 9476:             $resulttext = &mt('Changes made:').'<ul>';
 9477:             unless (($context eq 'requestcourses') ||
 9478:                     ($context eq 'requestauthor')) {
 9479:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9480:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9481:                     foreach my $type (@{$types},'default') {
 9482:                         if (defined($changes{'defaultquota'}{$type})) {
 9483:                             my $typetitle = $usertypes->{$type};
 9484:                             if ($type eq 'default') {
 9485:                                 $typetitle = $othertitle;
 9486:                             }
 9487:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9488:                         }
 9489:                     }
 9490:                     $resulttext .= '</ul></li>';
 9491:                 }
 9492:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9493:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9494:                     foreach my $type (@{$types},'default') {
 9495:                         if (defined($changes{'authorquota'}{$type})) {
 9496:                             my $typetitle = $usertypes->{$type};
 9497:                             if ($type eq 'default') {
 9498:                                 $typetitle = $othertitle;
 9499:                             }
 9500:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9501:                         }
 9502:                     }
 9503:                     $resulttext .= '</ul></li>';
 9504:                 }
 9505:             }
 9506:             my %newenv;
 9507:             foreach my $item (@usertools) {
 9508:                 my (%haschgs,%inconf);
 9509:                 if ($context eq 'requestauthor') {
 9510:                     %haschgs = %changes;
 9511:                     %inconf = %confhash;
 9512:                 } else {
 9513:                     if (ref($changes{$item}) eq 'HASH') {
 9514:                         %haschgs = %{$changes{$item}};
 9515:                     }
 9516:                     if (ref($confhash{$item}) eq 'HASH') {
 9517:                         %inconf = %{$confhash{$item}};
 9518:                     }
 9519:                 }
 9520:                 if (keys(%haschgs) > 0) {
 9521:                     my $newacc = 
 9522:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9523:                                                           $env{'user.domain'},
 9524:                                                           $item,'reload',$context);
 9525:                     if (($context eq 'requestcourses') ||
 9526:                         ($context eq 'requestauthor')) {
 9527:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9528:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9529:                         }
 9530:                     } else {
 9531:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9532:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9533:                         }
 9534:                     }
 9535:                     unless ($context eq 'requestauthor') {
 9536:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9537:                     }
 9538:                     foreach my $type (@{$types},'default','_LC_adv') {
 9539:                         if ($haschgs{$type}) {
 9540:                             my $typetitle = $usertypes->{$type};
 9541:                             if ($type eq 'default') {
 9542:                                 $typetitle = $othertitle;
 9543:                             } elsif ($type eq '_LC_adv') {
 9544:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9545:                             }
 9546:                             if ($inconf{$type}) {
 9547:                                 if ($context eq 'requestcourses') {
 9548:                                     my $cond;
 9549:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9550:                                         if ($1 eq '') {
 9551:                                             $cond = &mt('(Automatic processing of any request).');
 9552:                                         } else {
 9553:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9554:                                         }
 9555:                                     } else { 
 9556:                                         $cond = $conditions{$inconf{$type}};
 9557:                                     }
 9558:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9559:                                 } elsif ($context eq 'requestauthor') {
 9560:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9561:                                                              $titles{$inconf{$type}},$typetitle);
 9562: 
 9563:                                 } else {
 9564:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9565:                                 }
 9566:                             } else {
 9567:                                 if ($type eq '_LC_adv') {
 9568:                                     if ($inconf{$type} eq '0') {
 9569:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9570:                                     } else { 
 9571:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9572:                                     }
 9573:                                 } else {
 9574:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9575:                                 }
 9576:                             }
 9577:                         }
 9578:                     }
 9579:                     unless ($context eq 'requestauthor') {
 9580:                         $resulttext .= '</ul></li>';
 9581:                     }
 9582:                 }
 9583:             }
 9584:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9585:                 if (ref($changes{'notify'}) eq 'HASH') {
 9586:                     if ($changes{'notify'}{'approval'}) {
 9587:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9588:                             if ($confhash{'notify'}{'approval'}) {
 9589:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9590:                             } else {
 9591:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9592:                             }
 9593:                         }
 9594:                     }
 9595:                 }
 9596:             }
 9597:             if ($action eq 'requestcourses') {
 9598:                 my @offon = ('off','on');
 9599:                 if ($changes{'uniquecode'}) {
 9600:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9601:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9602:                         $resulttext .= '<li>'.
 9603:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9604:                                        '</li>';
 9605:                     } else {
 9606:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9607:                                        '</li>';
 9608:                     }
 9609:                 }
 9610:                 foreach my $type ('textbooks','templates') {
 9611:                     if (ref($changes{$type}) eq 'HASH') {
 9612:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9613:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9614:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9615:                             my $coursetitle = $coursehash{'description'};
 9616:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9617:                             $resulttext .= '<li>';
 9618:                             foreach my $item ('subject','title','publisher','author') {
 9619:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9620:                                          ($type eq 'templates'));
 9621:                                 my $name = $item.':';
 9622:                                 $name =~ s/^(\w)/\U$1/;
 9623:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9624:                             }
 9625:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9626:                             if ($type eq 'textbooks') {
 9627:                                 if ($confhash{$type}{$key}{'image'}) {
 9628:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9629:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9630:                                                    ' alt="Textbook cover" />').'<br />';
 9631:                                 }
 9632:                             }
 9633:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9634:                         }
 9635:                         $resulttext .= '</ul></li>';
 9636:                     }
 9637:                 }
 9638:                 if (ref($changes{'validation'}) eq 'HASH') {
 9639:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9640:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9641:                         foreach my $item (@{$validationitemsref}) {
 9642:                             if (exists($changes{'validation'}{$item})) {
 9643:                                 if ($item eq 'markup') {
 9644:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9645:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9646:                                 } else {
 9647:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9648:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9649:                                 }
 9650:                             }
 9651:                         }
 9652:                         if (exists($changes{'validation'}{'dc'})) {
 9653:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9654:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9655:                         }
 9656:                     }
 9657:                 }
 9658:             }
 9659:             $resulttext .= '</ul>';
 9660:             if (keys(%newenv)) {
 9661:                 &Apache::lonnet::appenv(\%newenv);
 9662:             }
 9663:         } else {
 9664:             if ($context eq 'requestcourses') {
 9665:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9666:             } elsif ($context eq 'requestauthor') {
 9667:                 $resulttext = &mt('No changes made to rights to request author space.');
 9668:             } else {
 9669:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9670:             }
 9671:         }
 9672:     } else {
 9673:         $resulttext = '<span class="LC_error">'.
 9674: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9675:     }
 9676:     if ($errors) {
 9677:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9678:                        '<ul>'.$errors.'</ul></p>';
 9679:     }
 9680:     return $resulttext;
 9681: }
 9682: 
 9683: sub process_textbook_image {
 9684:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9685:     my $filename = $env{'form.'.$caller.'.filename'};
 9686:     my ($error,$url);
 9687:     my ($width,$height) = (50,50);
 9688:     if ($configuserok eq 'ok') {
 9689:         if ($switchserver) {
 9690:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9691:                          $switchserver);
 9692:         } elsif ($author_ok eq 'ok') {
 9693:             my ($result,$imageurl) =
 9694:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9695:                              "$type/$cdom/$cnum/cover",$width,$height);
 9696:             if ($result eq 'ok') {
 9697:                 $url = $imageurl;
 9698:             } else {
 9699:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9700:             }
 9701:         } else {
 9702:             $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);
 9703:         }
 9704:     } else {
 9705:         $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);
 9706:     }
 9707:     return ($url,$error);
 9708: }
 9709: 
 9710: sub modify_autoenroll {
 9711:     my ($dom,$lastactref,%domconfig) = @_;
 9712:     my ($resulttext,%changes);
 9713:     my %currautoenroll;
 9714:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9715:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9716:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9717:         }
 9718:     }
 9719:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9720:     my %title = ( run => 'Auto-enrollment active',
 9721:                   sender => 'Sender for notification messages',
 9722:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9723:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9724:     my @offon = ('off','on');
 9725:     my $sender_uname = $env{'form.sender_uname'};
 9726:     my $sender_domain = $env{'form.sender_domain'};
 9727:     if ($sender_domain eq '') {
 9728:         $sender_uname = '';
 9729:     } elsif ($sender_uname eq '') {
 9730:         $sender_domain = '';
 9731:     }
 9732:     my $coowners = $env{'form.autoassign_coowners'};
 9733:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9734:     $failsafe =~ s{^\s+|\s+$}{}g;
 9735:     if ($failsafe =~ /\D/) {
 9736:         undef($failsafe);
 9737:     }
 9738:     my %autoenrollhash =  (
 9739:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9740:                                        'sender_uname' => $sender_uname,
 9741:                                        'sender_domain' => $sender_domain,
 9742:                                        'co-owners' => $coowners,
 9743:                                        'autofailsafe' => $failsafe,
 9744:                                 }
 9745:                      );
 9746:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9747:                                              $dom);
 9748:     if ($putresult eq 'ok') {
 9749:         if (exists($currautoenroll{'run'})) {
 9750:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9751:                  $changes{'run'} = 1;
 9752:              }
 9753:         } elsif ($autorun) {
 9754:             if ($env{'form.autoenroll_run'} ne '1') {
 9755:                  $changes{'run'} = 1;
 9756:             }
 9757:         }
 9758:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9759:             $changes{'sender'} = 1;
 9760:         }
 9761:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9762:             $changes{'sender'} = 1;
 9763:         }
 9764:         if ($currautoenroll{'co-owners'} ne '') {
 9765:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9766:                 $changes{'coowners'} = 1;
 9767:             }
 9768:         } elsif ($coowners) {
 9769:             $changes{'coowners'} = 1;
 9770:         }
 9771:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9772:             $changes{'autofailsafe'} = 1;
 9773:         }
 9774:         if (keys(%changes) > 0) {
 9775:             $resulttext = &mt('Changes made:').'<ul>';
 9776:             if ($changes{'run'}) {
 9777:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9778:             }
 9779:             if ($changes{'sender'}) {
 9780:                 if ($sender_uname eq '' || $sender_domain eq '') {
 9781:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 9782:                 } else {
 9783:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 9784:                 }
 9785:             }
 9786:             if ($changes{'coowners'}) {
 9787:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 9788:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9789:                 if (ref($lastactref) eq 'HASH') {
 9790:                     $lastactref->{'domainconfig'} = 1;
 9791:                 }
 9792:             }
 9793:             if ($changes{'autofailsafe'}) {
 9794:                 if ($failsafe ne '') {
 9795:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
 9796:                 } else {
 9797:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
 9798:                 }
 9799:                 &Apache::lonnet::get_domain_defaults($dom,1);
 9800:                 if (ref($lastactref) eq 'HASH') {
 9801:                     $lastactref->{'domdefaults'} = 1;
 9802:                 }
 9803:             }
 9804:             $resulttext .= '</ul>';
 9805:         } else {
 9806:             $resulttext = &mt('No changes made to auto-enrollment settings');
 9807:         }
 9808:     } else {
 9809:         $resulttext = '<span class="LC_error">'.
 9810: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9811:     }
 9812:     return $resulttext;
 9813: }
 9814: 
 9815: sub modify_autoupdate {
 9816:     my ($dom,%domconfig) = @_;
 9817:     my ($resulttext,%currautoupdate,%fields,%changes);
 9818:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 9819:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 9820:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 9821:         }
 9822:     }
 9823:     my @offon = ('off','on');
 9824:     my %title = &Apache::lonlocal::texthash (
 9825:                    run => 'Auto-update:',
 9826:                    classlists => 'Updates to user information in classlists?'
 9827:                 );
 9828:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9829:     my %fieldtitles = &Apache::lonlocal::texthash (
 9830:                         id => 'Student/Employee ID',
 9831:                         permanentemail => 'E-mail address',
 9832:                         lastname => 'Last Name',
 9833:                         firstname => 'First Name',
 9834:                         middlename => 'Middle Name',
 9835:                         generation => 'Generation',
 9836:                       );
 9837:     $othertitle = &mt('All users');
 9838:     if (keys(%{$usertypes}) >  0) {
 9839:         $othertitle = &mt('Other users');
 9840:     }
 9841:     foreach my $key (keys(%env)) {
 9842:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 9843:             my ($usertype,$item) = ($1,$2);
 9844:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 9845:                 if ($usertype eq 'default') {   
 9846:                     push(@{$fields{$1}},$2);
 9847:                 } elsif (ref($types) eq 'ARRAY') {
 9848:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 9849:                         push(@{$fields{$1}},$2);
 9850:                     }
 9851:                 }
 9852:             }
 9853:         }
 9854:     }
 9855:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 9856:     @lockablenames = sort(@lockablenames);
 9857:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 9858:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 9859:         if (@changed) {
 9860:             $changes{'lockablenames'} = 1;
 9861:         }
 9862:     } else {
 9863:         if (@lockablenames) {
 9864:             $changes{'lockablenames'} = 1;
 9865:         }
 9866:     }
 9867:     my %updatehash = (
 9868:                       autoupdate => { run => $env{'form.autoupdate_run'},
 9869:                                       classlists => $env{'form.classlists'},
 9870:                                       fields => {%fields},
 9871:                                       lockablenames => \@lockablenames,
 9872:                                     }
 9873:                      );
 9874:     foreach my $key (keys(%currautoupdate)) {
 9875:         if (($key eq 'run') || ($key eq 'classlists')) {
 9876:             if (exists($updatehash{autoupdate}{$key})) {
 9877:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 9878:                     $changes{$key} = 1;
 9879:                 }
 9880:             }
 9881:         } elsif ($key eq 'fields') {
 9882:             if (ref($currautoupdate{$key}) eq 'HASH') {
 9883:                 foreach my $item (@{$types},'default') {
 9884:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 9885:                         my $change = 0;
 9886:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 9887:                             if (!exists($fields{$item})) {
 9888:                                 $change = 1;
 9889:                                 last;
 9890:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 9891:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 9892:                                     $change = 1;
 9893:                                     last;
 9894:                                 }
 9895:                             }
 9896:                         }
 9897:                         if ($change) {
 9898:                             push(@{$changes{$key}},$item);
 9899:                         }
 9900:                     } 
 9901:                 }
 9902:             }
 9903:         } elsif ($key eq 'lockablenames') {
 9904:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 9905:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 9906:                 if (@changed) {
 9907:                     $changes{'lockablenames'} = 1;
 9908:                 }
 9909:             } else {
 9910:                 if (@lockablenames) {
 9911:                     $changes{'lockablenames'} = 1;
 9912:                 }
 9913:             }
 9914:         }
 9915:     }
 9916:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 9917:         if (@lockablenames) {
 9918:             $changes{'lockablenames'} = 1;
 9919:         }
 9920:     }
 9921:     foreach my $item (@{$types},'default') {
 9922:         if (defined($fields{$item})) {
 9923:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 9924:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 9925:                     my $change = 0;
 9926:                     if (ref($fields{$item}) eq 'ARRAY') {
 9927:                         foreach my $type (@{$fields{$item}}) {
 9928:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 9929:                                 $change = 1;
 9930:                                 last;
 9931:                             }
 9932:                         }
 9933:                     }
 9934:                     if ($change) {
 9935:                         push(@{$changes{'fields'}},$item);
 9936:                     }
 9937:                 } else {
 9938:                     push(@{$changes{'fields'}},$item);
 9939:                 }
 9940:             } else {
 9941:                 push(@{$changes{'fields'}},$item);
 9942:             }
 9943:         }
 9944:     }
 9945:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 9946:                                              $dom);
 9947:     if ($putresult eq 'ok') {
 9948:         if (keys(%changes) > 0) {
 9949:             $resulttext = &mt('Changes made:').'<ul>';
 9950:             foreach my $key (sort(keys(%changes))) {
 9951:                 if ($key eq 'lockablenames') {
 9952:                     $resulttext .= '<li>';
 9953:                     if (@lockablenames) {
 9954:                         $usertypes->{'default'} = $othertitle;
 9955:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 9956:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 9957:                     } else {
 9958:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 9959:                     }
 9960:                     $resulttext .= '</li>';
 9961:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 9962:                     foreach my $item (@{$changes{$key}}) {
 9963:                         my @newvalues;
 9964:                         foreach my $type (@{$fields{$item}}) {
 9965:                             push(@newvalues,$fieldtitles{$type});
 9966:                         }
 9967:                         my $newvaluestr;
 9968:                         if (@newvalues > 0) {
 9969:                             $newvaluestr = join(', ',@newvalues);
 9970:                         } else {
 9971:                             $newvaluestr = &mt('none');
 9972:                         }
 9973:                         if ($item eq 'default') {
 9974:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 9975:                         } else {
 9976:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 9977:                         }
 9978:                     }
 9979:                 } else {
 9980:                     my $newvalue;
 9981:                     if ($key eq 'run') {
 9982:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 9983:                     } else {
 9984:                         $newvalue = $offon[$env{'form.'.$key}];
 9985:                     }
 9986:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 9987:                 }
 9988:             }
 9989:             $resulttext .= '</ul>';
 9990:         } else {
 9991:             $resulttext = &mt('No changes made to autoupdates');
 9992:         }
 9993:     } else {
 9994:         $resulttext = '<span class="LC_error">'.
 9995: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9996:     }
 9997:     return $resulttext;
 9998: }
 9999: 
10000: sub modify_autocreate {
10001:     my ($dom,%domconfig) = @_;
10002:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10003:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10004:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10005:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10006:         }
10007:     }
10008:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10009:                  req => 'Auto-creation of validated requests for official courses',
10010:                  xmldc => 'Identity of course creator of courses from XML files',
10011:                );
10012:     my @types = ('xml','req');
10013:     foreach my $item (@types) {
10014:         $newvals{$item} = $env{'form.autocreate_'.$item};
10015:         $newvals{$item} =~ s/\D//g;
10016:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10017:     }
10018:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10019:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10020:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10021:         $newvals{'xmldc'} = '';
10022:     } 
10023:     %autocreatehash =  (
10024:                         autocreate => { xml => $newvals{'xml'},
10025:                                         req => $newvals{'req'},
10026:                                       }
10027:                        );
10028:     if ($newvals{'xmldc'} ne '') {
10029:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10030:     }
10031:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10032:                                              $dom);
10033:     if ($putresult eq 'ok') {
10034:         my @items = @types;
10035:         if ($newvals{'xml'}) {
10036:             push(@items,'xmldc');
10037:         }
10038:         foreach my $item (@items) {
10039:             if (exists($currautocreate{$item})) {
10040:                 if ($currautocreate{$item} ne $newvals{$item}) {
10041:                     $changes{$item} = 1;
10042:                 }
10043:             } elsif ($newvals{$item}) {
10044:                 $changes{$item} = 1;
10045:             }
10046:         }
10047:         if (keys(%changes) > 0) {
10048:             my @offon = ('off','on'); 
10049:             $resulttext = &mt('Changes made:').'<ul>';
10050:             foreach my $item (@types) {
10051:                 if ($changes{$item}) {
10052:                     my $newtxt = $offon[$newvals{$item}];
10053:                     $resulttext .= '<li>'.
10054:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10055:                                        '<b>','</b>').
10056:                                    '</li>';
10057:                 }
10058:             }
10059:             if ($changes{'xmldc'}) {
10060:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10061:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10062:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10063:             }
10064:             $resulttext .= '</ul>';
10065:         } else {
10066:             $resulttext = &mt('No changes made to auto-creation settings');
10067:         }
10068:     } else {
10069:         $resulttext = '<span class="LC_error">'.
10070:             &mt('An error occurred: [_1]',$putresult).'</span>';
10071:     }
10072:     return $resulttext;
10073: }
10074: 
10075: sub modify_directorysrch {
10076:     my ($dom,$lastactref,%domconfig) = @_;
10077:     my ($resulttext,%changes);
10078:     my %currdirsrch;
10079:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10080:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10081:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10082:         }
10083:     }
10084:     my %title = ( available => 'Institutional directory search available',
10085:                   localonly => 'Other domains can search institution',
10086:                   lcavailable => 'LON-CAPA directory search available',
10087:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10088:                   searchby => 'Search types',
10089:                   searchtypes => 'Search latitude');
10090:     my @offon = ('off','on');
10091:     my @otherdoms = ('Yes','No');
10092: 
10093:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10094:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10095:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10096: 
10097:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10098:     if (keys(%{$usertypes}) == 0) {
10099:         @cansearch = ('default');
10100:     } else {
10101:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10102:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10103:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10104:                     push(@{$changes{'cansearch'}},$type);
10105:                 }
10106:             }
10107:             foreach my $type (@cansearch) {
10108:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10109:                     push(@{$changes{'cansearch'}},$type);
10110:                 }
10111:             }
10112:         } else {
10113:             push(@{$changes{'cansearch'}},@cansearch);
10114:         }
10115:     }
10116: 
10117:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10118:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10119:             if (!grep(/^\Q$by\E$/,@searchby)) {
10120:                 push(@{$changes{'searchby'}},$by);
10121:             }
10122:         }
10123:         foreach my $by (@searchby) {
10124:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10125:                 push(@{$changes{'searchby'}},$by);
10126:             }
10127:         }
10128:     } else {
10129:         push(@{$changes{'searchby'}},@searchby);
10130:     }
10131: 
10132:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10133:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10134:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10135:                 push(@{$changes{'searchtypes'}},$type);
10136:             }
10137:         }
10138:         foreach my $type (@searchtypes) {
10139:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10140:                 push(@{$changes{'searchtypes'}},$type);
10141:             }
10142:         }
10143:     } else {
10144:         if (exists($currdirsrch{'searchtypes'})) {
10145:             foreach my $type (@searchtypes) {  
10146:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10147:                     push(@{$changes{'searchtypes'}},$type);
10148:                 }
10149:             }
10150:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10151:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10152:             }   
10153:         } else {
10154:             push(@{$changes{'searchtypes'}},@searchtypes); 
10155:         }
10156:     }
10157: 
10158:     my %dirsrch_hash =  (
10159:             directorysrch => { available => $env{'form.dirsrch_available'},
10160:                                cansearch => \@cansearch,
10161:                                localonly => $env{'form.dirsrch_instlocalonly'},
10162:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10163:                                lcavailable => $env{'form.dirsrch_domavailable'},
10164:                                searchby => \@searchby,
10165:                                searchtypes => \@searchtypes,
10166:                              }
10167:             );
10168:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10169:                                              $dom);
10170:     if ($putresult eq 'ok') {
10171:         if (exists($currdirsrch{'available'})) {
10172:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10173:                  $changes{'available'} = 1;
10174:              }
10175:         } else {
10176:             if ($env{'form.dirsrch_available'} eq '1') {
10177:                 $changes{'available'} = 1;
10178:             }
10179:         }
10180:         if (exists($currdirsrch{'lcavailable'})) {
10181:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10182:                 $changes{'lcavailable'} = 1;
10183:             }
10184:         } else {
10185:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10186:                 $changes{'lcavailable'} = 1;
10187:             }
10188:         }
10189:         if (exists($currdirsrch{'localonly'})) {
10190:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10191:                 $changes{'localonly'} = 1;
10192:             }
10193:         } else {
10194:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10195:                 $changes{'localonly'} = 1;
10196:             }
10197:         }
10198:         if (exists($currdirsrch{'lclocalonly'})) {
10199:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10200:                 $changes{'lclocalonly'} = 1;
10201:             }
10202:         } else {
10203:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10204:                 $changes{'lclocalonly'} = 1;
10205:             }
10206:         }
10207:         if (keys(%changes) > 0) {
10208:             $resulttext = &mt('Changes made:').'<ul>';
10209:             if ($changes{'available'}) {
10210:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10211:             }
10212:             if ($changes{'lcavailable'}) {
10213:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10214:             }
10215:             if ($changes{'localonly'}) {
10216:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10217:             }
10218:             if ($changes{'lclocalonly'}) {
10219:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10220:             }
10221:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10222:                 my $chgtext;
10223:                 if (ref($usertypes) eq 'HASH') {
10224:                     if (keys(%{$usertypes}) > 0) {
10225:                         foreach my $type (@{$types}) {
10226:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10227:                                 $chgtext .= $usertypes->{$type}.'; ';
10228:                             }
10229:                         }
10230:                         if (grep(/^default$/,@cansearch)) {
10231:                             $chgtext .= $othertitle;
10232:                         } else {
10233:                             $chgtext =~ s/\; $//;
10234:                         }
10235:                         $resulttext .=
10236:                             '<li>'.
10237:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10238:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10239:                             '</li>';
10240:                     }
10241:                 }
10242:             }
10243:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10244:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10245:                 my $chgtext;
10246:                 foreach my $type (@{$titleorder}) {
10247:                     if (grep(/^\Q$type\E$/,@searchby)) {
10248:                         if (defined($searchtitles->{$type})) {
10249:                             $chgtext .= $searchtitles->{$type}.'; ';
10250:                         }
10251:                     }
10252:                 }
10253:                 $chgtext =~ s/\; $//;
10254:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10255:             }
10256:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10257:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10258:                 my $chgtext;
10259:                 foreach my $type (@{$srchtypeorder}) {
10260:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10261:                         if (defined($srchtypes_desc->{$type})) {
10262:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10263:                         }
10264:                     }
10265:                 }
10266:                 $chgtext =~ s/\; $//;
10267:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10268:             }
10269:             $resulttext .= '</ul>';
10270:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10271:             if (ref($lastactref) eq 'HASH') {
10272:                 $lastactref->{'directorysrch'} = 1;
10273:             }
10274:         } else {
10275:             $resulttext = &mt('No changes made to directory search settings');
10276:         }
10277:     } else {
10278:         $resulttext = '<span class="LC_error">'.
10279:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10280:     }
10281:     return $resulttext;
10282: }
10283: 
10284: sub modify_contacts {
10285:     my ($dom,$lastactref,%domconfig) = @_;
10286:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10287:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10288:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10289:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10290:         }
10291:     }
10292:     my (%others,%to,%bcc,%includestr,%includeloc);
10293:     my @contacts = ('supportemail','adminemail');
10294:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10295:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
10296:     my @toggles = ('reporterrors','reportupdates');
10297:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10298:     foreach my $type (@mailings) {
10299:         @{$newsetting{$type}} = 
10300:             &Apache::loncommon::get_env_multiple('form.'.$type);
10301:         foreach my $item (@contacts) {
10302:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10303:                 $contacts_hash{contacts}{$type}{$item} = 1;
10304:             } else {
10305:                 $contacts_hash{contacts}{$type}{$item} = 0;
10306:             }
10307:         }
10308:         $others{$type} = $env{'form.'.$type.'_others'};
10309:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10310:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10311:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10312:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10313:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10314:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10315:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10316:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10317:             }
10318:         }
10319:     }
10320:     foreach my $item (@contacts) {
10321:         $to{$item} = $env{'form.'.$item};
10322:         $contacts_hash{'contacts'}{$item} = $to{$item};
10323:     }
10324:     foreach my $item (@toggles) {
10325:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10326:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10327:         }
10328:     }
10329:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10330:         foreach my $field (@{$fields}) {
10331:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10332:                 my $value = $env{'form.helpform_'.$field};
10333:                 $value =~ s/^\s+|\s+$//g;
10334:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10335:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
10336:                     if ($field eq 'screenshot') {
10337:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10338:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10339:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10340:                         }
10341:                     }
10342:                 }
10343:             }
10344:         }
10345:     }
10346:     if (keys(%currsetting) > 0) {
10347:         foreach my $item (@contacts) {
10348:             if ($to{$item} ne $currsetting{$item}) {
10349:                 $changes{$item} = 1;
10350:             }
10351:         }
10352:         foreach my $type (@mailings) {
10353:             foreach my $item (@contacts) {
10354:                 if (ref($currsetting{$type}) eq 'HASH') {
10355:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10356:                         push(@{$changes{$type}},$item);
10357:                     }
10358:                 } else {
10359:                     push(@{$changes{$type}},@{$newsetting{$type}});
10360:                 }
10361:             }
10362:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10363:                 push(@{$changes{$type}},'others');
10364:             }
10365:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10366:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10367:                     push(@{$changes{$type}},'bcc'); 
10368:                 }
10369:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10370:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10371:                     push(@{$changes{$type}},'include');
10372:                 }
10373:             }
10374:         }
10375:         if (ref($fields) eq 'ARRAY') {
10376:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10377:                 foreach my $field (@{$fields}) {
10378:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10379:                         push(@{$changes{'helpform'}},$field);
10380:                     }
10381:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10382:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10383:                             push(@{$changes{'helpform'}},'maxsize');
10384:                         }
10385:                     }
10386:                 }
10387:             } else {
10388:                 foreach my $field (@{$fields}) {
10389:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10390:                         push(@{$changes{'helpform'}},$field);
10391:                     }
10392:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10393:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10394:                             push(@{$changes{'helpform'}},'maxsize');
10395:                         }
10396:                     }
10397:                 }
10398:             }
10399:         }
10400:     } else {
10401:         my %default;
10402:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10403:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10404:         $default{'errormail'} = 'adminemail';
10405:         $default{'packagesmail'} = 'adminemail';
10406:         $default{'helpdeskmail'} = 'supportemail';
10407:         $default{'otherdomsmail'} = 'supportemail';
10408:         $default{'lonstatusmail'} = 'adminemail';
10409:         $default{'requestsmail'} = 'adminemail';
10410:         $default{'updatesmail'} = 'adminemail';
10411:         $default{'hostipmail'} = 'adminemail';
10412:         foreach my $item (@contacts) {
10413:            if ($to{$item} ne $default{$item}) {
10414:                $changes{$item} = 1;
10415:            }
10416:         }
10417:         foreach my $type (@mailings) {
10418:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10419:                 push(@{$changes{$type}},@{$newsetting{$type}});
10420:             }
10421:             if ($others{$type} ne '') {
10422:                 push(@{$changes{$type}},'others');
10423:             }
10424:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10425:                 if ($bcc{$type} ne '') {
10426:                     push(@{$changes{$type}},'bcc');
10427:                 }
10428:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10429:                     push(@{$changes{$type}},'include');
10430:                 }
10431:             }
10432:         }
10433:         if (ref($fields) eq 'ARRAY') {
10434:             foreach my $field (@{$fields}) {
10435:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10436:                     push(@{$changes{'helpform'}},$field);
10437:                 }
10438:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10439:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10440:                         push(@{$changes{'helpform'}},'maxsize');
10441:                     }
10442:                 }
10443:             }
10444:         }
10445:     }
10446:     foreach my $item (@toggles) {
10447:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10448:             $changes{$item} = 1;
10449:         } elsif ((!$env{'form.'.$item}) &&
10450:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10451:             $changes{$item} = 1;
10452:         }
10453:     }
10454:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10455:                                              $dom);
10456:     if ($putresult eq 'ok') {
10457:         if (keys(%changes) > 0) {
10458:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10459:             if (ref($lastactref) eq 'HASH') {
10460:                 $lastactref->{'domainconfig'} = 1;
10461:             }
10462:             my ($titles,$short_titles)  = &contact_titles();
10463:             $resulttext = &mt('Changes made:').'<ul>';
10464:             foreach my $item (@contacts) {
10465:                 if ($changes{$item}) {
10466:                     $resulttext .= '<li>'.$titles->{$item}.
10467:                                     &mt(' set to: ').
10468:                                     '<span class="LC_cusr_emph">'.
10469:                                     $to{$item}.'</span></li>';
10470:                 }
10471:             }
10472:             foreach my $type (@mailings) {
10473:                 if (ref($changes{$type}) eq 'ARRAY') {
10474:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10475:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10476:                     } else {
10477:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10478:                     }
10479:                     my @text;
10480:                     foreach my $item (@{$newsetting{$type}}) {
10481:                         push(@text,$short_titles->{$item});
10482:                     }
10483:                     if ($others{$type} ne '') {
10484:                         push(@text,$others{$type});
10485:                     }
10486:                     if (@text) {
10487:                         $resulttext .= '<span class="LC_cusr_emph">'.
10488:                                        join(', ',@text).'</span>';
10489:                     }
10490:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10491:                         if ($bcc{$type} ne '') {
10492:                             my $bcctext;
10493:                             if (@text) {
10494:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10495:                             } else {
10496:                                 $bcctext = '(Bcc)';
10497:                             }
10498:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10499:                         } elsif (!@text) {
10500:                             $resulttext .= &mt('No one');
10501:                         }
10502:                         if ($includestr{$type} ne '') {
10503:                             if ($includeloc{$type} eq 'b') {
10504:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10505:                             } elsif ($includeloc{$type} eq 's') {
10506:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10507:                             }
10508:                         }
10509:                     } elsif (!@text) {
10510:                         $resulttext .= &mt('No recipients');
10511:                     }
10512:                     $resulttext .= '</li>';
10513:                 }
10514:             }
10515:             my @offon = ('off','on');
10516:             if ($changes{'reporterrors'}) {
10517:                 $resulttext .= '<li>'.
10518:                                &mt('E-mail error reports to [_1] set to "'.
10519:                                    $offon[$env{'form.reporterrors'}].'".',
10520:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10521:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10522:                                '</li>';
10523:             }
10524:             if ($changes{'reportupdates'}) {
10525:                 $resulttext .= '<li>'.
10526:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10527:                                     $offon[$env{'form.reportupdates'}].'".',
10528:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10529:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10530:                                 '</li>';
10531:             }
10532:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10533:                 my (@optional,@required,@unused,$maxsizechg);
10534:                 foreach my $field (@{$changes{'helpform'}}) {
10535:                     if ($field eq 'maxsize') {
10536:                         $maxsizechg = 1;
10537:                         next;
10538:                     }
10539:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10540:                         push(@optional,$field);
10541:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10542:                         push(@unused,$field);
10543:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10544:                         push(@required,$field);
10545:                     }
10546:                 }
10547:                 if (@optional) {
10548:                     $resulttext .= '<li>'.
10549:                                    &mt('Help form fields changed to "Optional": [_1].',
10550:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10551:                                    '</li>';
10552:                 }
10553:                 if (@required) {
10554:                     $resulttext .= '<li>'.
10555:                                    &mt('Help form fields changed to "Required": [_1].',
10556:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10557:                                    '</li>';
10558:                 }
10559:                 if (@unused) {
10560:                     $resulttext .= '<li>'.
10561:                                    &mt('Help form fields changed to "Not shown": [_1].',
10562:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10563:                                    '</li>';
10564:                 }
10565:                 if ($maxsizechg) {
10566:                     $resulttext .= '<li>'.
10567:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10568:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10569:                                    '</li>';
10570: 
10571:                 }
10572:             }
10573:             $resulttext .= '</ul>';
10574:         } else {
10575:             $resulttext = &mt('No changes made to contacts and form settings');
10576:         }
10577:     } else {
10578:         $resulttext = '<span class="LC_error">'.
10579:             &mt('An error occurred: [_1].',$putresult).'</span>';
10580:     }
10581:     return $resulttext;
10582: }
10583: 
10584: sub modify_passwords {
10585:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10586:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
10587:         $updatedefaults,$updateconf);
10588:     my $customfn = 'resetpw.html';
10589:     if (ref($domconfig{'passwords'}) eq 'HASH') {
10590:         %current = %{$domconfig{'passwords'}};
10591:     }
10592:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10593:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10594:     if (ref($types) eq 'ARRAY') {
10595:         @oktypes = @{$types};
10596:     }
10597:     push(@oktypes,'default');
10598: 
10599:     my %titles = &Apache::lonlocal::texthash (
10600:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
10601:         intauth_check  => 'Check bcrypt cost if authenticated',
10602:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
10603:         permanent      => 'Permanent e-mail address',
10604:         critical       => 'Critical notification address',
10605:         notify         => 'Notification address',
10606:         min            => 'Minimum password length',
10607:         max            => 'Maximum password length',
10608:         chars          => 'Required characters',
10609:         numsaved       => 'Number of previous passwords to save',
10610:         reset          => 'Resetting Forgotten Password',
10611:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
10612:         rules          => 'Rules for LON-CAPA Passwords',
10613:         crsownerchg    => 'Course Owner Changing Student Passwords',
10614:         username       => 'Username',
10615:         email          => 'E-mail address',
10616:     );
10617: 
10618: #
10619: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
10620: #
10621:     my (%curr_defaults,%save_defaults);
10622:     if (ref($domconfig{'defaults'}) eq 'HASH') {
10623:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
10624:             if ($key =~ /^intauth_(cost|check|switch)$/) {
10625:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
10626:             } else {
10627:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
10628:             }
10629:         }
10630:     }
10631:     my %staticdefaults = (
10632:         'resetlink'      => 2,
10633:         'resetcase'      => \@oktypes,
10634:         'resetprelink'   => 'both',
10635:         'resetemail'     => ['critical','notify','permanent'],
10636:         'intauth_cost'   => 10,
10637:         'intauth_check'  => 0,
10638:         'intauth_switch' => 0,
10639:     );
10640:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
10641:     foreach my $type (@oktypes) {
10642:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
10643:     }
10644:     my $linklife = $env{'form.passwords_link'};
10645:     $linklife =~ s/^\s+|\s+$//g;
10646:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
10647:         $newvalues{'resetlink'} = $linklife;
10648:         if ($current{'resetlink'}) {
10649:             if ($current{'resetlink'} ne $linklife) {
10650:                 $changes{'reset'} = 1;
10651:             }
10652:         } elsif (!exists($domconfig{passwords})) {
10653:             if ($staticdefaults{'resetlink'} ne $linklife) {
10654:                 $changes{'reset'} = 1;
10655:             }
10656:         }
10657:     } elsif ($current{'resetlink'}) {
10658:         $changes{'reset'} = 1;
10659:     }
10660:     my @casesens;
10661:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
10662:     foreach my $case (sort(@posscase)) {
10663:         if (grep(/^\Q$case\E$/,@oktypes)) {
10664:             push(@casesens,$case);
10665:         }
10666:     }
10667:     $newvalues{'resetcase'} = \@casesens;
10668:     if (ref($current{'resetcase'}) eq 'ARRAY') {
10669:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
10670:         if (@diffs > 0) {
10671:             $changes{'reset'} = 1;
10672:         }
10673:     } elsif (!exists($domconfig{passwords})) {
10674:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
10675:         if (@diffs > 0) {
10676:             $changes{'reset'} = 1;
10677:         }
10678:     }
10679:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
10680:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
10681:         if (exists($current{'resetprelink'})) {
10682:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
10683:                 $changes{'reset'} = 1;
10684:             }
10685:         } elsif (!exists($domconfig{passwords})) {
10686:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
10687:                 $changes{'reset'} = 1;
10688:             }
10689:         }
10690:     } elsif ($current{'resetprelink'}) {
10691:         $changes{'reset'} = 1;
10692:     }
10693:     foreach my $type (@oktypes) {
10694:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
10695:         my @postlink;
10696:         foreach my $item (sort(@possplink)) {
10697:             if ($item =~ /^(email|username)$/) {
10698:                 push(@postlink,$item);
10699:             }
10700:         }
10701:         $newvalues{'resetpostlink'}{$type} = \@postlink;
10702:         unless ($changes{'reset'}) {
10703:             if (ref($current{'resetpostlink'}) eq 'HASH') {
10704:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
10705:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
10706:                     if (@diffs > 0) {
10707:                         $changes{'reset'} = 1;
10708:                     }
10709:                 } else {
10710:                     $changes{'reset'} = 1;
10711:                 }
10712:             } elsif (!exists($domconfig{passwords})) {
10713:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
10714:                 if (@diffs > 0) {
10715:                     $changes{'reset'} = 1;
10716:                 }
10717:             }
10718:         }
10719:     }
10720:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
10721:     my @resetemail;
10722:     foreach my $item (sort(@possemailsrc)) {
10723:         if ($item =~ /^(permanent|critical|notify)$/) {
10724:             push(@resetemail,$item);
10725:         }
10726:     }
10727:     $newvalues{'resetemail'} = \@resetemail;
10728:     unless ($changes{'reset'}) {
10729:         if (ref($current{'resetemail'}) eq 'ARRAY') {
10730:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
10731:             if (@diffs > 0) {
10732:                 $changes{'reset'} = 1;
10733:             }
10734:         } elsif (!exists($domconfig{passwords})) {
10735:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
10736:             if (@diffs > 0) {
10737:                 $changes{'reset'} = 1;
10738:             }
10739:         }
10740:     }
10741:     if ($env{'form.passwords_stdtext'} == 0) {
10742:         $newvalues{'resetremove'} = 1;
10743:         unless ($current{'resetremove'}) {
10744:             $changes{'reset'} = 1;
10745:         }
10746:     } elsif ($current{'resetremove'}) {
10747:         $changes{'reset'} = 1;
10748:     }
10749:     if ($env{'form.passwords_customfile.filename'} ne '') {
10750:         my $servadm = $r->dir_config('lonAdmEMail');
10751:         my $servadm = $r->dir_config('lonAdmEMail');
10752:         my ($configuserok,$author_ok,$switchserver) =
10753:             &config_check($dom,$confname,$servadm);
10754:         my $error;
10755:         if ($configuserok eq 'ok') {
10756:             if ($switchserver) {
10757:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
10758:             } else {
10759:                 if ($author_ok eq 'ok') {
10760:                     my ($result,$customurl) =
10761:                         &publishlogo($r,'upload','passwords_customfile',$dom,
10762:                                      $confname,'customtext/resetpw','','',$customfn);
10763:                     if ($result eq 'ok') {
10764:                         $newvalues{'resetcustom'} = $customurl;
10765:                         $changes{'reset'} = 1;
10766:                     } else {
10767:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
10768:                     }
10769:                 } else {
10770:                     $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);
10771:                 }
10772:             }
10773:         } else {
10774:             $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);
10775:         }
10776:         if ($error) {
10777:             &Apache::lonnet::logthis($error);
10778:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10779:         }
10780:     } elsif ($current{'resetcustom'}) {
10781:         if ($env{'form.passwords_custom_del'}) {
10782:             $changes{'reset'} = 1;
10783:         } else {
10784:             $newvalues{'resetcustom'} = $current{'resetcustom'};
10785:         }
10786:     }
10787:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
10788:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
10789:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
10790:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
10791:             $changes{'intauth'} = 1;
10792:         }
10793:     } else {
10794:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
10795:     }
10796:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
10797:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
10798:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
10799:             $changes{'intauth'} = 1;
10800:         }
10801:     } else {
10802:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
10803:     }
10804:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
10805:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
10806:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
10807:             $changes{'intauth'} = 1;
10808:         }
10809:     } else {
10810:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
10811:     }
10812:     foreach my $item ('cost','check','switch') {
10813:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
10814:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
10815:             $updatedefaults = 1;
10816:         }
10817:     }
10818:     foreach my $rule ('min','max','numsaved') {
10819:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
10820:         my $ruleok;
10821:         if ($rule eq 'min') {
10822:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
10823:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
10824:                     $ruleok = 1;
10825:                 }
10826:             }
10827:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
10828:                  ($env{'form.passwords_'.$rule} ne '0')) {
10829:             $ruleok = 1;
10830:         }
10831:         if ($ruleok) {
10832:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
10833:             if (exists($current{$rule})) {
10834:                 if ($newvalues{$rule} ne $current{$rule}) {
10835:                     $changes{'rules'} = 1;
10836:                 }
10837:             } elsif ($rule eq 'min') {
10838:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
10839:                     $changes{'rules'} = 1;
10840:                 }
10841:             }
10842:         } elsif (exists($current{$rule})) {
10843:             $changes{'rules'} = 1;
10844:         }
10845:     }
10846:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
10847:     my @chars;
10848:     foreach my $item (sort(@posschars)) {
10849:         if ($item =~ /^(uc|lc|num|spec)$/) {
10850:             push(@chars,$item);
10851:         }
10852:     }
10853:     $newvalues{'chars'} = \@chars;
10854:     unless ($changes{'rules'}) {
10855:         if (ref($current{'chars'}) eq 'ARRAY') {
10856:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
10857:             if (@diffs > 0) {
10858:                 $changes{'rules'} = 1;
10859:             }
10860:         } else {
10861:             if (@chars > 0) {
10862:                 $changes{'rules'} = 1;
10863:             }
10864:         }
10865:     }
10866:     my %crsownerchg = (
10867:                         by => [],
10868:                         for => [],
10869:                       );
10870:     foreach my $item ('by','for') {
10871:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
10872:         foreach my $type (sort(@posstypes)) {
10873:             if (grep(/^\Q$type\E$/,@oktypes)) {
10874:                 push(@{$crsownerchg{$item}},$type);
10875:             }
10876:         }
10877:     }
10878:     $newvalues{'crsownerchg'} = \%crsownerchg;
10879:     if (ref($current{'crsownerchg'}) eq 'HASH') {
10880:         foreach my $item ('by','for') {
10881:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
10882:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
10883:                 if (@diffs > 0) {
10884:                     $changes{'crsownerchg'} = 1;
10885:                     last;
10886:                 }
10887:             }
10888:         }
10889:     } elsif (!exists($domconfig{passwords})) {
10890:         foreach my $item ('by','for') {
10891:             if (@{$crsownerchg{$item}} > 0) {
10892:                 $changes{'crsownerchg'} = 1;
10893:                 last;
10894:             }
10895:         }
10896:     }
10897: 
10898:     my %confighash = (
10899:                         defaults  => \%save_defaults,
10900:                         passwords => \%newvalues,
10901:                      );
10902:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
10903: 
10904:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
10905:     if ($putresult eq 'ok') {
10906:         if (keys(%changes) > 0) {
10907:             $resulttext = &mt('Changes made: ').'<ul>';
10908:             foreach my $key ('reset','intauth','rules','crsownerchg') {
10909:                 if ($changes{$key}) {
10910:                     unless ($key eq 'intauth') {
10911:                         $updateconf = 1;
10912:                     }
10913:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
10914:                     if ($key eq 'reset') {
10915:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
10916:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
10917:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
10918:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
10919:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />'.
10920:                                            &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchapub'}).'</br>'.
10921:                                            &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchapriv'}).'</li>';
10922:                         } else {
10923:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
10924:                         }
10925:                         if ($confighash{'passwords'}{'resetlink'}) {
10926:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
10927:                         } else {
10928:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
10929:                                                   &mt('Will default to 2 hours').'</li>';
10930:                         }
10931:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
10932:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
10933:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
10934:                             } else {
10935:                                 my $casesens;
10936:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
10937:                                     if ($type eq 'default') {
10938:                                         $casesens .= $othertitle.', ';
10939:                                     } elsif ($usertypes->{$type} ne '') {
10940:                                         $casesens .= $usertypes->{$type}.', ';
10941:                                     }
10942:                                 }
10943:                                 $casesens =~ s/\Q, \E$//;
10944:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
10945:                             }
10946:                         } else {
10947:                             $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>';
10948:                         }
10949:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
10950:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
10951:                         } else {
10952:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
10953:                         }
10954:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
10955:                             my $output;
10956:                             if (ref($types) eq 'ARRAY') {
10957:                                 foreach my $type (@{$types}) {
10958:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
10959:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
10960:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
10961:                                         } else {
10962:                                             $output .= $usertypes->{$type}.' -- '.
10963:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
10964:                                         }
10965:                                     }
10966:                                 }
10967:                             }
10968:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
10969:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
10970:                                     $output .= $othertitle.' -- '.&mt('none');
10971:                                 } else {
10972:                                     $output .= $othertitle.' -- '.
10973:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
10974:                                 }
10975:                             }
10976:                             if ($output) {
10977:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
10978:                             } else {
10979:                                 $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>';
10980:                             }
10981:                         } else {
10982:                             $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>';
10983:                         }
10984:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
10985:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
10986:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
10987:                             } else {
10988:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
10989:                             }
10990:                         } else {
10991:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA usedfor verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
10992:                         }
10993:                         if ($confighash{'passwords'}{'resetremove'}) {
10994:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
10995:                         } else {
10996:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
10997:                         }
10998:                         if ($confighash{'passwords'}{'resetcustom'}) {
10999:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
11000:                                                                             $titles{custom},600,500);
11001:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes [_1]',$customlink).'</li>';
11002:                         } else {
11003:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
11004:                         }
11005:                     } elsif ($key eq 'intauth') {
11006:                         foreach my $item ('cost','switch','check') {
11007:                             my $value = $save_defaults{$key.'_'.$item};
11008:                             if ($item eq 'switch') {
11009:                                 my %optiondesc = &Apache::lonlocal::texthash (
11010:                                                      0 => 'No',
11011:                                                      1 => 'Yes',
11012:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
11013:                                                  );
11014:                                 if ($value =~ /^(0|1|2)$/) {
11015:                                     $value = $optiondesc{$value};
11016:                                 } else {
11017:                                     $value = &mt('none -- defaults to No');
11018:                                 }
11019:                             } elsif ($item eq 'check') {
11020:                                 my %optiondesc = &Apache::lonlocal::texthash (
11021:                                                      0 => 'No',
11022:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
11023:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
11024:                                                  );
11025:                                 if ($value =~ /^(0|1|2)$/) {
11026:                                     $value = $optiondesc{$value};
11027:                                 } else {
11028:                                     $value = &mt('none -- defaults to No');
11029:                                 }
11030:                             }
11031:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
11032:                         }
11033:                     } elsif ($key eq 'rules') {
11034:                         foreach my $rule ('min','max','numsaved') {
11035:                             if ($confighash{'passwords'}{$rule} eq '') {
11036:                                 if ($rule eq 'min') {
11037:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
11038:                                                    ' '.&mt('Default of [_1] will be used',
11039:                                                            $Apache::lonnet::passwdmin).'</li>';
11040:                                 } else {
11041:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
11042:                                 }
11043:                             } else {
11044:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
11045:                             }
11046:                         }
11047:                     } elsif ($key eq 'crsownerchg') {
11048:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
11049:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
11050:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
11051:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
11052:                             } else {
11053:                                 my %crsownerstr;
11054:                                 foreach my $item ('by','for') {
11055:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
11056:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
11057:                                             if ($type eq 'default') {
11058:                                                 $crsownerstr{$item} .= $othertitle.', ';
11059:                                             } elsif ($usertypes->{$type} ne '') {
11060:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
11061:                                             }
11062:                                         }
11063:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
11064:                                     }
11065:                                 }
11066:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
11067:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
11068:                             }
11069:                         } else {
11070:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
11071:                         }
11072:                     }
11073:                     $resulttext .= '</ul></li>';
11074:                 }
11075:             }
11076:             $resulttext .= '</ul>';
11077:         } else {
11078:             $resulttext = &mt('No changes made to password settings');
11079:         }
11080:         my $cachetime = 24*60*60;
11081:         if ($updatedefaults) {
11082:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11083:             if (ref($lastactref) eq 'HASH') {
11084:                 $lastactref->{'domdefaults'} = 1;
11085:             }
11086:         }
11087:         if ($updateconf) {
11088:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
11089:             if (ref($lastactref) eq 'HASH') {
11090:                 $lastactref->{'passwdconf'} = 1;
11091:             }
11092:         }
11093:     } else {
11094:         $resulttext = '<span class="LC_error">'.
11095:             &mt('An error occurred: [_1]',$putresult).'</span>';
11096:     }
11097:     if ($errors) {
11098:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
11099:                        $errors.'</ul></p>';
11100:     }
11101:     return $resulttext;
11102: }
11103: 
11104: sub modify_usercreation {
11105:     my ($dom,%domconfig) = @_;
11106:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
11107:     my $warningmsg;
11108:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11109:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11110:             if ($key eq 'cancreate') {
11111:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11112:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11113:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
11114:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11115:                         } else {
11116:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11117:                         }
11118:                     }
11119:                 }
11120:             } elsif ($key eq 'email_rule') {
11121:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11122:             } else {
11123:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11124:             }
11125:         }
11126:     }
11127:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
11128:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
11129:     my @contexts = ('author','course','requestcrs');
11130:     foreach my $item(@contexts) {
11131:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
11132:     }
11133:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11134:         foreach my $item (@contexts) {
11135:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
11136:                 push(@{$changes{'cancreate'}},$item);
11137:             }
11138:         }
11139:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
11140:         foreach my $item (@contexts) {
11141:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
11142:                 if ($cancreate{$item} ne 'any') {
11143:                     push(@{$changes{'cancreate'}},$item);
11144:                 }
11145:             } else {
11146:                 if ($cancreate{$item} ne 'none') {
11147:                     push(@{$changes{'cancreate'}},$item);
11148:                 }
11149:             }
11150:         }
11151:     } else {
11152:         foreach my $item (@contexts)  {
11153:             push(@{$changes{'cancreate'}},$item);
11154:         }
11155:     }
11156: 
11157:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
11158:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
11159:             if (!grep(/^\Q$type\E$/,@username_rule)) {
11160:                 push(@{$changes{'username_rule'}},$type);
11161:             }
11162:         }
11163:         foreach my $type (@username_rule) {
11164:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
11165:                 push(@{$changes{'username_rule'}},$type);
11166:             }
11167:         }
11168:     } else {
11169:         push(@{$changes{'username_rule'}},@username_rule);
11170:     }
11171: 
11172:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
11173:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
11174:             if (!grep(/^\Q$type\E$/,@id_rule)) {
11175:                 push(@{$changes{'id_rule'}},$type);
11176:             }
11177:         }
11178:         foreach my $type (@id_rule) {
11179:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11180:                 push(@{$changes{'id_rule'}},$type);
11181:             }
11182:         }
11183:     } else {
11184:         push(@{$changes{'id_rule'}},@id_rule);
11185:     }
11186: 
11187:     my @authen_contexts = ('author','course','domain');
11188:     my @authtypes = ('int','krb4','krb5','loc');
11189:     my %authhash;
11190:     foreach my $item (@authen_contexts) {
11191:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11192:         foreach my $auth (@authtypes) {
11193:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11194:                 $authhash{$item}{$auth} = 1;
11195:             } else {
11196:                 $authhash{$item}{$auth} = 0;
11197:             }
11198:         }
11199:     }
11200:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11201:         foreach my $item (@authen_contexts) {
11202:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11203:                 foreach my $auth (@authtypes) {
11204:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11205:                         push(@{$changes{'authtypes'}},$item);
11206:                         last;
11207:                     }
11208:                 }
11209:             }
11210:         }
11211:     } else {
11212:         foreach my $item (@authen_contexts) {
11213:             push(@{$changes{'authtypes'}},$item);
11214:         }
11215:     }
11216: 
11217:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11218:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11219:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11220:     $save_usercreate{'id_rule'} = \@id_rule;
11221:     $save_usercreate{'username_rule'} = \@username_rule,
11222:     $save_usercreate{'authtypes'} = \%authhash;
11223: 
11224:     my %usercreation_hash =  (
11225:         usercreation     => \%save_usercreate,
11226:     );
11227: 
11228:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11229:                                              $dom);
11230: 
11231:     if ($putresult eq 'ok') {
11232:         if (keys(%changes) > 0) {
11233:             $resulttext = &mt('Changes made:').'<ul>';
11234:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11235:                 my %lt = &usercreation_types();
11236:                 foreach my $type (@{$changes{'cancreate'}}) {
11237:                     my $chgtext = $lt{$type}.', ';
11238:                     if ($cancreate{$type} eq 'none') {
11239:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11240:                     } elsif ($cancreate{$type} eq 'any') {
11241:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11242:                     } elsif ($cancreate{$type} eq 'official') {
11243:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11244:                     } elsif ($cancreate{$type} eq 'unofficial') {
11245:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11246:                     }
11247:                     $resulttext .= '<li>'.$chgtext.'</li>';
11248:                 }
11249:             }
11250:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11251:                 my ($rules,$ruleorder) = 
11252:                     &Apache::lonnet::inst_userrules($dom,'username');
11253:                 my $chgtext = '<ul>';
11254:                 foreach my $type (@username_rule) {
11255:                     if (ref($rules->{$type}) eq 'HASH') {
11256:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11257:                     }
11258:                 }
11259:                 $chgtext .= '</ul>';
11260:                 if (@username_rule > 0) {
11261:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11262:                 } else {
11263:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11264:                 }
11265:             }
11266:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11267:                 my ($idrules,$idruleorder) = 
11268:                     &Apache::lonnet::inst_userrules($dom,'id');
11269:                 my $chgtext = '<ul>';
11270:                 foreach my $type (@id_rule) {
11271:                     if (ref($idrules->{$type}) eq 'HASH') {
11272:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11273:                     }
11274:                 }
11275:                 $chgtext .= '</ul>';
11276:                 if (@id_rule > 0) {
11277:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11278:                 } else {
11279:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11280:                 }
11281:             }
11282:             my %authname = &authtype_names();
11283:             my %context_title = &context_names();
11284:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11285:                 my $chgtext = '<ul>';
11286:                 foreach my $type (@{$changes{'authtypes'}}) {
11287:                     my @allowed;
11288:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11289:                     foreach my $auth (@authtypes) {
11290:                         if ($authhash{$type}{$auth}) {
11291:                             push(@allowed,$authname{$auth});
11292:                         }
11293:                     }
11294:                     if (@allowed > 0) {
11295:                         $chgtext .= join(', ',@allowed).'</li>';
11296:                     } else {
11297:                         $chgtext .= &mt('none').'</li>';
11298:                     }
11299:                 }
11300:                 $chgtext .= '</ul>';
11301:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11302:                 $resulttext .= '</li>';
11303:             }
11304:             $resulttext .= '</ul>';
11305:         } else {
11306:             $resulttext = &mt('No changes made to user creation settings');
11307:         }
11308:     } else {
11309:         $resulttext = '<span class="LC_error">'.
11310:             &mt('An error occurred: [_1]',$putresult).'</span>';
11311:     }
11312:     if ($warningmsg ne '') {
11313:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11314:     }
11315:     return $resulttext;
11316: }
11317: 
11318: sub modify_selfcreation {
11319:     my ($dom,$lastactref,%domconfig) = @_;
11320:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
11321:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
11322:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11323:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
11324:     if (ref($typesref) eq 'ARRAY') {
11325:         @types = @{$typesref};
11326:     }
11327:     if (ref($usertypesref) eq 'HASH') {
11328:         %usertypes = %{$usertypesref};
11329:     }
11330:     $usertypes{'default'} = $othertitle;
11331: #
11332: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11333: #
11334:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11335:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11336:             if ($key eq 'cancreate') {
11337:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11338:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11339:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11340:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
11341:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
11342:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
11343:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
11344:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
11345:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11346:                         } else {
11347:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11348:                         }
11349:                     }
11350:                 }
11351:             } elsif ($key eq 'email_rule') {
11352:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11353:             } else {
11354:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11355:             }
11356:         }
11357:     }
11358: #
11359: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11360: #
11361:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11362:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11363:             if ($key eq 'selfcreate') {
11364:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11365:             } else {
11366:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11367:             }
11368:         }
11369:     }
11370: #
11371: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
11372: #
11373:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11374:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
11375:             if ($key eq 'inststatusguest') {
11376:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
11377:             } else {
11378:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
11379:             }
11380:         }
11381:     }
11382: 
11383:     my @contexts = ('selfcreate');
11384:     @{$cancreate{'selfcreate'}} = ();
11385:     %{$cancreate{'emailusername'}} = ();
11386:     if (@types) {
11387:         @{$cancreate{'statustocreate'}} = ();
11388:     }
11389:     %{$cancreate{'selfcreateprocessing'}} = ();
11390:     %{$cancreate{'shibenv'}} = ();
11391:     %{$cancreate{'emailverified'}} = ();
11392:     %{$cancreate{'emailoptions'}} = ();
11393:     %{$cancreate{'emaildomain'}} = ();
11394:     my %selfcreatetypes = (
11395:                              sso   => 'users authenticated by institutional single sign on',
11396:                              login => 'users authenticated by institutional log-in',
11397:                              email => 'users verified by e-mail',
11398:                           );
11399: #
11400: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11401: # is permitted.
11402: #
11403: 
11404:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
11405: 
11406:     my (@statuses,%email_rule);
11407:     foreach my $item ('login','sso','email') {
11408:         if ($item eq 'email') {
11409:             if ($env{'form.cancreate_email'}) {
11410:                 if (@types) {
11411:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
11412:                     foreach my $status (@poss_statuses) {
11413:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
11414:                             push(@statuses,$status);
11415:                         }
11416:                     }
11417:                     $save_inststatus{'inststatusguest'} = \@statuses;
11418:                 } else {
11419:                     push(@statuses,'default');
11420:                 }
11421:                 if (@statuses) {
11422:                     my %curr_rule;
11423:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11424:                         foreach my $type (@statuses) {
11425:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
11426:                         }
11427:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
11428:                         foreach my $type (@statuses) {
11429:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
11430:                         }
11431:                     }
11432:                     push(@{$cancreate{'selfcreate'}},'email');
11433:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
11434:                     my %curremaildom;
11435:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
11436:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
11437:                     }
11438:                     foreach my $type (@statuses) {
11439:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
11440:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11441:                         }
11442:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
11443:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
11444:                         }
11445:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
11446: #
11447: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
11448: #
11449:                             my $chosen = $1;
11450:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
11451:                                 my $emaildom;
11452:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
11453:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
11454:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
11455:                                     if (ref($curremaildom{$type}) eq 'HASH') {
11456:                                         if (exists($curremaildom{$type}{$chosen})) {
11457:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
11458:                                                 push(@{$changes{'cancreate'}},'emaildomain');
11459:                                             }
11460:                                         } elsif ($emaildom ne '') {
11461:                                             push(@{$changes{'cancreate'}},'emaildomain');
11462:                                         }
11463:                                     } elsif ($emaildom ne '') {
11464:                                         push(@{$changes{'cancreate'}},'emaildomain');
11465:                                     }
11466:                                 }
11467:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11468:                             } elsif ($chosen eq 'custom') {
11469:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
11470:                                 $email_rule{$type} = [];
11471:                                 if (ref($emailrules) eq 'HASH') {
11472:                                     foreach my $rule (@possemail_rules) {
11473:                                         if (exists($emailrules->{$rule})) {
11474:                                             push(@{$email_rule{$type}},$rule);
11475:                                         }
11476:                                     }
11477:                                 }
11478:                                 if (@{$email_rule{$type}}) {
11479:                                     $cancreate{'emailoptions'}{$type} = 'custom';
11480:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
11481:                                         if (@{$curr_rule{$type}} > 0) {
11482:                                             foreach my $rule (@{$curr_rule{$type}}) {
11483:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
11484:                                                     push(@{$changes{'email_rule'}},$type);
11485:                                                 }
11486:                                             }
11487:                                         }
11488:                                         foreach my $type (@{$email_rule{$type}}) {
11489:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
11490:                                                 push(@{$changes{'email_rule'}},$type);
11491:                                             }
11492:                                         }
11493:                                     } else {
11494:                                         push(@{$changes{'email_rule'}},$type);
11495:                                     }
11496:                                 }
11497:                             } else {
11498:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11499:                             }
11500:                         }
11501:                     }
11502:                     if (@types) {
11503:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11504:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
11505:                             if (@changed) {
11506:                                 push(@{$changes{'inststatus'}},'inststatusguest');
11507:                             }
11508:                         } else {
11509:                             push(@{$changes{'inststatus'}},'inststatusguest');
11510:                         }
11511:                     }
11512:                 } else {
11513:                     delete($env{'form.cancreate_email'});
11514:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11515:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11516:                             push(@{$changes{'inststatus'}},'inststatusguest');
11517:                         }
11518:                     }
11519:                 }
11520:             } else {
11521:                 $save_inststatus{'inststatusguest'} = [];
11522:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11523:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11524:                         push(@{$changes{'inststatus'}},'inststatusguest');
11525:                     }
11526:                 }
11527:             }
11528:         } else {
11529:             if ($env{'form.cancreate_'.$item}) {
11530:                 push(@{$cancreate{'selfcreate'}},$item);
11531:             }
11532:         }
11533:     }
11534:     my (%userinfo,%savecaptcha);
11535:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11536: #
11537: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11538: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11539: #
11540: 
11541:     if ($env{'form.cancreate_email'}) {
11542:         push(@contexts,'emailusername');
11543:         if (@statuses) {
11544:             foreach my $type (@statuses) {
11545:                 if (ref($infofields) eq 'ARRAY') {
11546:                     foreach my $field (@{$infofields}) {
11547:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11548:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11549:                         }
11550:                     }
11551:                 }
11552:             }
11553:         }
11554: #
11555: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11556: # queued requests for self-creation of account verified by e-mail.
11557: #
11558: 
11559:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11560:         @approvalnotify = sort(@approvalnotify);
11561:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11562:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11563:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11564:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11565:                     push(@{$changes{'cancreate'}},'notify');
11566:                 }
11567:             } else {
11568:                 if ($cancreate{'notify'}{'approval'}) {
11569:                     push(@{$changes{'cancreate'}},'notify');
11570:                 }
11571:             }
11572:         } elsif ($cancreate{'notify'}{'approval'}) {
11573:             push(@{$changes{'cancreate'}},'notify');
11574:         }
11575: 
11576:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11577:     }
11578: #  
11579: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11580: # institutional log-in.
11581: #
11582:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11583:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11584:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11585:             $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.').' '.
11586:                           &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.');
11587:         }
11588:     }
11589:     my @fields = ('lastname','firstname','middlename','generation',
11590:                   'permanentemail','id');
11591:     my @shibfields = (@fields,'inststatus');
11592:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11593: #
11594: # Where usernames may created for institutional log-in and/or institutional single sign on:
11595: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11596: # may self-create accounts 
11597: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11598: # which the user may supply, if institutional data is unavailable.
11599: #
11600:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11601:         if (@types) {
11602:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11603:             push(@contexts,'statustocreate');
11604:             foreach my $type (@types) {
11605:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11606:                 foreach my $field (@fields) {
11607:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11608:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11609:                     } else {
11610:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11611:                     }
11612:                 }
11613:             }
11614:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11615:                 foreach my $type (@types) {
11616:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11617:                         foreach my $field (@fields) {
11618:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11619:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11620:                                 push(@{$changes{'selfcreate'}},$type);
11621:                                 last;
11622:                             }
11623:                         }
11624:                     }
11625:                 }
11626:             } else {
11627:                 foreach my $type (@types) {
11628:                     push(@{$changes{'selfcreate'}},$type);
11629:                 }
11630:             }
11631:         }
11632:         foreach my $field (@shibfields) {
11633:             if ($env{'form.shibenv_'.$field} ne '') {
11634:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11635:             }
11636:         }
11637:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11638:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11639:                 foreach my $field (@shibfields) {
11640:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11641:                         push(@{$changes{'cancreate'}},'shibenv');
11642:                     }
11643:                 }
11644:             } else {
11645:                 foreach my $field (@shibfields) {
11646:                     if ($env{'form.shibenv_'.$field}) {
11647:                         push(@{$changes{'cancreate'}},'shibenv');
11648:                         last;
11649:                     }
11650:                 }
11651:             }
11652:         }
11653:     }
11654:     foreach my $item (@contexts) {
11655:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11656:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11657:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11658:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11659:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11660:                             push(@{$changes{'cancreate'}},$item);
11661:                         }
11662:                     }
11663:                 }
11664:             }
11665:             if (ref($cancreate{$item}) eq 'ARRAY') {
11666:                 foreach my $type (@{$cancreate{$item}}) {
11667:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11668:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11669:                             push(@{$changes{'cancreate'}},$item);
11670:                         }
11671:                     }
11672:                 }
11673:             }
11674:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11675:             if (ref($cancreate{$item}) eq 'HASH') {
11676:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11677:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
11678:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
11679:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
11680:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11681:                                     push(@{$changes{'cancreate'}},$item);
11682:                                 }
11683:                             }
11684:                         }
11685:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11686:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
11687:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11688:                                 push(@{$changes{'cancreate'}},$item);
11689:                             }
11690:                         }
11691:                     }
11692:                 }
11693:                 foreach my $type (keys(%{$cancreate{$item}})) {
11694:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11695:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11696:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
11697:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
11698:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11699:                                         push(@{$changes{'cancreate'}},$item);
11700:                                     }
11701:                                 }
11702:                             } else {
11703:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11704:                                     push(@{$changes{'cancreate'}},$item);
11705:                                 }
11706:                             }
11707:                         }
11708:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11709:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
11710:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11711:                                 push(@{$changes{'cancreate'}},$item);
11712:                             }
11713:                         }
11714:                     }
11715:                 }
11716:             }
11717:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11718:             if (ref($cancreate{$item}) eq 'ARRAY') {
11719:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11720:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11721:                         push(@{$changes{'cancreate'}},$item);
11722:                     }
11723:                 }
11724:             }
11725:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11726:             if (ref($cancreate{$item}) eq 'HASH') {
11727:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11728:                     push(@{$changes{'cancreate'}},$item);
11729:                 }
11730:             }
11731:         } elsif ($item eq 'emailusername') {
11732:             if (ref($cancreate{$item}) eq 'HASH') {
11733:                 foreach my $type (keys(%{$cancreate{$item}})) {
11734:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11735:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11736:                             if ($cancreate{$item}{$type}{$field}) {
11737:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11738:                                     push(@{$changes{'cancreate'}},$item);
11739:                                 }
11740:                                 last;
11741:                             }
11742:                         }
11743:                     }
11744:                 }
11745:             }
11746:         }
11747:     }
11748: #
11749: # Populate %save_usercreate hash with updates to self-creation configuration.
11750: #
11751:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11752:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11753:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11754:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11755:     if (ref($cancreate{'notify'}) eq 'HASH') {
11756:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11757:     }
11758:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11759:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11760:     }
11761:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
11762:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
11763:     }
11764:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
11765:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
11766:     }
11767:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
11768:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
11769:     }
11770:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11771:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11772:     }
11773:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11774:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11775:     }
11776:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11777:     $save_usercreate{'email_rule'} = \%email_rule;
11778: 
11779:     my %userconfig_hash = (
11780:             usercreation     => \%save_usercreate,
11781:             usermodification => \%save_usermodify,
11782:             inststatus       => \%save_inststatus,
11783:     );
11784: 
11785:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11786:                                              $dom);
11787: #
11788: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
11789: #
11790:     if ($putresult eq 'ok') {
11791:         if (keys(%changes) > 0) {
11792:             $resulttext = &mt('Changes made:').'<ul>';
11793:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11794:                 my %lt = &selfcreation_types();
11795:                 foreach my $type (@{$changes{'cancreate'}}) {
11796:                     my $chgtext = '';
11797:                     if ($type eq 'selfcreate') {
11798:                         if (@{$cancreate{$type}} == 0) {
11799:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11800:                         } else {
11801:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11802:                                         '<ul>';
11803:                             foreach my $case (@{$cancreate{$type}}) {
11804:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11805:                             }
11806:                             $chgtext .= '</ul>';
11807:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11808:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11809:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11810:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11811:                                             $chgtext .= '<span class="LC_warning">'.
11812:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
11813:                                                         '</span><br />';
11814:                                         }
11815:                                     }
11816:                                 }
11817:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
11818:                                     if (!@statuses) {
11819:                                         $chgtext .= '<span class="LC_warning">'.
11820:                                                     &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.").
11821:                                                     '</span><br />';
11822: 
11823:                                     }
11824:                                 }
11825:                             }
11826:                         }
11827:                     } elsif ($type eq 'shibenv') {
11828:                         if (keys(%{$cancreate{$type}}) == 0) {
11829:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
11830:                         } else {
11831:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11832:                                         '<ul>';
11833:                             foreach my $field (@shibfields) {
11834:                                 next if ($cancreate{$type}{$field} eq '');
11835:                                 if ($field eq 'inststatus') {
11836:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11837:                                 } else {
11838:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11839:                                 }
11840:                             }
11841:                             $chgtext .= '</ul>';
11842:                         }
11843:                     } elsif ($type eq 'statustocreate') {
11844:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11845:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11846:                             if (@{$cancreate{'selfcreate'}} > 0) {
11847:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11848:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11849:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11850:                                         $chgtext .= '<br />'.
11851:                                                     '<span class="LC_warning">'.
11852:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11853:                                                     '</span>';
11854:                                     }
11855:                                 } elsif (keys(%usertypes) > 0) {
11856:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11857:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11858:                                     } else {
11859:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11860:                                     }
11861:                                     $chgtext .= '<ul>';
11862:                                     foreach my $case (@{$cancreate{$type}}) {
11863:                                         if ($case eq 'default') {
11864:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11865:                                         } else {
11866:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
11867:                                         }
11868:                                     }
11869:                                     $chgtext .= '</ul>';
11870:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11871:                                         $chgtext .= '<span class="LC_warning">'.
11872:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11873:                                                     '</span>';
11874:                                     }
11875:                                 }
11876:                             } else {
11877:                                 if (@{$cancreate{$type}} == 0) {
11878:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11879:                                 } else {
11880:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11881:                                 }
11882:                             }
11883:                             $chgtext .= '<br />';
11884:                         }
11885:                     } elsif ($type eq 'selfcreateprocessing') {
11886:                         my %choices = &Apache::lonlocal::texthash (
11887:                                                                     automatic => 'Automatic approval',
11888:                                                                     approval  => 'Queued for approval',
11889:                                                                   );
11890:                         if (@types) {
11891:                             if (@statuses) {
11892:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
11893:                                             '<ul>';
11894:                                 foreach my $status (@statuses) {
11895:                                     if ($status eq 'default') {
11896:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
11897:                                     } else {
11898:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
11899:                                     }
11900:                                 }
11901:                                 $chgtext .= '</ul>';
11902:                             }
11903:                         } else {
11904:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
11905:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11906:                         }
11907:                     } elsif ($type eq 'emailverified') {
11908:                         my %options = &Apache::lonlocal::texthash (
11909:                                                                     all   => 'Same as e-mail',
11910:                                                                     first => 'Omit @domain',
11911:                                                                     free  => 'Free to choose',
11912:                                                                   );
11913:                         if (@types) {
11914:                             if (@statuses) {
11915:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
11916:                                             '<ul>';
11917:                                 foreach my $status (@statuses) {
11918:                                     if ($status eq 'default') {
11919:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
11920:                                     } else {
11921:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
11922:                                     }
11923:                                 }
11924:                                 $chgtext .= '</ul>';
11925:                             }
11926:                         } else {
11927:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
11928:                                             $options{$cancreate{'emailverified'}{'default'}});
11929:                         }
11930:                     } elsif ($type eq 'emailoptions') {
11931:                         my %options = &Apache::lonlocal::texthash (
11932:                                                                     any     => 'Any e-mail',
11933:                                                                     inst    => 'Institutional only',
11934:                                                                     noninst => 'Non-institutional only',
11935:                                                                     custom  => 'Custom restrictions',
11936:                                                                   );
11937:                         if (@types) {
11938:                             if (@statuses) {
11939:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
11940:                                             '<ul>';
11941:                                 foreach my $status (@statuses) {
11942:                                     if ($type eq 'default') {
11943:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
11944:                                     } else {
11945:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
11946:                                     }
11947:                                 }
11948:                                 $chgtext .= '</ul>';
11949:                             }
11950:                         } else {
11951:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
11952:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
11953:                             } else {
11954:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
11955:                                                 $options{$cancreate{'emailoptions'}{'default'}});
11956:                             }
11957:                         }
11958:                     } elsif ($type eq 'emaildomain') {
11959:                         my $output;
11960:                         if (@statuses) {
11961:                             foreach my $type (@statuses) {
11962:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
11963:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
11964:                                         if ($type eq 'default') {
11965:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
11966:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
11967:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
11968:                                             } else {
11969:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
11970:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
11971:                                             }
11972:                                         } else {
11973:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
11974:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
11975:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
11976:                                             } else {
11977:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
11978:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
11979:                                             }
11980:                                         }
11981:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
11982:                                         if ($type eq 'default') {
11983:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
11984:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
11985:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
11986:                                             } else {
11987:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
11988:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
11989:                                             }
11990:                                         } else {
11991:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
11992:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
11993:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
11994:                                             } else {
11995:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
11996:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
11997:                                             }
11998:                                         }
11999:                                     }
12000:                                 }
12001:                             }
12002:                         }
12003:                         if ($output ne '') {
12004:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
12005:                                         '<ul>'.$output.'</ul>';
12006:                         }
12007:                     } elsif ($type eq 'captcha') {
12008:                         if ($savecaptcha{$type} eq 'notused') {
12009:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
12010:                         } else {
12011:                             my %captchas = &captcha_phrases();
12012:                             if ($captchas{$savecaptcha{$type}}) {
12013:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
12014:                             } else {
12015:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
12016:                             }
12017:                         }
12018:                     } elsif ($type eq 'recaptchakeys') {
12019:                         my ($privkey,$pubkey);
12020:                         if (ref($savecaptcha{$type}) eq 'HASH') {
12021:                             $pubkey = $savecaptcha{$type}{'public'};
12022:                             $privkey = $savecaptcha{$type}{'private'};
12023:                         }
12024:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
12025:                         if (!$pubkey) {
12026:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
12027:                         } else {
12028:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12029:                         }
12030:                         if (!$privkey) {
12031:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
12032:                         } else {
12033:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
12034:                         }
12035:                         $chgtext .= '</ul>';
12036:                     } elsif ($type eq 'recaptchaversion') {
12037:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
12038:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
12039:                         }
12040:                     } elsif ($type eq 'emailusername') {
12041:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
12042:                             if (@statuses) {
12043:                                 foreach my $type (@statuses) {
12044:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
12045:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
12046:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
12047:                                                     '<ul>';
12048:                                             foreach my $field (@{$infofields}) {
12049:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
12050:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
12051:                                                 }
12052:                                             }
12053:                                             $chgtext .= '</ul>';
12054:                                         } else {
12055:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12056:                                         }
12057:                                     } else {
12058:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12059:                                     }
12060:                                 }
12061:                             }
12062:                         }
12063:                     } elsif ($type eq 'notify') {
12064:                         my $numapprove = 0;
12065:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
12066:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
12067:                                 if ($cancreate{'notify'}{'approval'}) {
12068:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
12069:                                     $numapprove ++;
12070:                                 }
12071:                             }
12072:                         }
12073:                         unless ($numapprove) {
12074:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
12075:                         }
12076:                     }
12077:                     if ($chgtext) {
12078:                         $resulttext .= '<li>'.$chgtext.'</li>';
12079:                     }
12080:                 }
12081:             }
12082:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
12083:                 my ($emailrules,$emailruleorder) =
12084:                     &Apache::lonnet::inst_userrules($dom,'email');
12085:                 foreach my $type (@{$changes{'email_rule'}}) {
12086:                     if (ref($email_rule{$type}) eq 'ARRAY') {
12087:                         my $chgtext = '<ul>';
12088:                         foreach my $rule (@{$email_rule{$type}}) {
12089:                             if (ref($emailrules->{$rule}) eq 'HASH') {
12090:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
12091:                             }
12092:                         }
12093:                         $chgtext .= '</ul>';
12094:                         my $typename;
12095:                         if (@types) {
12096:                             if ($type eq 'default') {
12097:                                 $typename = $othertitle;
12098:                             } else {
12099:                                 $typename = $usertypes{$type};
12100:                             }
12101:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
12102:                         }
12103:                         if (@{$email_rule{$type}} > 0) {
12104:                             $resulttext .= '<li>'.
12105:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
12106:                                                $usertypes{$type}).
12107:                                            $chgtext.
12108:                                            '</li>';
12109:                         } else {
12110:                             $resulttext .= '<li>'.
12111:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
12112:                                            '</li>'.
12113:                                            &mt('(Affiliation: [_1])',$typename);
12114:                         }
12115:                     }
12116:                 }
12117:             }
12118:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
12119:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
12120:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
12121:                         my $chgtext = '<ul>';
12122:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
12123:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
12124:                         }
12125:                         $chgtext .= '</ul>';
12126:                         $resulttext .= '<li>'.
12127:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
12128:                                           $chgtext.
12129:                                        '</li>';
12130:                     } else {
12131:                         $resulttext .= '<li>'.
12132:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
12133:                                        '</li>';
12134:                     }
12135:                 }
12136:             }
12137:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
12138:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
12139:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12140:                 foreach my $type (@{$changes{'selfcreate'}}) {
12141:                     my $typename = $type;
12142:                     if (keys(%usertypes) > 0) {
12143:                         if ($usertypes{$type} ne '') {
12144:                             $typename = $usertypes{$type};
12145:                         }
12146:                     }
12147:                     my @modifiable;
12148:                     $resulttext .= '<li>'.
12149:                                     &mt('Self-creation of account by users with status: [_1]',
12150:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
12151:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
12152:                     foreach my $field (@fields) {
12153:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
12154:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
12155:                         }
12156:                     }
12157:                     if (@modifiable > 0) {
12158:                         $resulttext .= join(', ',@modifiable);
12159:                     } else {
12160:                         $resulttext .= &mt('none');
12161:                     }
12162:                     $resulttext .= '</li>';
12163:                 }
12164:                 $resulttext .= '</ul></li>';
12165:             }
12166:             $resulttext .= '</ul>';
12167:             my $cachetime = 24*60*60;
12168:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
12169:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12170:             if (ref($lastactref) eq 'HASH') {
12171:                 $lastactref->{'domdefaults'} = 1;
12172:             }
12173:         } else {
12174:             $resulttext = &mt('No changes made to self-creation settings');
12175:         }
12176:     } else {
12177:         $resulttext = '<span class="LC_error">'.
12178:             &mt('An error occurred: [_1]',$putresult).'</span>';
12179:     }
12180:     if ($warningmsg ne '') {
12181:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
12182:     }
12183:     return $resulttext;
12184: }
12185: 
12186: sub process_captcha {
12187:     my ($container,$changes,$newsettings,$current) = @_;
12188:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
12189:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
12190:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
12191:         $newsettings->{'captcha'} = 'original';
12192:     }
12193:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
12194:         if ($container eq 'cancreate') {
12195:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12196:                 push(@{$changes->{'cancreate'}},'captcha');
12197:             } elsif (!defined($changes->{'cancreate'})) {
12198:                 $changes->{'cancreate'} = ['captcha'];
12199:             }
12200:         } else {
12201:             $changes->{'captcha'} = 1;
12202:         }
12203:     }
12204:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
12205:     if ($newsettings->{'captcha'} eq 'recaptcha') {
12206:         $newpub = $env{'form.'.$container.'_recaptchapub'};
12207:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
12208:         $newpub =~ s/[^\w\-]//g;
12209:         $newpriv =~ s/[^\w\-]//g;
12210:         $newsettings->{'recaptchakeys'} = {
12211:                                              public  => $newpub,
12212:                                              private => $newpriv,
12213:                                           };
12214:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
12215:         $newversion =~ s/\D//g;
12216:         if ($newversion ne '2') {
12217:             $newversion = 1;
12218:         }
12219:         $newsettings->{'recaptchaversion'} = $newversion;
12220:     }
12221:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
12222:         $currpub = $current->{'recaptchakeys'}{'public'};
12223:         $currpriv = $current->{'recaptchakeys'}{'private'};
12224:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
12225:             $newsettings->{'recaptchakeys'} = {
12226:                                                  public  => '',
12227:                                                  private => '',
12228:                                               }
12229:         }
12230:     }
12231:     if ($current->{'captcha'} eq 'recaptcha') {
12232:         $currversion = $current->{'recaptchaversion'};
12233:         if ($currversion ne '2') {
12234:             $currversion = 1;
12235:         }
12236:     }
12237:     if ($currversion ne $newversion) {
12238:         if ($container eq 'cancreate') {
12239:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12240:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
12241:             } elsif (!defined($changes->{'cancreate'})) {
12242:                 $changes->{'cancreate'} = ['recaptchaversion'];
12243:             }
12244:         } else {
12245:             $changes->{'recaptchaversion'} = 1;
12246:         }
12247:     }
12248:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
12249:         if ($container eq 'cancreate') {
12250:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12251:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
12252:             } elsif (!defined($changes->{'cancreate'})) {
12253:                 $changes->{'cancreate'} = ['recaptchakeys'];
12254:             }
12255:         } else {
12256:             $changes->{'recaptchakeys'} = 1;
12257:         }
12258:     }
12259:     return;
12260: }
12261: 
12262: sub modify_usermodification {
12263:     my ($dom,%domconfig) = @_;
12264:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
12265:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
12266:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
12267:             if ($key eq 'selfcreate') {
12268:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
12269:             } else {  
12270:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
12271:             }
12272:         }
12273:     }
12274:     my @contexts = ('author','course');
12275:     my %context_title = (
12276:                            author => 'In author context',
12277:                            course => 'In course context',
12278:                         );
12279:     my @fields = ('lastname','firstname','middlename','generation',
12280:                   'permanentemail','id');
12281:     my %roles = (
12282:                   author => ['ca','aa'],
12283:                   course => ['st','ep','ta','in','cr'],
12284:                 );
12285:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12286:     foreach my $context (@contexts) {
12287:         foreach my $role (@{$roles{$context}}) {
12288:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
12289:             foreach my $item (@fields) {
12290:                 if (grep(/^\Q$item\E$/,@modifiable)) {
12291:                     $modifyhash{$context}{$role}{$item} = 1;
12292:                 } else {
12293:                     $modifyhash{$context}{$role}{$item} = 0;
12294:                 }
12295:             }
12296:         }
12297:         if (ref($curr_usermodification{$context}) eq 'HASH') {
12298:             foreach my $role (@{$roles{$context}}) {
12299:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
12300:                     foreach my $field (@fields) {
12301:                         if ($modifyhash{$context}{$role}{$field} ne 
12302:                                 $curr_usermodification{$context}{$role}{$field}) {
12303:                             push(@{$changes{$context}},$role);
12304:                             last;
12305:                         }
12306:                     }
12307:                 }
12308:             }
12309:         } else {
12310:             foreach my $context (@contexts) {
12311:                 foreach my $role (@{$roles{$context}}) {
12312:                     push(@{$changes{$context}},$role);
12313:                 }
12314:             }
12315:         }
12316:     }
12317:     my %usermodification_hash =  (
12318:                                    usermodification => \%modifyhash,
12319:                                  );
12320:     my $putresult = &Apache::lonnet::put_dom('configuration',
12321:                                              \%usermodification_hash,$dom);
12322:     if ($putresult eq 'ok') {
12323:         if (keys(%changes) > 0) {
12324:             $resulttext = &mt('Changes made: ').'<ul>';
12325:             foreach my $context (@contexts) {
12326:                 if (ref($changes{$context}) eq 'ARRAY') {
12327:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
12328:                     if (ref($changes{$context}) eq 'ARRAY') {
12329:                         foreach my $role (@{$changes{$context}}) {
12330:                             my $rolename;
12331:                             if ($role eq 'cr') {
12332:                                 $rolename = &mt('Custom');
12333:                             } else {
12334:                                 $rolename = &Apache::lonnet::plaintext($role);
12335:                             }
12336:                             my @modifiable;
12337:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
12338:                             foreach my $field (@fields) {
12339:                                 if ($modifyhash{$context}{$role}{$field}) {
12340:                                     push(@modifiable,$fieldtitles{$field});
12341:                                 }
12342:                             }
12343:                             if (@modifiable > 0) {
12344:                                 $resulttext .= join(', ',@modifiable);
12345:                             } else {
12346:                                 $resulttext .= &mt('none'); 
12347:                             }
12348:                             $resulttext .= '</li>';
12349:                         }
12350:                         $resulttext .= '</ul></li>';
12351:                     }
12352:                 }
12353:             }
12354:             $resulttext .= '</ul>';
12355:         } else {
12356:             $resulttext = &mt('No changes made to user modification settings');
12357:         }
12358:     } else {
12359:         $resulttext = '<span class="LC_error">'.
12360:             &mt('An error occurred: [_1]',$putresult).'</span>';
12361:     }
12362:     return $resulttext;
12363: }
12364: 
12365: sub modify_defaults {
12366:     my ($dom,$lastactref,%domconfig) = @_;
12367:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
12368:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12369:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
12370:                  'portal_def');
12371:     my @authtypes = ('internal','krb4','krb5','localauth');
12372:     foreach my $item (@items) {
12373:         $newvalues{$item} = $env{'form.'.$item};
12374:         if ($item eq 'auth_def') {
12375:             if ($newvalues{$item} ne '') {
12376:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
12377:                     push(@errors,$item);
12378:                 }
12379:             }
12380:         } elsif ($item eq 'lang_def') {
12381:             if ($newvalues{$item} ne '') {
12382:                 if ($newvalues{$item} =~ /^(\w+)/) {
12383:                     my $langcode = $1;
12384:                     if ($langcode ne 'x_chef') {
12385:                         if (code2language($langcode) eq '') {
12386:                             push(@errors,$item);
12387:                         }
12388:                     }
12389:                 } else {
12390:                     push(@errors,$item);
12391:                 }
12392:             }
12393:         } elsif ($item eq 'timezone_def') {
12394:             if ($newvalues{$item} ne '') {
12395:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
12396:                     push(@errors,$item);   
12397:                 }
12398:             }
12399:         } elsif ($item eq 'datelocale_def') {
12400:             if ($newvalues{$item} ne '') {
12401:                 my @datelocale_ids = DateTime::Locale->ids();
12402:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
12403:                     push(@errors,$item);
12404:                 }
12405:             }
12406:         } elsif ($item eq 'portal_def') {
12407:             if ($newvalues{$item} ne '') {
12408:                 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])\/?$/) {
12409:                     push(@errors,$item);
12410:                 }
12411:             }
12412:         }
12413:         if (grep(/^\Q$item\E$/,@errors)) {
12414:             $newvalues{$item} = $domdefaults{$item};
12415:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12416:             $changes{$item} = 1;
12417:         }
12418:         $domdefaults{$item} = $newvalues{$item};
12419:     }
12420:     my %staticdefaults = (
12421:                            'intauth_cost'   => 10,
12422:                            'intauth_check'  => 0,
12423:                            'intauth_switch' => 0,
12424:                          );
12425:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
12426:         if (exists($domdefaults{$item})) {
12427:             $newvalues{$item} = $domdefaults{$item};
12428:         } else {
12429:             $newvalues{$item} = $staticdefaults{$item};
12430:         }
12431:     }
12432:     my %defaults_hash = (
12433:                          defaults => \%newvalues,
12434:                         );
12435:     my $title = &defaults_titles();
12436: 
12437:     my $currinststatus;
12438:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12439:         $currinststatus = $domconfig{'inststatus'};
12440:     } else {
12441:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12442:         $currinststatus = {
12443:                              inststatustypes => $usertypes,
12444:                              inststatusorder => $types,
12445:                              inststatusguest => [],
12446:                           };
12447:     }
12448:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12449:     my @allpos;
12450:     my %alltypes;
12451:     my @inststatusguest;
12452:     if (ref($currinststatus) eq 'HASH') {
12453:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12454:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
12455:                 unless (grep(/^\Q$type\E$/,@todelete)) {
12456:                     push(@inststatusguest,$type);
12457:                 }
12458:             }
12459:         }
12460:     }
12461:     my ($currtitles,$currorder);
12462:     if (ref($currinststatus) eq 'HASH') {
12463:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12464:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12465:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12466:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12467:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12468:                     }
12469:                 }
12470:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12471:                     my $position = $env{'form.inststatus_pos_'.$type};
12472:                     $position =~ s/\D+//g;
12473:                     $allpos[$position] = $type;
12474:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12475:                     $alltypes{$type} =~ s/`//g;
12476:                 }
12477:             }
12478:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12479:             $currtitles =~ s/,$//;
12480:         }
12481:     }
12482:     if ($env{'form.addinststatus'}) {
12483:         my $newtype = $env{'form.addinststatus'};
12484:         $newtype =~ s/\W//g;
12485:         unless (exists($alltypes{$newtype})) {
12486:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12487:             $alltypes{$newtype} =~ s/`//g; 
12488:             my $position = $env{'form.addinststatus_pos'};
12489:             $position =~ s/\D+//g;
12490:             if ($position ne '') {
12491:                 $allpos[$position] = $newtype;
12492:             }
12493:         }
12494:     }
12495:     my @orderedstatus;
12496:     foreach my $type (@allpos) {
12497:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12498:             push(@orderedstatus,$type);
12499:         }
12500:     }
12501:     foreach my $type (keys(%alltypes)) {
12502:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12503:             delete($alltypes{$type});
12504:         }
12505:     }
12506:     $defaults_hash{'inststatus'} = {
12507:                                      inststatustypes => \%alltypes,
12508:                                      inststatusorder => \@orderedstatus,
12509:                                      inststatusguest => \@inststatusguest,
12510:                                    };
12511:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12512:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12513:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12514:         }
12515:     }
12516:     if ($currorder ne join(',',@orderedstatus)) {
12517:         $changes{'inststatus'}{'inststatusorder'} = 1;
12518:     }
12519:     my $newtitles;
12520:     foreach my $item (@orderedstatus) {
12521:         $newtitles .= $alltypes{$item}.',';
12522:     }
12523:     $newtitles =~ s/,$//;
12524:     if ($currtitles ne $newtitles) {
12525:         $changes{'inststatus'}{'inststatustypes'} = 1;
12526:     }
12527:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12528:                                              $dom);
12529:     if ($putresult eq 'ok') {
12530:         if (keys(%changes) > 0) {
12531:             $resulttext = &mt('Changes made:').'<ul>';
12532:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12533:             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";
12534:             foreach my $item (sort(keys(%changes))) {
12535:                 if ($item eq 'inststatus') {
12536:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12537:                         if (@orderedstatus) {
12538:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12539:                             foreach my $type (@orderedstatus) { 
12540:                                 $resulttext .= $alltypes{$type}.', ';
12541:                             }
12542:                             $resulttext =~ s/, $//;
12543:                             $resulttext .= '</li>';
12544:                         } else {
12545:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
12546:                         }
12547:                     }
12548:                 } else {
12549:                     my $value = $env{'form.'.$item};
12550:                     if ($value eq '') {
12551:                         $value = &mt('none');
12552:                     } elsif ($item eq 'auth_def') {
12553:                         my %authnames = &authtype_names();
12554:                         my %shortauth = (
12555:                                           internal   => 'int',
12556:                                           krb4       => 'krb4',
12557:                                           krb5       => 'krb5',
12558:                                           localauth  => 'loc',
12559:                         );
12560:                         $value = $authnames{$shortauth{$value}};
12561:                     }
12562:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12563:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12564:                 }
12565:             }
12566:             $resulttext .= '</ul>';
12567:             $mailmsgtext .= "\n";
12568:             my $cachetime = 24*60*60;
12569:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12570:             if (ref($lastactref) eq 'HASH') {
12571:                 $lastactref->{'domdefaults'} = 1;
12572:             }
12573:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12574:                 my $notify = 1;
12575:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12576:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12577:                         $notify = 0;
12578:                     }
12579:                 }
12580:                 if ($notify) {
12581:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12582:                                                "LON-CAPA Domain Settings Change - $dom",
12583:                                                $mailmsgtext);
12584:                 }
12585:             }
12586:         } else {
12587:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12588:         }
12589:     } else {
12590:         $resulttext = '<span class="LC_error">'.
12591:             &mt('An error occurred: [_1]',$putresult).'</span>';
12592:     }
12593:     if (@errors > 0) {
12594:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12595:         foreach my $item (@errors) {
12596:             $resulttext .= ' "'.$title->{$item}.'",';
12597:         }
12598:         $resulttext =~ s/,$//;
12599:     }
12600:     return $resulttext;
12601: }
12602: 
12603: sub modify_scantron {
12604:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12605:     my ($resulttext,%confhash,%changes,$errors);
12606:     my $custom = 'custom.tab';
12607:     my $default = 'default.tab';
12608:     my $servadm = $r->dir_config('lonAdmEMail');
12609:     my ($configuserok,$author_ok,$switchserver) =
12610:         &config_check($dom,$confname,$servadm);
12611:     if ($env{'form.scantronformat.filename'} ne '') {
12612:         my $error;
12613:         if ($configuserok eq 'ok') {
12614:             if ($switchserver) {
12615:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12616:             } else {
12617:                 if ($author_ok eq 'ok') {
12618:                     my ($result,$scantronurl) =
12619:                         &publishlogo($r,'upload','scantronformat',$dom,
12620:                                      $confname,'scantron','','',$custom);
12621:                     if ($result eq 'ok') {
12622:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12623:                         $changes{'scantronformat'} = 1;
12624:                     } else {
12625:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12626:                     }
12627:                 } else {
12628:                     $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);
12629:                 }
12630:             }
12631:         } else {
12632:             $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);
12633:         }
12634:         if ($error) {
12635:             &Apache::lonnet::logthis($error);
12636:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12637:         }
12638:     }
12639:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12640:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12641:             if ($env{'form.scantronformat_del'}) {
12642:                 $confhash{'scantron'}{'scantronformat'} = '';
12643:                 $changes{'scantronformat'} = 1;
12644:             } else {
12645:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
12646:             }
12647:         }
12648:     }
12649:     my @options = ('hdr','pad','rem');
12650:     my @fields = &scantroncsv_fields();
12651:     my %titles = &scantronconfig_titles();
12652:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
12653:     my ($newdat,$currdat,%newcol,%currcol);
12654:     if (grep(/^dat$/,@formats)) {
12655:         $confhash{'scantron'}{config}{dat} = 1;
12656:         $newdat = 1;
12657:     } else {
12658:         $newdat = 0;
12659:     }
12660:     if (grep(/^csv$/,@formats)) {
12661:         my %bynum;
12662:         foreach my $field (@fields) {
12663:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
12664:                 my $posscol = $1;
12665:                 if (($posscol < 20) && (!$bynum{$posscol})) {
12666:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
12667:                     $bynum{$posscol} = $field;
12668:                     $newcol{$field} = $posscol;
12669:                 }
12670:             }
12671:         }
12672:         if (keys(%newcol)) {
12673:             foreach my $option (@options) {
12674:                 if ($env{'form.scantroncsv_'.$option}) {
12675:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
12676:                 }
12677:             }
12678:         }
12679:     }
12680:     $currdat = 1;
12681:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12682:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
12683:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
12684:                 $currdat = 0;
12685:             }
12686:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
12687:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
12688:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
12689:                 }
12690:             }
12691:         }
12692:     }
12693:     if ($currdat != $newdat) {
12694:         $changes{'config'} = 1;
12695:     } else {
12696:         foreach my $field (@fields) {
12697:             if ($currcol{$field} ne '') {
12698:                 if ($currcol{$field} ne $newcol{$field}) {
12699:                     $changes{'config'} = 1;
12700:                     last;
12701:                 }
12702:             } elsif ($newcol{$field} ne '') {
12703:                 $changes{'config'} = 1;
12704:                 last;
12705:             }
12706:         }
12707:     }
12708:     if (keys(%confhash) > 0) {
12709:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12710:                                                  $dom);
12711:         if ($putresult eq 'ok') {
12712:             if (keys(%changes) > 0) {
12713:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12714:                     $resulttext = &mt('Changes made:').'<ul>';
12715:                     if ($changes{'scantronformat'}) {
12716:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
12717:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12718:                         } else {
12719:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12720:                         }
12721:                     }
12722:                     if ($changes{'config'}) {
12723:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
12724:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
12725:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
12726:                             }
12727:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
12728:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
12729:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
12730:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
12731:                                         foreach my $field (@fields) {
12732:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
12733:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
12734:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
12735:                                             }
12736:                                         }
12737:                                         $resulttext .= '</ul></li>';
12738:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
12739:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
12740:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
12741:                                                 foreach my $option (@options) {
12742:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
12743:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
12744:                                                     }
12745:                                                 }
12746:                                                 $resulttext .= '</ul></li>';
12747:                                             }
12748:                                         }
12749:                                     }
12750:                                 }
12751:                             }
12752:                         } else {
12753:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
12754:                         }
12755:                     }
12756:                     $resulttext .= '</ul>';
12757:                 } else {
12758:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12759:                 }
12760:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12761:                 if (ref($lastactref) eq 'HASH') {
12762:                     $lastactref->{'domainconfig'} = 1;
12763:                 }
12764:             } else {
12765:                 $resulttext = &mt('No changes made to bubblesheet format settings');
12766:             }
12767:         } else {
12768:             $resulttext = '<span class="LC_error">'.
12769:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12770:         }
12771:     } else {
12772:         $resulttext = &mt('No changes made to bubblesheet format settings');
12773:     }
12774:     if ($errors) {
12775:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12776:                        $errors.'</ul>';
12777:     }
12778:     return $resulttext;
12779: }
12780: 
12781: sub modify_coursecategories {
12782:     my ($dom,$lastactref,%domconfig) = @_;
12783:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12784:         $cathash);
12785:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12786:     my @catitems = ('unauth','auth');
12787:     my @cattypes = ('std','domonly','codesrch','none');
12788:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12789:         $cathash = $domconfig{'coursecategories'}{'cats'};
12790:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12791:             $changes{'togglecats'} = 1;
12792:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12793:         }
12794:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12795:             $changes{'categorize'} = 1;
12796:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12797:         }
12798:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12799:             $changes{'togglecatscomm'} = 1;
12800:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12801:         }
12802:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12803:             $changes{'categorizecomm'} = 1;
12804:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12805:         }
12806:         foreach my $item (@catitems) {
12807:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12808:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12809:                     $changes{$item} = 1;
12810:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12811:                 }
12812:             }
12813:         }
12814:     } else {
12815:         $changes{'togglecats'} = 1;
12816:         $changes{'categorize'} = 1;
12817:         $changes{'togglecatscomm'} = 1;
12818:         $changes{'categorizecomm'} = 1;
12819:         $domconfig{'coursecategories'} = {
12820:                                              togglecats => $env{'form.togglecats'},
12821:                                              categorize => $env{'form.categorize'},
12822:                                              togglecatscomm => $env{'form.togglecatscomm'},
12823:                                              categorizecomm => $env{'form.categorizecomm'},
12824:                                          };
12825:         foreach my $item (@catitems) {
12826:             if ($env{'form.coursecat_'.$item} ne 'std') {
12827:                 $changes{$item} = 1;
12828:             }
12829:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12830:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12831:             }
12832:         }
12833:     }
12834:     if (ref($cathash) eq 'HASH') {
12835:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12836:             push (@deletecategory,'instcode::0');
12837:         }
12838:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12839:             push(@deletecategory,'communities::0');
12840:         }
12841:     }
12842:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12843:     if (ref($cathash) eq 'HASH') {
12844:         if (@deletecategory > 0) {
12845:             #FIXME Need to remove category from all courses using a deleted category 
12846:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12847:             foreach my $item (@deletecategory) {
12848:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12849:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12850:                     $deletions{$item} = 1;
12851:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12852:                 }
12853:             }
12854:         }
12855:         foreach my $item (keys(%{$cathash})) {
12856:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12857:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12858:                 $reorderings{$item} = 1;
12859:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12860:             }
12861:             if ($env{'form.addcategory_name_'.$item} ne '') {
12862:                 my $newcat = $env{'form.addcategory_name_'.$item};
12863:                 my $newdepth = $depth+1;
12864:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12865:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12866:                 $adds{$newitem} = 1; 
12867:             }
12868:             if ($env{'form.subcat_'.$item} ne '') {
12869:                 my $newcat = $env{'form.subcat_'.$item};
12870:                 my $newdepth = $depth+1;
12871:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12872:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12873:                 $adds{$newitem} = 1;
12874:             }
12875:         }
12876:     }
12877:     if ($env{'form.instcode'} eq '1') {
12878:         if (ref($cathash) eq 'HASH') {
12879:             my $newitem = 'instcode::0';
12880:             if ($cathash->{$newitem} eq '') {  
12881:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12882:                 $adds{$newitem} = 1;
12883:             }
12884:         } else {
12885:             my $newitem = 'instcode::0';
12886:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12887:             $adds{$newitem} = 1;
12888:         }
12889:     }
12890:     if ($env{'form.communities'} eq '1') {
12891:         if (ref($cathash) eq 'HASH') {
12892:             my $newitem = 'communities::0';
12893:             if ($cathash->{$newitem} eq '') {
12894:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12895:                 $adds{$newitem} = 1;
12896:             }
12897:         } else {
12898:             my $newitem = 'communities::0';
12899:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12900:             $adds{$newitem} = 1;
12901:         }
12902:     }
12903:     if ($env{'form.addcategory_name'} ne '') {
12904:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12905:             ($env{'form.addcategory_name'} ne 'communities')) {
12906:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12907:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12908:             $adds{$newitem} = 1;
12909:         }
12910:     }
12911:     my $putresult;
12912:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12913:         if (keys(%deletions) > 0) {
12914:             foreach my $key (keys(%deletions)) {
12915:                 if ($predelallitems{$key} ne '') {
12916:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12917:                 }
12918:             }
12919:         }
12920:         my (@chkcats,@chktrails,%chkallitems);
12921:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12922:         if (ref($chkcats[0]) eq 'ARRAY') {
12923:             my $depth = 0;
12924:             my $chg = 0;
12925:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12926:                 my $name = $chkcats[0][$i];
12927:                 my $item;
12928:                 if ($name eq '') {
12929:                     $chg ++;
12930:                 } else {
12931:                     $item = &escape($name).'::0';
12932:                     if ($chg) {
12933:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12934:                     }
12935:                     $depth ++; 
12936:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12937:                     $depth --;
12938:                 }
12939:             }
12940:         }
12941:     }
12942:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12943:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12944:         if ($putresult eq 'ok') {
12945:             my %title = (
12946:                          togglecats     => 'Show/Hide a course in catalog',
12947:                          categorize     => 'Assign a category to a course',
12948:                          togglecatscomm => 'Show/Hide a community in catalog',
12949:                          categorizecomm => 'Assign a category to a community',
12950:                         );
12951:             my %level = (
12952:                          dom  => 'set in Domain ("Modify Course/Community")',
12953:                          crs  => 'set in Course ("Course Configuration")',
12954:                          comm => 'set in Community ("Community Configuration")',
12955:                          none     => 'No catalog',
12956:                          std      => 'Standard catalog',
12957:                          domonly  => 'Domain-only catalog',
12958:                          codesrch => 'Code search form',
12959:                         );
12960:             $resulttext = &mt('Changes made:').'<ul>';
12961:             if ($changes{'togglecats'}) {
12962:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12963:             }
12964:             if ($changes{'categorize'}) {
12965:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12966:             }
12967:             if ($changes{'togglecatscomm'}) {
12968:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12969:             }
12970:             if ($changes{'categorizecomm'}) {
12971:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12972:             }
12973:             if ($changes{'unauth'}) {
12974:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12975:             }
12976:             if ($changes{'auth'}) {
12977:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12978:             }
12979:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12980:                 my $cathash;
12981:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12982:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12983:                 } else {
12984:                     $cathash = {};
12985:                 } 
12986:                 my (@cats,@trails,%allitems);
12987:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12988:                 if (keys(%deletions) > 0) {
12989:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12990:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12991:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12992:                     }
12993:                     $resulttext .= '</ul></li>';
12994:                 }
12995:                 if (keys(%reorderings) > 0) {
12996:                     my %sort_by_trail;
12997:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12998:                     foreach my $key (keys(%reorderings)) {
12999:                         if ($allitems{$key} ne '') {
13000:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13001:                         }
13002:                     }
13003:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13004:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13005:                     }
13006:                     $resulttext .= '</ul></li>';
13007:                 }
13008:                 if (keys(%adds) > 0) {
13009:                     my %sort_by_trail;
13010:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
13011:                     foreach my $key (keys(%adds)) {
13012:                         if ($allitems{$key} ne '') {
13013:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13014:                         }
13015:                     }
13016:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13017:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13018:                     }
13019:                     $resulttext .= '</ul></li>';
13020:                 }
13021:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
13022:                 if (ref($lastactref) eq 'HASH') {
13023:                     $lastactref->{'cats'} = 1;
13024:                 }
13025:             }
13026:             $resulttext .= '</ul>';
13027:             if ($changes{'unauth'} || $changes{'auth'}) {
13028:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13029:                 if ($changes{'auth'}) {
13030:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
13031:                 }
13032:                 if ($changes{'unauth'}) {
13033:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
13034:                 }
13035:                 my $cachetime = 24*60*60;
13036:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13037:                 if (ref($lastactref) eq 'HASH') {
13038:                     $lastactref->{'domdefaults'} = 1;
13039:                 }
13040:             }
13041:         } else {
13042:             $resulttext = '<span class="LC_error">'.
13043:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13044:         }
13045:     } else {
13046:         $resulttext = &mt('No changes made to course and community categories');
13047:     }
13048:     return $resulttext;
13049: }
13050: 
13051: sub modify_serverstatuses {
13052:     my ($dom,%domconfig) = @_;
13053:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
13054:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
13055:         %currserverstatus = %{$domconfig{'serverstatuses'}};
13056:     }
13057:     my @pages = &serverstatus_pages();
13058:     foreach my $type (@pages) {
13059:         $newserverstatus{$type}{'namedusers'} = '';
13060:         $newserverstatus{$type}{'machines'} = '';
13061:         if (defined($env{'form.'.$type.'_namedusers'})) {
13062:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
13063:             my @okusers;
13064:             foreach my $user (@users) {
13065:                 my ($uname,$udom) = split(/:/,$user);
13066:                 if (($udom =~ /^$match_domain$/) &&   
13067:                     (&Apache::lonnet::domain($udom)) &&
13068:                     ($uname =~ /^$match_username$/)) {
13069:                     if (!grep(/^\Q$user\E/,@okusers)) {
13070:                         push(@okusers,$user);
13071:                     }
13072:                 }
13073:             }
13074:             if (@okusers > 0) {
13075:                  @okusers = sort(@okusers);
13076:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
13077:             }
13078:         }
13079:         if (defined($env{'form.'.$type.'_machines'})) {
13080:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
13081:             my @okmachines;
13082:             foreach my $ip (@machines) {
13083:                 my @parts = split(/\./,$ip);
13084:                 next if (@parts < 4);
13085:                 my $badip = 0;
13086:                 for (my $i=0; $i<4; $i++) {
13087:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
13088:                         $badip = 1;
13089:                         last;
13090:                     }
13091:                 }
13092:                 if (!$badip) {
13093:                     push(@okmachines,$ip);     
13094:                 }
13095:             }
13096:             @okmachines = sort(@okmachines);
13097:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
13098:         }
13099:     }
13100:     my %serverstatushash =  (
13101:                                 serverstatuses => \%newserverstatus,
13102:                             );
13103:     foreach my $type (@pages) {
13104:         foreach my $setting ('namedusers','machines') {
13105:             my (@current,@new);
13106:             if (ref($currserverstatus{$type}) eq 'HASH') {
13107:                 if ($currserverstatus{$type}{$setting} ne '') { 
13108:                     @current = split(/,/,$currserverstatus{$type}{$setting});
13109:                 }
13110:             }
13111:             if ($newserverstatus{$type}{$setting} ne '') {
13112:                 @new = split(/,/,$newserverstatus{$type}{$setting});
13113:             }
13114:             if (@current > 0) {
13115:                 if (@new > 0) {
13116:                     foreach my $item (@current) {
13117:                         if (!grep(/^\Q$item\E$/,@new)) {
13118:                             $changes{$type}{$setting} = 1;
13119:                             last;
13120:                         }
13121:                     }
13122:                     foreach my $item (@new) {
13123:                         if (!grep(/^\Q$item\E$/,@current)) {
13124:                             $changes{$type}{$setting} = 1;
13125:                             last;
13126:                         }
13127:                     }
13128:                 } else {
13129:                     $changes{$type}{$setting} = 1;
13130:                 }
13131:             } elsif (@new > 0) {
13132:                 $changes{$type}{$setting} = 1;
13133:             }
13134:         }
13135:     }
13136:     if (keys(%changes) > 0) {
13137:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
13138:         my $putresult = &Apache::lonnet::put_dom('configuration',
13139:                                                  \%serverstatushash,$dom);
13140:         if ($putresult eq 'ok') {
13141:             $resulttext .= &mt('Changes made:').'<ul>';
13142:             foreach my $type (@pages) {
13143:                 if (ref($changes{$type}) eq 'HASH') {
13144:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
13145:                     if ($changes{$type}{'namedusers'}) {
13146:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
13147:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
13148:                         } else {
13149:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
13150:                         }
13151:                     }
13152:                     if ($changes{$type}{'machines'}) {
13153:                         if ($newserverstatus{$type}{'machines'} eq '') {
13154:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
13155:                         } else {
13156:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
13157:                         }
13158: 
13159:                     }
13160:                     $resulttext .= '</ul></li>';
13161:                 }
13162:             }
13163:             $resulttext .= '</ul>';
13164:         } else {
13165:             $resulttext = '<span class="LC_error">'.
13166:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
13167: 
13168:         }
13169:     } else {
13170:         $resulttext = &mt('No changes made to access to server status pages');
13171:     }
13172:     return $resulttext;
13173: }
13174: 
13175: sub modify_helpsettings {
13176:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13177:     my ($resulttext,$errors,%changes,%helphash);
13178:     my %defaultchecked = ('submitbugs' => 'on');
13179:     my @offon = ('off','on');
13180:     my @toggles = ('submitbugs');
13181:     my %current = ('submitbugs' => '',
13182:                    'adhoc'      => {},
13183:                   );
13184:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
13185:         %current = %{$domconfig{'helpsettings'}};
13186:     }
13187:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13188:     foreach my $item (@toggles) {
13189:         if ($defaultchecked{$item} eq 'on') { 
13190:             if ($current{$item} eq '') {
13191:                 if ($env{'form.'.$item} eq '0') {
13192:                     $changes{$item} = 1;
13193:                 }
13194:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13195:                 $changes{$item} = 1;
13196:             }
13197:         } elsif ($defaultchecked{$item} eq 'off') {
13198:             if ($current{$item} eq '') {
13199:                 if ($env{'form.'.$item} eq '1') {
13200:                     $changes{$item} = 1;
13201:                 }
13202:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13203:                 $changes{$item} = 1;
13204:             }
13205:         }
13206:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
13207:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
13208:         }
13209:     }
13210:     my $maxnum = $env{'form.helproles_maxnum'};
13211:     my $confname = $dom.'-domainconfig';
13212:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
13213:     my (@allpos,%newsettings,%changedprivs,$newrole);
13214:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13215:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
13216:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
13217:     my %lt = &Apache::lonlocal::texthash(
13218:                     s      => 'system',
13219:                     d      => 'domain',
13220:                     order  => 'Display order',
13221:                     access => 'Role usage',
13222:                     all    => 'All with domain helpdesk or helpdesk assistant role',
13223:                     dh     => 'All with domain helpdesk role',
13224:                     da     => 'All with domain helpdesk assistant role',
13225:                     none   => 'None',
13226:                     status => 'Determined based on institutional status',
13227:                     inc    => 'Include all, but exclude specific personnel',
13228:                     exc    => 'Exclude all, but include specific personnel',
13229:     );
13230:     for (my $num=0; $num<=$maxnum; $num++) {
13231:         my ($prefix,$identifier,$rolename,%curr);
13232:         if ($num == $maxnum) {
13233:             next unless ($env{'form.newcusthelp'} == $maxnum);
13234:             $identifier = 'custhelp'.$num;
13235:             $prefix = 'helproles_'.$num;
13236:             $rolename = $env{'form.custhelpname'.$num};
13237:             $rolename=~s/[^A-Za-z0-9]//gs;
13238:             next if ($rolename eq '');
13239:             next if (exists($existing{'rolesdef_'.$rolename}));
13240:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13241:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13242:                                                      $newprivs{'c'},$confname,$dom);
13243:             if ($result ne 'ok') {
13244:                 $errors .= '<li><span class="LC_error">'.
13245:                            &mt('An error occurred storing the new custom role: [_1]',
13246:                            $result).'</span></li>';
13247:                 next;
13248:             } else {
13249:                 $changedprivs{$rolename} = \%newprivs;
13250:                 $newrole = $rolename;
13251:             }
13252:         } else {
13253:             $prefix = 'helproles_'.$num;
13254:             $rolename = $env{'form.'.$prefix};
13255:             next if ($rolename eq '');
13256:             next unless (exists($existing{'rolesdef_'.$rolename}));
13257:             $identifier = 'custhelp'.$num;
13258:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13259:             my %currprivs;
13260:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
13261:                 split(/\_/,$existing{'rolesdef_'.$rolename});
13262:             foreach my $level ('c','d','s') {
13263:                 if ($newprivs{$level} ne $currprivs{$level}) {
13264:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13265:                                                              $newprivs{'c'},$confname,$dom);
13266:                     if ($result ne 'ok') {
13267:                         $errors .= '<li><span class="LC_error">'.
13268:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
13269:                                        $rolename,$result).'</span></li>';
13270:                     } else {
13271:                         $changedprivs{$rolename} = \%newprivs;
13272:                     }
13273:                     last;
13274:                 }
13275:             }
13276:             if (ref($current{'adhoc'}) eq 'HASH') {
13277:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13278:                     %curr = %{$current{'adhoc'}{$rolename}};
13279:                 }
13280:             }
13281:         }
13282:         my $newpos = $env{'form.'.$prefix.'_pos'};
13283:         $newpos =~ s/\D+//g;
13284:         $allpos[$newpos] = $rolename;
13285:         my $newdesc = $env{'form.'.$prefix.'_desc'};
13286:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
13287:         if ($curr{'desc'}) {
13288:             if ($curr{'desc'} ne $newdesc) {
13289:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
13290:                 $newsettings{$rolename}{'desc'} = $newdesc;
13291:             }
13292:         } elsif ($newdesc ne '') {
13293:             $changes{'customrole'}{$rolename}{'desc'} = 1;
13294:             $newsettings{$rolename}{'desc'} = $newdesc;
13295:         }
13296:         my $access = $env{'form.'.$prefix.'_access'};
13297:         if (grep(/^\Q$access\E$/,@accesstypes)) {
13298:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
13299:             if ($access eq 'status') {
13300:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
13301:                 if (scalar(@statuses) == 0) {
13302:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
13303:                 } else {
13304:                     my (@shownstatus,$numtypes);
13305:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13306:                     if (ref($types) eq 'ARRAY') {
13307:                         $numtypes = scalar(@{$types});
13308:                         foreach my $type (sort(@statuses)) {
13309:                             if ($type eq 'default') {
13310:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13311:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
13312:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13313:                                 push(@shownstatus,$usertypes->{$type});
13314:                             }
13315:                         }
13316:                     }
13317:                     if (grep(/^default$/,@statuses)) {
13318:                         push(@shownstatus,$othertitle);
13319:                     }
13320:                     if (scalar(@shownstatus) == 1+$numtypes) {
13321:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
13322:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
13323:                     } else {
13324:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
13325:                         if (ref($curr{'status'}) eq 'ARRAY') {
13326:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13327:                             if (@diffs) {
13328:                                 $changes{'customrole'}{$rolename}{$access} = 1;
13329:                             }
13330:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13331:                             $changes{'customrole'}{$rolename}{$access} = 1;
13332:                         }
13333:                     }
13334:                 }
13335:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
13336:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
13337:                 my @newspecstaff;
13338:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13339:                 foreach my $person (sort(@personnel)) {
13340:                     if ($domhelpdesk{$person}) {
13341:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
13342:                     }
13343:                 }
13344:                 if (ref($curr{$access}) eq 'ARRAY') {
13345:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13346:                     if (@diffs) {
13347:                         $changes{'customrole'}{$rolename}{$access} = 1;
13348:                     }
13349:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13350:                     $changes{'customrole'}{$rolename}{$access} = 1;
13351:                 }
13352:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13353:                     my ($uname,$udom) = split(/:/,$person);
13354:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
13355:                 }
13356:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
13357:             }
13358:         } else {
13359:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
13360:         }
13361:         unless ($curr{'access'} eq $access) {
13362:             $changes{'customrole'}{$rolename}{'access'} = 1;
13363:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
13364:         }
13365:     }
13366:     if (@allpos > 0) {
13367:         my $idx = 0;
13368:         foreach my $rolename (@allpos) {
13369:             if ($rolename ne '') {
13370:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
13371:                 if (ref($current{'adhoc'}) eq 'HASH') {
13372:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13373:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
13374:                             $changes{'customrole'}{$rolename}{'order'} = 1;
13375:                             $newsettings{$rolename}{'order'} = $idx+1;
13376:                         }
13377:                     }
13378:                 }
13379:                 $idx ++;
13380:             }
13381:         }
13382:     }
13383:     my $putresult;
13384:     if (keys(%changes) > 0) {
13385:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
13386:         if ($putresult eq 'ok') {
13387:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
13388:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
13389:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
13390:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
13391:                 }
13392:             }
13393:             my $cachetime = 24*60*60;
13394:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13395:             if (ref($lastactref) eq 'HASH') {
13396:                 $lastactref->{'domdefaults'} = 1;
13397:             }
13398:         } else {
13399:             $errors .= '<li><span class="LC_error">'.
13400:                        &mt('An error occurred storing the settings: [_1]',
13401:                            $putresult).'</span></li>';
13402:         }
13403:     }
13404:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
13405:         $resulttext = &mt('Changes made:').'<ul>';
13406:         my (%shownprivs,@levelorder);
13407:         @levelorder = ('c','d','s');
13408:         if ((keys(%changes)) && ($putresult eq 'ok')) {
13409:             foreach my $item (sort(keys(%changes))) {
13410:                 if ($item eq 'submitbugs') {
13411:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
13412:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
13413:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
13414:                 } elsif ($item eq 'customrole') {
13415:                     if (ref($changes{'customrole'}) eq 'HASH') {
13416:                         my @keyorder = ('order','desc','access','status','exc','inc');
13417:                         my %keytext = &Apache::lonlocal::texthash(
13418:                                                                    order  => 'Order',
13419:                                                                    desc   => 'Role description',
13420:                                                                    access => 'Role usage',
13421:                                                                    status => 'Allowed institutional types',
13422:                                                                    exc    => 'Allowed personnel',
13423:                                                                    inc    => 'Disallowed personnel',
13424:                         );
13425:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
13426:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
13427:                                 if ($role eq $newrole) {
13428:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
13429:                                                               $role).'<ul>';
13430:                                 } else {
13431:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13432:                                                               $role).'<ul>';
13433:                                 }
13434:                                 foreach my $key (@keyorder) {
13435:                                     if ($changes{'customrole'}{$role}{$key}) {
13436:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
13437:                                                                   $keytext{$key},$newsettings{$role}{$key}).
13438:                                                        '</li>';
13439:                                     }
13440:                                 }
13441:                                 if (ref($changedprivs{$role}) eq 'HASH') {
13442:                                     $shownprivs{$role} = 1;
13443:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
13444:                                     foreach my $level (@levelorder) {
13445:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13446:                                             next if ($item eq '');
13447:                                             my ($priv) = split(/\&/,$item,2);
13448:                                             if (&Apache::lonnet::plaintext($priv)) {
13449:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13450:                                                 unless ($level eq 'c') {
13451:                                                     $resulttext .= ' ('.$lt{$level}.')';
13452:                                                 }
13453:                                                 $resulttext .= '</li>';
13454:                                             }
13455:                                         }
13456:                                     }
13457:                                     $resulttext .= '</ul>';
13458:                                 }
13459:                                 $resulttext .= '</ul></li>';
13460:                             }
13461:                         }
13462:                     }
13463:                 }
13464:             }
13465:         }
13466:         if (keys(%changedprivs)) {
13467:             foreach my $role (sort(keys(%changedprivs))) {
13468:                 unless ($shownprivs{$role}) {
13469:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13470:                                               $role).'<ul>'.
13471:                                    '<li>'.&mt('Privileges set to :').'<ul>';
13472:                     foreach my $level (@levelorder) {
13473:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13474:                             next if ($item eq '');
13475:                             my ($priv) = split(/\&/,$item,2);
13476:                             if (&Apache::lonnet::plaintext($priv)) {
13477:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13478:                                 unless ($level eq 'c') {
13479:                                     $resulttext .= ' ('.$lt{$level}.')';
13480:                                 }
13481:                                 $resulttext .= '</li>';
13482:                             }
13483:                         }
13484:                     }
13485:                     $resulttext .= '</ul></li></ul></li>';
13486:                 }
13487:             }
13488:         }
13489:         $resulttext .= '</ul>';
13490:     } else {
13491:         $resulttext = &mt('No changes made to help settings');
13492:     }
13493:     if ($errors) {
13494:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13495:                                     $errors.'</ul>';
13496:     }
13497:     return $resulttext;
13498: }
13499: 
13500: sub modify_coursedefaults {
13501:     my ($dom,$lastactref,%domconfig) = @_;
13502:     my ($resulttext,$errors,%changes,%defaultshash);
13503:     my %defaultchecked = (
13504:                            'uselcmath'       => 'on',
13505:                            'usejsme'         => 'on'
13506:                          );
13507:     my @toggles = ('uselcmath','usejsme');
13508:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13509:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
13510:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
13511:     my @types = ('official','unofficial','community','textbook');
13512:     my %staticdefaults = (
13513:                            anonsurvey_threshold => 10,
13514:                            uploadquota          => 500,
13515:                            postsubmit           => 60,
13516:                            mysqltables          => 172800,
13517:                          );
13518:     my %texoptions = (
13519:                         MathJax  => 'MathJax',
13520:                         mimetex  => &mt('Convert to Images'),
13521:                         tth      => &mt('TeX to HTML'),
13522:                      );
13523:     $defaultshash{'coursedefaults'} = {};
13524: 
13525:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13526:         if ($domconfig{'coursedefaults'} eq '') {
13527:             $domconfig{'coursedefaults'} = {};
13528:         }
13529:     }
13530: 
13531:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13532:         foreach my $item (@toggles) {
13533:             if ($defaultchecked{$item} eq 'on') {
13534:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13535:                     ($env{'form.'.$item} eq '0')) {
13536:                     $changes{$item} = 1;
13537:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13538:                     $changes{$item} = 1;
13539:                 }
13540:             } elsif ($defaultchecked{$item} eq 'off') {
13541:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13542:                     ($env{'form.'.$item} eq '1')) {
13543:                     $changes{$item} = 1;
13544:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13545:                     $changes{$item} = 1;
13546:                 }
13547:             }
13548:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13549:         }
13550:         foreach my $item (@numbers) {
13551:             my ($currdef,$newdef);
13552:             $newdef = $env{'form.'.$item};
13553:             if ($item eq 'anonsurvey_threshold') {
13554:                 $currdef = $domconfig{'coursedefaults'}{$item};
13555:                 $newdef =~ s/\D//g;
13556:                 if ($newdef eq '' || $newdef < 1) {
13557:                     $newdef = 1;
13558:                 }
13559:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13560:             } else {
13561:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13562:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13563:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13564:                 }
13565:                 $newdef =~ s/[^\w.\-]//g;
13566:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13567:             }
13568:             if ($currdef ne $newdef) {
13569:                 if ($item eq 'anonsurvey_threshold') {
13570:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13571:                         $changes{$item} = 1;
13572:                     }
13573:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13574:                     my $setting = $1;
13575:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13576:                         $changes{$setting} = 1;
13577:                     }
13578:                 }
13579:             }
13580:         }
13581:         my $texengine;
13582:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
13583:             $texengine = $env{'form.texengine'};
13584:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
13585:             if ($currdef eq '') {
13586:                 unless ($texengine eq $Apache::lonnet::deftex) {
13587:                     $changes{'texengine'} = 1;
13588:                 }
13589:             } elsif ($currdef ne $texengine) {
13590:                 $changes{'texengine'} = 1;
13591:             }
13592:         }
13593:         if ($texengine ne '') {
13594:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
13595:         }
13596:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13597:         my @currclonecode;
13598:         if (ref($currclone) eq 'HASH') {
13599:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13600:                 @currclonecode = @{$currclone->{'instcode'}};
13601:             }
13602:         }
13603:         my $newclone;
13604:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13605:             $newclone = $env{'form.canclone'};
13606:         }
13607:         if ($newclone eq 'instcode') {
13608:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13609:             my (%codedefaults,@code_order,@clonecode);
13610:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13611:                                                     \@code_order);
13612:             foreach my $item (@code_order) {
13613:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13614:                     push(@clonecode,$item);
13615:                 }
13616:             }
13617:             if (@clonecode) {
13618:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13619:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13620:                 if (@diffs) {
13621:                     $changes{'canclone'} = 1;
13622:                 }
13623:             } else {
13624:                 $newclone eq '';
13625:             }
13626:         } elsif ($newclone ne '') {
13627:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13628:         }
13629:         if ($newclone ne $currclone) {
13630:             $changes{'canclone'} = 1;
13631:         }
13632:         my %credits;
13633:         foreach my $type (@types) {
13634:             unless ($type eq 'community') {
13635:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13636:                 $credits{$type} =~ s/[^\d.]+//g;
13637:             }
13638:         }
13639:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13640:             ($env{'form.coursecredits'} eq '1')) {
13641:             $changes{'coursecredits'} = 1;
13642:             foreach my $type (keys(%credits)) {
13643:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13644:             }
13645:         } else {
13646:             if ($env{'form.coursecredits'} eq '1') {
13647:                 foreach my $type (@types) {
13648:                     unless ($type eq 'community') {
13649:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13650:                             $changes{'coursecredits'} = 1;
13651:                         }
13652:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13653:                     }
13654:                 }
13655:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13656:                 foreach my $type (@types) {
13657:                     unless ($type eq 'community') {
13658:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13659:                             $changes{'coursecredits'} = 1;
13660:                             last;
13661:                         }
13662:                     }
13663:                 }
13664:             }
13665:         }
13666:         if ($env{'form.postsubmit'} eq '1') {
13667:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13668:             my %currtimeout;
13669:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13670:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13671:                     $changes{'postsubmit'} = 1;
13672:                 }
13673:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13674:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13675:                 }
13676:             } else {
13677:                 $changes{'postsubmit'} = 1;
13678:             }
13679:             foreach my $type (@types) {
13680:                 my $timeout = $env{'form.'.$type.'_timeout'};
13681:                 $timeout =~ s/\D//g;
13682:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13683:                     $timeout = '';
13684:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13685:                     $timeout = '0';
13686:                 }
13687:                 unless ($timeout eq '') {
13688:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13689:                 }
13690:                 if (exists($currtimeout{$type})) {
13691:                     if ($timeout ne $currtimeout{$type}) {
13692:                         $changes{'postsubmit'} = 1;
13693:                     }
13694:                 } elsif ($timeout ne '') {
13695:                     $changes{'postsubmit'} = 1;
13696:                 }
13697:             }
13698:         } else {
13699:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13700:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13701:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
13702:                     $changes{'postsubmit'} = 1;
13703:                 }
13704:             } else {
13705:                 $changes{'postsubmit'} = 1;
13706:             }
13707:         }
13708:     }
13709:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13710:                                              $dom);
13711:     if ($putresult eq 'ok') {
13712:         if (keys(%changes) > 0) {
13713:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13714:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13715:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13716:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
13717:                 foreach my $item ('uselcmath','usejsme','texengine') {
13718:                     if ($changes{$item}) {
13719:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13720:                     }
13721:                 }
13722:                 if ($changes{'coursecredits'}) {
13723:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13724:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13725:                             $domdefaults{$type.'credits'} =
13726:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13727:                         }
13728:                     }
13729:                 }
13730:                 if ($changes{'postsubmit'}) {
13731:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13732:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13733:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13734:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13735:                                 $domdefaults{$type.'postsubtimeout'} =
13736:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13737:                             }
13738:                         }
13739:                     }
13740:                 }
13741:                 if ($changes{'uploadquota'}) {
13742:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13743:                         foreach my $type (@types) {
13744:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13745:                         }
13746:                     }
13747:                 }
13748:                 if ($changes{'canclone'}) {
13749:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13750:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13751:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13752:                             if (@clonecodes) {
13753:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13754:                             }
13755:                         }
13756:                     } else {
13757:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13758:                     }
13759:                 }
13760:                 my $cachetime = 24*60*60;
13761:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13762:                 if (ref($lastactref) eq 'HASH') {
13763:                     $lastactref->{'domdefaults'} = 1;
13764:                 }
13765:             }
13766:             $resulttext = &mt('Changes made:').'<ul>';
13767:             foreach my $item (sort(keys(%changes))) {
13768:                 if ($item eq 'uselcmath') {
13769:                     if ($env{'form.'.$item} eq '1') {
13770:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13771:                     } else {
13772:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13773:                     }
13774:                 } elsif ($item eq 'usejsme') {
13775:                     if ($env{'form.'.$item} eq '1') {
13776:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13777:                     } else {
13778:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13779:                     }
13780:                 } elsif ($item eq 'texengine') {
13781:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
13782:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
13783:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
13784:                     }
13785:                 } elsif ($item eq 'anonsurvey_threshold') {
13786:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13787:                 } elsif ($item eq 'uploadquota') {
13788:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13789:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13790:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13791:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13792:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13793: 
13794:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13795:                                        '</ul>'.
13796:                                        '</li>';
13797:                     } else {
13798:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13799:                     }
13800:                 } elsif ($item eq 'mysqltables') {
13801:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13802:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13803:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13804:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13805:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13806:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13807:                                        '</ul>'.
13808:                                        '</li>';
13809:                     } else {
13810:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13811:                     }
13812:                 } elsif ($item eq 'postsubmit') {
13813:                     if ($domdefaults{'postsubmit'} eq 'off') {
13814:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13815:                     } else {
13816:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13817:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13818:                             $resulttext .= &mt('durations:').'<ul>';
13819:                             foreach my $type (@types) {
13820:                                 $resulttext .= '<li>';
13821:                                 my $timeout;
13822:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13823:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13824:                                 }
13825:                                 my $display;
13826:                                 if ($timeout eq '0') {
13827:                                     $display = &mt('unlimited');
13828:                                 } elsif ($timeout eq '') {
13829:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13830:                                 } else {
13831:                                     $display = &mt('[quant,_1,second]',$timeout);
13832:                                 }
13833:                                 if ($type eq 'community') {
13834:                                     $resulttext .= &mt('Communities');
13835:                                 } elsif ($type eq 'official') {
13836:                                     $resulttext .= &mt('Official courses');
13837:                                 } elsif ($type eq 'unofficial') {
13838:                                     $resulttext .= &mt('Unofficial courses');
13839:                                 } elsif ($type eq 'textbook') {
13840:                                     $resulttext .= &mt('Textbook courses');
13841:                                 }
13842:                                 $resulttext .= ' -- '.$display.'</li>';
13843:                             }
13844:                             $resulttext .= '</ul>';
13845:                         }
13846:                         $resulttext .= '</li>';
13847:                     }
13848:                 } elsif ($item eq 'coursecredits') {
13849:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13850:                         if (($domdefaults{'officialcredits'} eq '') &&
13851:                             ($domdefaults{'unofficialcredits'} eq '') &&
13852:                             ($domdefaults{'textbookcredits'} eq '')) {
13853:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13854:                         } else {
13855:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13856:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13857:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13858:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13859:                                            '</ul>'.
13860:                                            '</li>';
13861:                         }
13862:                     } else {
13863:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13864:                     }
13865:                 } elsif ($item eq 'canclone') {
13866:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13867:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13868:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13869:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13870:                         }
13871:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13872:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13873:                     } else {
13874:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
13875:                     }
13876:                 }
13877:             }
13878:             $resulttext .= '</ul>';
13879:         } else {
13880:             $resulttext = &mt('No changes made to course defaults');
13881:         }
13882:     } else {
13883:         $resulttext = '<span class="LC_error">'.
13884:             &mt('An error occurred: [_1]',$putresult).'</span>';
13885:     }
13886:     return $resulttext;
13887: }
13888: 
13889: sub modify_selfenrollment {
13890:     my ($dom,$lastactref,%domconfig) = @_;
13891:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13892:     my @types = ('official','unofficial','community','textbook');
13893:     my %titles = &tool_titles();
13894:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13895:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13896:     $ordered{'default'} = ['types','registered','approval','limit'];
13897: 
13898:     my (%roles,%shown,%toplevel);
13899:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13900: 
13901:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13902:         if ($domconfig{'selfenrollment'} eq '') {
13903:             $domconfig{'selfenrollment'} = {};
13904:         }
13905:     }
13906:     %toplevel = (
13907:                   admin      => 'Configuration Rights',
13908:                   default    => 'Default settings',
13909:                   validation => 'Validation of self-enrollment requests',
13910:                 );
13911:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13912: 
13913:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13914:         foreach my $item (@{$ordered{'admin'}}) {
13915:             foreach my $type (@types) {
13916:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13917:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13918:                 } else {
13919:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13920:                 }
13921:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13922:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13923:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13924:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13925:                             push(@{$changes{'admin'}{$type}},$item);
13926:                         }
13927:                     } else {
13928:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13929:                             push(@{$changes{'admin'}{$type}},$item);
13930:                         }
13931:                     }
13932:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13933:                     push(@{$changes{'admin'}{$type}},$item);
13934:                 }
13935:             }
13936:         }
13937:     }
13938: 
13939:     foreach my $item (@{$ordered{'default'}}) {
13940:         foreach my $type (@types) {
13941:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13942:             if ($item eq 'types') {
13943:                 unless (($value eq 'all') || ($value eq 'dom')) {
13944:                     $value = '';
13945:                 }
13946:             } elsif ($item eq 'registered') {
13947:                 unless ($value eq '1') {
13948:                     $value = 0;
13949:                 }
13950:             } elsif ($item eq 'approval') {
13951:                 unless ($value =~ /^[012]$/) {
13952:                     $value = 0;
13953:                 }
13954:             } else {
13955:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13956:                     $value = 'none';
13957:                 }
13958:             }
13959:             $selfenrollhash{'default'}{$type}{$item} = $value;
13960:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13961:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13962:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13963:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13964:                          push(@{$changes{'default'}{$type}},$item);
13965:                     }
13966:                 } else {
13967:                     push(@{$changes{'default'}{$type}},$item);
13968:                 }
13969:             } else {
13970:                 push(@{$changes{'default'}{$type}},$item);
13971:             }
13972:             if ($item eq 'limit') {
13973:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13974:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13975:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13976:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13977:                     }
13978:                 } else {
13979:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13980:                 }
13981:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13982:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13983:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13984:                          push(@{$changes{'default'}{$type}},'cap');
13985:                     }
13986:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13987:                     push(@{$changes{'default'}{$type}},'cap');
13988:                 }
13989:             }
13990:         }
13991:     }
13992: 
13993:     foreach my $item (@{$itemsref}) {
13994:         if ($item eq 'fields') {
13995:             my @changed;
13996:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13997:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13998:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13999:             }
14000:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14001:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
14002:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
14003:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
14004:                 } else {
14005:                     @changed = @{$selfenrollhash{'validation'}{$item}};
14006:                 }
14007:             } else {
14008:                 @changed = @{$selfenrollhash{'validation'}{$item}};
14009:             }
14010:             if (@changed) {
14011:                 if ($selfenrollhash{'validation'}{$item}) { 
14012:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
14013:                 } else {
14014:                     $changes{'validation'}{$item} = &mt('None');
14015:                 }
14016:             }
14017:         } else {
14018:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
14019:             if ($item eq 'markup') {
14020:                if ($env{'form.selfenroll_validation_'.$item}) {
14021:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14022:                }
14023:             }
14024:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14025:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
14026:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
14027:                 }
14028:             }
14029:         }
14030:     }
14031: 
14032:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
14033:                                              $dom);
14034:     if ($putresult eq 'ok') {
14035:         if (keys(%changes) > 0) {
14036:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14037:             $resulttext = &mt('Changes made:').'<ul>';
14038:             foreach my $key ('admin','default','validation') {
14039:                 if (ref($changes{$key}) eq 'HASH') {
14040:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
14041:                     if ($key eq 'validation') {
14042:                         foreach my $item (@{$itemsref}) {
14043:                             if (exists($changes{$key}{$item})) {
14044:                                 if ($item eq 'markup') {
14045:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14046:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
14047:                                 } else {  
14048:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14049:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
14050:                                 }
14051:                             }
14052:                         }
14053:                     } else {
14054:                         foreach my $type (@types) {
14055:                             if ($type eq 'community') {
14056:                                 $roles{'1'} = &mt('Community personnel');
14057:                             } else {
14058:                                 $roles{'1'} = &mt('Course personnel');
14059:                             }
14060:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
14061:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
14062:                                     if ($key eq 'admin') {
14063:                                         my @mgrdc = ();
14064:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14065:                                             foreach my $item (@{$ordered{'admin'}}) {
14066:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
14067:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
14068:                                                         push(@mgrdc,$item);
14069:                                                     }
14070:                                                 }
14071:                                             }
14072:                                             if (@mgrdc) {
14073:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
14074:                                             } else {
14075:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
14076:                                             }
14077:                                         }
14078:                                     } else {
14079:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14080:                                             foreach my $item (@{$ordered{$key}}) {
14081:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14082:                                                     $domdefaults{$type.'selfenroll'.$item} =
14083:                                                         $selfenrollhash{$key}{$type}{$item};
14084:                                                 }
14085:                                             }
14086:                                         }
14087:                                     }
14088:                                 }
14089:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
14090:                                 foreach my $item (@{$ordered{$key}}) {
14091:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14092:                                         $resulttext .= '<li>';
14093:                                         if ($key eq 'admin') {
14094:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
14095:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
14096:                                         } else {
14097:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
14098:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
14099:                                         }
14100:                                         $resulttext .= '</li>';
14101:                                     }
14102:                                 }
14103:                                 $resulttext .= '</ul></li>';
14104:                             }
14105:                         }
14106:                         $resulttext .= '</ul></li>'; 
14107:                     }
14108:                 }
14109:             }
14110:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
14111:                 my $cachetime = 24*60*60;
14112:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14113:                 if (ref($lastactref) eq 'HASH') {
14114:                     $lastactref->{'domdefaults'} = 1;
14115:                 }
14116:             }
14117:             $resulttext .= '</ul>';
14118:         } else {
14119:             $resulttext = &mt('No changes made to self-enrollment settings');
14120:         }
14121:     } else {
14122:         $resulttext = '<span class="LC_error">'.
14123:             &mt('An error occurred: [_1]',$putresult).'</span>';
14124:     }
14125:     return $resulttext;
14126: }
14127: 
14128: sub modify_usersessions {
14129:     my ($dom,$lastactref,%domconfig) = @_;
14130:     my @hostingtypes = ('version','excludedomain','includedomain');
14131:     my @offloadtypes = ('primary','default');
14132:     my %types = (
14133:                   remote => \@hostingtypes,
14134:                   hosted => \@hostingtypes,
14135:                   spares => \@offloadtypes,
14136:                 );
14137:     my @prefixes = ('remote','hosted','spares');
14138:     my @lcversions = &Apache::lonnet::all_loncaparevs();
14139:     my (%by_ip,%by_location,@intdoms);
14140:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
14141:     my @locations = sort(keys(%by_location));
14142:     my (%defaultshash,%changes);
14143:     foreach my $prefix (@prefixes) {
14144:         $defaultshash{'usersessions'}{$prefix} = {};
14145:     }
14146:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14147:     my $resulttext;
14148:     my %iphost = &Apache::lonnet::get_iphost();
14149:     foreach my $prefix (@prefixes) {
14150:         next if ($prefix eq 'spares');
14151:         foreach my $type (@{$types{$prefix}}) {
14152:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14153:             if ($type eq 'version') {
14154:                 my $value = $env{'form.'.$prefix.'_'.$type};
14155:                 my $okvalue;
14156:                 if ($value ne '') {
14157:                     if (grep(/^\Q$value\E$/,@lcversions)) {
14158:                         $okvalue = $value;
14159:                     }
14160:                 }
14161:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14162:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14163:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
14164:                             if ($inuse == 0) {
14165:                                 $changes{$prefix}{$type} = 1;
14166:                             } else {
14167:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
14168:                                     $changes{$prefix}{$type} = 1;
14169:                                 }
14170:                                 if ($okvalue ne '') {
14171:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14172:                                 } 
14173:                             }
14174:                         } else {
14175:                             if (($inuse == 1) && ($okvalue ne '')) {
14176:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14177:                                 $changes{$prefix}{$type} = 1;
14178:                             }
14179:                         }
14180:                     } else {
14181:                         if (($inuse == 1) && ($okvalue ne '')) {
14182:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14183:                             $changes{$prefix}{$type} = 1;
14184:                         }
14185:                     }
14186:                 } else {
14187:                     if (($inuse == 1) && ($okvalue ne '')) {
14188:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14189:                         $changes{$prefix}{$type} = 1;
14190:                     }
14191:                 }
14192:             } else {
14193:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14194:                 my @okvals;
14195:                 foreach my $val (@vals) {
14196:                     if ($val =~ /:/) {
14197:                         my @items = split(/:/,$val);
14198:                         foreach my $item (@items) {
14199:                             if (ref($by_location{$item}) eq 'ARRAY') {
14200:                                 push(@okvals,$item);
14201:                             }
14202:                         }
14203:                     } else {
14204:                         if (ref($by_location{$val}) eq 'ARRAY') {
14205:                             push(@okvals,$val);
14206:                         }
14207:                     }
14208:                 }
14209:                 @okvals = sort(@okvals);
14210:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14211:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14212:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14213:                             if ($inuse == 0) {
14214:                                 $changes{$prefix}{$type} = 1; 
14215:                             } else {
14216:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14217:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
14218:                                 if (@changed > 0) {
14219:                                     $changes{$prefix}{$type} = 1;
14220:                                 }
14221:                             }
14222:                         } else {
14223:                             if ($inuse == 1) {
14224:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14225:                                 $changes{$prefix}{$type} = 1;
14226:                             }
14227:                         } 
14228:                     } else {
14229:                         if ($inuse == 1) {
14230:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14231:                             $changes{$prefix}{$type} = 1;
14232:                         }
14233:                     }
14234:                 } else {
14235:                     if ($inuse == 1) {
14236:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14237:                         $changes{$prefix}{$type} = 1;
14238:                     }
14239:                 }
14240:             }
14241:         }
14242:     }
14243: 
14244:     my @alldoms = &Apache::lonnet::all_domains();
14245:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14246:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
14247:     my $savespares;
14248: 
14249:     foreach my $lonhost (sort(keys(%servers))) {
14250:         my $serverhomeID =
14251:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
14252:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
14253:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
14254:         my %spareschg;
14255:         foreach my $type (@{$types{'spares'}}) {
14256:             my @okspares;
14257:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
14258:             foreach my $server (@checked) {
14259:                 if (&Apache::lonnet::hostname($server) ne '') {
14260:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
14261:                         unless (grep(/^\Q$server\E$/,@okspares)) {
14262:                             push(@okspares,$server);
14263:                         }
14264:                     }
14265:                 }
14266:             }
14267:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
14268:             my $newspare;
14269:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
14270:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
14271:                     $newspare = $new;
14272:                 }
14273:             }
14274:             my @spares;
14275:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
14276:                 @spares = sort(@okspares,$newspare);
14277:             } else {
14278:                 @spares = sort(@okspares);
14279:             }
14280:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
14281:             if (ref($spareid{$lonhost}) eq 'HASH') {
14282:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
14283:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
14284:                     if (@diffs > 0) {
14285:                         $spareschg{$type} = 1;
14286:                     }
14287:                 }
14288:             }
14289:         }
14290:         if (keys(%spareschg) > 0) {
14291:             $changes{'spares'}{$lonhost} = \%spareschg;
14292:         }
14293:     }
14294:     $defaultshash{'usersessions'}{'offloadnow'} = {};
14295:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
14296:     my @okoffload;
14297:     if (@offloadnow) {
14298:         foreach my $server (@offloadnow) {
14299:             if (&Apache::lonnet::hostname($server) ne '') {
14300:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
14301:                     push(@okoffload,$server);
14302:                 }
14303:             }
14304:         }
14305:         if (@okoffload) {
14306:             foreach my $lonhost (@okoffload) {
14307:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
14308:             }
14309:         }
14310:     }
14311:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
14312:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
14313:             if (ref($changes{'spares'}) eq 'HASH') {
14314:                 if (keys(%{$changes{'spares'}}) > 0) {
14315:                     $savespares = 1;
14316:                 }
14317:             }
14318:         } else {
14319:             $savespares = 1;
14320:         }
14321:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
14322:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
14323:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
14324:                     $changes{'offloadnow'} = 1;
14325:                     last;
14326:                 }
14327:             }
14328:             unless ($changes{'offloadnow'}) {
14329:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
14330:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
14331:                         $changes{'offloadnow'} = 1;
14332:                         last;
14333:                     }
14334:                 }
14335:             }
14336:         } elsif (@okoffload) {
14337:             $changes{'offloadnow'} = 1;
14338:         }
14339:     } elsif (@okoffload) {
14340:         $changes{'offloadnow'} = 1;
14341:     }
14342:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
14343:     if ((keys(%changes) > 0) || ($savespares)) {
14344:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14345:                                                  $dom);
14346:         if ($putresult eq 'ok') {
14347:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14348:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
14349:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
14350:                 }
14351:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
14352:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
14353:                 }
14354:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14355:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
14356:                 }
14357:             }
14358:             my $cachetime = 24*60*60;
14359:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14360:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
14361:             if (ref($lastactref) eq 'HASH') {
14362:                 $lastactref->{'domdefaults'} = 1;
14363:                 $lastactref->{'usersessions'} = 1;
14364:             }
14365:             if (keys(%changes) > 0) {
14366:                 my %lt = &usersession_titles();
14367:                 $resulttext = &mt('Changes made:').'<ul>';
14368:                 foreach my $prefix (@prefixes) {
14369:                     if (ref($changes{$prefix}) eq 'HASH') {
14370:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14371:                         if ($prefix eq 'spares') {
14372:                             if (ref($changes{$prefix}) eq 'HASH') {
14373:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
14374:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
14375:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
14376:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
14377:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14378:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
14379:                                         foreach my $type (@{$types{$prefix}}) {
14380:                                             if ($changes{$prefix}{$lonhost}{$type}) {
14381:                                                 my $offloadto = &mt('None');
14382:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
14383:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
14384:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
14385:                                                     }
14386:                                                 }
14387:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
14388:                                             }
14389:                                         }
14390:                                     }
14391:                                     $resulttext .= '</li>';
14392:                                 }
14393:                             }
14394:                         } else {
14395:                             foreach my $type (@{$types{$prefix}}) {
14396:                                 if (defined($changes{$prefix}{$type})) {
14397:                                     my $newvalue;
14398:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14399:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
14400:                                             if ($type eq 'version') {
14401:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
14402:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14403:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
14404:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
14405:                                                 }
14406:                                             }
14407:                                         }
14408:                                     }
14409:                                     if ($newvalue eq '') {
14410:                                         if ($type eq 'version') {
14411:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
14412:                                         } else {
14413:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14414:                                         }
14415:                                     } else {
14416:                                         if ($type eq 'version') {
14417:                                             $newvalue .= ' '.&mt('(or later)'); 
14418:                                         }
14419:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14420:                                     }
14421:                                 }
14422:                             }
14423:                         }
14424:                         $resulttext .= '</ul>';
14425:                     }
14426:                 }
14427:                 if ($changes{'offloadnow'}) {
14428:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14429:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
14430:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
14431:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
14432:                                 $resulttext .= '<li>'.$lonhost.'</li>';
14433:                             }
14434:                             $resulttext .= '</ul>';
14435:                         } else {
14436:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
14437:                         }
14438:                     } else {
14439:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
14440:                     }
14441:                 }
14442:                 $resulttext .= '</ul>';
14443:             } else {
14444:                 $resulttext = $nochgmsg;
14445:             }
14446:         } else {
14447:             $resulttext = '<span class="LC_error">'.
14448:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14449:         }
14450:     } else {
14451:         $resulttext = $nochgmsg;
14452:     }
14453:     return $resulttext;
14454: }
14455: 
14456: sub modify_loadbalancing {
14457:     my ($dom,%domconfig) = @_;
14458:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
14459:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
14460:     my ($othertitle,$usertypes,$types) =
14461:         &Apache::loncommon::sorted_inst_types($dom);
14462:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14463:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
14464:     my @sparestypes = ('primary','default');
14465:     my %typetitles = &sparestype_titles();
14466:     my $resulttext;
14467:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
14468:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14469:         %existing = %{$domconfig{'loadbalancing'}};
14470:     }
14471:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
14472:                               \%currtargets,\%currrules,\%currcookies);
14473:     my ($saveloadbalancing,%defaultshash,%changes);
14474:     my ($alltypes,$othertypes,$titles) =
14475:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
14476:     my %ruletitles = &offloadtype_text();
14477:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
14478:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
14479:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
14480:         if ($balancer eq '') {
14481:             next;
14482:         }
14483:         if (!exists($servers{$balancer})) {
14484:             if (exists($currbalancer{$balancer})) {
14485:                 push(@{$changes{'delete'}},$balancer);
14486:             }
14487:             next;
14488:         }
14489:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14490:             push(@{$changes{'delete'}},$balancer);
14491:             next;
14492:         }
14493:         if (!exists($currbalancer{$balancer})) {
14494:             push(@{$changes{'add'}},$balancer);
14495:         }
14496:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14497:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14498:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14499:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14500:             $saveloadbalancing = 1;
14501:         }
14502:         foreach my $sparetype (@sparestypes) {
14503:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14504:             my @offloadto;
14505:             foreach my $target (@targets) {
14506:                 if (($servers{$target}) && ($target ne $balancer)) {
14507:                     if ($sparetype eq 'default') {
14508:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14509:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14510:                         }
14511:                     }
14512:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14513:                         push(@offloadto,$target);
14514:                     }
14515:                 }
14516:             }
14517:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14518:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14519:                     push(@offloadto,$balancer);
14520:                 }
14521:             }
14522:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14523:         }
14524:         if ($env{'form.loadbalancing_cookie_'.$i}) {
14525:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
14526:             if (exists($currbalancer{$balancer})) {
14527:                 unless ($currcookies{$balancer}) {
14528:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
14529:                 }
14530:             }
14531:         } elsif (exists($currbalancer{$balancer})) {
14532:             if ($currcookies{$balancer}) {
14533:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
14534:             }
14535:         }
14536:         if (ref($currtargets{$balancer}) eq 'HASH') {
14537:             foreach my $sparetype (@sparestypes) {
14538:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14539:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14540:                     if (@targetdiffs > 0) {
14541:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14542:                     }
14543:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14544:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14545:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14546:                     }
14547:                 }
14548:             }
14549:         } else {
14550:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14551:                 foreach my $sparetype (@sparestypes) {
14552:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14553:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14554:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14555:                         }
14556:                     }
14557:                 }
14558:             }
14559:         }
14560:         my $ishomedom;
14561:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14562:             $ishomedom = 1;
14563:         }
14564:         if (ref($alltypes) eq 'ARRAY') {
14565:             foreach my $type (@{$alltypes}) {
14566:                 my $rule;
14567:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14568:                          (!$ishomedom)) {
14569:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14570:                 }
14571:                 if ($rule eq 'specific') {
14572:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14573:                     if (exists($servers{$specifiedhost})) {
14574:                         $rule = $specifiedhost;
14575:                     }
14576:                 }
14577:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14578:                 if (ref($currrules{$balancer}) eq 'HASH') {
14579:                     if ($rule ne $currrules{$balancer}{$type}) {
14580:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14581:                     }
14582:                 } elsif ($rule ne '') {
14583:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14584:                 }
14585:             }
14586:         }
14587:     }
14588:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14589:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14590:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14591:             $defaultshash{'loadbalancing'} = {};
14592:         }
14593:         my $putresult = &Apache::lonnet::put_dom('configuration',
14594:                                                  \%defaultshash,$dom);
14595:         if ($putresult eq 'ok') {
14596:             if (keys(%changes) > 0) {
14597:                 my %toupdate;
14598:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14599:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14600:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14601:                         $toupdate{$balancer} = 1;
14602:                     }
14603:                 }
14604:                 if (ref($changes{'add'}) eq 'ARRAY') {
14605:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14606:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14607:                         $toupdate{$balancer} = 1;
14608:                     }
14609:                 }
14610:                 if (ref($changes{'curr'}) eq 'HASH') {
14611:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14612:                         $toupdate{$balancer} = 1;
14613:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14614:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14615:                                 my %offloadstr;
14616:                                 foreach my $sparetype (@sparestypes) {
14617:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14618:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14619:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14620:                                         }
14621:                                     }
14622:                                 }
14623:                                 if (keys(%offloadstr) == 0) {
14624:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14625:                                 } else {
14626:                                     my $showoffload;
14627:                                     foreach my $sparetype (@sparestypes) {
14628:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14629:                                         if (defined($offloadstr{$sparetype})) {
14630:                                             $showoffload .= $offloadstr{$sparetype};
14631:                                         } else {
14632:                                             $showoffload .= &mt('None');
14633:                                         }
14634:                                         $showoffload .= ('&nbsp;'x3);
14635:                                     }
14636:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14637:                                 }
14638:                             }
14639:                         }
14640:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14641:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14642:                                 foreach my $type (@{$alltypes}) {
14643:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14644:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14645:                                         my $balancetext;
14646:                                         if ($rule eq '') {
14647:                                             $balancetext =  $ruletitles{'default'};
14648:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14649:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14650:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14651:                                                 foreach my $sparetype (@sparestypes) {
14652:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14653:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14654:                                                     }
14655:                                                 }
14656:                                                 foreach my $item (@{$alltypes}) {
14657:                                                     next if ($item =~  /^_LC_ipchange/);
14658:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14659:                                                     if ($hasrule eq 'homeserver') {
14660:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14661:                                                     } else {
14662:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14663:                                                             if ($servers{$hasrule}) {
14664:                                                                 $toupdate{$hasrule} = 1;
14665:                                                             }
14666:                                                         }
14667:                                                     }
14668:                                                 }
14669:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14670:                                                     $balancetext =  $ruletitles{$rule};
14671:                                                 } else {
14672:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14673:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14674:                                                     if ($receiver) {
14675:                                                         $toupdate{$receiver};
14676:                                                     }
14677:                                                 }
14678:                                             } else {
14679:                                                 $balancetext =  $ruletitles{$rule};
14680:                                             }
14681:                                         } else {
14682:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14683:                                         }
14684:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14685:                                     }
14686:                                 }
14687:                             }
14688:                         }
14689:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
14690:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
14691:                                                       $balancer).'</li>';
14692:                         }
14693:                         if (keys(%toupdate)) {
14694:                             my %thismachine;
14695:                             my $updatedhere;
14696:                             my $cachetime = 60*60*24;
14697:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14698:                             foreach my $lonhost (keys(%toupdate)) {
14699:                                 if ($thismachine{$lonhost}) {
14700:                                     unless ($updatedhere) {
14701:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
14702:                                                                       $defaultshash{'loadbalancing'},
14703:                                                                       $cachetime);
14704:                                         $updatedhere = 1;
14705:                                     }
14706:                                 } else {
14707:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
14708:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14709:                                 }
14710:                             }
14711:                         }
14712:                     }
14713:                 }
14714:                 if ($resulttext ne '') {
14715:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
14716:                 } else {
14717:                     $resulttext = $nochgmsg;
14718:                 }
14719:             } else {
14720:                 $resulttext = $nochgmsg;
14721:             }
14722:         } else {
14723:             $resulttext = '<span class="LC_error">'.
14724:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14725:         }
14726:     } else {
14727:         $resulttext = $nochgmsg;
14728:     }
14729:     return $resulttext;
14730: }
14731: 
14732: sub recurse_check {
14733:     my ($chkcats,$categories,$depth,$name) = @_;
14734:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
14735:         my $chg = 0;
14736:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
14737:             my $category = $chkcats->[$depth]{$name}[$j];
14738:             my $item;
14739:             if ($category eq '') {
14740:                 $chg ++;
14741:             } else {
14742:                 my $deeper = $depth + 1;
14743:                 $item = &escape($category).':'.&escape($name).':'.$depth;
14744:                 if ($chg) {
14745:                     $categories->{$item} -= $chg;
14746:                 }
14747:                 &recurse_check($chkcats,$categories,$deeper,$category);
14748:                 $deeper --;
14749:             }
14750:         }
14751:     }
14752:     return;
14753: }
14754: 
14755: sub recurse_cat_deletes {
14756:     my ($item,$coursecategories,$deletions) = @_;
14757:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14758:     my $subdepth = $depth + 1;
14759:     if (ref($coursecategories) eq 'HASH') {
14760:         foreach my $subitem (keys(%{$coursecategories})) {
14761:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14762:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14763:                 delete($coursecategories->{$subitem});
14764:                 $deletions->{$subitem} = 1;
14765:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14766:             }
14767:         }
14768:     }
14769:     return;
14770: }
14771: 
14772: sub active_dc_picker {
14773:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14774:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14775:     my @domcoord = keys(%domcoords);
14776:     if (keys(%currhash)) {
14777:         foreach my $dc (keys(%currhash)) {
14778:             unless (exists($domcoords{$dc})) {
14779:                 push(@domcoord,$dc);
14780:             }
14781:         }
14782:     }
14783:     @domcoord = sort(@domcoord);
14784:     my $numdcs = scalar(@domcoord);
14785:     my $rows = 0;
14786:     my $table;
14787:     if ($numdcs > 1) {
14788:         $table = '<table>';
14789:         for (my $i=0; $i<@domcoord; $i++) {
14790:             my $rem = $i%($numinrow);
14791:             if ($rem == 0) {
14792:                 if ($i > 0) {
14793:                     $table .= '</tr>';
14794:                 }
14795:                 $table .= '<tr>';
14796:                 $rows ++;
14797:             }
14798:             my $check = '';
14799:             if ($inputtype eq 'radio') {
14800:                 if (keys(%currhash) == 0) {
14801:                     if (!$i) {
14802:                         $check = ' checked="checked"';
14803:                     }
14804:                 } elsif (exists($currhash{$domcoord[$i]})) {
14805:                     $check = ' checked="checked"';
14806:                 }
14807:             } else {
14808:                 if (exists($currhash{$domcoord[$i]})) {
14809:                     $check = ' checked="checked"';
14810:                 }
14811:             }
14812:             if ($i == @domcoord - 1) {
14813:                 my $colsleft = $numinrow - $rem;
14814:                 if ($colsleft > 1) {
14815:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14816:                 } else {
14817:                     $table .= '<td class="LC_left_item">';
14818:                 }
14819:             } else {
14820:                 $table .= '<td class="LC_left_item">';
14821:             }
14822:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14823:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14824:             $table .= '<span class="LC_nobreak"><label>'.
14825:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14826:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14827:             if ($user ne $dcname.':'.$dcdom) {
14828:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14829:             }
14830:             $table .= '</label></span></td>';
14831:         }
14832:         $table .= '</tr></table>';
14833:     } elsif ($numdcs == 1) {
14834:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14835:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14836:         if ($inputtype eq 'radio') {
14837:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14838:             if ($user ne $dcname.':'.$dcdom) {
14839:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14840:             }
14841:         } else {
14842:             my $check;
14843:             if (exists($currhash{$domcoord[0]})) {
14844:                 $check = ' checked="checked"';
14845:             }
14846:             $table = '<span class="LC_nobreak"><label>'.
14847:                      '<input type="checkbox" name="'.$name.'" '.
14848:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14849:             if ($user ne $dcname.':'.$dcdom) {
14850:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14851:             }
14852:             $table .= '</label></span>';
14853:             $rows ++;
14854:         }
14855:     }
14856:     return ($numdcs,$table,$rows);
14857: }
14858: 
14859: sub usersession_titles {
14860:     return &Apache::lonlocal::texthash(
14861:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14862:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14863:                spares => 'Servers offloaded to, when busy',
14864:                version => 'LON-CAPA version requirement',
14865:                excludedomain => 'Allow all, but exclude specific domains',
14866:                includedomain => 'Deny all, but include specific domains',
14867:                primary => 'Primary (checked first)',
14868:                default => 'Default',
14869:            );
14870: }
14871: 
14872: sub id_for_thisdom {
14873:     my (%servers) = @_;
14874:     my %altids;
14875:     foreach my $server (keys(%servers)) {
14876:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14877:         if ($serverhome ne $server) {
14878:             $altids{$serverhome} = $server;
14879:         }
14880:     }
14881:     return %altids;
14882: }
14883: 
14884: sub count_servers {
14885:     my ($currbalancer,%servers) = @_;
14886:     my (@spares,$numspares);
14887:     foreach my $lonhost (sort(keys(%servers))) {
14888:         next if ($currbalancer eq $lonhost);
14889:         push(@spares,$lonhost);
14890:     }
14891:     if ($currbalancer) {
14892:         $numspares = scalar(@spares);
14893:     } else {
14894:         $numspares = scalar(@spares) - 1;
14895:     }
14896:     return ($numspares,@spares);
14897: }
14898: 
14899: sub lonbalance_targets_js {
14900:     my ($dom,$types,$servers,$settings) = @_;
14901:     my $select = &mt('Select');
14902:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14903:     if (ref($servers) eq 'HASH') {
14904:         $alltargets = join("','",sort(keys(%{$servers})));
14905:         my @homedoms;
14906:         foreach my $server (sort(keys(%{$servers}))) {
14907:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14908:                 push(@homedoms,'1');
14909:             } else {
14910:                 push(@homedoms,'0');
14911:             }
14912:         }
14913:         $allishome = join("','",@homedoms);
14914:     }
14915:     if (ref($types) eq 'ARRAY') {
14916:         if (@{$types} > 0) {
14917:             @alltypes = @{$types};
14918:         }
14919:     }
14920:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14921:     $allinsttypes = join("','",@alltypes);
14922:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
14923:     if (ref($settings) eq 'HASH') {
14924:         %existing = %{$settings};
14925:     }
14926:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14927:                               \%currtargets,\%currrules,\%currcookies);
14928:     my $balancers = join("','",sort(keys(%currbalancer)));
14929:     return <<"END";
14930: 
14931: <script type="text/javascript">
14932: // <![CDATA[
14933: 
14934: currBalancers = new Array('$balancers');
14935: 
14936: function toggleTargets(balnum) {
14937:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14938:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14939:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14940:     var prevbalancer = prevhostitem.value;
14941:     var baltotal = document.getElementById('loadbalancing_total').value;
14942:     prevhostitem.value = balancer;
14943:     if (prevbalancer != '') {
14944:         var prevIdx = currBalancers.indexOf(prevbalancer);
14945:         if (prevIdx != -1) {
14946:             currBalancers.splice(prevIdx,1);
14947:         }
14948:     }
14949:     if (balancer == '') {
14950:         hideSpares(balnum);
14951:     } else {
14952:         var currIdx = currBalancers.indexOf(balancer);
14953:         if (currIdx == -1) {
14954:             currBalancers.push(balancer);
14955:         }
14956:         var homedoms = new Array('$allishome');
14957:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14958:         showSpares(balancer,ishomedom,balnum);
14959:     }
14960:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14961:     return;
14962: }
14963: 
14964: function showSpares(balancer,ishomedom,balnum) {
14965:     var alltargets = new Array('$alltargets');
14966:     var insttypes = new Array('$allinsttypes');
14967:     var offloadtypes = new Array('primary','default');
14968: 
14969:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14970:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14971:  
14972:     for (var i=0; i<offloadtypes.length; i++) {
14973:         var count = 0;
14974:         for (var j=0; j<alltargets.length; j++) {
14975:             if (alltargets[j] != balancer) {
14976:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14977:                 item.value = alltargets[j];
14978:                 item.style.textAlign='left';
14979:                 item.style.textFace='normal';
14980:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14981:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14982:                     item.disabled = '';
14983:                 } else {
14984:                     item.disabled = 'disabled';
14985:                     item.checked = false;
14986:                 }
14987:                 count ++;
14988:             }
14989:         }
14990:     }
14991:     for (var k=0; k<insttypes.length; k++) {
14992:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14993:             if (ishomedom == 1) {
14994:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14995:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14996:             } else {
14997:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14998:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14999:             }
15000:         } else {
15001:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15002:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15003:         }
15004:         if ((insttypes[k] != '_LC_external') && 
15005:             ((insttypes[k] != '_LC_internetdom') ||
15006:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
15007:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
15008:             item.options.length = 0;
15009:             item.options[0] = new Option("","",true,true);
15010:             var idx = 0;
15011:             for (var m=0; m<alltargets.length; m++) {
15012:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
15013:                     idx ++;
15014:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15015:                 }
15016:             }
15017:         }
15018:     }
15019:     return;
15020: }
15021: 
15022: function hideSpares(balnum) {
15023:     var alltargets = new Array('$alltargets');
15024:     var insttypes = new Array('$allinsttypes');
15025:     var offloadtypes = new Array('primary','default');
15026: 
15027:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
15028:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
15029: 
15030:     var total = alltargets.length - 1;
15031:     for (var i=0; i<offloadtypes; i++) {
15032:         for (var j=0; j<total; j++) {
15033:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
15034:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
15035:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
15036:         }
15037:     }
15038:     for (var k=0; k<insttypes.length; k++) {
15039:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15040:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15041:         if (insttypes[k] != '_LC_external') {
15042:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
15043:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
15044:         }
15045:     }
15046:     return;
15047: }
15048: 
15049: function checkOffloads(item,balnum,type) {
15050:     var alltargets = new Array('$alltargets');
15051:     var offloadtypes = new Array('primary','default');
15052:     if (item.checked) {
15053:         var total = alltargets.length - 1;
15054:         var other;
15055:         if (type == offloadtypes[0]) {
15056:             other = offloadtypes[1];
15057:         } else {
15058:             other = offloadtypes[0];
15059:         }
15060:         for (var i=0; i<total; i++) {
15061:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
15062:             if (server == item.value) {
15063:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
15064:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
15065:                 }
15066:             }
15067:         }
15068:     }
15069:     return;
15070: }
15071: 
15072: function singleServerToggle(balnum,type) {
15073:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
15074:     if (offloadtoSelIdx == 0) {
15075:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
15076:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15077: 
15078:     } else {
15079:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
15080:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15081:     }
15082:     return;
15083: }
15084: 
15085: function balanceruleChange(formname,balnum,type) {
15086:     if (type == '_LC_external') {
15087:         return;
15088:     }
15089:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
15090:     for (var i=0; i<typesRules.length; i++) {
15091:         if (formname.elements[typesRules[i]].checked) {
15092:             if (formname.elements[typesRules[i]].value != 'specific') {
15093:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
15094:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15095:             } else {
15096:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15097:             }
15098:         }
15099:     }
15100:     return;
15101: }
15102: 
15103: function balancerDeleteChange(balnum) {
15104:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15105:     var baltotal = document.getElementById('loadbalancing_total').value;
15106:     var addtarget;
15107:     var removetarget;
15108:     var action = 'delete';
15109:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
15110:         var lonhost = hostitem.value;
15111:         var currIdx = currBalancers.indexOf(lonhost);
15112:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
15113:             if (currIdx != -1) {
15114:                 currBalancers.splice(currIdx,1);
15115:             }
15116:             addtarget = lonhost;
15117:         } else {
15118:             if (currIdx == -1) {
15119:                 currBalancers.push(lonhost);
15120:             }
15121:             removetarget = lonhost;
15122:             action = 'undelete';
15123:         }
15124:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
15125:     }
15126:     return;
15127: }
15128: 
15129: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
15130:     if (baltotal > 1) {
15131:         var offloadtypes = new Array('primary','default');
15132:         var alltargets = new Array('$alltargets');
15133:         var insttypes = new Array('$allinsttypes');
15134:         for (var i=0; i<baltotal; i++) {
15135:             if (i != balnum) {
15136:                 for (var j=0; j<offloadtypes.length; j++) {
15137:                     var total = alltargets.length - 1;
15138:                     for (var k=0; k<total; k++) {
15139:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
15140:                         var server = serveritem.value;
15141:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
15142:                             if (server == addtarget) {
15143:                                 serveritem.disabled = '';
15144:                             }
15145:                         }
15146:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15147:                             if (server == removetarget) {
15148:                                 serveritem.disabled = 'disabled';
15149:                                 serveritem.checked = false;
15150:                             }
15151:                         }
15152:                     }
15153:                 }
15154:                 for (var j=0; j<insttypes.length; j++) {
15155:                     if (insttypes[j] != '_LC_external') {
15156:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
15157:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
15158:                             var currSel = singleserver.selectedIndex;
15159:                             var currVal = singleserver.options[currSel].value;
15160:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
15161:                                 var numoptions = singleserver.options.length;
15162:                                 var needsnew = 1;
15163:                                 for (var k=0; k<numoptions; k++) {
15164:                                     if (singleserver.options[k] == addtarget) {
15165:                                         needsnew = 0;
15166:                                         break;
15167:                                     }
15168:                                 }
15169:                                 if (needsnew == 1) {
15170:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
15171:                                 }
15172:                             }
15173:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15174:                                 singleserver.options.length = 0;
15175:                                 if ((currVal) && (currVal != removetarget)) {
15176:                                     singleserver.options[0] = new Option("","",false,false);
15177:                                 } else {
15178:                                     singleserver.options[0] = new Option("","",true,true);
15179:                                 }
15180:                                 var idx = 0;
15181:                                 for (var m=0; m<alltargets.length; m++) {
15182:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
15183:                                         idx ++;
15184:                                         if (currVal == alltargets[m]) {
15185:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
15186:                                         } else {
15187:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15188:                                         }
15189:                                     }
15190:                                 }
15191:                             }
15192:                         }
15193:                     }
15194:                 }
15195:             }
15196:         }
15197:     }
15198:     return;
15199: }
15200: 
15201: // ]]>
15202: </script>
15203: 
15204: END
15205: }
15206: 
15207: sub new_spares_js {
15208:     my @sparestypes = ('primary','default');
15209:     my $types = join("','",@sparestypes);
15210:     my $select = &mt('Select');
15211:     return <<"END";
15212: 
15213: <script type="text/javascript">
15214: // <![CDATA[
15215: 
15216: function updateNewSpares(formname,lonhost) {
15217:     var types = new Array('$types');
15218:     var include = new Array();
15219:     var exclude = new Array();
15220:     for (var i=0; i<types.length; i++) {
15221:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
15222:         for (var j=0; j<spareboxes.length; j++) {
15223:             if (formname.elements[spareboxes[j]].checked) {
15224:                 exclude.push(formname.elements[spareboxes[j]].value);
15225:             } else {
15226:                 include.push(formname.elements[spareboxes[j]].value);
15227:             }
15228:         }
15229:     }
15230:     for (var i=0; i<types.length; i++) {
15231:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
15232:         var selIdx = newSpare.selectedIndex;
15233:         var currnew = newSpare.options[selIdx].value;
15234:         var okSpares = new Array();
15235:         for (var j=0; j<newSpare.options.length; j++) {
15236:             var possible = newSpare.options[j].value;
15237:             if (possible != '') {
15238:                 if (exclude.indexOf(possible) == -1) {
15239:                     okSpares.push(possible);
15240:                 } else {
15241:                     if (currnew == possible) {
15242:                         selIdx = 0;
15243:                     }
15244:                 }
15245:             }
15246:         }
15247:         for (var k=0; k<include.length; k++) {
15248:             if (okSpares.indexOf(include[k]) == -1) {
15249:                 okSpares.push(include[k]);
15250:             }
15251:         }
15252:         okSpares.sort();
15253:         newSpare.options.length = 0;
15254:         if (selIdx == 0) {
15255:             newSpare.options[0] = new Option("$select","",true,true);
15256:         } else {
15257:             newSpare.options[0] = new Option("$select","",false,false);
15258:         }
15259:         for (var m=0; m<okSpares.length; m++) {
15260:             var idx = m+1;
15261:             var selThis = 0;
15262:             if (selIdx != 0) {
15263:                 if (okSpares[m] == currnew) {
15264:                     selThis = 1;
15265:                 }
15266:             }
15267:             if (selThis == 1) {
15268:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
15269:             } else {
15270:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
15271:             }
15272:         }
15273:     }
15274:     return;
15275: }
15276: 
15277: function checkNewSpares(lonhost,type) {
15278:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
15279:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
15280:     if (chosen != '') { 
15281:         var othertype;
15282:         var othernewSpare;
15283:         if (type == 'primary') {
15284:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
15285:         }
15286:         if (type == 'default') {
15287:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
15288:         }
15289:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
15290:             othernewSpare.selectedIndex = 0;
15291:         }
15292:     }
15293:     return;
15294: }
15295: 
15296: // ]]>
15297: </script>
15298: 
15299: END
15300: 
15301: }
15302: 
15303: sub common_domprefs_js {
15304:     return <<"END";
15305: 
15306: <script type="text/javascript">
15307: // <![CDATA[
15308: 
15309: function getIndicesByName(formname,item) {
15310:     var group = new Array();
15311:     for (var i=0;i<formname.elements.length;i++) {
15312:         if (formname.elements[i].name == item) {
15313:             group.push(formname.elements[i].id);
15314:         }
15315:     }
15316:     return group;
15317: }
15318: 
15319: // ]]>
15320: </script>
15321: 
15322: END
15323: 
15324: }
15325: 
15326: sub recaptcha_js {
15327:     my %lt = &captcha_phrases();
15328:     return <<"END";
15329: 
15330: <script type="text/javascript">
15331: // <![CDATA[
15332: 
15333: function updateCaptcha(caller,context) {
15334:     var privitem;
15335:     var pubitem;
15336:     var privtext;
15337:     var pubtext;
15338:     var versionitem;
15339:     var versiontext;
15340:     if (document.getElementById(context+'_recaptchapub')) {
15341:         pubitem = document.getElementById(context+'_recaptchapub');
15342:     } else {
15343:         return;
15344:     }
15345:     if (document.getElementById(context+'_recaptchapriv')) {
15346:         privitem = document.getElementById(context+'_recaptchapriv');
15347:     } else {
15348:         return;
15349:     }
15350:     if (document.getElementById(context+'_recaptchapubtxt')) {
15351:         pubtext = document.getElementById(context+'_recaptchapubtxt');
15352:     } else {
15353:         return;
15354:     }
15355:     if (document.getElementById(context+'_recaptchaprivtxt')) {
15356:         privtext = document.getElementById(context+'_recaptchaprivtxt');
15357:     } else {
15358:         return;
15359:     }
15360:     if (document.getElementById(context+'_recaptchaversion')) {
15361:         versionitem = document.getElementById(context+'_recaptchaversion');
15362:     } else {
15363:         return;
15364:     }
15365:     if (document.getElementById(context+'_recaptchavertxt')) {
15366:         versiontext = document.getElementById(context+'_recaptchavertxt');
15367:     } else {
15368:         return;
15369:     }
15370:     if (caller.checked) {
15371:         if (caller.value == 'recaptcha') {
15372:             pubitem.type = 'text';
15373:             privitem.type = 'text';
15374:             pubitem.size = '40';
15375:             privitem.size = '40';
15376:             pubtext.innerHTML = "$lt{'pub'}";
15377:             privtext.innerHTML = "$lt{'priv'}";
15378:             versionitem.type = 'text';
15379:             versionitem.size = '3';
15380:             versiontext.innerHTML = "$lt{'ver'}";
15381:         } else {
15382:             pubitem.type = 'hidden';
15383:             privitem.type = 'hidden';
15384:             versionitem.type = 'hidden';
15385:             pubtext.innerHTML = '';
15386:             privtext.innerHTML = '';
15387:             versiontext.innerHTML = '';
15388:         }
15389:     }
15390:     return;
15391: }
15392: 
15393: // ]]>
15394: </script>
15395: 
15396: END
15397: 
15398: }
15399: 
15400: sub toggle_display_js {
15401:     return <<"END";
15402: 
15403: <script type="text/javascript">
15404: // <![CDATA[
15405: 
15406: function toggleDisplay(domForm,caller) {
15407:     if (document.getElementById(caller)) {
15408:         var divitem = document.getElementById(caller);
15409:         var optionsElement = domForm.coursecredits;
15410:         var checkval = 1;
15411:         var dispval = 'block';
15412:         var selfcreateRegExp = /^cancreate_emailverified/;
15413:         if (caller == 'emailoptions') {
15414:             optionsElement = domForm.cancreate_email; 
15415:         }
15416:         if (caller == 'studentsubmission') {
15417:             optionsElement = domForm.postsubmit;
15418:         }
15419:         if (caller == 'cloneinstcode') {
15420:             optionsElement = domForm.canclone;
15421:             checkval = 'instcode';
15422:         }
15423:         if (selfcreateRegExp.test(caller)) {
15424:             optionsElement = domForm.elements[caller];
15425:             checkval = 'other';
15426:             dispval = 'inline'
15427:         }
15428:         if (optionsElement.length) {
15429:             var currval;
15430:             for (var i=0; i<optionsElement.length; i++) {
15431:                 if (optionsElement[i].checked) {
15432:                    currval = optionsElement[i].value;
15433:                 }
15434:             }
15435:             if (currval == checkval) {
15436:                 divitem.style.display = dispval;
15437:             } else {
15438:                 divitem.style.display = 'none';
15439:             }
15440:         }
15441:     }
15442:     return;
15443: }
15444: 
15445: // ]]>
15446: </script>
15447: 
15448: END
15449: 
15450: }
15451: 
15452: sub captcha_phrases {
15453:     return &Apache::lonlocal::texthash (
15454:                  priv => 'Private key',
15455:                  pub  => 'Public key',
15456:                  original  => 'original (CAPTCHA)',
15457:                  recaptcha => 'successor (ReCAPTCHA)',
15458:                  notused   => 'unused',
15459:                  ver => 'ReCAPTCHA version (1 or 2)',
15460:     );
15461: }
15462: 
15463: sub devalidate_remote_domconfs {
15464:     my ($dom,$cachekeys) = @_;
15465:     return unless (ref($cachekeys) eq 'HASH');
15466:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15467:     my %thismachine;
15468:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15469:     my @posscached = ('domainconfig','domdefaults','usersessions',
15470:                       'directorysrch','passwdconf','cats');
15471:     if (keys(%servers)) {
15472:         foreach my $server (keys(%servers)) {
15473:             next if ($thismachine{$server});
15474:             my @cached;
15475:             foreach my $name (@posscached) {
15476:                 if ($cachekeys->{$name}) {
15477:                     push(@cached,&escape($name).':'.&escape($dom));
15478:                 }
15479:             }
15480:             if (@cached) {
15481:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
15482:             }
15483:         }
15484:     }
15485:     return;
15486: }
15487: 
15488: 1;

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