File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.102.2.4: download - view: text, annotated - select for diffs
Sat Jul 18 20:28:24 2020 UTC (4 years ago) by raeburn
Branches: version_2_11_3_msu
- For 2.11.3 (modified)
  Include changes in 1.371

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.102.2.4 2020/07/18 20:28:24 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',
  220:                 'passwords','ltitools'],$dom);
  221:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  222:         my %encconfig =
  223:             &Apache::lonnet::get_dom('encconfig',['ltitools'],$dom);
  224:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  225:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  226:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
  227:                     foreach my $item ('key','secret') {
  228:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  229:                     }
  230:                 }
  231:             }
  232:         }
  233:     }
  234:     my @prefs_order = ('rolecolors','login','defaults','passwords','quotas','autoenroll',
  235:                        'autoupdate','autocreate','directorysrch','contacts',
  236:                        'usercreation','selfcreation','usermodification','scantron',
  237:                        'requestcourses','requestauthor','coursecategories',
  238:                        'serverstatuses','helpsettings','coursedefaults',
  239:                        'ltitools','selfenrollment','usersessions');
  240:     my %existing;
  241:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  242:         %existing = %{$domconfig{'loadbalancing'}};
  243:     }
  244:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  245:         push(@prefs_order,'loadbalancing');
  246:     }
  247:     my %prefs = (
  248:         'rolecolors' =>
  249:                    { text => 'Default color schemes',
  250:                      help => 'Domain_Configuration_Color_Schemes',
  251:                      header => [{col1 => 'Student Settings',
  252:                                  col2 => '',},
  253:                                 {col1 => 'Coordinator Settings',
  254:                                  col2 => '',},
  255:                                 {col1 => 'Author Settings',
  256:                                  col2 => '',},
  257:                                 {col1 => 'Administrator Settings',
  258:                                  col2 => '',}],
  259:                       print => \&print_rolecolors,
  260:                       modify => \&modify_rolecolors,
  261:                     },
  262:         'login' =>
  263:                     { text => 'Log-in page options',
  264:                       help => 'Domain_Configuration_Login_Page',
  265:                       header => [{col1 => 'Log-in Page Items',
  266:                                   col2 => '',},
  267:                                  {col1 => 'Log-in Help',
  268:                                   col2 => 'Value'},
  269:                                  {col1 => 'Custom HTML in document head',
  270:                                   col2 => 'Value'}],
  271:                       print => \&print_login,
  272:                       modify => \&modify_login,
  273:                     },
  274:         'defaults' => 
  275:                     { text => 'Default authentication/language/timezone/portal/types',
  276:                       help => 'Domain_Configuration_LangTZAuth',
  277:                       header => [{col1 => 'Setting',
  278:                                   col2 => 'Value'},
  279:                                  {col1 => 'Institutional user types',
  280:                                   col2 => 'Name displayed'}],
  281:                       print => \&print_defaults,
  282:                       modify => \&modify_defaults,
  283:                     },
  284:         'passwords' =>
  285:                     { text => 'Passwords (Internal authentication)',
  286:                       help => 'Domain_Configuration_Passwords',
  287:                       header => [{col1 => 'Resetting Forgotten Password',
  288:                                   col2 => 'Settings'},
  289:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  290:                                   col2 => 'Settings'},
  291:                                  {col1 => 'Rules for LON-CAPA Passwords',
  292:                                   col2 => 'Settings'},
  293:                                  {col1 => 'Course Owner Changing Student Passwords',
  294:                                   col2 => 'Settings'}],
  295:                       print => \&print_passwords,
  296:                       modify => \&modify_passwords,
  297:                     },
  298:         'quotas' => 
  299:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  300:                       help => 'Domain_Configuration_Quotas',
  301:                       header => [{col1 => 'User affiliation',
  302:                                   col2 => 'Available tools',
  303:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  304:                       print => \&print_quotas,
  305:                       modify => \&modify_quotas,
  306:                     },
  307:         'autoenroll' =>
  308:                    { text => 'Auto-enrollment settings',
  309:                      help => 'Domain_Configuration_Auto_Enrollment',
  310:                      header => [{col1 => 'Configuration setting',
  311:                                  col2 => 'Value(s)'}],
  312:                      print => \&print_autoenroll,
  313:                      modify => \&modify_autoenroll,
  314:                    },
  315:         'autoupdate' => 
  316:                    { text => 'Auto-update settings',
  317:                      help => 'Domain_Configuration_Auto_Updates',
  318:                      header => [{col1 => 'Setting',
  319:                                  col2 => 'Value',},
  320:                                 {col1 => 'Setting',
  321:                                  col2 => 'Affiliation'},
  322:                                 {col1 => 'User population',
  323:                                  col2 => 'Updatable user data'}],
  324:                      print => \&print_autoupdate,
  325:                      modify => \&modify_autoupdate,
  326:                   },
  327:         'autocreate' => 
  328:                   { text => 'Auto-course creation settings',
  329:                      help => 'Domain_Configuration_Auto_Creation',
  330:                      header => [{col1 => 'Configuration Setting',
  331:                                  col2 => 'Value',}],
  332:                      print => \&print_autocreate,
  333:                      modify => \&modify_autocreate,
  334:                   },
  335:         'directorysrch' => 
  336:                   { text => 'Directory searches',
  337:                     help => 'Domain_Configuration_InstDirectory_Search',
  338:                     header => [{col1 => 'Institutional Directory Setting',
  339:                                 col2 => 'Value',},
  340:                                {col1 => 'LON-CAPA Directory Setting',
  341:                                 col2 => 'Value',}],
  342:                     print => \&print_directorysrch,
  343:                     modify => \&modify_directorysrch,
  344:                   },
  345:         'contacts' =>
  346:                   { text => 'E-mail addresses and helpform',
  347:                     help => 'Domain_Configuration_Contact_Info',
  348:                     header => [{col1 => 'Default e-mail addresses',
  349:                                 col2 => 'Value',},
  350:                                {col1 => 'Recipient(s) for notifications',
  351:                                 col2 => 'Value',},
  352:                                {col1 => 'Ask helpdesk form settings',
  353:                                 col2 => 'Value',},],
  354:                     print => \&print_contacts,
  355:                     modify => \&modify_contacts,
  356:                   },
  357:         'usercreation' => 
  358:                   { text => 'User creation',
  359:                     help => 'Domain_Configuration_User_Creation',
  360:                     header => [{col1 => 'Format rule type',
  361:                                 col2 => 'Format rules in force'},
  362:                                {col1 => 'User account creation',
  363:                                 col2 => 'Usernames which may be created',},
  364:                                {col1 => 'Context',
  365:                                 col2 => 'Assignable authentication types'}],
  366:                     print => \&print_usercreation,
  367:                     modify => \&modify_usercreation,
  368:                   },
  369:         'selfcreation' => 
  370:                   { text => 'Users self-creating accounts',
  371:                     help => 'Domain_Configuration_Self_Creation', 
  372:                     header => [{col1 => 'Self-creation with institutional username',
  373:                                 col2 => 'Enabled?'},
  374:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  375:                                 col2 => 'Information user can enter'},
  376:                                {col1 => 'Self-creation with e-mail verification',
  377:                                 col2 => 'Settings'}],
  378:                     print => \&print_selfcreation,
  379:                     modify => \&modify_selfcreation,
  380:                   },
  381:         'usermodification' =>
  382:                   { text => 'User modification',
  383:                     help => 'Domain_Configuration_User_Modification',
  384:                     header => [{col1 => 'Target user has role',
  385:                                 col2 => 'User information updatable in author context'},
  386:                                {col1 => 'Target user has role',
  387:                                 col2 => 'User information updatable in course context'}],
  388:                     print => \&print_usermodification,
  389:                     modify => \&modify_usermodification,
  390:                   },
  391:         'scantron' =>
  392:                   { text => 'Bubblesheet format',
  393:                     help => 'Domain_Configuration_Scantron_Format',
  394:                     header => [ {col1 => 'Bubblesheet format file',
  395:                                  col2 => ''},
  396:                                 {col1 => 'Bubblesheet data upload formats',
  397:                                  col2 => 'Settings'}],
  398:                     print => \&print_scantron,
  399:                     modify => \&modify_scantron,
  400:                   },
  401:         'requestcourses' => 
  402:                  {text => 'Request creation of courses',
  403:                   help => 'Domain_Configuration_Request_Courses',
  404:                   header => [{col1 => 'User affiliation',
  405:                               col2 => 'Availability/Processing of requests',},
  406:                              {col1 => 'Setting',
  407:                               col2 => 'Value'},
  408:                              {col1 => 'Available textbooks',
  409:                               col2 => ''},
  410:                              {col1 => 'Available templates',
  411:                               col2 => ''},
  412:                              {col1 => 'Validation (not official courses)',
  413:                               col2 => 'Value'},],
  414:                   print => \&print_quotas,
  415:                   modify => \&modify_quotas,
  416:                  },
  417:         'requestauthor' =>
  418:                  {text => 'Request Authoring Space',
  419:                   help => 'Domain_Configuration_Request_Author',
  420:                   header => [{col1 => 'User affiliation',
  421:                               col2 => 'Availability/Processing of requests',},
  422:                              {col1 => 'Setting',
  423:                               col2 => 'Value'}],
  424:                   print => \&print_quotas,
  425:                   modify => \&modify_quotas,
  426:                  },
  427:         'coursecategories' =>
  428:                   { text => 'Cataloging of courses/communities',
  429:                     help => 'Domain_Configuration_Cataloging_Courses',
  430:                     header => [{col1 => 'Catalog type/availability',
  431:                                 col2 => '',},
  432:                                {col1 => 'Category settings for standard catalog',
  433:                                 col2 => '',},
  434:                                {col1 => 'Categories',
  435:                                 col2 => '',
  436:                                }],
  437:                     print => \&print_coursecategories,
  438:                     modify => \&modify_coursecategories,
  439:                   },
  440:         'serverstatuses' =>
  441:                  {text   => 'Access to server status pages',
  442:                   help   => 'Domain_Configuration_Server_Status',
  443:                   header => [{col1 => 'Status Page',
  444:                               col2 => 'Other named users',
  445:                               col3 => 'Specific IPs',
  446:                             }],
  447:                   print => \&print_serverstatuses,
  448:                   modify => \&modify_serverstatuses,
  449:                  },
  450:         'helpsettings' =>
  451:                  {text   => 'Support settings',
  452:                   help   => 'Domain_Configuration_Help_Settings',
  453:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  454:                               col2 => 'Value'},
  455:                              {col1 => 'Helpdesk Roles',
  456:                               col2 => 'Settings'},],
  457:                   print  => \&print_helpsettings,
  458:                   modify => \&modify_helpsettings,
  459:                  },
  460:         'coursedefaults' => 
  461:                  {text => 'Course/Community defaults',
  462:                   help => 'Domain_Configuration_Course_Defaults',
  463:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  464:                               col2 => 'Value',},
  465:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  466:                               col2 => 'Value',},],
  467:                   print => \&print_coursedefaults,
  468:                   modify => \&modify_coursedefaults,
  469:                  },
  470:         'selfenrollment' => 
  471:                  {text   => 'Self-enrollment in Course/Community',
  472:                   help   => 'Domain_Configuration_Selfenrollment',
  473:                   header => [{col1 => 'Configuration Rights',
  474:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  475:                              {col1 => 'Defaults',
  476:                               col2 => 'Value'},
  477:                              {col1 => 'Self-enrollment validation (optional)',
  478:                               col2 => 'Value'},],
  479:                   print => \&print_selfenrollment,
  480:                   modify => \&modify_selfenrollment,
  481:                  },
  482:         'usersessions' =>
  483:                  {text  => 'User session hosting/offloading',
  484:                   help  => 'Domain_Configuration_User_Sessions',
  485:                   header => [{col1 => 'Domain server',
  486:                               col2 => 'Servers to offload sessions to when busy'},
  487:                              {col1 => 'Hosting of users from other domains',
  488:                               col2 => 'Rules'},
  489:                              {col1 => "Hosting domain's own users elsewhere",
  490:                               col2 => 'Rules'}],
  491:                   print => \&print_usersessions,
  492:                   modify => \&modify_usersessions,
  493:                  },
  494:         'loadbalancing' =>
  495:                  {text  => 'Dedicated Load Balancer(s)',
  496:                   help  => 'Domain_Configuration_Load_Balancing',
  497:                   header => [{col1 => 'Balancers',
  498:                               col2 => 'Default destinations',
  499:                               col3 => 'User affiliation',
  500:                               col4 => 'Overrides'},
  501:                             ],
  502:                   print => \&print_loadbalancing,
  503:                   modify => \&modify_loadbalancing,
  504:                  },
  505:         'ltitools' =>
  506:                  {text => 'External Tools (LTI)',
  507:                   help => 'Domain_Configuration_LTI_Tools',
  508:                   header => [{col1 => 'Setting',
  509:                               col2 => 'Value',}],
  510:                   print => \&print_ltitools,
  511:                   modify => \&modify_ltitools,
  512:                  },
  513:     );
  514:     if (keys(%servers) > 1) {
  515:         $prefs{'login'}  = { text   => 'Log-in page options',
  516:                              help   => 'Domain_Configuration_Login_Page',
  517:                             header => [{col1 => 'Log-in Service',
  518:                                         col2 => 'Server Setting',},
  519:                                        {col1 => 'Log-in Page Items',
  520:                                         col2 => ''},
  521:                                        {col1 => 'Log-in Help',
  522:                                         col2 => 'Value'},
  523:                                        {col1 => 'Custom HTML in document head',
  524:                                         col2 => 'Value'}],
  525:                             print => \&print_login,
  526:                             modify => \&modify_login,
  527:                            };
  528:     }
  529: 
  530:     my @roles = ('student','coordinator','author','admin');
  531:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  532:     &Apache::lonhtmlcommon::add_breadcrumb
  533:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  534:       text=>"Settings to display/modify"});
  535:     my $confname = $dom.'-domainconfig';
  536: 
  537:     if ($phase eq 'process') {
  538:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  539:                                                               \%prefs,\%domconfig,$confname,\@roles);
  540:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  541:             $r->rflush();
  542:             &devalidate_remote_domconfs($dom,$result);
  543:         }
  544:     } elsif ($phase eq 'display') {
  545:         my $js = &recaptcha_js().
  546:                  &toggle_display_js();
  547:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  548:             my ($othertitle,$usertypes,$types) =
  549:                 &Apache::loncommon::sorted_inst_types($dom);
  550:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  551:                                           $domconfig{'loadbalancing'}).
  552:                    &new_spares_js().
  553:                    &common_domprefs_js().
  554:                    &Apache::loncommon::javascript_array_indexof();
  555:         }
  556:         if (grep(/^requestcourses$/,@actions)) {
  557:             my $javascript_validations;
  558:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  559:             $js .= <<END;
  560: <script type="text/javascript">
  561: $javascript_validations
  562: </script>
  563: $coursebrowserjs
  564: END
  565:         }
  566:         if (grep(/^selfcreation$/,@actions)) {
  567:             $js .= &selfcreate_javascript();
  568:         }
  569:         if (grep(/^contacts$/,@actions)) {
  570:             $js .= &contacts_javascript();
  571:         }
  572:         if (grep(/^scantron$/,@actions)) {
  573:             $js .= &scantron_javascript();
  574:         }
  575:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  576:     } else {
  577: # check if domconfig user exists for the domain.
  578:         my $servadm = $r->dir_config('lonAdmEMail');
  579:         my ($configuserok,$author_ok,$switchserver) =
  580:             &config_check($dom,$confname,$servadm);
  581:         unless ($configuserok eq 'ok') {
  582:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  583:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  584:                           $confname).
  585:                       '<br />'
  586:             );
  587:             if ($switchserver) {
  588:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  589:                           '<br />'.
  590:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  591:                           '<br />'.
  592:                           &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).
  593:                           '<br />'.
  594:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  595:                 );
  596:             } else {
  597:                 $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.').
  598:                           '<br />'.
  599:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  600:                 );
  601:             }
  602:             $r->print(&Apache::loncommon::end_page());
  603:             return OK;
  604:         }
  605:         if (keys(%domconfig) == 0) {
  606:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  607:             my @ids=&Apache::lonnet::current_machine_ids();
  608:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  609:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  610:                 my @loginimages = ('img','logo','domlogo','login');
  611:                 my $custom_img_count = 0;
  612:                 foreach my $img (@loginimages) {
  613:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  614:                         $custom_img_count ++;
  615:                     }
  616:                 }
  617:                 foreach my $role (@roles) {
  618:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  619:                         $custom_img_count ++;
  620:                     }
  621:                 }
  622:                 if ($custom_img_count > 0) {
  623:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  624:                     my $switch_server = &check_switchserver($dom,$confname);
  625:                     $r->print(
  626:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  627:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  628:     &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 />'.
  629:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  630:                     if ($switch_server) {
  631:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  632:                     }
  633:                     $r->print(&Apache::loncommon::end_page());
  634:                     return OK;
  635:                 }
  636:             }
  637:         }
  638:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  639:     }
  640:     return OK;
  641: }
  642: 
  643: sub process_changes {
  644:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  645:     my %domconfig;
  646:     if (ref($values) eq 'HASH') {
  647:         %domconfig = %{$values};
  648:     }
  649:     my $output;
  650:     if ($action eq 'login') {
  651:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  652:     } elsif ($action eq 'rolecolors') {
  653:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  654:                                      $lastactref,%domconfig);
  655:     } elsif ($action eq 'quotas') {
  656:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  657:     } elsif ($action eq 'autoenroll') {
  658:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  659:     } elsif ($action eq 'autoupdate') {
  660:         $output = &modify_autoupdate($dom,%domconfig);
  661:     } elsif ($action eq 'autocreate') {
  662:         $output = &modify_autocreate($dom,%domconfig);
  663:     } elsif ($action eq 'directorysrch') {
  664:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  665:     } elsif ($action eq 'usercreation') {
  666:         $output = &modify_usercreation($dom,%domconfig);
  667:     } elsif ($action eq 'selfcreation') {
  668:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  669:     } elsif ($action eq 'usermodification') {
  670:         $output = &modify_usermodification($dom,%domconfig);
  671:     } elsif ($action eq 'contacts') {
  672:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  673:     } elsif ($action eq 'defaults') {
  674:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  675:     } elsif ($action eq 'scantron') {
  676:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  677:     } elsif ($action eq 'coursecategories') {
  678:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  679:     } elsif ($action eq 'serverstatuses') {
  680:         $output = &modify_serverstatuses($dom,%domconfig);
  681:     } elsif ($action eq 'requestcourses') {
  682:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  683:     } elsif ($action eq 'requestauthor') {
  684:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  685:     } elsif ($action eq 'helpsettings') {
  686:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  687:     } elsif ($action eq 'coursedefaults') {
  688:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  689:     } elsif ($action eq 'selfenrollment') {
  690:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  691:     } elsif ($action eq 'usersessions') {
  692:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  693:     } elsif ($action eq 'loadbalancing') {
  694:         $output = &modify_loadbalancing($dom,%domconfig);
  695:     } elsif ($action eq 'passwords') {
  696:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  697:     } elsif ($action eq 'ltitools') {
  698:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  699:     }
  700:     return $output;
  701: }
  702: 
  703: sub print_config_box {
  704:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  705:     my $rowtotal = 0;
  706:     my $output;
  707:     if ($action eq 'coursecategories') {
  708:         $output = &coursecategories_javascript($settings);
  709:     } elsif ($action eq 'defaults') {
  710:         $output = &defaults_javascript($settings); 
  711:     } elsif ($action eq 'passwords') {
  712:         $output = &passwords_javascript();
  713:     } elsif ($action eq 'helpsettings') {
  714:         my (%privs,%levelscurrent);
  715:         my %full=();
  716:         my %levels=(
  717:                      course => {},
  718:                      domain => {},
  719:                      system => {},
  720:                    );
  721:         my $context = 'domain';
  722:         my $crstype = 'Course';
  723:         my $formname = 'display';
  724:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  725:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  726:         $output =
  727:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  728:                                                       \@templateroles);
  729:     }
  730:     $output .=
  731:          '<table class="LC_nested_outer">
  732:           <tr>
  733:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  734:            &mt($item->{text}).'&nbsp;'.
  735:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  736:           '</tr>';
  737:     $rowtotal ++;
  738:     my $numheaders = 1;
  739:     if (ref($item->{'header'}) eq 'ARRAY') {
  740:         $numheaders = scalar(@{$item->{'header'}});
  741:     }
  742:     if ($numheaders > 1) {
  743:         my $colspan = '';
  744:         my $rightcolspan = '';
  745:         my $leftnobr = '';
  746:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  747:             ($action eq 'directorysrch') ||
  748:             (($action eq 'login') && ($numheaders < 4))) {
  749:             $colspan = ' colspan="2"';
  750:         }
  751:         if ($action eq 'usersessions') {
  752:             $rightcolspan = ' colspan="3"'; 
  753:         }
  754:         if ($action eq 'passwords') {
  755:             $leftnobr = ' LC_nobreak';
  756:         }
  757:         $output .= '
  758:           <tr>
  759:            <td>
  760:             <table class="LC_nested">
  761:              <tr class="LC_info_row">
  762:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  763:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  764:              </tr>';
  765:         $rowtotal ++;
  766:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  767:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  768:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  769:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  770:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  771:         } elsif ($action eq 'passwords') {
  772:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  773:         } elsif ($action eq 'coursecategories') {
  774:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  775:         } elsif ($action eq 'scantron') {
  776:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  777:         } elsif ($action eq 'login') {
  778:             if ($numheaders == 4) {
  779:                 $colspan = ' colspan="2"';
  780:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  781:             } else {
  782:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  783:             }
  784:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  785:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  786:         } elsif ($action eq 'rolecolors') {
  787:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  788:         }
  789:         $output .= '
  790:            </table>
  791:           </td>
  792:          </tr>
  793:          <tr>
  794:            <td>
  795:             <table class="LC_nested">
  796:              <tr class="LC_info_row">
  797:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  798:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  799:              </tr>';
  800:             $rowtotal ++;
  801:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  802:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  803:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  804:             ($action eq 'contacts') || ($action eq 'passwords')) {
  805:             if ($action eq 'coursecategories') {
  806:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  807:                 $colspan = ' colspan="2"';
  808:             } elsif ($action eq 'passwords') {
  809:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  810:             } else {
  811:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  812:             }
  813:             $output .= '
  814:            </table>
  815:           </td>
  816:          </tr>
  817:          <tr>
  818:            <td>
  819:             <table class="LC_nested">
  820:              <tr class="LC_info_row">
  821:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  822:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  823:              </tr>'."\n";
  824:             if ($action eq 'coursecategories') {
  825:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  826:             } elsif ($action eq 'passwords') {
  827:                 $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal).'
  828:              </tr>
  829:             </table>
  830:            </td>
  831:           </tr>
  832:           <tr>
  833:            <td>
  834:             <table class="LC_nested">
  835:              <tr class="LC_info_row">
  836:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  837:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n".
  838:               $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal).'
  839:             </table>
  840:           </td>
  841:          </tr>
  842:          <tr>';
  843:             } else {
  844:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  845:             }
  846:             $rowtotal ++;
  847:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  848:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  849:                  ($action eq 'helpsettings')) {
  850:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  851:         } elsif ($action eq 'scantron') {
  852:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
  853:         } elsif ($action eq 'login') {
  854:             if ($numheaders == 4) {
  855:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  856:            </table>
  857:           </td>
  858:          </tr>
  859:          <tr>
  860:            <td>
  861:             <table class="LC_nested">
  862:              <tr class="LC_info_row">
  863:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  864:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  865:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  866:                 $rowtotal ++;
  867:             } else {
  868:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  869:             }
  870:             $output .= '
  871:            </table>
  872:           </td>
  873:          </tr>
  874:          <tr>
  875:            <td>
  876:             <table class="LC_nested">
  877:              <tr class="LC_info_row">';
  878:             if ($numheaders == 4) {
  879:                 $output .= '
  880:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  881:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  882:              </tr>';
  883:             } else {
  884:                 $output .= '
  885:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  886:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  887:              </tr>';
  888:             }
  889:             $rowtotal ++;
  890:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  891:         } elsif ($action eq 'requestcourses') {
  892:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  893:             $rowtotal ++;
  894:             $output .= &print_studentcode($settings,\$rowtotal).'
  895:            </table>
  896:           </td>
  897:          </tr>
  898:          <tr>
  899:            <td>
  900:             <table class="LC_nested">
  901:              <tr class="LC_info_row">
  902:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  903:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  904:                        &textbookcourses_javascript($settings).
  905:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  906:             </table>
  907:            </td>
  908:           </tr>
  909:          <tr>
  910:            <td>
  911:             <table class="LC_nested">
  912:              <tr class="LC_info_row">
  913:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  914:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  915:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  916:             </table>
  917:            </td>
  918:           </tr>
  919:           <tr>
  920:            <td>
  921:             <table class="LC_nested">
  922:              <tr class="LC_info_row">
  923:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  924:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  925:              </tr>'.
  926:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  927:         } elsif ($action eq 'requestauthor') {
  928:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  929:             $rowtotal ++;
  930:         } elsif ($action eq 'rolecolors') {
  931:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  932:            </table>
  933:           </td>
  934:          </tr>
  935:          <tr>
  936:            <td>
  937:             <table class="LC_nested">
  938:              <tr class="LC_info_row">
  939:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  940:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  941:               <td class="LC_right_item" valign="top">'.
  942:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  943:              </tr>'.
  944:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  945:            </table>
  946:           </td>
  947:          </tr>
  948:          <tr>
  949:            <td>
  950:             <table class="LC_nested">
  951:              <tr class="LC_info_row">
  952:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  953:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  954:              </tr>'.
  955:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  956:             $rowtotal += 2;
  957:         }
  958:     } else {
  959:         $output .= '
  960:           <tr>
  961:            <td>
  962:             <table class="LC_nested">
  963:              <tr class="LC_info_row">';
  964:         if ($action eq 'login') {
  965:             $output .= '  
  966:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  967:         } elsif ($action eq 'serverstatuses') {
  968:             $output .= '
  969:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  970:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  971: 
  972:         } else {
  973:             $output .= '
  974:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  975:         }
  976:         if (defined($item->{'header'}->[0]->{'col3'})) {
  977:             $output .= '<td class="LC_left_item" valign="top">'.
  978:                        &mt($item->{'header'}->[0]->{'col2'});
  979:             if ($action eq 'serverstatuses') {
  980:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  981:             } 
  982:         } else {
  983:             $output .= '<td class="LC_right_item" valign="top">'.
  984:                        &mt($item->{'header'}->[0]->{'col2'});
  985:         }
  986:         $output .= '</td>';
  987:         if ($item->{'header'}->[0]->{'col3'}) {
  988:             if (defined($item->{'header'}->[0]->{'col4'})) {
  989:                 $output .= '<td class="LC_left_item" valign="top">'.
  990:                             &mt($item->{'header'}->[0]->{'col3'});
  991:             } else {
  992:                 $output .= '<td class="LC_right_item" valign="top">'.
  993:                            &mt($item->{'header'}->[0]->{'col3'});
  994:             }
  995:             if ($action eq 'serverstatuses') {
  996:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  997:             }
  998:             $output .= '</td>';
  999:         }
 1000:         if ($item->{'header'}->[0]->{'col4'}) {
 1001:             $output .= '<td class="LC_right_item" valign="top">'.
 1002:                        &mt($item->{'header'}->[0]->{'col4'});
 1003:         }
 1004:         $output .= '</tr>';
 1005:         $rowtotal ++;
 1006:         if ($action eq 'quotas') {
 1007:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1008:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1009:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') ||
 1010:                  ($action eq 'ltitools')) {
 1011:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1012:         }
 1013:     }
 1014:     $output .= '
 1015:    </table>
 1016:   </td>
 1017:  </tr>
 1018: </table><br />';
 1019:     return ($output,$rowtotal);
 1020: }
 1021: 
 1022: sub print_login {
 1023:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1024:     my ($css_class,$datatable);
 1025:     my %choices = &login_choices();
 1026: 
 1027:     if ($caller eq 'service') {
 1028:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1029:         my $choice = $choices{'disallowlogin'};
 1030:         $css_class = ' class="LC_odd_row"';
 1031:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1032:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1033:                       '<th>'.$choices{'server'}.'</th>'.
 1034:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1035:                       '<th>'.$choices{'custompath'}.'</th>'.
 1036:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1037:         my %disallowed;
 1038:         if (ref($settings) eq 'HASH') {
 1039:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1040:                %disallowed = %{$settings->{'loginvia'}};
 1041:             }
 1042:         }
 1043:         foreach my $lonhost (sort(keys(%servers))) {
 1044:             my $direct = 'selected="selected"';
 1045:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1046:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1047:                     $direct = '';
 1048:                 }
 1049:             }
 1050:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1051:                           '<td><select name="'.$lonhost.'_server">'.
 1052:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1053:                           '</option>';
 1054:             foreach my $hostid (sort(keys(%servers))) {
 1055:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1056:                 my $selected = '';
 1057:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1058:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1059:                         $selected = 'selected="selected"';
 1060:                     }
 1061:                 }
 1062:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1063:                               $servers{$hostid}.'</option>';
 1064:             }
 1065:             $datatable .= '</select></td>'.
 1066:                           '<td><select name="'.$lonhost.'_serverpath">';
 1067:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1068:                 my $pathname = $path;
 1069:                 if ($path eq 'custom') {
 1070:                     $pathname = &mt('Custom Path').' ->';
 1071:                 }
 1072:                 my $selected = '';
 1073:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1074:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1075:                         $selected = 'selected="selected"';
 1076:                     }
 1077:                 } elsif ($path eq '') {
 1078:                     $selected = 'selected="selected"';
 1079:                 }
 1080:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1081:             }
 1082:             $datatable .= '</select></td>';
 1083:             my ($custom,$exempt);
 1084:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1085:                 $custom = $disallowed{$lonhost}{'custompath'};
 1086:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1087:             }
 1088:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1089:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1090:                           '</tr>';
 1091:         }
 1092:         $datatable .= '</table></td></tr>';
 1093:         return $datatable;
 1094:     } elsif ($caller eq 'page') {
 1095:         my %defaultchecked = ( 
 1096:                                'coursecatalog' => 'on',
 1097:                                'helpdesk'      => 'on',
 1098:                                'adminmail'     => 'off',
 1099:                                'newuser'       => 'off',
 1100:                              );
 1101:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1102:         my (%checkedon,%checkedoff);
 1103:         foreach my $item (@toggles) {
 1104:             if ($defaultchecked{$item} eq 'on') { 
 1105:                 $checkedon{$item} = ' checked="checked" ';
 1106:                 $checkedoff{$item} = ' ';
 1107:             } elsif ($defaultchecked{$item} eq 'off') {
 1108:                 $checkedoff{$item} = ' checked="checked" ';
 1109:                 $checkedon{$item} = ' ';
 1110:             }
 1111:         }
 1112:         my @images = ('img','logo','domlogo','login');
 1113:         my @logintext = ('textcol','bgcol');
 1114:         my @bgs = ('pgbg','mainbg','sidebg');
 1115:         my @links = ('link','alink','vlink');
 1116:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1117:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1118:         my (%is_custom,%designs);
 1119:         my %defaults = (
 1120:                        font => $defaultdesign{'login.font'},
 1121:                        );
 1122:         foreach my $item (@images) {
 1123:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1124:             $defaults{'showlogo'}{$item} = 1;
 1125:         }
 1126:         foreach my $item (@bgs) {
 1127:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1128:         }
 1129:         foreach my $item (@logintext) {
 1130:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1131:         }
 1132:         foreach my $item (@links) {
 1133:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1134:         }
 1135:         if (ref($settings) eq 'HASH') {
 1136:             foreach my $item (@toggles) {
 1137:                 if ($settings->{$item} eq '1') {
 1138:                     $checkedon{$item} =  ' checked="checked" ';
 1139:                     $checkedoff{$item} = ' ';
 1140:                 } elsif ($settings->{$item} eq '0') {
 1141:                     $checkedoff{$item} =  ' checked="checked" ';
 1142:                     $checkedon{$item} = ' ';
 1143:                 }
 1144:             }
 1145:             foreach my $item (@images) {
 1146:                 if (defined($settings->{$item})) {
 1147:                     $designs{$item} = $settings->{$item};
 1148:                     $is_custom{$item} = 1;
 1149:                 }
 1150:                 if (defined($settings->{'showlogo'}{$item})) {
 1151:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1152:                 }
 1153:             }
 1154:             foreach my $item (@logintext) {
 1155:                 if ($settings->{$item} ne '') {
 1156:                     $designs{'logintext'}{$item} = $settings->{$item};
 1157:                     $is_custom{$item} = 1;
 1158:                 }
 1159:             }
 1160:             if ($settings->{'font'} ne '') {
 1161:                 $designs{'font'} = $settings->{'font'};
 1162:                 $is_custom{'font'} = 1;
 1163:             }
 1164:             foreach my $item (@bgs) {
 1165:                 if ($settings->{$item} ne '') {
 1166:                     $designs{'bgs'}{$item} = $settings->{$item};
 1167:                     $is_custom{$item} = 1;
 1168:                 }
 1169:             }
 1170:             foreach my $item (@links) {
 1171:                 if ($settings->{$item} ne '') {
 1172:                     $designs{'links'}{$item} = $settings->{$item};
 1173:                     $is_custom{$item} = 1;
 1174:                 }
 1175:             }
 1176:         } else {
 1177:             if ($designhash{$dom.'.login.font'} ne '') {
 1178:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1179:                 $is_custom{'font'} = 1;
 1180:             }
 1181:             foreach my $item (@images) {
 1182:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1183:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1184:                     $is_custom{$item} = 1;
 1185:                 }
 1186:             }
 1187:             foreach my $item (@bgs) {
 1188:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1189:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1190:                     $is_custom{$item} = 1;
 1191:                 }
 1192:             }
 1193:             foreach my $item (@links) {
 1194:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1195:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1196:                     $is_custom{$item} = 1;
 1197:                 }
 1198:             }
 1199:         }
 1200:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1201:                                                       logo => 'Institution Logo',
 1202:                                                       domlogo => 'Domain Logo',
 1203:                                                       login => 'Login box');
 1204:         my $itemcount = 1;
 1205:         foreach my $item (@toggles) {
 1206:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1207:             $datatable .=  
 1208:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1209:                 '</td><td>'.
 1210:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1211:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1212:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1213:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1214:                 '</tr>';
 1215:             $itemcount ++;
 1216:         }
 1217:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1218:         $datatable .= '</tr></table></td></tr>';
 1219:     } elsif ($caller eq 'help') {
 1220:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1221:         my $switchserver = &check_switchserver($dom,$confname);
 1222:         my $itemcount = 1;
 1223:         $defaulturl = '/adm/loginproblems.html';
 1224:         $defaulttype = 'default';
 1225:         %lt = &Apache::lonlocal::texthash (
 1226:                      del     => 'Delete?',
 1227:                      rep     => 'Replace:',
 1228:                      upl     => 'Upload:',
 1229:                      default => 'Default',
 1230:                      custom  => 'Custom',
 1231:                                              );
 1232:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1233:         my @currlangs;
 1234:         if (ref($settings) eq 'HASH') {
 1235:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1236:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1237:                     next if ($settings->{'helpurl'}{$key} eq '');
 1238:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1239:                     $type{$key} = 'custom';
 1240:                     unless ($key eq 'nolang') {
 1241:                         push(@currlangs,$key);
 1242:                     }
 1243:                 }
 1244:             } elsif ($settings->{'helpurl'} ne '') {
 1245:                 $type{'nolang'} = 'custom';
 1246:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1247:             }
 1248:         }
 1249:         foreach my $lang ('nolang',sort(@currlangs)) {
 1250:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1251:             $datatable .= '<tr'.$css_class.'>';
 1252:             if ($url{$lang} eq '') {
 1253:                 $url{$lang} = $defaulturl;
 1254:             }
 1255:             if ($type{$lang} eq '') {
 1256:                 $type{$lang} = $defaulttype;
 1257:             }
 1258:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1259:             if ($lang eq 'nolang') {
 1260:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1261:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1262:             } else {
 1263:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1264:                                   $langchoices{$lang},
 1265:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1266:             }
 1267:             $datatable .= '</span></td>'."\n".
 1268:                           '<td class="LC_left_item">';
 1269:             if ($type{$lang} eq 'custom') {
 1270:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1271:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1272:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1273:             } else {
 1274:                 $datatable .= $lt{'upl'};
 1275:             }
 1276:             $datatable .='<br />';
 1277:             if ($switchserver) {
 1278:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1279:             } else {
 1280:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1281:             }
 1282:             $datatable .= '</td></tr>';
 1283:             $itemcount ++;
 1284:         }
 1285:         my @addlangs;
 1286:         foreach my $lang (sort(keys(%langchoices))) {
 1287:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1288:             push(@addlangs,$lang);
 1289:         }
 1290:         if (@addlangs > 0) {
 1291:             my %toadd;
 1292:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1293:             $toadd{''} = &mt('Select');
 1294:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1295:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1296:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1297:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1298:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1299:             if ($switchserver) {
 1300:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1301:             } else {
 1302:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1303:             }
 1304:             $datatable .= '</td></tr>';
 1305:             $itemcount ++;
 1306:         }
 1307:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1308:     } elsif ($caller eq 'headtag') {
 1309:         my %domservers = &Apache::lonnet::get_servers($dom);
 1310:         my $choice = $choices{'headtag'};
 1311:         $css_class = ' class="LC_odd_row"';
 1312:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1313:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1314:                       '<th>'.$choices{'current'}.'</th>'.
 1315:                       '<th>'.$choices{'action'}.'</th>'.
 1316:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1317:         my (%currurls,%currexempt);
 1318:         if (ref($settings) eq 'HASH') {
 1319:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1320:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1321:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1322:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1323:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1324:                     }
 1325:                 }
 1326:             }
 1327:         }
 1328:         my %lt = &Apache::lonlocal::texthash(
 1329:                                                del  => 'Delete?',
 1330:                                                rep  => 'Replace:',
 1331:                                                upl  => 'Upload:',
 1332:                                                curr => 'View contents',
 1333:                                                none => 'None',
 1334:         );
 1335:         my $switchserver = &check_switchserver($dom,$confname);
 1336:         foreach my $lonhost (sort(keys(%domservers))) {
 1337:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1338:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1339:             if ($currurls{$lonhost}) {
 1340:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1341:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1342:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1343:                               '">'.$lt{'curr'}.'</a></td>'.
 1344:                               '<td><span class="LC_nobreak"><label>'.
 1345:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1346:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1347:             } else {
 1348:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1349:             }
 1350:             $datatable .='<br />';
 1351:             if ($switchserver) {
 1352:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1353:             } else {
 1354:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1355:             }
 1356:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1357:         }
 1358:         $datatable .= '</table></td></tr>';
 1359:     }
 1360:     return $datatable;
 1361: }
 1362: 
 1363: sub login_choices {
 1364:     my %choices =
 1365:         &Apache::lonlocal::texthash (
 1366:             coursecatalog => 'Display Course/Community Catalog link?',
 1367:             adminmail     => "Display Administrator's E-mail Address?",
 1368:             helpdesk      => 'Display "Contact Helpdesk" link',
 1369:             disallowlogin => "Login page requests redirected",
 1370:             hostid        => "Server",
 1371:             server        => "Redirect to:",
 1372:             serverpath    => "Path",
 1373:             custompath    => "Custom", 
 1374:             exempt        => "Exempt IP(s)",
 1375:             directlogin   => "No redirect",
 1376:             newuser       => "Link to create a user account",
 1377:             img           => "Header",
 1378:             logo          => "Main Logo",
 1379:             domlogo       => "Domain Logo",
 1380:             login         => "Log-in Header", 
 1381:             textcol       => "Text color",
 1382:             bgcol         => "Box color",
 1383:             bgs           => "Background colors",
 1384:             links         => "Link colors",
 1385:             font          => "Font color",
 1386:             pgbg          => "Header",
 1387:             mainbg        => "Page",
 1388:             sidebg        => "Login box",
 1389:             link          => "Link",
 1390:             alink         => "Active link",
 1391:             vlink         => "Visited link",
 1392:             headtag       => "Custom markup",
 1393:             action        => "Action",
 1394:             current       => "Current",
 1395:         );
 1396:     return %choices;
 1397: }
 1398: 
 1399: sub print_rolecolors {
 1400:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1401:     my %choices = &color_font_choices();
 1402:     my @bgs = ('pgbg','tabbg','sidebg');
 1403:     my @links = ('link','alink','vlink');
 1404:     my @images = ('img');
 1405:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1406:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1407:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1408:     my (%is_custom,%designs);
 1409:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1410:     if (ref($settings) eq 'HASH') {
 1411:         if (ref($settings->{$role}) eq 'HASH') {
 1412:             if ($settings->{$role}->{'img'} ne '') {
 1413:                 $designs{'img'} = $settings->{$role}->{'img'};
 1414:                 $is_custom{'img'} = 1;
 1415:             }
 1416:             if ($settings->{$role}->{'font'} ne '') {
 1417:                 $designs{'font'} = $settings->{$role}->{'font'};
 1418:                 $is_custom{'font'} = 1;
 1419:             }
 1420:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1421:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1422:                 $is_custom{'fontmenu'} = 1;
 1423:             }
 1424:             foreach my $item (@bgs) {
 1425:                 if ($settings->{$role}->{$item} ne '') {
 1426:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1427:                     $is_custom{$item} = 1;
 1428:                 }
 1429:             }
 1430:             foreach my $item (@links) {
 1431:                 if ($settings->{$role}->{$item} ne '') {
 1432:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1433:                     $is_custom{$item} = 1;
 1434:                 }
 1435:             }
 1436:         }
 1437:     } else {
 1438:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1439:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1440:             $is_custom{'img'} = 1;
 1441:         }
 1442:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1443:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1444:             $is_custom{'fontmenu'} = 1; 
 1445:         }
 1446:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1447:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1448:             $is_custom{'font'} = 1;
 1449:         }
 1450:         foreach my $item (@bgs) {
 1451:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1452:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1453:                 $is_custom{$item} = 1;
 1454:             
 1455:             }
 1456:         }
 1457:         foreach my $item (@links) {
 1458:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1459:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1460:                 $is_custom{$item} = 1;
 1461:             }
 1462:         }
 1463:     }
 1464:     my $itemcount = 1;
 1465:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1466:     $datatable .= '</tr></table></td></tr>';
 1467:     return $datatable;
 1468: }
 1469: 
 1470: sub role_defaults {
 1471:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1472:     my %defaults;
 1473:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1474:         return %defaults;
 1475:     }
 1476:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1477:     if ($role eq 'login') {
 1478:         %defaults = (
 1479:                        font => $defaultdesign{$role.'.font'},
 1480:                     );
 1481:         if (ref($logintext) eq 'ARRAY') {
 1482:             foreach my $item (@{$logintext}) {
 1483:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1484:             }
 1485:         }
 1486:         foreach my $item (@{$images}) {
 1487:             $defaults{'showlogo'}{$item} = 1;
 1488:         }
 1489:     } else {
 1490:         %defaults = (
 1491:                        img => $defaultdesign{$role.'.img'},
 1492:                        font => $defaultdesign{$role.'.font'},
 1493:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1494:                     );
 1495:     }
 1496:     foreach my $item (@{$bgs}) {
 1497:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1498:     }
 1499:     foreach my $item (@{$links}) {
 1500:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1501:     }
 1502:     foreach my $item (@{$images}) {
 1503:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1504:     }
 1505:     return %defaults;
 1506: }
 1507: 
 1508: sub display_color_options {
 1509:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1510:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1511:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1512:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1513:     my $datatable = '<tr'.$css_class.'>'.
 1514:         '<td>'.$choices->{'font'}.'</td>';
 1515:     if (!$is_custom->{'font'}) {
 1516:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1517:     } else {
 1518:         $datatable .= '<td>&nbsp;</td>';
 1519:     }
 1520:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1521: 
 1522:     $datatable .= '<td><span class="LC_nobreak">'.
 1523:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1524:                   ' value="'.$current_color.'" />&nbsp;'.
 1525:                   '&nbsp;</span></td></tr>';
 1526:     unless ($role eq 'login') { 
 1527:         $datatable .= '<tr'.$css_class.'>'.
 1528:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1529:         if (!$is_custom->{'fontmenu'}) {
 1530:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1531:         } else {
 1532:             $datatable .= '<td>&nbsp;</td>';
 1533:         }
 1534: 	$current_color = $designs->{'fontmenu'} ?
 1535: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1536:         $datatable .= '<td><span class="LC_nobreak">'.
 1537:                       '<input class="colorchooser" type="text" size="10" name="'
 1538: 		      .$role.'_fontmenu"'.
 1539:                       ' value="'.$current_color.'" />&nbsp;'.
 1540:                       '&nbsp;</span></td></tr>';
 1541:     }
 1542:     my $switchserver = &check_switchserver($dom,$confname);
 1543:     foreach my $img (@{$images}) {
 1544: 	$itemcount ++;
 1545:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1546:         $datatable .= '<tr'.$css_class.'>'.
 1547:                       '<td>'.$choices->{$img};
 1548:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1549:         if ($role eq 'login') {
 1550:             if ($img eq 'login') {
 1551:                 $login_hdr_pick =
 1552:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1553:                 $logincolors =
 1554:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1555:                                        $designs,$defaults);
 1556:             } elsif ($img ne 'domlogo') {
 1557:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1558:             }
 1559:         }
 1560:         $datatable .= '</td>';
 1561:         if ($designs->{$img} ne '') {
 1562:             $imgfile = $designs->{$img};
 1563: 	    $img_import = ($imgfile =~ m{^/adm/});
 1564:         } else {
 1565:             $imgfile = $defaults->{$img};
 1566:         }
 1567:         if ($imgfile) {
 1568:             my ($showfile,$fullsize);
 1569:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1570:                 my $urldir = $1;
 1571:                 my $filename = $2;
 1572:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1573:                 if (@info) {
 1574:                     my $thumbfile = 'tn-'.$filename;
 1575:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1576:                     if (@thumb) {
 1577:                         $showfile = $urldir.'/'.$thumbfile;
 1578:                     } else {
 1579:                         $showfile = $imgfile;
 1580:                     }
 1581:                 } else {
 1582:                     $showfile = '';
 1583:                 }
 1584:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1585:                 $showfile = $imgfile;
 1586:                 my $imgdir = $1;
 1587:                 my $filename = $2;
 1588:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1589:                     $showfile = "/$imgdir/tn-".$filename;
 1590:                 } else {
 1591:                     my $input = $londocroot.$imgfile;
 1592:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1593:                     if (!-e $output) {
 1594:                         my ($width,$height) = &thumb_dimensions();
 1595:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1596:                         if ($fullwidth ne '' && $fullheight ne '') {
 1597:                             if ($fullwidth > $width && $fullheight > $height) { 
 1598:                                 my $size = $width.'x'.$height;
 1599:                                 my @args = ('convert','-sample',$size,$input,$output);
 1600:                                 system({$args[0]} @args);
 1601:                                 $showfile = "/$imgdir/tn-".$filename;
 1602:                             }
 1603:                         }
 1604:                     }
 1605:                 }
 1606:             }
 1607:             if ($showfile) {
 1608:                 if ($showfile =~ m{^/(adm|res)/}) {
 1609:                     if ($showfile =~ m{^/res/}) {
 1610:                         my $local_showfile =
 1611:                             &Apache::lonnet::filelocation('',$showfile);
 1612:                         &Apache::lonnet::repcopy($local_showfile);
 1613:                     }
 1614:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1615:                 }
 1616:                 if ($imgfile) {
 1617:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1618:                         if ($imgfile =~ m{^/res/}) {
 1619:                             my $local_imgfile =
 1620:                                 &Apache::lonnet::filelocation('',$imgfile);
 1621:                             &Apache::lonnet::repcopy($local_imgfile);
 1622:                         }
 1623:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1624:                     } else {
 1625:                         $fullsize = $imgfile;
 1626:                     }
 1627:                 }
 1628:                 $datatable .= '<td>';
 1629:                 if ($img eq 'login') {
 1630:                     $datatable .= $login_hdr_pick;
 1631:                 } 
 1632:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1633:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1634:             } else {
 1635:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1636:                               &mt('Upload:').'<br />';
 1637:             }
 1638:         } else {
 1639:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1640:                           &mt('Upload:').'<br />';
 1641:         }
 1642:         if ($switchserver) {
 1643:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1644:         } else {
 1645:             if ($img ne 'login') { # suppress file selection for Log-in header
 1646:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1647:             }
 1648:         }
 1649:         $datatable .= '</td></tr>';
 1650:     }
 1651:     $itemcount ++;
 1652:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1653:     $datatable .= '<tr'.$css_class.'>'.
 1654:                   '<td>'.$choices->{'bgs'}.'</td>';
 1655:     my $bgs_def;
 1656:     foreach my $item (@{$bgs}) {
 1657:         if (!$is_custom->{$item}) {
 1658:             $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>';
 1659:         }
 1660:     }
 1661:     if ($bgs_def) {
 1662:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1663:     } else {
 1664:         $datatable .= '<td>&nbsp;</td>';
 1665:     }
 1666:     $datatable .= '<td class="LC_right_item">'.
 1667:                   '<table border="0"><tr>';
 1668: 
 1669:     foreach my $item (@{$bgs}) {
 1670:         $datatable .= '<td align="center">'.$choices->{$item};
 1671: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1672:         if ($designs->{'bgs'}{$item}) {
 1673:             $datatable .= '&nbsp;';
 1674:         }
 1675:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1676:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1677:     }
 1678:     $datatable .= '</tr></table></td></tr>';
 1679:     $itemcount ++;
 1680:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1681:     $datatable .= '<tr'.$css_class.'>'.
 1682:                   '<td>'.$choices->{'links'}.'</td>';
 1683:     my $links_def;
 1684:     foreach my $item (@{$links}) {
 1685:         if (!$is_custom->{$item}) {
 1686:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1687:         }
 1688:     }
 1689:     if ($links_def) {
 1690:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1691:     } else {
 1692:         $datatable .= '<td>&nbsp;</td>';
 1693:     }
 1694:     $datatable .= '<td class="LC_right_item">'.
 1695:                   '<table border="0"><tr>';
 1696:     foreach my $item (@{$links}) {
 1697: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1698:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1699:         if ($designs->{'links'}{$item}) {
 1700:             $datatable.='&nbsp;';
 1701:         }
 1702:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1703:                       '" /></td>';
 1704:     }
 1705:     $$rowtotal += $itemcount;
 1706:     return $datatable;
 1707: }
 1708: 
 1709: sub logo_display_options {
 1710:     my ($img,$defaults,$designs) = @_;
 1711:     my $checkedon;
 1712:     if (ref($defaults) eq 'HASH') {
 1713:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1714:             if ($defaults->{'showlogo'}{$img}) {
 1715:                 $checkedon = 'checked="checked" ';     
 1716:             }
 1717:         } 
 1718:     }
 1719:     if (ref($designs) eq 'HASH') {
 1720:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1721:             if (defined($designs->{'showlogo'}{$img})) {
 1722:                 if ($designs->{'showlogo'}{$img} == 0) {
 1723:                     $checkedon = '';
 1724:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1725:                     $checkedon = 'checked="checked" ';
 1726:                 }
 1727:             }
 1728:         }
 1729:     }
 1730:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1731:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1732:            &mt('show').'</label>'."\n";
 1733: }
 1734: 
 1735: sub login_header_options  {
 1736:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1737:     my $output = '';
 1738:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1739:         $output .= &mt('Text default(s):').'<br />';
 1740:         if (!$is_custom->{'textcol'}) {
 1741:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1742:                        '&nbsp;&nbsp;&nbsp;';
 1743:         }
 1744:         if (!$is_custom->{'bgcol'}) {
 1745:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1746:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1747:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1748:         }
 1749:         $output .= '<br />';
 1750:     }
 1751:     $output .='<br />';
 1752:     return $output;
 1753: }
 1754: 
 1755: sub login_text_colors {
 1756:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1757:     my $color_menu = '<table border="0"><tr>';
 1758:     foreach my $item (@{$logintext}) {
 1759:         $color_menu .= '<td align="center">'.$choices->{$item};
 1760:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1761:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1762:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1763:     }
 1764:     $color_menu .= '</tr></table><br />';
 1765:     return $color_menu;
 1766: }
 1767: 
 1768: sub image_changes {
 1769:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1770:     my $output;
 1771:     if ($img eq 'login') {
 1772:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1773:     } elsif (!$is_custom) {
 1774:         if ($img ne 'domlogo') {
 1775:             $output = &mt('Default image:').'<br />';
 1776:         } else {
 1777:             $output = &mt('Default in use:').'<br />';
 1778:         }
 1779:     }
 1780:     if ($img ne 'login') {
 1781:         if ($img_import) {
 1782:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1783:         }
 1784:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1785:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1786:         if ($is_custom) {
 1787:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1788:                        '<input type="checkbox" name="'.
 1789:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1790:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1791:         } else {
 1792:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1793:         }
 1794:     }
 1795:     return $output;
 1796: }
 1797: 
 1798: sub print_quotas {
 1799:     my ($dom,$settings,$rowtotal,$action) = @_;
 1800:     my $context;
 1801:     if ($action eq 'quotas') {
 1802:         $context = 'tools';
 1803:     } else {
 1804:         $context = $action;
 1805:     }
 1806:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1807:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1808:     my $typecount = 0;
 1809:     my ($css_class,%titles);
 1810:     if ($context eq 'requestcourses') {
 1811:         @usertools = ('official','unofficial','community','textbook');
 1812:         @options =('norequest','approval','validate','autolimit');
 1813:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1814:         %titles = &courserequest_titles();
 1815:     } elsif ($context eq 'requestauthor') {
 1816:         @usertools = ('author');
 1817:         @options = ('norequest','approval','automatic');
 1818:         %titles = &authorrequest_titles();
 1819:     } else {
 1820:         @usertools = ('aboutme','blog','webdav','portfolio');
 1821:         %titles = &tool_titles();
 1822:     }
 1823:     if (ref($types) eq 'ARRAY') {
 1824:         foreach my $type (@{$types}) {
 1825:             my ($currdefquota,$currauthorquota);
 1826:             unless (($context eq 'requestcourses') ||
 1827:                     ($context eq 'requestauthor')) {
 1828:                 if (ref($settings) eq 'HASH') {
 1829:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1830:                         $currdefquota = $settings->{defaultquota}->{$type};
 1831:                     } else {
 1832:                         $currdefquota = $settings->{$type};
 1833:                     }
 1834:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1835:                         $currauthorquota = $settings->{authorquota}->{$type};
 1836:                     }
 1837:                 }
 1838:             }
 1839:             if (defined($usertypes->{$type})) {
 1840:                 $typecount ++;
 1841:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1842:                 $datatable .= '<tr'.$css_class.'>'.
 1843:                               '<td>'.$usertypes->{$type}.'</td>'.
 1844:                               '<td class="LC_left_item">';
 1845:                 if ($context eq 'requestcourses') {
 1846:                     $datatable .= '<table><tr>';
 1847:                 }
 1848:                 my %cell;  
 1849:                 foreach my $item (@usertools) {
 1850:                     if ($context eq 'requestcourses') {
 1851:                         my ($curroption,$currlimit);
 1852:                         if (ref($settings) eq 'HASH') {
 1853:                             if (ref($settings->{$item}) eq 'HASH') {
 1854:                                 $curroption = $settings->{$item}->{$type};
 1855:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1856:                                     $currlimit = $1; 
 1857:                                 }
 1858:                             }
 1859:                         }
 1860:                         if (!$curroption) {
 1861:                             $curroption = 'norequest';
 1862:                         }
 1863:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1864:                         foreach my $option (@options) {
 1865:                             my $val = $option;
 1866:                             if ($option eq 'norequest') {
 1867:                                 $val = 0;  
 1868:                             }
 1869:                             if ($option eq 'validate') {
 1870:                                 my $canvalidate = 0;
 1871:                                 if (ref($validations{$item}) eq 'HASH') { 
 1872:                                     if ($validations{$item}{$type}) {
 1873:                                         $canvalidate = 1;
 1874:                                     }
 1875:                                 }
 1876:                                 next if (!$canvalidate);
 1877:                             }
 1878:                             my $checked = '';
 1879:                             if ($option eq $curroption) {
 1880:                                 $checked = ' checked="checked"';
 1881:                             } elsif ($option eq 'autolimit') {
 1882:                                 if ($curroption =~ /^autolimit/) {
 1883:                                     $checked = ' checked="checked"';
 1884:                                 }                       
 1885:                             } 
 1886:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1887:                                   '<input type="radio" name="crsreq_'.$item.
 1888:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1889:                                   $titles{$option}.'</label>';
 1890:                             if ($option eq 'autolimit') {
 1891:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1892:                                                 $item.'_limit_'.$type.'" size="1" '.
 1893:                                                 'value="'.$currlimit.'" />';
 1894:                             }
 1895:                             $cell{$item} .= '</span> ';
 1896:                             if ($option eq 'autolimit') {
 1897:                                 $cell{$item} .= $titles{'unlimited'};
 1898:                             }
 1899:                         }
 1900:                     } elsif ($context eq 'requestauthor') {
 1901:                         my $curroption;
 1902:                         if (ref($settings) eq 'HASH') {
 1903:                             $curroption = $settings->{$type};
 1904:                         }
 1905:                         if (!$curroption) {
 1906:                             $curroption = 'norequest';
 1907:                         }
 1908:                         foreach my $option (@options) {
 1909:                             my $val = $option;
 1910:                             if ($option eq 'norequest') {
 1911:                                 $val = 0;
 1912:                             }
 1913:                             my $checked = '';
 1914:                             if ($option eq $curroption) {
 1915:                                 $checked = ' checked="checked"';
 1916:                             }
 1917:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1918:                                   '<input type="radio" name="authorreq_'.$type.
 1919:                                   '" value="'.$val.'"'.$checked.' />'.
 1920:                                   $titles{$option}.'</label></span>&nbsp; ';
 1921:                         }
 1922:                     } else {
 1923:                         my $checked = 'checked="checked" ';
 1924:                         if (ref($settings) eq 'HASH') {
 1925:                             if (ref($settings->{$item}) eq 'HASH') {
 1926:                                 if ($settings->{$item}->{$type} == 0) {
 1927:                                     $checked = '';
 1928:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1929:                                     $checked =  'checked="checked" ';
 1930:                                 }
 1931:                             }
 1932:                         }
 1933:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1934:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1935:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1936:                                       '</label></span>&nbsp; ';
 1937:                     }
 1938:                 }
 1939:                 if ($context eq 'requestcourses') {
 1940:                     $datatable .= '</tr><tr>';
 1941:                     foreach my $item (@usertools) {
 1942:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1943:                     }
 1944:                     $datatable .= '</tr></table>';
 1945:                 }
 1946:                 $datatable .= '</td>';
 1947:                 unless (($context eq 'requestcourses') ||
 1948:                         ($context eq 'requestauthor')) {
 1949:                     $datatable .= 
 1950:                               '<td class="LC_right_item">'.
 1951:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1952:                               '<input type="text" name="quota_'.$type.
 1953:                               '" value="'.$currdefquota.
 1954:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1955:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1956:                               '<input type="text" name="authorquota_'.$type.
 1957:                               '" value="'.$currauthorquota.
 1958:                               '" size="5" /></span></td>';
 1959:                 }
 1960:                 $datatable .= '</tr>';
 1961:             }
 1962:         }
 1963:     }
 1964:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1965:         $defaultquota = '20';
 1966:         $authorquota = '500';
 1967:         if (ref($settings) eq 'HASH') {
 1968:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1969:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1970:             } elsif (defined($settings->{'default'})) {
 1971:                 $defaultquota = $settings->{'default'};
 1972:             }
 1973:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1974:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1975:             }
 1976:         }
 1977:     }
 1978:     $typecount ++;
 1979:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1980:     $datatable .= '<tr'.$css_class.'>'.
 1981:                   '<td>'.$othertitle.'</td>'.
 1982:                   '<td class="LC_left_item">';
 1983:     if ($context eq 'requestcourses') {
 1984:         $datatable .= '<table><tr>';
 1985:     }
 1986:     my %defcell;
 1987:     foreach my $item (@usertools) {
 1988:         if ($context eq 'requestcourses') {
 1989:             my ($curroption,$currlimit);
 1990:             if (ref($settings) eq 'HASH') {
 1991:                 if (ref($settings->{$item}) eq 'HASH') {
 1992:                     $curroption = $settings->{$item}->{'default'};
 1993:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1994:                         $currlimit = $1;
 1995:                     }
 1996:                 }
 1997:             }
 1998:             if (!$curroption) {
 1999:                 $curroption = 'norequest';
 2000:             }
 2001:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2002:             foreach my $option (@options) {
 2003:                 my $val = $option;
 2004:                 if ($option eq 'norequest') {
 2005:                     $val = 0;
 2006:                 }
 2007:                 if ($option eq 'validate') {
 2008:                     my $canvalidate = 0;
 2009:                     if (ref($validations{$item}) eq 'HASH') {
 2010:                         if ($validations{$item}{'default'}) {
 2011:                             $canvalidate = 1;
 2012:                         }
 2013:                     }
 2014:                     next if (!$canvalidate);
 2015:                 }
 2016:                 my $checked = '';
 2017:                 if ($option eq $curroption) {
 2018:                     $checked = ' checked="checked"';
 2019:                 } elsif ($option eq 'autolimit') {
 2020:                     if ($curroption =~ /^autolimit/) {
 2021:                         $checked = ' checked="checked"';
 2022:                     }
 2023:                 }
 2024:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2025:                                   '<input type="radio" name="crsreq_'.$item.
 2026:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2027:                                   $titles{$option}.'</label>';
 2028:                 if ($option eq 'autolimit') {
 2029:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2030:                                        $item.'_limit_default" size="1" '.
 2031:                                        'value="'.$currlimit.'" />';
 2032:                 }
 2033:                 $defcell{$item} .= '</span> ';
 2034:                 if ($option eq 'autolimit') {
 2035:                     $defcell{$item} .= $titles{'unlimited'};
 2036:                 }
 2037:             }
 2038:         } elsif ($context eq 'requestauthor') {
 2039:             my $curroption;
 2040:             if (ref($settings) eq 'HASH') {
 2041:                 $curroption = $settings->{'default'};
 2042:             }
 2043:             if (!$curroption) {
 2044:                 $curroption = 'norequest';
 2045:             }
 2046:             foreach my $option (@options) {
 2047:                 my $val = $option;
 2048:                 if ($option eq 'norequest') {
 2049:                     $val = 0;
 2050:                 }
 2051:                 my $checked = '';
 2052:                 if ($option eq $curroption) {
 2053:                     $checked = ' checked="checked"';
 2054:                 }
 2055:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2056:                               '<input type="radio" name="authorreq_default"'.
 2057:                               ' value="'.$val.'"'.$checked.' />'.
 2058:                               $titles{$option}.'</label></span>&nbsp; ';
 2059:             }
 2060:         } else {
 2061:             my $checked = 'checked="checked" ';
 2062:             if (ref($settings) eq 'HASH') {
 2063:                 if (ref($settings->{$item}) eq 'HASH') {
 2064:                     if ($settings->{$item}->{'default'} == 0) {
 2065:                         $checked = '';
 2066:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2067:                         $checked = 'checked="checked" ';
 2068:                     }
 2069:                 }
 2070:             }
 2071:             $datatable .= '<span class="LC_nobreak"><label>'.
 2072:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2073:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2074:                           '</label></span>&nbsp; ';
 2075:         }
 2076:     }
 2077:     if ($context eq 'requestcourses') {
 2078:         $datatable .= '</tr><tr>';
 2079:         foreach my $item (@usertools) {
 2080:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2081:         }
 2082:         $datatable .= '</tr></table>';
 2083:     }
 2084:     $datatable .= '</td>';
 2085:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2086:         $datatable .= '<td class="LC_right_item">'.
 2087:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2088:                       '<input type="text" name="defaultquota" value="'.
 2089:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2090:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2091:                       '<input type="text" name="authorquota" value="'.
 2092:                       $authorquota.'" size="5" /></span></td>';
 2093:     }
 2094:     $datatable .= '</tr>';
 2095:     $typecount ++;
 2096:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2097:     $datatable .= '<tr'.$css_class.'>'.
 2098:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2099:     if ($context eq 'requestcourses') {
 2100:         $datatable .= &mt('(overrides affiliation, if set)').
 2101:                       '</td>'.
 2102:                       '<td class="LC_left_item">'.
 2103:                       '<table><tr>';
 2104:     } else {
 2105:         $datatable .= &mt('(overrides affiliation, if checked)').
 2106:                       '</td>'.
 2107:                       '<td class="LC_left_item" colspan="2">'.
 2108:                       '<br />';
 2109:     }
 2110:     my %advcell;
 2111:     foreach my $item (@usertools) {
 2112:         if ($context eq 'requestcourses') {
 2113:             my ($curroption,$currlimit);
 2114:             if (ref($settings) eq 'HASH') {
 2115:                 if (ref($settings->{$item}) eq 'HASH') {
 2116:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2117:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2118:                         $currlimit = $1;
 2119:                     }
 2120:                 }
 2121:             }
 2122:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2123:             my $checked = '';
 2124:             if ($curroption eq '') {
 2125:                 $checked = ' checked="checked"';
 2126:             }
 2127:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2128:                                '<input type="radio" name="crsreq_'.$item.
 2129:                                '__LC_adv" value=""'.$checked.' />'.
 2130:                                &mt('No override set').'</label></span>&nbsp; ';
 2131:             foreach my $option (@options) {
 2132:                 my $val = $option;
 2133:                 if ($option eq 'norequest') {
 2134:                     $val = 0;
 2135:                 }
 2136:                 if ($option eq 'validate') {
 2137:                     my $canvalidate = 0;
 2138:                     if (ref($validations{$item}) eq 'HASH') {
 2139:                         if ($validations{$item}{'_LC_adv'}) {
 2140:                             $canvalidate = 1;
 2141:                         }
 2142:                     }
 2143:                     next if (!$canvalidate);
 2144:                 }
 2145:                 my $checked = '';
 2146:                 if ($val eq $curroption) {
 2147:                     $checked = ' checked="checked"';
 2148:                 } elsif ($option eq 'autolimit') {
 2149:                     if ($curroption =~ /^autolimit/) {
 2150:                         $checked = ' checked="checked"';
 2151:                     }
 2152:                 }
 2153:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2154:                                   '<input type="radio" name="crsreq_'.$item.
 2155:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2156:                                   $titles{$option}.'</label>';
 2157:                 if ($option eq 'autolimit') {
 2158:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2159:                                        $item.'_limit__LC_adv" size="1" '.
 2160:                                        'value="'.$currlimit.'" />';
 2161:                 }
 2162:                 $advcell{$item} .= '</span> ';
 2163:                 if ($option eq 'autolimit') {
 2164:                     $advcell{$item} .= $titles{'unlimited'};
 2165:                 }
 2166:             }
 2167:         } elsif ($context eq 'requestauthor') {
 2168:             my $curroption;
 2169:             if (ref($settings) eq 'HASH') {
 2170:                 $curroption = $settings->{'_LC_adv'};
 2171:             }
 2172:             my $checked = '';
 2173:             if ($curroption eq '') {
 2174:                 $checked = ' checked="checked"';
 2175:             }
 2176:             $datatable .= '<span class="LC_nobreak"><label>'.
 2177:                           '<input type="radio" name="authorreq__LC_adv"'.
 2178:                           ' value=""'.$checked.' />'.
 2179:                           &mt('No override set').'</label></span>&nbsp; ';
 2180:             foreach my $option (@options) {
 2181:                 my $val = $option;
 2182:                 if ($option eq 'norequest') {
 2183:                     $val = 0;
 2184:                 }
 2185:                 my $checked = '';
 2186:                 if ($val eq $curroption) {
 2187:                     $checked = ' checked="checked"';
 2188:                 }
 2189:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2190:                               '<input type="radio" name="authorreq__LC_adv"'.
 2191:                               ' value="'.$val.'"'.$checked.' />'.
 2192:                               $titles{$option}.'</label></span>&nbsp; ';
 2193:             }
 2194:         } else {
 2195:             my $checked = 'checked="checked" ';
 2196:             if (ref($settings) eq 'HASH') {
 2197:                 if (ref($settings->{$item}) eq 'HASH') {
 2198:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2199:                         $checked = '';
 2200:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2201:                         $checked = 'checked="checked" ';
 2202:                     }
 2203:                 }
 2204:             }
 2205:             $datatable .= '<span class="LC_nobreak"><label>'.
 2206:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2207:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2208:                           '</label></span>&nbsp; ';
 2209:         }
 2210:     }
 2211:     if ($context eq 'requestcourses') {
 2212:         $datatable .= '</tr><tr>';
 2213:         foreach my $item (@usertools) {
 2214:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2215:         }
 2216:         $datatable .= '</tr></table>';
 2217:     }
 2218:     $datatable .= '</td></tr>';
 2219:     $$rowtotal += $typecount;
 2220:     return $datatable;
 2221: }
 2222: 
 2223: sub print_requestmail {
 2224:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2225:     my ($now,$datatable,%currapp);
 2226:     $now = time;
 2227:     if (ref($settings) eq 'HASH') {
 2228:         if (ref($settings->{'notify'}) eq 'HASH') {
 2229:             if ($settings->{'notify'}{'approval'} ne '') {
 2230:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2231:             }
 2232:         }
 2233:     }
 2234:     my $numinrow = 2;
 2235:     my $css_class;
 2236:     if ($$rowtotal%2) {
 2237:         $css_class = 'LC_odd_row';
 2238:     }
 2239:     if ($customcss) {
 2240:         $css_class .= " $customcss";
 2241:     }
 2242:     $css_class =~ s/^\s+//;
 2243:     if ($css_class) {
 2244:         $css_class = ' class="'.$css_class.'"';
 2245:     }
 2246:     if ($rowstyle) {
 2247:         $css_class .= ' style="'.$rowstyle.'"';
 2248:     }
 2249:     my $text;
 2250:     if ($action eq 'requestcourses') {
 2251:         $text = &mt('Receive notification of course requests requiring approval');
 2252:     } elsif ($action eq 'requestauthor') {
 2253:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2254:     } else {
 2255:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2256:     }
 2257:     $datatable = '<tr'.$css_class.'>'.
 2258:                  ' <td>'.$text.'</td>'.
 2259:                  ' <td class="LC_left_item">';
 2260:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2261:                                                  $action.'notifyapproval',%currapp);
 2262:     if ($numdc > 0) {
 2263:         $datatable .= $table;
 2264:     } else {
 2265:         $datatable .= &mt('There are no active Domain Coordinators');
 2266:     }
 2267:     $datatable .='</td></tr>';
 2268:     return $datatable;
 2269: }
 2270: 
 2271: sub print_studentcode {
 2272:     my ($settings,$rowtotal) = @_;
 2273:     my $rownum = 0; 
 2274:     my ($output,%current);
 2275:     my @crstypes = ('official','unofficial','community','textbook');
 2276:     if (ref($settings) eq 'HASH') {
 2277:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2278:             foreach my $type (@crstypes) {
 2279:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2280:             }
 2281:         }
 2282:     }
 2283:     $output .= '<tr>'.
 2284:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2285:                '<td class="LC_left_item">';
 2286:     foreach my $type (@crstypes) {
 2287:         my $check = ' ';
 2288:         if ($current{$type}) {
 2289:             $check = ' checked="checked" ';
 2290:         }
 2291:         $output .= '<span class="LC_nobreak"><label>'.
 2292:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2293:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2294:     }
 2295:     $output .= '</td></tr>';
 2296:     $$rowtotal ++;
 2297:     return $output;
 2298: }
 2299: 
 2300: sub print_textbookcourses {
 2301:     my ($dom,$type,$settings,$rowtotal) = @_;
 2302:     my $rownum = 0;
 2303:     my $css_class;
 2304:     my $itemcount = 1;
 2305:     my $maxnum = 0;
 2306:     my $bookshash;
 2307:     if (ref($settings) eq 'HASH') {
 2308:         $bookshash = $settings->{$type};
 2309:     }
 2310:     my %ordered;
 2311:     if (ref($bookshash) eq 'HASH') {
 2312:         foreach my $item (keys(%{$bookshash})) {
 2313:             if (ref($bookshash->{$item}) eq 'HASH') {
 2314:                 my $num = $bookshash->{$item}{'order'};
 2315:                 $ordered{$num} = $item;
 2316:             }
 2317:         }
 2318:     }
 2319:     my $confname = $dom.'-domainconfig';
 2320:     my $switchserver = &check_switchserver($dom,$confname);
 2321:     my $maxnum = scalar(keys(%ordered));
 2322:     my $datatable;
 2323:     if (keys(%ordered)) {
 2324:         my @items = sort { $a <=> $b } keys(%ordered);
 2325:         for (my $i=0; $i<@items; $i++) {
 2326:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2327:             my $key = $ordered{$items[$i]};
 2328:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2329:             my $coursetitle = $coursehash{'description'};
 2330:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2331:             if (ref($bookshash->{$key}) eq 'HASH') {
 2332:                 $subject = $bookshash->{$key}->{'subject'};
 2333:                 $title = $bookshash->{$key}->{'title'};
 2334:                 if ($type eq 'textbooks') {
 2335:                     $publisher = $bookshash->{$key}->{'publisher'};
 2336:                     $author = $bookshash->{$key}->{'author'};
 2337:                     $image = $bookshash->{$key}->{'image'};
 2338:                     if ($image ne '') {
 2339:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2340:                         my $imagethumb = "$path/tn-".$imagefile;
 2341:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2342:                     }
 2343:                 }
 2344:             }
 2345:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2346:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2347:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2348:             for (my $k=0; $k<=$maxnum; $k++) {
 2349:                 my $vpos = $k+1;
 2350:                 my $selstr;
 2351:                 if ($k == $i) {
 2352:                     $selstr = ' selected="selected" ';
 2353:                 }
 2354:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2355:             }
 2356:             $datatable .= '</select>'.('&nbsp;'x2).
 2357:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2358:                 &mt('Delete?').'</label></span></td>'.
 2359:                 '<td colspan="2">'.
 2360:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2361:                 ('&nbsp;'x2).
 2362:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2363:             if ($type eq 'textbooks') {
 2364:                 $datatable .= ('&nbsp;'x2).
 2365:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2366:                               ('&nbsp;'x2).
 2367:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2368:                               ('&nbsp;'x2).
 2369:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2370:                 if ($image) {
 2371:                     $datatable .= '<span class="LC_nobreak">'.
 2372:                                   $imgsrc.
 2373:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2374:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2375:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2376:                 }
 2377:                 if ($switchserver) {
 2378:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2379:                 } else {
 2380:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2381:                 }
 2382:             }
 2383:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2384:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2385:                           $coursetitle.'</span></td></tr>'."\n";
 2386:             $itemcount ++;
 2387:         }
 2388:     }
 2389:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2390:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2391:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2392:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2393:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2394:     for (my $k=0; $k<$maxnum+1; $k++) {
 2395:         my $vpos = $k+1;
 2396:         my $selstr;
 2397:         if ($k == $maxnum) {
 2398:             $selstr = ' selected="selected" ';
 2399:         }
 2400:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2401:     }
 2402:     $datatable .= '</select>&nbsp;'."\n".
 2403:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2404:                   '<td colspan="2">'.
 2405:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2406:                   ('&nbsp;'x2).
 2407:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2408:                   ('&nbsp;'x2);
 2409:     if ($type eq 'textbooks') {
 2410:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2411:                       ('&nbsp;'x2).
 2412:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2413:                       ('&nbsp;'x2).
 2414:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2415:         if ($switchserver) {
 2416:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2417:         } else {
 2418:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2419:         }
 2420:         $datatable .= '</span>'."\n";
 2421:     }
 2422:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2423:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2424:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2425:                   &Apache::loncommon::selectcourse_link
 2426:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2427:                   '</span></td>'."\n".
 2428:                   '</tr>'."\n";
 2429:     $itemcount ++;
 2430:     return $datatable;
 2431: }
 2432: 
 2433: sub textbookcourses_javascript {
 2434:     my ($settings) = @_;
 2435:     return unless(ref($settings) eq 'HASH');
 2436:     my (%ordered,%total,%jstext);
 2437:     foreach my $type ('textbooks','templates') {
 2438:         $total{$type} = 0;
 2439:         if (ref($settings->{$type}) eq 'HASH') {
 2440:             foreach my $item (keys(%{$settings->{$type}})) {
 2441:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2442:                     my $num = $settings->{$type}->{$item}{'order'};
 2443:                     $ordered{$type}{$num} = $item;
 2444:                 }
 2445:             }
 2446:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2447:         }
 2448:         my @jsarray = ();
 2449:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2450:             push(@jsarray,$ordered{$type}{$item});
 2451:         }
 2452:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2453:     }
 2454:     return <<"ENDSCRIPT";
 2455: <script type="text/javascript">
 2456: // <![CDATA[
 2457: function reorderBooks(form,item,caller) {
 2458:     var changedVal;
 2459: $jstext{'textbooks'};
 2460: $jstext{'templates'};
 2461:     var newpos;
 2462:     var maxh;
 2463:     if (caller == 'textbooks') {  
 2464:         newpos = 'textbooks_addbook_pos';
 2465:         maxh = 1 + $total{'textbooks'};
 2466:     } else {
 2467:         newpos = 'templates_addbook_pos';
 2468:         maxh = 1 + $total{'templates'};
 2469:     }
 2470:     var current = new Array;
 2471:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2472:     if (item == newpos) {
 2473:         changedVal = newitemVal;
 2474:     } else {
 2475:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2476:         current[newitemVal] = newpos;
 2477:     }
 2478:     if (caller == 'textbooks') {
 2479:         for (var i=0; i<textbooks.length; i++) {
 2480:             var elementName = 'textbooks_'+textbooks[i];
 2481:             if (elementName != item) {
 2482:                 if (form.elements[elementName]) {
 2483:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2484:                     current[currVal] = elementName;
 2485:                 }
 2486:             }
 2487:         }
 2488:     }
 2489:     if (caller == 'templates') {
 2490:         for (var i=0; i<templates.length; i++) {
 2491:             var elementName = 'templates_'+templates[i];
 2492:             if (elementName != item) {
 2493:                 if (form.elements[elementName]) {
 2494:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2495:                     current[currVal] = elementName;
 2496:                 }
 2497:             }
 2498:         }
 2499:     }
 2500:     var oldVal;
 2501:     for (var j=0; j<maxh; j++) {
 2502:         if (current[j] == undefined) {
 2503:             oldVal = j;
 2504:         }
 2505:     }
 2506:     if (oldVal < changedVal) {
 2507:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2508:            var elementName = current[k];
 2509:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2510:         }
 2511:     } else {
 2512:         for (var k=changedVal; k<oldVal; k++) {
 2513:             var elementName = current[k];
 2514:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2515:         }
 2516:     }
 2517:     return;
 2518: }
 2519: 
 2520: // ]]>
 2521: </script>
 2522: 
 2523: ENDSCRIPT
 2524: }
 2525: 
 2526: sub ltitools_javascript {
 2527:     my ($settings) = @_;
 2528:     return unless(ref($settings) eq 'HASH');
 2529:     my (%ordered,$total,%jstext);
 2530:     $total = 0;
 2531:     foreach my $item (keys(%{$settings})) {
 2532:         if (ref($settings->{$item}) eq 'HASH') {
 2533:             my $num = $settings->{$item}{'order'};
 2534:             $ordered{$num} = $item;
 2535:         }
 2536:     }
 2537:     $total = scalar(keys(%{$settings}));
 2538:     my @jsarray = ();
 2539:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2540:         push(@jsarray,$ordered{$item});
 2541:     }
 2542:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2543:     return <<"ENDSCRIPT";
 2544: <script type="text/javascript">
 2545: // <![CDATA[
 2546: function reorderLTI(form,item) {
 2547:     var changedVal;
 2548: $jstext
 2549:     var newpos = 'ltitools_add_pos';
 2550:     var maxh = 1 + $total;
 2551:     var current = new Array;
 2552:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2553:     if (item == newpos) {
 2554:         changedVal = newitemVal;
 2555:     } else {
 2556:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2557:         current[newitemVal] = newpos;
 2558:     }
 2559:     for (var i=0; i<ltitools.length; i++) {
 2560:         var elementName = 'ltitools_'+ltitools[i];
 2561:         if (elementName != item) {
 2562:             if (form.elements[elementName]) {
 2563:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2564:                 current[currVal] = elementName;
 2565:             }
 2566:         }
 2567:     }
 2568:     var oldVal;
 2569:     for (var j=0; j<maxh; j++) {
 2570:         if (current[j] == undefined) {
 2571:             oldVal = j;
 2572:         }
 2573:     }
 2574:     if (oldVal < changedVal) {
 2575:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2576:            var elementName = current[k];
 2577:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2578:         }
 2579:     } else {
 2580:         for (var k=changedVal; k<oldVal; k++) {
 2581:             var elementName = current[k];
 2582:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2583:         }
 2584:     }
 2585:     return;
 2586: }
 2587: 
 2588: // ]]>
 2589: </script>
 2590: 
 2591: ENDSCRIPT
 2592: }
 2593: 
 2594: sub print_autoenroll {
 2595:     my ($dom,$settings,$rowtotal) = @_;
 2596:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2597:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2598:     if (ref($settings) eq 'HASH') {
 2599:         if (exists($settings->{'run'})) {
 2600:             if ($settings->{'run'} eq '0') {
 2601:                 $runoff = ' checked="checked" ';
 2602:                 $runon = ' ';
 2603:             } else {
 2604:                 $runon = ' checked="checked" ';
 2605:                 $runoff = ' ';
 2606:             }
 2607:         } else {
 2608:             if ($autorun) {
 2609:                 $runon = ' checked="checked" ';
 2610:                 $runoff = ' ';
 2611:             } else {
 2612:                 $runoff = ' checked="checked" ';
 2613:                 $runon = ' ';
 2614:             }
 2615:         }
 2616:         if (exists($settings->{'co-owners'})) {
 2617:             if ($settings->{'co-owners'} eq '0') {
 2618:                 $coownersoff = ' checked="checked" ';
 2619:                 $coownerson = ' ';
 2620:             } else {
 2621:                 $coownerson = ' checked="checked" ';
 2622:                 $coownersoff = ' ';
 2623:             }
 2624:         } else {
 2625:             $coownersoff = ' checked="checked" ';
 2626:             $coownerson = ' ';
 2627:         }
 2628:         if (exists($settings->{'sender_domain'})) {
 2629:             $defdom = $settings->{'sender_domain'};
 2630:         }
 2631:         if (exists($settings->{'autofailsafe'})) {
 2632:             $failsafe = $settings->{'autofailsafe'};
 2633:         }
 2634:     } else {
 2635:         if ($autorun) {
 2636:             $runon = ' checked="checked" ';
 2637:             $runoff = ' ';
 2638:         } else {
 2639:             $runoff = ' checked="checked" ';
 2640:             $runon = ' ';
 2641:         }
 2642:     }
 2643:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2644:     my $notif_sender;
 2645:     if (ref($settings) eq 'HASH') {
 2646:         $notif_sender = $settings->{'sender_uname'};
 2647:     }
 2648:     my $datatable='<tr class="LC_odd_row">'.
 2649:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2650:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2651:                   '<input type="radio" name="autoenroll_run"'.
 2652:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2653:                   '<label><input type="radio" name="autoenroll_run"'.
 2654:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2655:                   '</tr><tr>'.
 2656:                   '<td>'.&mt('Notification messages - sender').
 2657:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2658:                   &mt('username').':&nbsp;'.
 2659:                   '<input type="text" name="sender_uname" value="'.
 2660:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2661:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2662:                   '<tr class="LC_odd_row">'.
 2663:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2664:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2665:                   '<input type="radio" name="autoassign_coowners"'.
 2666:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2667:                   '<label><input type="radio" name="autoassign_coowners"'.
 2668:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2669:                   '</tr><tr>'.
 2670:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2671:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2672:                   '<input type="text" name="autoenroll_failsafe"'.
 2673:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 2674:     $$rowtotal += 4;
 2675:     return $datatable;
 2676: }
 2677: 
 2678: sub print_autoupdate {
 2679:     my ($position,$dom,$settings,$rowtotal) = @_;
 2680:     my $datatable;
 2681:     if ($position eq 'top') {
 2682:         my $updateon = ' ';
 2683:         my $updateoff = ' checked="checked" ';
 2684:         my $classlistson = ' ';
 2685:         my $classlistsoff = ' checked="checked" ';
 2686:         if (ref($settings) eq 'HASH') {
 2687:             if ($settings->{'run'} eq '1') {
 2688:                 $updateon = $updateoff;
 2689:                 $updateoff = ' ';
 2690:             }
 2691:             if ($settings->{'classlists'} eq '1') {
 2692:                 $classlistson = $classlistsoff;
 2693:                 $classlistsoff = ' ';
 2694:             }
 2695:         }
 2696:         my %title = (
 2697:                    run => 'Auto-update active?',
 2698:                    classlists => 'Update information in classlists?',
 2699:                     );
 2700:         $datatable = '<tr class="LC_odd_row">'. 
 2701:                   '<td>'.&mt($title{'run'}).'</td>'.
 2702:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2703:                   '<input type="radio" name="autoupdate_run"'.
 2704:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2705:                   '<label><input type="radio" name="autoupdate_run"'.
 2706:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2707:                   '</tr><tr>'.
 2708:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2709:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2710:                   '<label><input type="radio" name="classlists"'.
 2711:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2712:                   '<label><input type="radio" name="classlists"'.
 2713:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2714:                   '</tr>';
 2715:         $$rowtotal += 2;
 2716:     } elsif ($position eq 'middle') {
 2717:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2718:         my $numinrow = 3;
 2719:         my $locknamesettings;
 2720:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2721:                                      $dom,$numinrow,$othertitle,
 2722:                                     'lockablenames',$rowtotal);
 2723:         $$rowtotal ++;
 2724:     } else {
 2725:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2726:         my @fields = ('lastname','firstname','middlename','generation',
 2727:                       'permanentemail','id');
 2728:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2729:         my $numrows = 0;
 2730:         if (ref($types) eq 'ARRAY') {
 2731:             if (@{$types} > 0) {
 2732:                 $datatable = 
 2733:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2734:                                          \@fields,$types,\$numrows);
 2735:                     $$rowtotal += @{$types}; 
 2736:             }
 2737:         }
 2738:         $datatable .= 
 2739:             &usertype_update_row($settings,{'default' => $othertitle},
 2740:                                  \%fieldtitles,\@fields,['default'],
 2741:                                  \$numrows);
 2742:         $$rowtotal ++;     
 2743:     }
 2744:     return $datatable;
 2745: }
 2746: 
 2747: sub print_autocreate {
 2748:     my ($dom,$settings,$rowtotal) = @_;
 2749:     my (%createon,%createoff,%currhash);
 2750:     my @types = ('xml','req');
 2751:     if (ref($settings) eq 'HASH') {
 2752:         foreach my $item (@types) {
 2753:             $createoff{$item} = ' checked="checked" ';
 2754:             $createon{$item} = ' ';
 2755:             if (exists($settings->{$item})) {
 2756:                 if ($settings->{$item}) {
 2757:                     $createon{$item} = ' checked="checked" ';
 2758:                     $createoff{$item} = ' ';
 2759:                 }
 2760:             }
 2761:         }
 2762:         if ($settings->{'xmldc'} ne '') {
 2763:             $currhash{$settings->{'xmldc'}} = 1;
 2764:         }
 2765:     } else {
 2766:         foreach my $item (@types) {
 2767:             $createoff{$item} = ' checked="checked" ';
 2768:             $createon{$item} = ' ';
 2769:         }
 2770:     }
 2771:     $$rowtotal += 2;
 2772:     my $numinrow = 2;
 2773:     my $datatable='<tr class="LC_odd_row">'.
 2774:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2775:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2776:                   '<input type="radio" name="autocreate_xml"'.
 2777:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2778:                   '<label><input type="radio" name="autocreate_xml"'.
 2779:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2780:                   '</td></tr><tr>'.
 2781:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2782:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2783:                   '<input type="radio" name="autocreate_req"'.
 2784:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2785:                   '<label><input type="radio" name="autocreate_req"'.
 2786:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2787:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2788:                                                    'autocreate_xmldc',%currhash);
 2789:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2790:     if ($numdc > 1) {
 2791:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2792:                       '</td><td class="LC_left_item">';
 2793:     } else {
 2794:         $datatable .= &mt('Course creation processed as:').
 2795:                       '</td><td class="LC_right_item">';
 2796:     }
 2797:     $datatable .= $dctable.'</td></tr>';
 2798:     $$rowtotal += $rows;
 2799:     return $datatable;
 2800: }
 2801: 
 2802: sub print_directorysrch {
 2803:     my ($position,$dom,$settings,$rowtotal) = @_;
 2804:     my $datatable;
 2805:     if ($position eq 'top') {
 2806:         my $instsrchon = ' ';
 2807:         my $instsrchoff = ' checked="checked" ';
 2808:         my ($exacton,$containson,$beginson);
 2809:         my $instlocalon = ' ';
 2810:         my $instlocaloff = ' checked="checked" ';
 2811:         if (ref($settings) eq 'HASH') {
 2812:             if ($settings->{'available'} eq '1') {
 2813:                 $instsrchon = $instsrchoff;
 2814:                 $instsrchoff = ' ';
 2815:             }
 2816:             if ($settings->{'localonly'} eq '1') {
 2817:                 $instlocalon = $instlocaloff;
 2818:                 $instlocaloff = ' ';
 2819:             }
 2820:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2821:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2822:                     if ($type eq 'exact') {
 2823:                         $exacton = ' checked="checked" ';
 2824:                     } elsif ($type eq 'contains') {
 2825:                         $containson = ' checked="checked" ';
 2826:                     } elsif ($type eq 'begins') {
 2827:                         $beginson = ' checked="checked" ';
 2828:                     }
 2829:                 }
 2830:             } else {
 2831:                 if ($settings->{'searchtypes'} eq 'exact') {
 2832:                     $exacton = ' checked="checked" ';
 2833:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2834:                     $containson = ' checked="checked" ';
 2835:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2836:                     $exacton = ' checked="checked" ';
 2837:                     $containson = ' checked="checked" ';
 2838:                 }
 2839:             }
 2840:         }
 2841:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2842:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2843: 
 2844:         my $numinrow = 4;
 2845:         my $cansrchrow = 0;
 2846:         $datatable='<tr class="LC_odd_row">'.
 2847:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2848:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2849:                    '<input type="radio" name="dirsrch_available"'.
 2850:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2851:                    '<label><input type="radio" name="dirsrch_available"'.
 2852:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2853:                    '</tr><tr>'.
 2854:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2855:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2856:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2857:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2858:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2859:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2860:                    '</tr>';
 2861:         $$rowtotal += 2;
 2862:         if (ref($usertypes) eq 'HASH') {
 2863:             if (keys(%{$usertypes}) > 0) {
 2864:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2865:                                              $numinrow,$othertitle,'cansearch',
 2866:                                              $rowtotal);
 2867:                 $cansrchrow = 1;
 2868:             }
 2869:         }
 2870:         if ($cansrchrow) {
 2871:             $$rowtotal ++;
 2872:             $datatable .= '<tr>';
 2873:         } else {
 2874:             $datatable .= '<tr class="LC_odd_row">';
 2875:         }
 2876:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2877:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2878:         foreach my $title (@{$titleorder}) {
 2879:             if (defined($searchtitles->{$title})) {
 2880:                 my $check = ' ';
 2881:                 if (ref($settings) eq 'HASH') {
 2882:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2883:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2884:                             $check = ' checked="checked" ';
 2885:                         }
 2886:                     }
 2887:                 }
 2888:                 $datatable .= '<td class="LC_left_item">'.
 2889:                               '<span class="LC_nobreak"><label>'.
 2890:                               '<input type="checkbox" name="searchby" '.
 2891:                               'value="'.$title.'"'.$check.'/>'.
 2892:                               $searchtitles->{$title}.'</label></span></td>';
 2893:             }
 2894:         }
 2895:         $datatable .= '</tr></table></td></tr>';
 2896:         $$rowtotal ++;
 2897:         if ($cansrchrow) {
 2898:             $datatable .= '<tr class="LC_odd_row">';
 2899:         } else {
 2900:             $datatable .= '<tr>';
 2901:         }
 2902:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2903:                       '<td class="LC_left_item" colspan="2">'.
 2904:                       '<span class="LC_nobreak"><label>'.
 2905:                       '<input type="checkbox" name="searchtypes" '.
 2906:                       $exacton.' value="exact" />'.&mt('Exact match').
 2907:                       '</label>&nbsp;'.
 2908:                       '<label><input type="checkbox" name="searchtypes" '.
 2909:                       $beginson.' value="begins" />'.&mt('Begins with').
 2910:                       '</label>&nbsp;'.
 2911:                       '<label><input type="checkbox" name="searchtypes" '.
 2912:                       $containson.' value="contains" />'.&mt('Contains').
 2913:                       '</label></span></td></tr>';
 2914:         $$rowtotal ++;
 2915:     } else {
 2916:         my $domsrchon = ' checked="checked" ';
 2917:         my $domsrchoff = ' ';
 2918:         my $domlocalon = ' ';
 2919:         my $domlocaloff = ' checked="checked" ';
 2920:         if (ref($settings) eq 'HASH') {
 2921:             if ($settings->{'lclocalonly'} eq '1') {
 2922:                 $domlocalon = $domlocaloff;
 2923:                 $domlocaloff = ' ';
 2924:             }
 2925:             if ($settings->{'lcavailable'} eq '0') {
 2926:                 $domsrchoff = $domsrchon;
 2927:                 $domsrchon = ' ';
 2928:             }
 2929:         }
 2930:         $datatable='<tr class="LC_odd_row">'.
 2931:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2932:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2933:                       '<input type="radio" name="dirsrch_domavailable"'.
 2934:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2935:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2936:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2937:                       '</tr><tr>'.
 2938:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2939:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2940:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2941:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2942:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2943:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2944:                       '</tr>';
 2945:         $$rowtotal += 2;
 2946:     }
 2947:     return $datatable;
 2948: }
 2949: 
 2950: sub print_contacts {
 2951:     my ($position,$dom,$settings,$rowtotal) = @_;
 2952:     my $datatable;
 2953:     my @contacts = ('adminemail','supportemail');
 2954:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2955:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2956:     if ($position eq 'top') {
 2957:         if (ref($settings) eq 'HASH') {
 2958:             foreach my $item (@contacts) {
 2959:                 if (exists($settings->{$item})) {
 2960:                     $to{$item} = $settings->{$item};
 2961:                 }
 2962:             }
 2963:         }
 2964:     } elsif ($position eq 'middle') {
 2965:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2966:                      'updatesmail','idconflictsmail','hostipmail');
 2967:         foreach my $type (@mailings) {
 2968:             $otheremails{$type} = '';
 2969:         }
 2970:     } else {
 2971:         @mailings = ('helpdeskmail','otherdomsmail');
 2972:         foreach my $type (@mailings) {
 2973:             $otheremails{$type} = '';
 2974:         }
 2975:         $bccemails{'helpdeskmail'} = '';
 2976:         $bccemails{'otherdomsmail'} = '';
 2977:         $includestr{'helpdeskmail'} = '';
 2978:         $includestr{'otherdomsmail'} = '';
 2979:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2980:     }
 2981:     if (ref($settings) eq 'HASH') {
 2982:         unless ($position eq 'top') {
 2983:             foreach my $type (@mailings) {
 2984:                 if (exists($settings->{$type})) {
 2985:                     if (ref($settings->{$type}) eq 'HASH') {
 2986:                         foreach my $item (@contacts) {
 2987:                             if ($settings->{$type}{$item}) {
 2988:                                 $checked{$type}{$item} = ' checked="checked" ';
 2989:                             }
 2990:                         }
 2991:                         $otheremails{$type} = $settings->{$type}{'others'};
 2992:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2993:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2994:                             if ($settings->{$type}{'include'} ne '') {
 2995:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2996:                                 $includestr{$type} = &unescape($includestr{$type});
 2997:                             }
 2998:                         }
 2999:                     }
 3000:                 } elsif ($type eq 'lonstatusmail') {
 3001:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3002:                 }
 3003:             }
 3004:         }
 3005:         if ($position eq 'bottom') {
 3006:             foreach my $type (@mailings) {
 3007:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3008:                 if ($settings->{$type}{'include'} ne '') {
 3009:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3010:                     $includestr{$type} = &unescape($includestr{$type});
 3011:                 }
 3012:             }
 3013:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3014:                 if (ref($fields) eq 'ARRAY') {
 3015:                     foreach my $field (@{$fields}) {
 3016:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3017:                     }
 3018:                 }
 3019:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3020:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3021:                 } else {
 3022:                     $maxsize = '1.0';
 3023:                 }
 3024:             } else {
 3025:                 if (ref($fields) eq 'ARRAY') {
 3026:                     foreach my $field (@{$fields}) {
 3027:                         $currfield{$field} = 'yes';
 3028:                     }
 3029:                 }
 3030:                 $maxsize = '1.0';
 3031:             }
 3032:         }
 3033:     } else {
 3034:         if ($position eq 'top') {
 3035:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3036:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3037:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3038:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3039:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3040:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3041:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3042:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3043:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3044:         } elsif ($position eq 'bottom') {
 3045:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3046:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3047:             if (ref($fields) eq 'ARRAY') {
 3048:                 foreach my $field (@{$fields}) {
 3049:                     $currfield{$field} = 'yes';
 3050:                 }
 3051:             }
 3052:             $maxsize = '1.0';
 3053:         }
 3054:     }
 3055:     my ($titles,$short_titles) = &contact_titles();
 3056:     my $rownum = 0;
 3057:     my $css_class;
 3058:     if ($position eq 'top') {
 3059:         foreach my $item (@contacts) {
 3060:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3061:             $datatable .= '<tr'.$css_class.'>'. 
 3062:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3063:                           '</span></td><td class="LC_right_item">'.
 3064:                           '<input type="text" name="'.$item.'" value="'.
 3065:                           $to{$item}.'" /></td></tr>';
 3066:             $rownum ++;
 3067:         }
 3068:     } elsif ($position eq 'bottom') {
 3069:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3070:         $datatable .= '<tr'.$css_class.'>'.
 3071:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3072:                       &mt('(e-mail, subject, and description always shown)').
 3073:                       '</td><td class="LC_left_item">';
 3074:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3075:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3076:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3077:             foreach my $field (@{$fields}) {
 3078:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3079:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3080:                     $datatable .= ' '.&mt('(logged-in users)');
 3081:                 }
 3082:                 $datatable .='</td><td>';
 3083:                 my $clickaction;
 3084:                 if ($field eq 'screenshot') {
 3085:                     $clickaction = ' onclick="screenshotSize(this);"';
 3086:                 }
 3087:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3088:                     foreach my $option (@{$possoptions->{$field}}) {
 3089:                         my $checked;
 3090:                         if ($currfield{$field} eq $option) {
 3091:                             $checked = ' checked="checked"';
 3092:                         }
 3093:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3094:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3095:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3096:                                       '</label></span>'.('&nbsp;'x2);
 3097:                     }
 3098:                 }
 3099:                 if ($field eq 'screenshot') {
 3100:                     my $display;
 3101:                     if ($currfield{$field} eq 'no') {
 3102:                         $display = ' style="display:none"';
 3103:                     }
 3104:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3105:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3106:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3107:                 }
 3108:                 $datatable .= '</td></tr>';
 3109:             }
 3110:             $datatable .= '</table>';
 3111:         }
 3112:         $datatable .= '</td></tr>'."\n";
 3113:         $rownum ++;
 3114:     }
 3115:     unless ($position eq 'top') {
 3116:         foreach my $type (@mailings) {
 3117:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3118:             $datatable .= '<tr'.$css_class.'>'.
 3119:                           '<td><span class="LC_nobreak">'.
 3120:                           $titles->{$type}.': </span></td>'.
 3121:                           '<td class="LC_left_item">';
 3122:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3123:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3124:             }
 3125:             $datatable .= '<span class="LC_nobreak">';
 3126:             foreach my $item (@contacts) {
 3127:                 $datatable .= '<label>'.
 3128:                               '<input type="checkbox" name="'.$type.'"'.
 3129:                               $checked{$type}{$item}.
 3130:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3131:                               '</label>&nbsp;';
 3132:             }
 3133:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3134:                           '<input type="text" name="'.$type.'_others" '.
 3135:                           'value="'.$otheremails{$type}.'"  />';
 3136:             my %locchecked;
 3137:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3138:                 foreach my $loc ('s','b') {
 3139:                     if ($includeloc{$type} eq $loc) {
 3140:                         $locchecked{$loc} = ' checked="checked"';
 3141:                         last;
 3142:                     }
 3143:                 }
 3144:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3145:                               '<input type="text" name="'.$type.'_bcc" '.
 3146:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3147:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3148:                               &mt('Text automatically added to e-mail:').' '.
 3149:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3150:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3151:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3152:                               ('&nbsp;'x2).
 3153:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3154:                               '</span></fieldset>';
 3155:             }
 3156:             $datatable .= '</td></tr>'."\n";
 3157:             $rownum ++;
 3158:         }
 3159:     }
 3160:     if ($position eq 'middle') {
 3161:         my %choices;
 3162:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3163:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3164:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3165:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3166:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3167:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3168:         my @toggles = ('reporterrors','reportupdates');
 3169:         my %defaultchecked = ('reporterrors'  => 'on',
 3170:                               'reportupdates' => 'on');
 3171:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3172:                                                    \%choices,$rownum);
 3173:         $datatable .= $reports;
 3174:     } elsif ($position eq 'bottom') {
 3175:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3176:         my (@posstypes,%usertypeshash);
 3177:         if (ref($types) eq 'ARRAY') {
 3178:             @posstypes = @{$types};
 3179:         }
 3180:         if (@posstypes) {
 3181:             if (ref($usertypes) eq 'HASH') {
 3182:                 %usertypeshash = %{$usertypes};
 3183:             }
 3184:             my @overridden;
 3185:             my $numinrow = 4;
 3186:             if (ref($settings) eq 'HASH') {
 3187:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3188:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3189:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3190:                             push(@overridden,$key);
 3191:                             foreach my $item (@contacts) {
 3192:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3193:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3194:                                 }
 3195:                             }
 3196:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3197:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3198:                             $includeloc{'override_'.$key} = '';
 3199:                             $includestr{'override_'.$key} = '';
 3200:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3201:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 3202:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3203:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3204:                             }
 3205:                         }
 3206:                     }
 3207:                 }
 3208:             }
 3209:             my $customclass = 'LC_helpdesk_override';
 3210:             my $optionsprefix = 'LC_options_helpdesk_';
 3211: 
 3212:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3213: 
 3214:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3215:                                          $numinrow,$othertitle,'overrides',
 3216:                                          \$rownum,$onclicktypes,$customclass);
 3217:             $rownum ++;
 3218:             $usertypeshash{'default'} = $othertitle;
 3219:             foreach my $status (@posstypes) {
 3220:                 my $css_class;
 3221:                 if ($rownum%2) {
 3222:                     $css_class = 'LC_odd_row ';
 3223:                 }
 3224:                 $css_class .= $customclass;
 3225:                 my $rowid = $optionsprefix.$status;
 3226:                 my $hidden = 1;
 3227:                 my $currstyle = 'display:none';
 3228:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3229:                     $currstyle = 'display:table-row';
 3230:                     $hidden = 0;
 3231:                 }
 3232:                 my $key = 'override_'.$status;
 3233:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3234:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3235:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3236:                                                   \@contacts,$short_titles);
 3237:                 unless ($hidden) {
 3238:                     $rownum ++;
 3239:                 }
 3240:             }
 3241:         }
 3242:     }
 3243:     $$rowtotal += $rownum;
 3244:     return $datatable;
 3245: }
 3246: 
 3247: sub overridden_helpdesk {
 3248:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3249:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3250:     my $class = 'LC_left_item';
 3251:     if ($css_class) {
 3252:         $css_class = ' class="'.$css_class.'"';
 3253:     }
 3254:     if ($rowid) {
 3255:         $rowid = ' id="'.$rowid.'"';
 3256:     }
 3257:     if ($rowstyle) {
 3258:         $rowstyle = ' style="'.$rowstyle.'"';
 3259:     }
 3260:     my ($output,$description);
 3261:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3262:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3263:               "<td>$description</td>\n".
 3264:               '<td class="'.$class.'" colspan="2">'.
 3265:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3266:               '<span class="LC_nobreak">';
 3267:     if (ref($contacts) eq 'ARRAY') {
 3268:         foreach my $item (@{$contacts}) {
 3269:             my $check;
 3270:             if (ref($checked) eq 'HASH') {
 3271:                $check = $checked->{$item};
 3272:             }
 3273:             my $title;
 3274:             if (ref($short_titles) eq 'HASH') {
 3275:                 $title = $short_titles->{$item};
 3276:             }
 3277:             $output .= '<label>'.
 3278:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3279:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3280:         }
 3281:     }
 3282:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3283:                '<input type="text" name="override_'.$type.'_others" '.
 3284:                'value="'.$otheremails.'"  />';
 3285:     my %locchecked;
 3286:     foreach my $loc ('s','b') {
 3287:         if ($includeloc eq $loc) {
 3288:             $locchecked{$loc} = ' checked="checked"';
 3289:             last;
 3290:         }
 3291:     }
 3292:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3293:                '<input type="text" name="override_'.$type.'_bcc" '.
 3294:                'value="'.$bccemails.'"  /></fieldset>'.
 3295:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3296:                &mt('Text automatically added to e-mail:').' '.
 3297:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3298:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3299:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3300:                ('&nbsp;'x2).
 3301:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3302:                '</span></fieldset>'.
 3303:                '</td></tr>'."\n";
 3304:     return $output;
 3305: }
 3306: 
 3307: sub contacts_javascript {
 3308:     return <<"ENDSCRIPT";
 3309: 
 3310: <script type="text/javascript">
 3311: // <![CDATA[
 3312: 
 3313: function screenshotSize(field) {
 3314:     if (document.getElementById('help_screenshotsize')) {
 3315:         if (field.value == 'no') {
 3316:             document.getElementById('help_screenshotsize').style.display="none";
 3317:         } else {
 3318:             document.getElementById('help_screenshotsize').style.display="";
 3319:         }
 3320:     }
 3321:     return;
 3322: }
 3323: 
 3324: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3325:     if (form.elements[checkbox].length != undefined) {
 3326:         var count = 0;
 3327:         if (docount) {
 3328:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3329:                 if (form.elements[checkbox][i].checked) {
 3330:                     count ++;
 3331:                 }
 3332:             }
 3333:         }
 3334:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3335:             var type = form.elements[checkbox][i].value;
 3336:             if (document.getElementById(prefix+type)) {
 3337:                 if (form.elements[checkbox][i].checked) {
 3338:                     document.getElementById(prefix+type).style.display = 'table-row';
 3339:                     if (count % 2 == 1) {
 3340:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3341:                     } else {
 3342:                         document.getElementById(prefix+type).className = target;
 3343:                     }
 3344:                     count ++;
 3345:                 } else {
 3346:                     document.getElementById(prefix+type).style.display = 'none';
 3347:                 }
 3348:             }
 3349:         }
 3350:     }
 3351:     return;
 3352: }
 3353: 
 3354: // ]]>
 3355: </script>
 3356: 
 3357: ENDSCRIPT
 3358: }
 3359: 
 3360: sub print_helpsettings {
 3361:     my ($position,$dom,$settings,$rowtotal) = @_;
 3362:     my $confname = $dom.'-domainconfig';
 3363:     my $formname = 'display';
 3364:     my ($datatable,$itemcount);
 3365:     if ($position eq 'top') {
 3366:         $itemcount = 1;
 3367:         my (%choices,%defaultchecked,@toggles);
 3368:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3369:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3370:                                      &mt('LON-CAPA bug tracker'),600,500));
 3371:         %defaultchecked = ('submitbugs' => 'on');
 3372:         @toggles = ('submitbugs');
 3373:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3374:                                                      \%choices,$itemcount);
 3375:         $$rowtotal ++;
 3376:     } else {
 3377:         my $css_class;
 3378:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3379:         my (%customroles,%ordered,%current);
 3380:         if (ref($settings) eq 'HASH') {
 3381:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3382:                 %current = %{$settings->{'adhoc'}};
 3383:             }
 3384:         }
 3385:         my $count = 0;
 3386:         foreach my $key (sort(keys(%existing))) {
 3387:             if ($key=~/^rolesdef\_(\w+)$/) {
 3388:                 my $rolename = $1;
 3389:                 my (%privs,$order);
 3390:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3391:                 $customroles{$rolename} = \%privs;
 3392:                 if (ref($current{$rolename}) eq 'HASH') {
 3393:                     $order = $current{$rolename}{'order'};
 3394:                 }
 3395:                 if ($order eq '') {
 3396:                     $order = $count;
 3397:                 }
 3398:                 $ordered{$order} = $rolename;
 3399:                 $count++;
 3400:             }
 3401:         }
 3402:         my $maxnum = scalar(keys(%ordered));
 3403:         my @roles_by_num = ();
 3404:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3405:             push(@roles_by_num,$item);
 3406:         }
 3407:         my $context = 'domprefs';
 3408:         my $crstype = 'Course';
 3409:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3410:         my @accesstypes = ('all','dh','da','none');
 3411:         my ($numstatustypes,@jsarray);
 3412:         if (ref($types) eq 'ARRAY') {
 3413:             if (@{$types} > 0) {
 3414:                 $numstatustypes = scalar(@{$types});
 3415:                 push(@accesstypes,'status');
 3416:                 @jsarray = ('bystatus');
 3417:             }
 3418:         }
 3419:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3420:         if (keys(%domhelpdesk)) {
 3421:             push(@accesstypes,('inc','exc'));
 3422:             push(@jsarray,('notinc','notexc'));
 3423:         }
 3424:         my $hiddenstr = join("','",@jsarray);
 3425:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3426:         my $context = 'domprefs';
 3427:         my $crstype = 'Course';
 3428:         my $prefix = 'helproles_';
 3429:         my $add_class = 'LC_hidden';
 3430:         foreach my $num (@roles_by_num) {
 3431:             my $role = $ordered{$num};
 3432:             my ($desc,$access,@statuses);
 3433:             if (ref($current{$role}) eq 'HASH') {
 3434:                 $desc = $current{$role}{'desc'};
 3435:                 $access = $current{$role}{'access'};
 3436:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3437:                     @statuses = @{$current{$role}{'insttypes'}};
 3438:                 }
 3439:             }
 3440:             if ($desc eq '') {
 3441:                 $desc = $role;
 3442:             }
 3443:             my $identifier = 'custhelp'.$num;
 3444:             my %full=();
 3445:             my %levels= (
 3446:                          course => {},
 3447:                          domain => {},
 3448:                          system => {},
 3449:                         );
 3450:             my %levelscurrent=(
 3451:                                course => {},
 3452:                                domain => {},
 3453:                                system => {},
 3454:                               );
 3455:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3456:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3457:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3458:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3459:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3460:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3461:             for (my $k=0; $k<=$maxnum; $k++) {
 3462:                 my $vpos = $k+1;
 3463:                 my $selstr;
 3464:                 if ($k == $num) {
 3465:                     $selstr = ' selected="selected" ';
 3466:                 }
 3467:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3468:             }
 3469:             $datatable .= '</select>'.('&nbsp;'x2).
 3470:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3471:                           '</td>'.
 3472:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3473:                           &mt('Name shown to users:').
 3474:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3475:                           '</fieldset>'.
 3476:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3477:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3478:                           '<fieldset>'.
 3479:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3480:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3481:                                                                    \%levelscurrent,$identifier,
 3482:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3483:                           '</fieldset></td>';
 3484:             $itemcount ++;
 3485:         }
 3486:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3487:         my $newcust = 'custhelp'.$count;
 3488:         my (%privs,%levelscurrent);
 3489:         my %full=();
 3490:         my %levels= (
 3491:                      course => {},
 3492:                      domain => {},
 3493:                      system => {},
 3494:                     );
 3495:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3496:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3497:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3498:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3499:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3500:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3501:         for (my $k=0; $k<$maxnum+1; $k++) {
 3502:             my $vpos = $k+1;
 3503:             my $selstr;
 3504:             if ($k == $maxnum) {
 3505:                 $selstr = ' selected="selected" ';
 3506:             }
 3507:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3508:         }
 3509:         $datatable .= '</select>&nbsp;'."\n".
 3510:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3511:                       '</label></span></td>'.
 3512:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3513:                       '<span class="LC_nobreak">'.
 3514:                       &mt('Internal name:').
 3515:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3516:                       '</span>'.('&nbsp;'x4).
 3517:                       '<span class="LC_nobreak">'.
 3518:                       &mt('Name shown to users:').
 3519:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3520:                       '</span></fieldset>'.
 3521:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3522:                                              $usertypes,$types,\%domhelpdesk).
 3523:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3524:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3525:                                                                 \@templateroles,$newcust).
 3526:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3527:                                                                \%levelscurrent,$newcust).
 3528:                       '</fieldset>'.
 3529:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 3530:                       '</td></tr>';
 3531:         $count ++;
 3532:         $$rowtotal += $count;
 3533:     }
 3534:     return $datatable;
 3535: }
 3536: 
 3537: sub adhocbutton {
 3538:     my ($prefix,$num,$field,$visibility) = @_;
 3539:     my %lt = &Apache::lonlocal::texthash(
 3540:                                           show => 'Show details',
 3541:                                           hide => 'Hide details',
 3542:                                         );
 3543:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3544:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3545:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3546:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3547: }
 3548: 
 3549: sub helpsettings_javascript {
 3550:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3551:     return unless(ref($roles_by_num) eq 'ARRAY');
 3552:     my %html_js_lt = &Apache::lonlocal::texthash(
 3553:                                           show => 'Show details',
 3554:                                           hide => 'Hide details',
 3555:                                         );
 3556:     &html_escape(\%html_js_lt);
 3557:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3558:     return <<"ENDSCRIPT";
 3559: <script type="text/javascript">
 3560: // <![CDATA[
 3561: 
 3562: function reorderHelpRoles(form,item) {
 3563:     var changedVal;
 3564: $jstext
 3565:     var newpos = 'helproles_${total}_pos';
 3566:     var maxh = 1 + $total;
 3567:     var current = new Array();
 3568:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3569:     if (item == newpos) {
 3570:         changedVal = newitemVal;
 3571:     } else {
 3572:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3573:         current[newitemVal] = newpos;
 3574:     }
 3575:     for (var i=0; i<helproles.length; i++) {
 3576:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3577:         if (elementName != item) {
 3578:             if (form.elements[elementName]) {
 3579:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3580:                 current[currVal] = elementName;
 3581:             }
 3582:         }
 3583:     }
 3584:     var oldVal;
 3585:     for (var j=0; j<maxh; j++) {
 3586:         if (current[j] == undefined) {
 3587:             oldVal = j;
 3588:         }
 3589:     }
 3590:     if (oldVal < changedVal) {
 3591:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3592:            var elementName = current[k];
 3593:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3594:         }
 3595:     } else {
 3596:         for (var k=changedVal; k<oldVal; k++) {
 3597:             var elementName = current[k];
 3598:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3599:         }
 3600:     }
 3601:     return;
 3602: }
 3603: 
 3604: function helpdeskAccess(num) {
 3605:     var curraccess = null;
 3606:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3607:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3608:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3609:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3610:             }
 3611:         }
 3612:     }
 3613:     var shown = Array();
 3614:     var hidden = Array();
 3615:     if (curraccess == 'none') {
 3616:         hidden = Array('$hiddenstr');
 3617:     } else {
 3618:         if (curraccess == 'status') {
 3619:             shown = Array('bystatus');
 3620:             hidden = Array('notinc','notexc');
 3621:         } else {
 3622:             if (curraccess == 'exc') {
 3623:                 shown = Array('notexc');
 3624:                 hidden = Array('notinc','bystatus');
 3625:             }
 3626:             if (curraccess == 'inc') {
 3627:                 shown = Array('notinc');
 3628:                 hidden = Array('notexc','bystatus');
 3629:             }
 3630:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3631:                 hidden = Array('notinc','notexc','bystatus');
 3632:             }
 3633:         }
 3634:     }
 3635:     if (hidden.length > 0) {
 3636:         for (var i=0; i<hidden.length; i++) {
 3637:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3638:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3639:             }
 3640:         }
 3641:     }
 3642:     if (shown.length > 0) {
 3643:         for (var i=0; i<shown.length; i++) {
 3644:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3645:                 if (shown[i] == 'privs') {
 3646:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3647:                 } else {
 3648:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3649:                 }
 3650:             }
 3651:         }
 3652:     }
 3653:     return;
 3654: }
 3655: 
 3656: function toggleHelpdeskItem(num,field) {
 3657:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3658:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3659:             document.getElementById('helproles_'+num+'_'+field).className =
 3660:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3661:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3662:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3663:             }
 3664:         } else {
 3665:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3666:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3667:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3668:             }
 3669:         }
 3670:     }
 3671:     return;
 3672: }
 3673: 
 3674: // ]]>
 3675: </script>
 3676: 
 3677: ENDSCRIPT
 3678: }
 3679: 
 3680: sub helpdeskroles_access {
 3681:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3682:         $usertypes,$types,$domhelpdesk) = @_;
 3683:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3684:     my %lt = &Apache::lonlocal::texthash(
 3685:                     'rou'    => 'Role usage',
 3686:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3687:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3688:                     'dh'     => 'All with domain helpdesk role',
 3689:                     'da'     => 'All with domain helpdesk assistant role',
 3690:                     'none'   => 'None',
 3691:                     'status' => 'Determined based on institutional status',
 3692:                     'inc'    => 'Include all, but exclude specific personnel',
 3693:                     'exc'    => 'Exclude all, but include specific personnel',
 3694:                   );
 3695:     my %usecheck = (
 3696:                      all => ' checked="checked"',
 3697:                    );
 3698:     my %displaydiv = (
 3699:                       status => 'none',
 3700:                       inc    => 'none',
 3701:                       exc    => 'none',
 3702:                       priv   => 'block',
 3703:                      );
 3704:     my $output;
 3705:     if (ref($current) eq 'HASH') {
 3706:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3707:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3708:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3709:                 delete($usecheck{'all'});
 3710:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3711:                     my $access = $1;
 3712:                     $displaydiv{$access} = 'inline';
 3713:                 } elsif ($current->{access} eq 'none') {
 3714:                     $displaydiv{'priv'} = 'none';
 3715:                 }
 3716:             }
 3717:         }
 3718:     }
 3719:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3720:               '<p>'.$lt{'whi'}.'</p>';
 3721:     foreach my $access (@{$accesstypes}) {
 3722:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3723:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3724:                    $lt{$access}.'</label>';
 3725:         if ($access eq 'status') {
 3726:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3727:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3728:                                                                  $othertitle,$usertypes,$types).
 3729:                        '</div>';
 3730:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3731:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3732:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3733:                        '</div>';
 3734:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3735:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3736:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3737:                        '</div>';
 3738:         }
 3739:         $output .= '</p>';
 3740:     }
 3741:     $output .= '</fieldset>';
 3742:     return $output;
 3743: }
 3744: 
 3745: sub radiobutton_prefs {
 3746:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3747:         $additional,$align) = @_;
 3748:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3749:                    (ref($choices) eq 'HASH'));
 3750: 
 3751:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3752: 
 3753:     foreach my $item (@{$toggles}) {
 3754:         if ($defaultchecked->{$item} eq 'on') {
 3755:             $checkedon{$item} = ' checked="checked" ';
 3756:             $checkedoff{$item} = ' ';
 3757:         } elsif ($defaultchecked->{$item} eq 'off') {
 3758:             $checkedoff{$item} = ' checked="checked" ';
 3759:             $checkedon{$item} = ' ';
 3760:         }
 3761:     }
 3762:     if (ref($settings) eq 'HASH') {
 3763:         foreach my $item (@{$toggles}) {
 3764:             if ($settings->{$item} eq '1') {
 3765:                 $checkedon{$item} =  ' checked="checked" ';
 3766:                 $checkedoff{$item} = ' ';
 3767:             } elsif ($settings->{$item} eq '0') {
 3768:                 $checkedoff{$item} =  ' checked="checked" ';
 3769:                 $checkedon{$item} = ' ';
 3770:             }
 3771:         }
 3772:     }
 3773:     if ($onclick) {
 3774:         $onclick = ' onclick="'.$onclick.'"';
 3775:     }
 3776:     foreach my $item (@{$toggles}) {
 3777:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3778:         $datatable .=
 3779:             '<tr'.$css_class.'><td valign="top">'.
 3780:             '<span class="LC_nobreak">'.$choices->{$item}.
 3781:             '</span></td>';
 3782:         if ($align eq 'left') {
 3783:             $datatable .= '<td class="LC_left_item">';
 3784:         } else {
 3785:             $datatable .= '<td class="LC_right_item">';
 3786:         }
 3787:         $datatable .=
 3788:             '<span class="LC_nobreak">'.
 3789:             '<label><input type="radio" name="'.
 3790:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3791:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3792:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3793:             '</span>'.$additional.
 3794:             '</td>'.
 3795:             '</tr>';
 3796:         $itemcount ++;
 3797:     }
 3798:     return ($datatable,$itemcount);
 3799: }
 3800: 
 3801: sub print_ltitools {
 3802:     my ($dom,$settings,$rowtotal) = @_;
 3803:     my $rownum = 0;
 3804:     my $css_class;
 3805:     my $itemcount = 1;
 3806:     my $maxnum = 0;
 3807:     my %ordered;
 3808:     if (ref($settings) eq 'HASH') {
 3809:         foreach my $item (keys(%{$settings})) {
 3810:             if (ref($settings->{$item}) eq 'HASH') {
 3811:                 my $num = $settings->{$item}{'order'};
 3812:                 $ordered{$num} = $item;
 3813:             }
 3814:         }
 3815:     }
 3816:     my $confname = $dom.'-domainconfig';
 3817:     my $switchserver = &check_switchserver($dom,$confname);
 3818:     my $maxnum = scalar(keys(%ordered));
 3819:     my $datatable = &ltitools_javascript($settings);
 3820:     my %lt = &ltitools_names();
 3821:     my @courseroles = ('cc','in','ta','ep','st');
 3822:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3823:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3824:     if (keys(%ordered)) {
 3825:         my @items = sort { $a <=> $b } keys(%ordered);
 3826:         for (my $i=0; $i<@items; $i++) {
 3827:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3828:             my $item = $ordered{$items[$i]};
 3829:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3830:             if (ref($settings->{$item}) eq 'HASH') {
 3831:                 $title = $settings->{$item}->{'title'};
 3832:                 $url = $settings->{$item}->{'url'};
 3833:                 $key = $settings->{$item}->{'key'};
 3834:                 $secret = $settings->{$item}->{'secret'};
 3835:                 my $image = $settings->{$item}->{'image'};
 3836:                 if ($image ne '') {
 3837:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3838:                 }
 3839:             }
 3840:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3841:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3842:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3843:             for (my $k=0; $k<=$maxnum; $k++) {
 3844:                 my $vpos = $k+1;
 3845:                 my $selstr;
 3846:                 if ($k == $i) {
 3847:                     $selstr = ' selected="selected" ';
 3848:                 }
 3849:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3850:             }
 3851:             $datatable .= '</select>'.('&nbsp;'x2).
 3852:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3853:                 &mt('Delete?').'</label></span></td>'.
 3854:                 '<td colspan="2">'.
 3855:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3856:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3857:                 ('&nbsp;'x2).
 3858:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3859:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3860:                 ('&nbsp;'x2).
 3861:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3862:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3863:                 '<br /><br />'.
 3864:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3865:                 ' value="'.$url.'" /></span>'.
 3866:                 ('&nbsp;'x2).
 3867:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3868:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3869:                 ('&nbsp;'x2).
 3870:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3871:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3872:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.ltitools_secret_'.$i.'.type='."'text'".' } else { this.form.ltitools_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>'.
 3873:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3874:                 '</fieldset>'.
 3875:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3876:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3877:             my %currdisp;
 3878:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3879:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3880:                     $currdisp{'window'} = ' checked="checked"';
 3881:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3882:                     $currdisp{'tab'} = ' checked="checked"';
 3883:                 } else {
 3884:                     $currdisp{'iframe'} = ' checked="checked"';
 3885:                 }
 3886:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3887:                     $currdisp{'width'} = $1;
 3888:                 }
 3889:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3890:                      $currdisp{'height'} = $1;
 3891:                 }
 3892:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3893:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3894:             } else {
 3895:                 $currdisp{'iframe'} = ' checked="checked"';
 3896:             }
 3897:             foreach my $disp ('iframe','tab','window') {
 3898:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3899:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3900:             }
 3901:             $datatable .= ('&nbsp;'x4);
 3902:             foreach my $dimen ('width','height') {
 3903:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3904:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3905:                               ('&nbsp;'x2);
 3906:             }
 3907:             $datatable .= '<br />'.
 3908:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3909:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3910:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3911:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3912:                           '</textarea></div><div style=""></div><br />';
 3913:             $datatable .= '<br />';
 3914:             foreach my $extra ('passback','roster') {
 3915:                 my $checkedon = '';
 3916:                 my $checkedoff = ' checked="checked"';
 3917:                 if ($settings->{$item}->{$extra}) {
 3918:                     $checkedon = $checkedoff;
 3919:                     $checkedoff = '';
 3920:                 }
 3921:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3922:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3923:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3924:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3925:                               &mt('No').'</label>'.('&nbsp;'x4);
 3926:             }
 3927:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3928:             if ($imgsrc) {
 3929:                 $datatable .= $imgsrc.
 3930:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3931:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3932:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3933:             } else {
 3934:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3935:             }
 3936:             if ($switchserver) {
 3937:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3938:             } else {
 3939:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3940:             }
 3941:             $datatable .= '</span></fieldset>';
 3942:             my (%checkedfields,%rolemaps);
 3943:             if (ref($settings->{$item}) eq 'HASH') {
 3944:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3945:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3946:                 }
 3947:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3948:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3949:                     $checkedfields{'roles'} = 1;
 3950:                 }
 3951:             }
 3952:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3953:                           '<span class="LC_nobreak">';
 3954:             foreach my $field (@fields) {
 3955:                 my $checked;
 3956:                 if ($checkedfields{$field}) {
 3957:                     $checked = ' checked="checked"';
 3958:                 }
 3959:                 $datatable .= '<label>'.
 3960:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3961:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3962:             }
 3963:             $datatable .= '</span></fieldset>'.
 3964:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3965:             foreach my $role (@courseroles) {
 3966:                 my ($selected,$selectnone);
 3967:                 if (!$rolemaps{$role}) {
 3968:                     $selectnone = ' selected="selected"';
 3969:                 }
 3970:                 $datatable .= '<td align="center">'.
 3971:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3972:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3973:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3974:                 foreach my $ltirole (@ltiroles) {
 3975:                     unless ($selectnone) {
 3976:                         if ($rolemaps{$role} eq $ltirole) {
 3977:                             $selected = ' selected="selected"';
 3978:                         } else {
 3979:                             $selected = '';
 3980:                         }
 3981:                     }
 3982:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3983:                 }
 3984:                 $datatable .= '</select></td>';
 3985:             }
 3986:             $datatable .= '</tr></table></fieldset>';
 3987:             my %courseconfig;
 3988:             if (ref($settings->{$item}) eq 'HASH') {
 3989:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3990:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3991:                 }
 3992:             }
 3993:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3994:             foreach my $item ('label','title','target','linktext','explanation') {
 3995:                 my $checked;
 3996:                 if ($courseconfig{$item}) {
 3997:                     $checked = ' checked="checked"';
 3998:                 }
 3999:                 $datatable .= '<label>'.
 4000:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4001:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4002:             }
 4003:             $datatable .= '</span></fieldset>'.
 4004:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4005:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4006:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4007:                 my %custom = %{$settings->{$item}->{'custom'}};
 4008:                 if (keys(%custom) > 0) {
 4009:                     foreach my $key (sort(keys(%custom))) {
 4010:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4011:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4012:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4013:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4014:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4015:                     }
 4016:                 }
 4017:             }
 4018:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4019:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4020:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4021:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4022:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4023:             $itemcount ++;
 4024:         }
 4025:     }
 4026:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4027:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 4028:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4029:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4030:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4031:     for (my $k=0; $k<$maxnum+1; $k++) {
 4032:         my $vpos = $k+1;
 4033:         my $selstr;
 4034:         if ($k == $maxnum) {
 4035:             $selstr = ' selected="selected" ';
 4036:         }
 4037:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4038:     }
 4039:     $datatable .= '</select>&nbsp;'."\n".
 4040:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 4041:                   '<td colspan="2">'.
 4042:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4043:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 4044:                   ('&nbsp;'x2).
 4045:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4046:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4047:                   ('&nbsp;'x2).
 4048:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4049:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4050:                   '<br />'.
 4051:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 4052:                   ('&nbsp;'x2).
 4053:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4054:                   ('&nbsp;'x2).
 4055:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4056:                   '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.ltitools_add_secret.type='."'text'".' } else { this.form.ltitools_add_secret.type='."'password'".' }" />'.&mt('Visible input').'</label></span> '."\n".
 4057:                   '</fieldset>'.
 4058:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4059:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4060:     my %defaultdisp;
 4061:     $defaultdisp{'iframe'} = ' checked="checked"';
 4062:     foreach my $disp ('iframe','tab','window') {
 4063:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4064:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4065:     }
 4066:     $datatable .= ('&nbsp;'x4);
 4067:     foreach my $dimen ('width','height') {
 4068:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4069:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4070:                       ('&nbsp;'x2);
 4071:     }
 4072:     $datatable .= '<br />'.
 4073:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4074:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 4075:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4076:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4077:                   '</div><div style=""></div><br />';
 4078:     foreach my $extra ('passback','roster') {
 4079:         $datatable .= $lt{$extra}.'&nbsp;'.
 4080:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 4081:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 4082:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 4083:                       &mt('No').'</label>'.('&nbsp;'x4);
 4084:     }
 4085:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4086:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4087:     if ($switchserver) {
 4088:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4089:     } else {
 4090:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4091:     }
 4092:     $datatable .= '</span></fieldset>'.
 4093:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4094:                   '<span class="LC_nobreak">';
 4095:     foreach my $field (@fields) {
 4096:         $datatable .= '<label>'.
 4097:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 4098:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 4099:     }
 4100:     $datatable .= '</span></fieldset>'.
 4101:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4102:     foreach my $role (@courseroles) {
 4103:         my ($checked,$checkednone);
 4104:         $datatable .= '<td align="center">'.
 4105:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4106:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4107:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4108:         foreach my $ltirole (@ltiroles) {
 4109:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4110:         }
 4111:         $datatable .= '</select></td>';
 4112:     }
 4113:     $datatable .= '</tr></table></fieldset>'.
 4114:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4115:     foreach my $item ('label','title','target','linktext','explanation') {
 4116:         $datatable .= '<label>'.
 4117:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4118:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4119:     }
 4120:     $datatable .= '</span></fieldset>'.
 4121:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4122:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4123:                   '<tr><td><span class="LC_nobreak">'.
 4124:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4125:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4126:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4127:                   '</table></fieldset></td></tr>'."\n".
 4128:                   '</td>'."\n".
 4129:                   '</tr>'."\n";
 4130:     $itemcount ++;
 4131:     return $datatable;
 4132: }
 4133: 
 4134: sub ltitools_names {
 4135:     my %lt = &Apache::lonlocal::texthash(
 4136:                                           'title'          => 'Title',
 4137:                                           'version'        => 'Version',
 4138:                                           'msgtype'        => 'Message Type',
 4139:                                           'url'            => 'URL',
 4140:                                           'key'            => 'Key',
 4141:                                           'secret'         => 'Secret',
 4142:                                           'icon'           => 'Icon',
 4143:                                           'user'           => 'Username:domain',
 4144:                                           'fullname'       => 'Full Name',
 4145:                                           'firstname'      => 'First Name',
 4146:                                           'lastname'       => 'Last Name',
 4147:                                           'email'          => 'E-mail',
 4148:                                           'roles'          => 'Role',
 4149:                                           'window'         => 'Window',
 4150:                                           'tab'            => 'Tab',
 4151:                                           'iframe'         => 'iFrame',
 4152:                                           'height'         => 'Height',
 4153:                                           'width'          => 'Width',
 4154:                                           'linktext'       => 'Default Link Text',
 4155:                                           'explanation'    => 'Default Explanation',
 4156:                                           'passback'       => 'Tool can return grades:',
 4157:                                           'roster'         => 'Tool can retrieve roster:',
 4158:                                           'crstarget'      => 'Display target',
 4159:                                           'crslabel'       => 'Course label',
 4160:                                           'crstitle'       => 'Course title',
 4161:                                           'crslinktext'    => 'Link Text',
 4162:                                           'crsexplanation' => 'Explanation',
 4163:                                         );
 4164: 
 4165:     return %lt;
 4166: }
 4167: 
 4168: sub print_coursedefaults {
 4169:     my ($position,$dom,$settings,$rowtotal) = @_;
 4170:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4171:     my $itemcount = 1;
 4172:     my %choices =  &Apache::lonlocal::texthash (
 4173:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4174:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4175:         coursecredits        => 'Credits can be specified for courses',
 4176:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4177:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4178:         texengine            => 'Default method to display mathematics',
 4179:         postsubmit           => 'Disable submit button/keypress following student submission',
 4180:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4181:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4182:     );
 4183:     my %staticdefaults = (
 4184:                            anonsurvey_threshold => 10,
 4185:                            uploadquota          => 500,
 4186:                            postsubmit           => 60,
 4187:                            mysqltables          => 172800,
 4188:                          );
 4189:     if ($position eq 'top') {
 4190:         %defaultchecked = (
 4191:                             'uselcmath'       => 'on',
 4192:                             'usejsme'         => 'on',
 4193:                             'canclone'        => 'none',
 4194:                           );
 4195:         @toggles = ('uselcmath','usejsme');
 4196:         my $deftex = $Apache::lonnet::deftex;
 4197:         if (ref($settings) eq 'HASH') {
 4198:             if ($settings->{'texengine'}) {
 4199:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 4200:                     $deftex = $settings->{'texengine'};
 4201:                 }
 4202:             }
 4203:         }
 4204:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4205:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 4206:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 4207:                        '</span></td><td class="LC_right_item">'.
 4208:                        '<select name="texengine">'."\n";
 4209:         my %texoptions = (
 4210:                             MathJax  => 'MathJax',
 4211:                             mimetex  => &mt('Convert to Images'),
 4212:                             tth      => &mt('TeX to HTML'),
 4213:                          );
 4214:         foreach my $renderer ('MathJax','mimetex','tth') {
 4215:             my $selected = '';
 4216:             if ($renderer eq $deftex) {
 4217:                 $selected = ' selected="selected"';
 4218:             }
 4219:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 4220:         }
 4221:         $mathdisp .= '</select></td></tr>'."\n";
 4222:         $itemcount ++;
 4223:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4224:                                                      \%choices,$itemcount);
 4225:         $datatable = $mathdisp.$datatable;
 4226:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4227:         $datatable .=
 4228:             '<tr'.$css_class.'><td valign="top">'.
 4229:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4230:             '</span></td><td class="LC_left_item">';
 4231:         my $currcanclone = 'none';
 4232:         my $onclick;
 4233:         my @cloneoptions = ('none','domain');
 4234:         my %clonetitles = (
 4235:                              none     => 'No additional course requesters',
 4236:                              domain   => "Any course requester in course's domain",
 4237:                              instcode => 'Course requests for official courses ...',
 4238:                           );
 4239:         my (%codedefaults,@code_order,@posscodes);
 4240:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4241:                                                     \@code_order) eq 'ok') {
 4242:             if (@code_order > 0) {
 4243:                 push(@cloneoptions,'instcode');
 4244:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4245:             }
 4246:         }
 4247:         if (ref($settings) eq 'HASH') {
 4248:             if ($settings->{'canclone'}) {
 4249:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4250:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4251:                         if (@code_order > 0) {
 4252:                             $currcanclone = 'instcode';
 4253:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4254:                         }
 4255:                     }
 4256:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4257:                     $currcanclone = $settings->{'canclone'};
 4258:                 }
 4259:             }
 4260:         }
 4261:         foreach my $option (@cloneoptions) {
 4262:             my ($checked,$additional);
 4263:             if ($currcanclone eq $option) {
 4264:                 $checked = ' checked="checked"';
 4265:             }
 4266:             if ($option eq 'instcode') {
 4267:                 if (@code_order) {
 4268:                     my $show = 'none';
 4269:                     if ($checked) {
 4270:                         $show = 'block';
 4271:                     }
 4272:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4273:                                   &mt('Institutional codes for new and cloned course have identical:').
 4274:                                   '<br />';
 4275:                     foreach my $item (@code_order) {
 4276:                         my $codechk;
 4277:                         if ($checked) {
 4278:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4279:                                 $codechk = ' checked="checked"';
 4280:                             }
 4281:                         }
 4282:                         $additional .= '<label>'.
 4283:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4284:                                        $item.'</label>';
 4285:                     }
 4286:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4287:                 }
 4288:             }
 4289:             $datatable .=
 4290:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4291:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4292:                 '</label>&nbsp;'.$additional.'</span><br />';
 4293:         }
 4294:         $datatable .= '</td>'.
 4295:                       '</tr>';
 4296:         $itemcount ++;
 4297:     } else {
 4298:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4299:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4300:         my $currusecredits = 0;
 4301:         my $postsubmitclient = 1;
 4302:         my @types = ('official','unofficial','community','textbook');
 4303:         if (ref($settings) eq 'HASH') {
 4304:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4305:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4306:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4307:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4308:                 }
 4309:             }
 4310:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4311:                 foreach my $type (@types) {
 4312:                     next if ($type eq 'community');
 4313:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4314:                     if ($defcredits{$type} ne '') {
 4315:                         $currusecredits = 1;
 4316:                     }
 4317:                 }
 4318:             }
 4319:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4320:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4321:                     $postsubmitclient = 0;
 4322:                     foreach my $type (@types) {
 4323:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4324:                     }
 4325:                 } else {
 4326:                     foreach my $type (@types) {
 4327:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4328:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4329:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4330:                             } else {
 4331:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4332:                             }
 4333:                         } else {
 4334:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4335:                         }
 4336:                     }
 4337:                 }
 4338:             } else {
 4339:                 foreach my $type (@types) {
 4340:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4341:                 }
 4342:             }
 4343:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4344:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4345:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4346:                 }
 4347:             } else {
 4348:                 foreach my $type (@types) {
 4349:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4350:                 }
 4351:             }
 4352:         } else {
 4353:             foreach my $type (@types) {
 4354:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4355:             }
 4356:         }
 4357:         if (!$currdefresponder) {
 4358:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4359:         } elsif ($currdefresponder < 1) {
 4360:             $currdefresponder = 1;
 4361:         }
 4362:         foreach my $type (@types) {
 4363:             if ($curruploadquota{$type} eq '') {
 4364:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4365:             }
 4366:         }
 4367:         $datatable .=
 4368:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4369:                 $choices{'anonsurvey_threshold'}.
 4370:                 '</span></td>'.
 4371:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4372:                 '<input type="text" name="anonsurvey_threshold"'.
 4373:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4374:                 '</td></tr>'."\n";
 4375:         $itemcount ++;
 4376:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4377:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4378:                       $choices{'uploadquota'}.
 4379:                       '</span></td>'.
 4380:                       '<td align="right" class="LC_right_item">'.
 4381:                       '<table><tr>';
 4382:         foreach my $type (@types) {
 4383:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4384:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4385:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4386:         }
 4387:         $datatable .= '</tr></table></td></tr>'."\n";
 4388:         $itemcount ++;
 4389:         my $onclick = "toggleDisplay(this.form,'credits');";
 4390:         my $display = 'none';
 4391:         if ($currusecredits) {
 4392:             $display = 'block';
 4393:         }
 4394:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4395:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4396:         foreach my $type (@types) {
 4397:             next if ($type eq 'community');
 4398:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4399:                            '<input type="text" name="'.$type.'_credits"'.
 4400:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4401:         }
 4402:         $additional .= '</tr></table></div>'."\n";
 4403:         %defaultchecked = ('coursecredits' => 'off');
 4404:         @toggles = ('coursecredits');
 4405:         my $current = {
 4406:                         'coursecredits' => $currusecredits,
 4407:                       };
 4408:         (my $table,$itemcount) =
 4409:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4410:                                \%choices,$itemcount,$onclick,$additional,'left');
 4411:         $datatable .= $table;
 4412:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4413:         my $display = 'none';
 4414:         if ($postsubmitclient) {
 4415:             $display = 'block';
 4416:         }
 4417:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4418:                       &mt('Number of seconds submit is disabled').'<br />'.
 4419:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4420:                       '<table><tr>';
 4421:         foreach my $type (@types) {
 4422:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4423:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4424:                            $deftimeout{$type}.'" size="5" /></td>';
 4425:         }
 4426:         $additional .= '</tr></table></div>'."\n";
 4427:         %defaultchecked = ('postsubmit' => 'on');
 4428:         @toggles = ('postsubmit');
 4429:         $current = {
 4430:                        'postsubmit' => $postsubmitclient,
 4431:                    };
 4432:         ($table,$itemcount) =
 4433:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4434:                                \%choices,$itemcount,$onclick,$additional,'left');
 4435:         $datatable .= $table;
 4436:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4437:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4438:                       $choices{'mysqltables'}.
 4439:                       '</span></td>'.
 4440:                       '<td align="right" class="LC_right_item">'.
 4441:                       '<table><tr>';
 4442:         foreach my $type (@types) {
 4443:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4444:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4445:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4446:         }
 4447:         $datatable .= '</tr></table></td></tr>'."\n";
 4448:         $itemcount ++;
 4449: 
 4450:     }
 4451:     $$rowtotal += $itemcount;
 4452:     return $datatable;
 4453: }
 4454: 
 4455: sub print_selfenrollment {
 4456:     my ($position,$dom,$settings,$rowtotal) = @_;
 4457:     my ($css_class,$datatable);
 4458:     my $itemcount = 1;
 4459:     my @types = ('official','unofficial','community','textbook');
 4460:     if (($position eq 'top') || ($position eq 'middle')) {
 4461:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4462:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4463:         my @rows;
 4464:         my $key;
 4465:         if ($position eq 'top') {
 4466:             $key = 'admin'; 
 4467:             if (ref($rowsref) eq 'ARRAY') {
 4468:                 @rows = @{$rowsref};
 4469:             }
 4470:         } elsif ($position eq 'middle') {
 4471:             $key = 'default';
 4472:             @rows = ('types','registered','approval','limit');
 4473:         }
 4474:         foreach my $row (@rows) {
 4475:             if (defined($titlesref->{$row})) {
 4476:                 $itemcount ++;
 4477:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4478:                 $datatable .= '<tr'.$css_class.'>'.
 4479:                               '<td>'.$titlesref->{$row}.'</td>'.
 4480:                               '<td class="LC_left_item">'.
 4481:                               '<table><tr>';
 4482:                 my (%current,%currentcap);
 4483:                 if (ref($settings) eq 'HASH') {
 4484:                     if (ref($settings->{$key}) eq 'HASH') {
 4485:                         foreach my $type (@types) {
 4486:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4487:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4488:                             }
 4489:                             if (($row eq 'limit') && ($key eq 'default')) {
 4490:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4491:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4492:                                 }
 4493:                             }
 4494:                         }
 4495:                     }
 4496:                 }
 4497:                 my %roles = (
 4498:                              '0' => &Apache::lonnet::plaintext('dc'),
 4499:                             ); 
 4500:             
 4501:                 foreach my $type (@types) {
 4502:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4503:                         $datatable .= '<th>'.&mt($type).'</th>';
 4504:                     }
 4505:                 }
 4506:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4507:                     $datatable .= '</tr><tr>';
 4508:                 }
 4509:                 foreach my $type (@types) {
 4510:                     if ($type eq 'community') {
 4511:                         $roles{'1'} = &mt('Community personnel');
 4512:                     } else {
 4513:                         $roles{'1'} = &mt('Course personnel');
 4514:                     }
 4515:                     $datatable .= '<td style="vertical-align: top">';
 4516:                     if ($position eq 'top') {
 4517:                         my %checked;
 4518:                         if ($current{$type} eq '0') {
 4519:                             $checked{'0'} = ' checked="checked"';
 4520:                         } else {
 4521:                             $checked{'1'} = ' checked="checked"';
 4522:                         }
 4523:                         foreach my $role ('1','0') {
 4524:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4525:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4526:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4527:                                           $roles{$role}.'</label></span> ';
 4528:                         }
 4529:                     } else {
 4530:                         if ($row eq 'types') {
 4531:                             my %checked;
 4532:                             if ($current{$type} =~ /^(all|dom)$/) {
 4533:                                 $checked{$1} = ' checked="checked"';
 4534:                             } else {
 4535:                                 $checked{''} = ' checked="checked"';
 4536:                             }
 4537:                             foreach my $val ('','dom','all') {
 4538:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4539:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4540:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4541:                             }
 4542:                         } elsif ($row eq 'registered') {
 4543:                             my %checked;
 4544:                             if ($current{$type} eq '1') {
 4545:                                 $checked{'1'} = ' checked="checked"';
 4546:                             } else {
 4547:                                 $checked{'0'} = ' checked="checked"';
 4548:                             }
 4549:                             foreach my $val ('0','1') {
 4550:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4551:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4552:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4553:                             }
 4554:                         } elsif ($row eq 'approval') {
 4555:                             my %checked;
 4556:                             if ($current{$type} =~ /^([12])$/) {
 4557:                                 $checked{$1} = ' checked="checked"';
 4558:                             } else {
 4559:                                 $checked{'0'} = ' checked="checked"';
 4560:                             }
 4561:                             for my $val (0..2) {
 4562:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4563:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4564:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4565:                             }
 4566:                         } elsif ($row eq 'limit') {
 4567:                             my %checked;
 4568:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4569:                                 $checked{$1} = ' checked="checked"';
 4570:                             } else {
 4571:                                 $checked{'none'} = ' checked="checked"';
 4572:                             }
 4573:                             my $cap;
 4574:                             if ($currentcap{$type} =~ /^\d+$/) {
 4575:                                 $cap = $currentcap{$type};
 4576:                             }
 4577:                             foreach my $val ('none','allstudents','selfenrolled') {
 4578:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4579:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4580:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4581:                             }
 4582:                             $datatable .= '<br />'.
 4583:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4584:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4585:                                           '</span>'; 
 4586:                         }
 4587:                     }
 4588:                     $datatable .= '</td>';
 4589:                 }
 4590:                 $datatable .= '</tr>';
 4591:             }
 4592:             $datatable .= '</table></td></tr>';
 4593:         }
 4594:     } elsif ($position eq 'bottom') {
 4595:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4596:     }
 4597:     $$rowtotal += $itemcount;
 4598:     return $datatable;
 4599: }
 4600: 
 4601: sub print_validation_rows {
 4602:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4603:     my ($itemsref,$namesref,$fieldsref);
 4604:     if ($caller eq 'selfenroll') { 
 4605:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4606:     } elsif ($caller eq 'requestcourses') {
 4607:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4608:     }
 4609:     my %currvalidation;
 4610:     if (ref($settings) eq 'HASH') {
 4611:         if (ref($settings->{'validation'}) eq 'HASH') {
 4612:             %currvalidation = %{$settings->{'validation'}};
 4613:         }
 4614:     }
 4615:     my $datatable;
 4616:     my $itemcount = 0;
 4617:     foreach my $item (@{$itemsref}) {
 4618:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4619:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4620:                       $namesref->{$item}.
 4621:                       '</span></td>'.
 4622:                       '<td class="LC_left_item">';
 4623:         if (($item eq 'url') || ($item eq 'button')) {
 4624:             $datatable .= '<span class="LC_nobreak">'.
 4625:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4626:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4627:         } elsif ($item eq 'fields') {
 4628:             my @currfields;
 4629:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4630:                 @currfields = @{$currvalidation{$item}};
 4631:             }
 4632:             foreach my $field (@{$fieldsref}) {
 4633:                 my $check = '';
 4634:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4635:                     $check = ' checked="checked"';
 4636:                 }
 4637:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4638:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4639:                               ' value="'.$field.'"'.$check.' />'.$field.
 4640:                               '</label></span> ';
 4641:             }
 4642:         } elsif ($item eq 'markup') {
 4643:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 4644:                            $currvalidation{$item}.
 4645:                               '</textarea>';
 4646:         }
 4647:         $datatable .= '</td></tr>'."\n";
 4648:         if (ref($rowtotal)) {
 4649:             $itemcount ++;
 4650:         }
 4651:     }
 4652:     if ($caller eq 'requestcourses') {
 4653:         my %currhash;
 4654:         if (ref($settings) eq 'HASH') {
 4655:             if (ref($settings->{'validation'}) eq 'HASH') {
 4656:                 if ($settings->{'validation'}{'dc'} ne '') {
 4657:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4658:                 }
 4659:             }
 4660:         }
 4661:         my $numinrow = 2;
 4662:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4663:                                                        'validationdc',%currhash);
 4664:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4665:         $datatable .= '<tr'.$css_class.'><td>';
 4666:         if ($numdc > 1) {
 4667:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4668:         } else {
 4669:             $datatable .=  &mt('Course creation processed as: ');
 4670:         }
 4671:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4672:         $itemcount ++;
 4673:     }
 4674:     if (ref($rowtotal)) {
 4675:         $$rowtotal += $itemcount;
 4676:     }
 4677:     return $datatable;
 4678: }
 4679: 
 4680: sub print_passwords {
 4681:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 4682:     my ($datatable,$css_class);
 4683:     my $itemcount = 0;
 4684:     my %titles = &Apache::lonlocal::texthash (
 4685:         captcha        => '"Forgot Password" CAPTCHA validation',
 4686:         link           => 'Reset link expiration (hours)',
 4687:         case           => 'Case-sensitive usernames/e-mail',
 4688:         prelink        => 'Information required (form 1)',
 4689:         postlink       => 'Information required (form 2)',
 4690:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 4691:         customtext     => 'Domain specific text (HTML)',
 4692:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 4693:         intauth_check  => 'Check bcrypt cost if authenticated',
 4694:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 4695:         permanent      => 'Permanent e-mail address',
 4696:         critical       => 'Critical notification address',
 4697:         notify         => 'Notification address',
 4698:         min            => 'Minimum password length',
 4699:         max            => 'Maximum password length',
 4700:         chars          => 'Required characters',
 4701:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 4702:     );
 4703:     if ($position eq 'top') {
 4704:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4705:         my $shownlinklife = 2;
 4706:         my $prelink = 'both';
 4707:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 4708:         if (ref($settings) eq 'HASH') {
 4709:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 4710:                 $shownlinklife = $settings->{resetlink};
 4711:             }
 4712:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 4713:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 4714:             }
 4715:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 4716:                 $prelink = $settings->{resetprelink};
 4717:             }
 4718:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 4719:                 %postlink = %{$settings->{resetpostlink}};
 4720:             }
 4721:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 4722:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 4723:             }
 4724:             if ($settings->{resetremove}) {
 4725:                 $nostdtext = 1;
 4726:             }
 4727:             if ($settings->{resetcustom}) {
 4728:                 $customurl = $settings->{resetcustom};
 4729:             }
 4730:         } else {
 4731:             if (ref($types) eq 'ARRAY') {
 4732:                 foreach my $item (@{$types}) {
 4733:                     $casesens{$item} = 1;
 4734:                     $postlink{$item} = ['username','email'];
 4735:                 }
 4736:             }
 4737:             $casesens{'default'} = 1;
 4738:             $postlink{'default'} = ['username','email'];
 4739:             $prelink = 'both';
 4740:             %emailsrc = (
 4741:                           permanent => 1,
 4742:                           critical  => 1,
 4743:                           notify    => 1,
 4744:             );
 4745:         }
 4746:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 4747:         $itemcount ++;
 4748:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4749:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 4750:                       '<td class="LC_left_item">'.
 4751:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 4752:                       'name="passwords_link" size="3" /></td></tr>';
 4753:         $itemcount ++;
 4754:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4755:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 4756:                       '<td class="LC_left_item">';
 4757:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4758:             foreach my $item (@{$types}) {
 4759:                 my $checkedcase;
 4760:                 if ($casesens{$item}) {
 4761:                     $checkedcase = ' checked="checked"';
 4762:                 }
 4763:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4764:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 4765:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 4766:                               '</span>&nbsp;&nbsp; ';
 4767:             }
 4768:         }
 4769:         my $checkedcase;
 4770:         if ($casesens{'default'}) {
 4771:             $checkedcase = ' checked="checked"';
 4772:         }
 4773:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 4774:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 4775:                       $othertitle.'</label></span></td>';
 4776:         $itemcount ++;
 4777:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4778:         my %checkedpre = (
 4779:                              both => ' checked="checked"',
 4780:                              either => '',
 4781:                          );
 4782:         if ($prelink eq 'either') {
 4783:             $checkedpre{either} = ' checked="checked"';
 4784:             $checkedpre{both} = '';
 4785:         }
 4786:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 4787:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4788:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 4789:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 4790:                       '<span class="LC_nobreak"><label>'.
 4791:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 4792:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 4793:         $itemcount ++;
 4794:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4795:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 4796:                       '<td class="LC_left_item">';
 4797:         my %postlinked;
 4798:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4799:             foreach my $item (@{$types}) {
 4800:                 undef(%postlinked);
 4801:                 $datatable .= '<fieldset style="display: inline-block;">'.
 4802:                               '<legend>'.$usertypes->{$item}.'</legend>';
 4803:                 if (ref($postlink{$item}) eq 'ARRAY') {
 4804:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 4805:                 }
 4806:                 foreach my $field ('email','username') {
 4807:                     my $checked;
 4808:                     if ($postlinked{$field}) {
 4809:                         $checked = ' checked="checked"';
 4810:                     }
 4811:                     $datatable .= '<span class="LC_nobreak"><label>'.
 4812:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 4813:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 4814:                                   '<span>&nbsp;&nbsp; ';
 4815:                 }
 4816:                 $datatable .= '</fieldset>';
 4817:             }
 4818:         }
 4819:         if (ref($postlink{'default'}) eq 'ARRAY') {
 4820:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 4821:         }
 4822:         $datatable .= '<fieldset style="display: inline-block;">'.
 4823:                       '<legend>'.$othertitle.'</legend>';
 4824:         foreach my $field ('email','username') {
 4825:             my $checked;
 4826:             if ($postlinked{$field}) {
 4827:                 $checked = ' checked="checked"';
 4828:             }
 4829:             $datatable .= '<span class="LC_nobreak"><label>'.
 4830:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 4831:                           $field.'"'.$checked.' />'.$field.'</label>'.
 4832:                           '<span>&nbsp;&nbsp; ';
 4833:         }
 4834:         $datatable .= '</fieldset></td></tr>';
 4835:         $itemcount ++;
 4836:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4837:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 4838:                       '<td class="LC_left_item">';
 4839:         foreach my $type ('permanent','critical','notify') {
 4840:             my $checkedemail;
 4841:             if ($emailsrc{$type}) {
 4842:                 $checkedemail = ' checked="checked"';
 4843:             }
 4844:             $datatable .= '<span class="LC_nobreak"><label>'.
 4845:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 4846:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 4847:                           '<span>&nbsp;&nbsp; ';
 4848:         }
 4849:         $datatable .= '</td></tr>';
 4850:         $itemcount ++;
 4851:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4852:         my $switchserver = &check_switchserver($dom,$confname);
 4853:         my ($showstd,$noshowstd);
 4854:         if ($nostdtext) {
 4855:             $noshowstd = ' checked="checked"';
 4856:         } else {
 4857:             $showstd = ' checked="checked"';
 4858:         }
 4859:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 4860:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4861:                       &mt('Retain standard text:').
 4862:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 4863:                       &mt('Yes').'</label>'.'&nbsp;'.
 4864:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 4865:                       &mt('No').'</label></span><br />'.
 4866:                       '<span class="LC_fontsize_small">'.
 4867:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 4868:                       &mt('Include custom text:');
 4869:         if ($customurl) {
 4870:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 4871:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4872:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 4873:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 4874:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 4875:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 4876:         }
 4877:         if ($switchserver) {
 4878:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 4879:         } else {
 4880:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4881:                          '<input type="file" name="passwords_customfile" /></span>';
 4882:         }
 4883:         $datatable .= '</td></tr>';
 4884:     } elsif ($position eq 'middle') {
 4885:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 4886:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 4887:         my %defaults;
 4888:         if (ref($domconf{'defaults'}) eq 'HASH') {
 4889:             %defaults = %{$domconf{'defaults'}};
 4890:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 4891:                 $defaults{'intauth_cost'} = 10;
 4892:             }
 4893:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 4894:                 $defaults{'intauth_check'} = 0;
 4895:             }
 4896:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 4897:                 $defaults{'intauth_switch'} = 0;
 4898:             }
 4899:         } else {
 4900:             %defaults = (
 4901:                           'intauth_cost'   => 10,
 4902:                           'intauth_check'  => 0,
 4903:                           'intauth_switch' => 0,
 4904:                         );
 4905:         }
 4906:         foreach my $item (@items) {
 4907:             if ($itemcount%2) {
 4908:                 $css_class = '';
 4909:             } else {
 4910:                 $css_class = ' class="LC_odd_row" ';
 4911:             }
 4912:             $datatable .= '<tr'.$css_class.'>'.
 4913:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 4914:                           '</span></td><td class="LC_left_item" colspan="3">';
 4915:             if ($item eq 'intauth_switch') {
 4916:                 my @options = (0,1,2);
 4917:                 my %optiondesc = &Apache::lonlocal::texthash (
 4918:                                    0 => 'No',
 4919:                                    1 => 'Yes',
 4920:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 4921:                                  );
 4922:                 $datatable .= '<table width="100%">';
 4923:                 foreach my $option (@options) {
 4924:                     my $checked = ' ';
 4925:                     if ($defaults{$item} eq $option) {
 4926:                         $checked = ' checked="checked"';
 4927:                     }
 4928:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4929:                                   '<label><input type="radio" name="'.$item.
 4930:                                   '" value="'.$option.'"'.$checked.' />'.
 4931:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4932:                 }
 4933:                 $datatable .= '</table>';
 4934:             } elsif ($item eq 'intauth_check') {
 4935:                 my @options = (0,1,2);
 4936:                 my %optiondesc = &Apache::lonlocal::texthash (
 4937:                                    0 => 'No',
 4938:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 4939:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 4940:                                  );
 4941:                 $datatable .= '<table width="100%">';
 4942:                 foreach my $option (@options) {
 4943:                     my $checked = ' ';
 4944:                     my $onclick;
 4945:                     if ($defaults{$item} eq $option) {
 4946:                         $checked = ' checked="checked"';
 4947:                     }
 4948:                     if ($option == 2) {
 4949:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 4950:                     }
 4951:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 4952:                                   '<label><input type="radio" name="'.$item.
 4953:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 4954:                                   $optiondesc{$option}.'</label></span></td></tr>';
 4955:                 }
 4956:                 $datatable .= '</table>';
 4957:             } else {
 4958:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4959:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 4960:             }
 4961:             $datatable .= '</td></tr>';
 4962:             $itemcount ++;
 4963:         }
 4964:     } elsif ($position eq 'lower') {
 4965:         my ($min,$max,%chars,$numsaved);
 4966:         $min = $Apache::lonnet::passwdmin;
 4967:         if (ref($settings) eq 'HASH') {
 4968:             if ($settings->{min}) {
 4969:                 $min = $settings->{min};
 4970:             }
 4971:             if ($settings->{max}) {
 4972:                 $max = $settings->{max};
 4973:             }
 4974:             if (ref($settings->{chars}) eq 'ARRAY') {
 4975:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 4976:             }
 4977:             if ($settings->{numsaved}) {
 4978:                 $numsaved = $settings->{numsaved};
 4979:             }
 4980:         }
 4981:         my %rulenames = &Apache::lonlocal::texthash(
 4982:                                                      uc => 'At least one upper case letter',
 4983:                                                      lc => 'At least one lower case letter',
 4984:                                                      num => 'At least one number',
 4985:                                                      spec => 'At least one non-alphanumeric',
 4986:                                                    );
 4987:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4988:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 4989:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4990:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 4991:                       'onblur="javascript:warnIntPass(this);" />'.
 4992:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 4993:                       '</span></td></tr>';
 4994:         $itemcount ++;
 4995:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4996:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 4997:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4998:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 4999:                       'onblur="javascript:warnIntPass(this);" />'.
 5000:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 5001:                       '</span></td></tr>';
 5002:         $itemcount ++;
 5003:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5004:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 5005:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 5006:                       '</span></td>';
 5007:         my $numinrow = 2;
 5008:         my @possrules = ('uc','lc','num','spec');
 5009:         $datatable .= '<td class="LC_left_item"><table>';
 5010:         for (my $i=0; $i<@possrules; $i++) {
 5011:             my ($rem,$checked);
 5012:             if ($chars{$possrules[$i]}) {
 5013:                 $checked = ' checked="checked"';
 5014:             }
 5015:             $rem = $i%($numinrow);
 5016:             if ($rem == 0) {
 5017:                 if ($i > 0) {
 5018:                     $datatable .= '</tr>';
 5019:                 }
 5020:                 $datatable .= '<tr>';
 5021:             }
 5022:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 5023:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 5024:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 5025:         }
 5026:         my $rem = @possrules%($numinrow);
 5027:         my $colsleft = $numinrow - $rem;
 5028:         if ($colsleft > 1 ) {
 5029:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5030:                           '&nbsp;</td>';
 5031:         } elsif ($colsleft == 1) {
 5032:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5033:         }
 5034:         $datatable .='</table></td></tr>';
 5035:         $itemcount ++;
 5036:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5037:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 5038:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5039:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 5040:                       'onblur="javascript:warnIntPass(this);" />'.
 5041:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 5042:                       '</span></td></tr>';
 5043:     } else {
 5044:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5045:         my %ownerchg = (
 5046:                           by  => {},
 5047:                           for => {},
 5048:                        );
 5049:         my %ownertitles = &Apache::lonlocal::texthash (
 5050:                             by  => 'Course owner status(es) allowed',
 5051:                             for => 'Student status(es) allowed',
 5052:                           );
 5053:         if (ref($settings) eq 'HASH') {
 5054:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 5055:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 5056:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 5057:                 }
 5058:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 5059:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 5060:                 }
 5061:             }
 5062:         }
 5063:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5064:         $datatable .= '<tr '.$css_class.'>'.
 5065:                       '<td>'.
 5066:                       &mt('Requirements').'<ul>'.
 5067:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 5068:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 5069:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 5070:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 5071:                       '</ul>'.
 5072:                       '</td>'.
 5073:                       '<td class="LC_left_item">';
 5074:         foreach my $item ('by','for') {
 5075:             $datatable .= '<fieldset style="display: inline-block;">'.
 5076:                           '<legend>'.$ownertitles{$item}.'</legend>';
 5077:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5078:                 foreach my $type (@{$types}) {
 5079:                     my $checked;
 5080:                     if ($ownerchg{$item}{$type}) {
 5081:                         $checked = ' checked="checked"';
 5082:                     }
 5083:                     $datatable .= '<span class="LC_nobreak"><label>'.
 5084:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 5085:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 5086:                                   '</span>&nbsp;&nbsp; ';
 5087:                 }
 5088:             }
 5089:             my $checked;
 5090:             if ($ownerchg{$item}{'default'}) {
 5091:                 $checked = ' checked="checked"';
 5092:             }
 5093:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 5094:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 5095:                           $othertitle.'</label></span></fieldset>';
 5096:         }
 5097:         $datatable .= '</td></tr>';
 5098:     }
 5099:     return $datatable;
 5100: }
 5101: 
 5102: sub print_usersessions {
 5103:     my ($position,$dom,$settings,$rowtotal) = @_;
 5104:     my ($css_class,$datatable,%checked,%choices);
 5105:     my (%by_ip,%by_location,@intdoms);
 5106:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 5107: 
 5108:     my @alldoms = &Apache::lonnet::all_domains();
 5109:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5110:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5111:     my %altids = &id_for_thisdom(%servers);
 5112:     my $itemcount = 1;
 5113:     if ($position eq 'top') {
 5114:         if (keys(%serverhomes) > 1) {
 5115:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 5116:             my ($curroffloadnow,$curroffloadoth);
 5117:             if (ref($settings) eq 'HASH') {
 5118:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 5119:                     $curroffloadnow = $settings->{'offloadnow'};
 5120:                 }
 5121:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 5122:                     $curroffloadoth = $settings->{'offloadoth'};
 5123:                 }
 5124:             }
 5125:             my $other_insts = scalar(keys(%by_location));
 5126:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 5127:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 5128:         } else {
 5129:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5130:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 5131:         }
 5132:     } else {
 5133:         if (keys(%by_location) == 0) {
 5134:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5135:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 5136:         } else {
 5137:             my %lt = &usersession_titles();
 5138:             my $numinrow = 5;
 5139:             my $prefix;
 5140:             my @types;
 5141:             if ($position eq 'bottom') {
 5142:                 $prefix = 'remote';
 5143:                 @types = ('version','excludedomain','includedomain');
 5144:             } else {
 5145:                 $prefix = 'hosted';
 5146:                 @types = ('excludedomain','includedomain');
 5147:             }
 5148:             my (%current,%checkedon,%checkedoff);
 5149:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 5150:             my @locations = sort(keys(%by_location));
 5151:             foreach my $type (@types) {
 5152:                 $checkedon{$type} = '';
 5153:                 $checkedoff{$type} = ' checked="checked"';
 5154:             }
 5155:             if (ref($settings) eq 'HASH') {
 5156:                 if (ref($settings->{$prefix}) eq 'HASH') {
 5157:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 5158:                         $current{$key} = $settings->{$prefix}{$key};
 5159:                         if ($key eq 'version') {
 5160:                             if ($current{$key} ne '') {
 5161:                                 $checkedon{$key} = ' checked="checked"';
 5162:                                 $checkedoff{$key} = '';
 5163:                             }
 5164:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 5165:                             $checkedon{$key} = ' checked="checked"';
 5166:                             $checkedoff{$key} = '';
 5167:                         }
 5168:                     }
 5169:                 }
 5170:             }
 5171:             foreach my $type (@types) {
 5172:                 next if ($type ne 'version' && !@locations);
 5173:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5174:                 $datatable .= '<tr'.$css_class.'>
 5175:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 5176:                                <span class="LC_nobreak">&nbsp;
 5177:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 5178:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 5179:                 if ($type eq 'version') {
 5180:                     my $selector = '<select name="'.$prefix.'_version">';
 5181:                     foreach my $version (@lcversions) {
 5182:                         my $selected = '';
 5183:                         if ($current{'version'} eq $version) {
 5184:                             $selected = ' selected="selected"';
 5185:                         }
 5186:                         $selector .= ' <option value="'.$version.'"'.
 5187:                                      $selected.'>'.$version.'</option>';
 5188:                     }
 5189:                     $selector .= '</select> ';
 5190:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 5191:                 } else {
 5192:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 5193:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 5194:                                  ' />'.('&nbsp;'x2).
 5195:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 5196:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 5197:                                  "\n".
 5198:                                  '</div><div><table>';
 5199:                     my $rem;
 5200:                     for (my $i=0; $i<@locations; $i++) {
 5201:                         my ($showloc,$value,$checkedtype);
 5202:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 5203:                             my $ip = $by_location{$locations[$i]}->[0];
 5204:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 5205:                                  $value = join(':',@{$by_ip{$ip}});
 5206:                                 $showloc = join(', ',@{$by_ip{$ip}});
 5207:                                 if (ref($current{$type}) eq 'ARRAY') {
 5208:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 5209:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 5210:                                             $checkedtype = ' checked="checked"';
 5211:                                             last;
 5212:                                         }
 5213:                                     }
 5214:                                 }
 5215:                             }
 5216:                         }
 5217:                         $rem = $i%($numinrow);
 5218:                         if ($rem == 0) {
 5219:                             if ($i > 0) {
 5220:                                 $datatable .= '</tr>';
 5221:                             }
 5222:                             $datatable .= '<tr>';
 5223:                         }
 5224:                         $datatable .= '<td class="LC_left_item">'.
 5225:                                       '<span class="LC_nobreak"><label>'.
 5226:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 5227:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 5228:                                       '</label></span></td>';
 5229:                     }
 5230:                     $rem = @locations%($numinrow);
 5231:                     my $colsleft = $numinrow - $rem;
 5232:                     if ($colsleft > 1 ) {
 5233:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5234:                                       '&nbsp;</td>';
 5235:                     } elsif ($colsleft == 1) {
 5236:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5237:                     }
 5238:                     $datatable .= '</tr></table>';
 5239:                 }
 5240:                 $datatable .= '</td></tr>';
 5241:                 $itemcount ++;
 5242:             }
 5243:         }
 5244:     }
 5245:     $$rowtotal += $itemcount;
 5246:     return $datatable;
 5247: }
 5248: 
 5249: sub build_location_hashes {
 5250:     my ($intdoms,$by_ip,$by_location) = @_;
 5251:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 5252:                   (ref($by_location) eq 'HASH')); 
 5253:     my %iphost = &Apache::lonnet::get_iphost();
 5254:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 5255:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 5256:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 5257:         foreach my $id (@{$iphost{$primary_ip}}) {
 5258:             my $intdom = &Apache::lonnet::internet_dom($id);
 5259:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 5260:                 push(@{$intdoms},$intdom);
 5261:             }
 5262:         }
 5263:     }
 5264:     foreach my $ip (keys(%iphost)) {
 5265:         if (ref($iphost{$ip}) eq 'ARRAY') {
 5266:             foreach my $id (@{$iphost{$ip}}) {
 5267:                 my $location = &Apache::lonnet::internet_dom($id);
 5268:                 if ($location) {
 5269:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 5270:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5271:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 5272:                             push(@{$by_ip->{$ip}},$location);
 5273:                         }
 5274:                     } else {
 5275:                         $by_ip->{$ip} = [$location];
 5276:                     }
 5277:                 }
 5278:             }
 5279:         }
 5280:     }
 5281:     foreach my $ip (sort(keys(%{$by_ip}))) {
 5282:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5283:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 5284:             my $first = $by_ip->{$ip}->[0];
 5285:             if (ref($by_location->{$first}) eq 'ARRAY') {
 5286:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 5287:                     push(@{$by_location->{$first}},$ip);
 5288:                 }
 5289:             } else {
 5290:                 $by_location->{$first} = [$ip];
 5291:             }
 5292:         }
 5293:     }
 5294:     return;
 5295: }
 5296: 
 5297: sub current_offloads_to {
 5298:     my ($dom,$settings,$servers) = @_;
 5299:     my (%spareid,%otherdomconfigs);
 5300:     if (ref($servers) eq 'HASH') {
 5301:         foreach my $lonhost (sort(keys(%{$servers}))) {
 5302:             my $gotspares;
 5303:             if (ref($settings) eq 'HASH') {
 5304:                 if (ref($settings->{'spares'}) eq 'HASH') {
 5305:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 5306:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 5307:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 5308:                         $gotspares = 1;
 5309:                     }
 5310:                 }
 5311:             }
 5312:             unless ($gotspares) {
 5313:                 my $gotspares;
 5314:                 my $serverhomeID =
 5315:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 5316:                 my $serverhomedom =
 5317:                     &Apache::lonnet::host_domain($serverhomeID);
 5318:                 if ($serverhomedom ne $dom) {
 5319:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 5320:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5321:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5322:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5323:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5324:                                 $gotspares = 1;
 5325:                             }
 5326:                         }
 5327:                     } else {
 5328:                         $otherdomconfigs{$serverhomedom} =
 5329:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 5330:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 5331:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5332:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5333:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 5334:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5335:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5336:                                         $gotspares = 1;
 5337:                                     }
 5338:                                 }
 5339:                             }
 5340:                         }
 5341:                     }
 5342:                 }
 5343:             }
 5344:             unless ($gotspares) {
 5345:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5346:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5347:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5348:                } else {
 5349:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 5350:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 5351:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5352:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5353:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5354:                     } else {
 5355:                         my %what = (
 5356:                              spareid => 1,
 5357:                         );
 5358:                         my ($result,$returnhash) = 
 5359:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 5360:                         if ($result eq 'ok') { 
 5361:                             if (ref($returnhash) eq 'HASH') {
 5362:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 5363:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 5364:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 5365:                                 }
 5366:                             }
 5367:                         }
 5368:                     }
 5369:                 }
 5370:             }
 5371:         }
 5372:     }
 5373:     return %spareid;
 5374: }
 5375: 
 5376: sub spares_row {
 5377:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 5378:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 5379:     my $css_class;
 5380:     my $numinrow = 4;
 5381:     my $itemcount = 1;
 5382:     my $datatable;
 5383:     my %typetitles = &sparestype_titles();
 5384:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5385:         foreach my $server (sort(keys(%{$servers}))) {
 5386:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5387:             my ($othercontrol,$serverdom);
 5388:             if ($serverhome ne $server) {
 5389:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5390:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5391:             } else {
 5392:                 $serverdom = &Apache::lonnet::host_domain($server);
 5393:                 if ($serverdom ne $dom) {
 5394:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5395:                 }
 5396:             }
 5397:             next unless (ref($spareid->{$server}) eq 'HASH');
 5398:             my ($checkednow,$checkedoth);
 5399:             if (ref($curroffloadnow) eq 'HASH') {
 5400:                 if ($curroffloadnow->{$server}) {
 5401:                     $checkednow = ' checked="checked"';
 5402:                 }
 5403:             }
 5404:             if (ref($curroffloadoth) eq 'HASH') {
 5405:                 if ($curroffloadoth->{$server}) {
 5406:                     $checkedoth = ' checked="checked"';
 5407:                 }
 5408:             }
 5409:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5410:             $datatable .= '<tr'.$css_class.'>
 5411:                            <td rowspan="2">
 5412:                             <span class="LC_nobreak">'.
 5413:                           &mt('[_1] when busy, offloads to:'
 5414:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5415:                           '<span class="LC_nobreak">'."\n".
 5416:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5417:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 5418:                           "\n";
 5419:             if ($other_insts) {
 5420:                 $datatable .= '<br />'.
 5421:                               '<span class="LC_nobreak">'."\n".
 5422:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 5423:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 5424:                           "\n";
 5425:             }
 5426:             my (%current,%canselect);
 5427:             my @choices = 
 5428:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5429:             foreach my $type ('primary','default') {
 5430:                 if (ref($spareid->{$server}) eq 'HASH') {
 5431:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5432:                         my @spares = @{$spareid->{$server}{$type}};
 5433:                         if (@spares > 0) {
 5434:                             if ($othercontrol) {
 5435:                                 $current{$type} = join(', ',@spares);
 5436:                             } else {
 5437:                                 $current{$type} .= '<table>';
 5438:                                 my $numspares = scalar(@spares);
 5439:                                 for (my $i=0;  $i<@spares; $i++) {
 5440:                                     my $rem = $i%($numinrow);
 5441:                                     if ($rem == 0) {
 5442:                                         if ($i > 0) {
 5443:                                             $current{$type} .= '</tr>';
 5444:                                         }
 5445:                                         $current{$type} .= '<tr>';
 5446:                                     }
 5447:                                     $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;'.
 5448:                                                        $spareid->{$server}{$type}[$i].
 5449:                                                        '</label></td>'."\n";
 5450:                                 }
 5451:                                 my $rem = @spares%($numinrow);
 5452:                                 my $colsleft = $numinrow - $rem;
 5453:                                 if ($colsleft > 1 ) {
 5454:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5455:                                                        '" class="LC_left_item">'.
 5456:                                                        '&nbsp;</td>';
 5457:                                 } elsif ($colsleft == 1) {
 5458:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5459:                                 }
 5460:                                 $current{$type} .= '</tr></table>';
 5461:                             }
 5462:                         }
 5463:                     }
 5464:                     if ($current{$type} eq '') {
 5465:                         $current{$type} = &mt('None specified');
 5466:                     }
 5467:                     if ($othercontrol) {
 5468:                         if ($type eq 'primary') {
 5469:                             $canselect{$type} = $othercontrol;
 5470:                         }
 5471:                     } else {
 5472:                         $canselect{$type} = 
 5473:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5474:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5475:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5476:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5477:                         if (@choices > 0) {
 5478:                             foreach my $lonhost (@choices) {
 5479:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5480:                             }
 5481:                         }
 5482:                         $canselect{$type} .= '</select>'."\n";
 5483:                     }
 5484:                 } else {
 5485:                     $current{$type} = &mt('Could not be determined');
 5486:                     if ($type eq 'primary') {
 5487:                         $canselect{$type} =  $othercontrol;
 5488:                     }
 5489:                 }
 5490:                 if ($type eq 'default') {
 5491:                     $datatable .= '<tr'.$css_class.'>';
 5492:                 }
 5493:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5494:                               '<td>'.$current{$type}.'</td>'."\n".
 5495:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5496:             }
 5497:             $itemcount ++;
 5498:         }
 5499:     }
 5500:     $$rowtotal += $itemcount;
 5501:     return $datatable;
 5502: }
 5503: 
 5504: sub possible_newspares {
 5505:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5506:     my $serverhostname = &Apache::lonnet::hostname($server);
 5507:     my %excluded;
 5508:     if ($serverhostname ne '') {
 5509:         %excluded = (
 5510:                        $serverhostname => 1,
 5511:                     );
 5512:     }
 5513:     if (ref($currspares) eq 'HASH') {
 5514:         foreach my $type (keys(%{$currspares})) {
 5515:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5516:                 if (@{$currspares->{$type}} > 0) {
 5517:                     foreach my $curr (@{$currspares->{$type}}) {
 5518:                         my $hostname = &Apache::lonnet::hostname($curr);
 5519:                         $excluded{$hostname} = 1;
 5520:                     }
 5521:                 }
 5522:             }
 5523:         }
 5524:     }
 5525:     my @choices;
 5526:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5527:         if (keys(%{$serverhomes}) > 1) {
 5528:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5529:                 unless ($excluded{$name}) {
 5530:                     if (exists($altids->{$serverhomes->{$name}})) {
 5531:                         push(@choices,$altids->{$serverhomes->{$name}});
 5532:                     } else {
 5533:                         push(@choices,$serverhomes->{$name});
 5534:                     }
 5535:                 }
 5536:             }
 5537:         }
 5538:     }
 5539:     return sort(@choices);
 5540: }
 5541: 
 5542: sub print_loadbalancing {
 5543:     my ($dom,$settings,$rowtotal) = @_;
 5544:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5545:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5546:     my $numinrow = 1;
 5547:     my $datatable;
 5548:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5549:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 5550:     if (ref($settings) eq 'HASH') {
 5551:         %existing = %{$settings};
 5552:     }
 5553:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5554:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5555:                                   \%currtargets,\%currrules,\%currcookies);
 5556:     } else {
 5557:         return;
 5558:     }
 5559:     my ($othertitle,$usertypes,$types) =
 5560:         &Apache::loncommon::sorted_inst_types($dom);
 5561:     my $rownum = 8;
 5562:     if (ref($types) eq 'ARRAY') {
 5563:         $rownum += scalar(@{$types});
 5564:     }
 5565:     my @css_class = ('LC_odd_row','LC_even_row');
 5566:     my $balnum = 0;
 5567:     my $islast;
 5568:     my (@toshow,$disabledtext);
 5569:     if (keys(%currbalancer) > 0) {
 5570:         @toshow = sort(keys(%currbalancer));
 5571:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5572:             push(@toshow,'');
 5573:         }
 5574:     } else {
 5575:         @toshow = ('');
 5576:         $disabledtext = &mt('No existing load balancer');
 5577:     }
 5578:     foreach my $lonhost (@toshow) {
 5579:         if ($balnum == scalar(@toshow)-1) {
 5580:             $islast = 1;
 5581:         } else {
 5582:             $islast = 0;
 5583:         }
 5584:         my $cssidx = $balnum%2;
 5585:         my $targets_div_style = 'display: none';
 5586:         my $disabled_div_style = 'display: block';
 5587:         my $homedom_div_style = 'display: none';
 5588:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5589:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5590:                       '<p>';
 5591:         if ($lonhost eq '') {
 5592:             $datatable .= '<span class="LC_nobreak">';
 5593:             if (keys(%currbalancer) > 0) {
 5594:                 $datatable .= &mt('Add balancer:');
 5595:             } else {
 5596:                 $datatable .= &mt('Enable balancer:');
 5597:             }
 5598:             $datatable .= '&nbsp;'.
 5599:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5600:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5601:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5602:                           '<option value="" selected="selected">'.&mt('None').
 5603:                           '</option>'."\n";
 5604:             foreach my $server (sort(keys(%servers))) {
 5605:                 next if ($currbalancer{$server});
 5606:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5607:             }
 5608:             $datatable .=
 5609:                 '</select>'."\n".
 5610:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5611:         } else {
 5612:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5613:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5614:                            &mt('Stop balancing').'</label>'.
 5615:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5616:             $targets_div_style = 'display: block';
 5617:             $disabled_div_style = 'display: none';
 5618:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5619:                 $homedom_div_style = 'display: block';
 5620:             }
 5621:         }
 5622:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5623:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5624:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5625:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5626:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5627:         my @sparestypes = ('primary','default');
 5628:         my %typetitles = &sparestype_titles();
 5629:         my %hostherechecked = (
 5630:                                   no => ' checked="checked"',
 5631:                               );
 5632:         my %balcookiechecked = (
 5633:                                   no => ' checked="checked"',
 5634:                                );
 5635:         foreach my $sparetype (@sparestypes) {
 5636:             my $targettable;
 5637:             for (my $i=0; $i<$numspares; $i++) {
 5638:                 my $checked;
 5639:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5640:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5641:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5642:                             $checked = ' checked="checked"';
 5643:                         }
 5644:                     }
 5645:                 }
 5646:                 my ($chkboxval,$disabled);
 5647:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5648:                     $chkboxval = $spares[$i];
 5649:                 }
 5650:                 if (exists($currbalancer{$spares[$i]})) {
 5651:                     $disabled = ' disabled="disabled"';
 5652:                 }
 5653:                 $targettable .=
 5654:                     '<td><span class="LC_nobreak"><label>'.
 5655:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5656:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5657:                     '</span></label></span></td>';
 5658:                 my $rem = $i%($numinrow);
 5659:                 if ($rem == 0) {
 5660:                     if (($i > 0) && ($i < $numspares-1)) {
 5661:                         $targettable .= '</tr>';
 5662:                     }
 5663:                     if ($i < $numspares-1) {
 5664:                         $targettable .= '<tr>';
 5665:                     }
 5666:                 }
 5667:             }
 5668:             if ($targettable ne '') {
 5669:                 my $rem = $numspares%($numinrow);
 5670:                 my $colsleft = $numinrow - $rem;
 5671:                 if ($colsleft > 1 ) {
 5672:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5673:                                     '&nbsp;</td>';
 5674:                 } elsif ($colsleft == 1) {
 5675:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5676:                 }
 5677:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5678:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5679:             }
 5680:             $hostherechecked{$sparetype} = '';
 5681:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5682:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5683:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5684:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5685:                         $hostherechecked{'no'} = '';
 5686:                     }
 5687:                 }
 5688:             }
 5689:         }
 5690:         if ($currcookies{$lonhost}) {
 5691:             %balcookiechecked = (
 5692:                                     yes => ' checked="checked"',
 5693:                                 );
 5694:         }
 5695:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5696:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5697:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5698:         foreach my $sparetype (@sparestypes) {
 5699:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5700:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5701:                           '</i></label><br />';
 5702:         }
 5703:         $datatable .= &mt('Use balancer cookie').'<br />'.
 5704:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 5705:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 5706:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 5707:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 5708:                       '</div></td></tr>'.
 5709:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5710:                                            $othertitle,$usertypes,$types,\%servers,
 5711:                                            \%currbalancer,$lonhost,
 5712:                                            $targets_div_style,$homedom_div_style,
 5713:                                            $css_class[$cssidx],$balnum,$islast);
 5714:         $$rowtotal += $rownum;
 5715:         $balnum ++;
 5716:     }
 5717:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5718:     return $datatable;
 5719: }
 5720: 
 5721: sub get_loadbalancers_config {
 5722:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 5723:     return unless ((ref($servers) eq 'HASH') &&
 5724:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5725:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 5726:                    (ref($currcookies) eq 'HASH'));
 5727:     if (keys(%{$existing}) > 0) {
 5728:         my $oldlonhost;
 5729:         foreach my $key (sort(keys(%{$existing}))) {
 5730:             if ($key eq 'lonhost') {
 5731:                 $oldlonhost = $existing->{'lonhost'};
 5732:                 $currbalancer->{$oldlonhost} = 1;
 5733:             } elsif ($key eq 'targets') {
 5734:                 if ($oldlonhost) {
 5735:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5736:                 }
 5737:             } elsif ($key eq 'rules') {
 5738:                 if ($oldlonhost) {
 5739:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5740:                 }
 5741:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5742:                 $currbalancer->{$key} = 1;
 5743:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5744:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5745:                 if ($existing->{$key}{'cookie'}) {
 5746:                     $currcookies->{$key} = 1;
 5747:                 }
 5748:             }
 5749:         }
 5750:     } else {
 5751:         my ($balancerref,$targetsref) =
 5752:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5753:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5754:             foreach my $server (sort(keys(%{$balancerref}))) {
 5755:                 $currbalancer->{$server} = 1;
 5756:                 $currtargets->{$server} = $targetsref->{$server};
 5757:             }
 5758:         }
 5759:     }
 5760:     return;
 5761: }
 5762: 
 5763: sub loadbalancing_rules {
 5764:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5765:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5766:         $css_class,$balnum,$islast) = @_;
 5767:     my $output;
 5768:     my $num = 0;
 5769:     my ($alltypes,$othertypes,$titles) =
 5770:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5771:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5772:         foreach my $type (@{$alltypes}) {
 5773:             $num ++;
 5774:             my $current;
 5775:             if (ref($currrules) eq 'HASH') {
 5776:                 $current = $currrules->{$type};
 5777:             }
 5778:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5779:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5780:                     $current = '';
 5781:                 }
 5782:             }
 5783:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5784:                                              $servers,$currbalancer,$lonhost,$dom,
 5785:                                              $targets_div_style,$homedom_div_style,
 5786:                                              $css_class,$balnum,$num,$islast);
 5787:         }
 5788:     }
 5789:     return $output;
 5790: }
 5791: 
 5792: sub loadbalancing_titles {
 5793:     my ($dom,$intdom,$usertypes,$types) = @_;
 5794:     my %othertypes = (
 5795:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5796:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5797:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5798:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5799:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5800:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5801:                      );
 5802:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5803:     my @available;
 5804:     if (ref($types) eq 'ARRAY') {
 5805:         @available = @{$types};
 5806:     }
 5807:     unless (grep(/^default$/,@available)) {
 5808:         push(@available,'default');
 5809:     }
 5810:     unshift(@alltypes,@available);
 5811:     my %titles;
 5812:     foreach my $type (@alltypes) {
 5813:         if ($type =~ /^_LC_/) {
 5814:             $titles{$type} = $othertypes{$type};
 5815:         } elsif ($type eq 'default') {
 5816:             $titles{$type} = &mt('All users from [_1]',$dom);
 5817:             if (ref($types) eq 'ARRAY') {
 5818:                 if (@{$types} > 0) {
 5819:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5820:                 }
 5821:             }
 5822:         } elsif (ref($usertypes) eq 'HASH') {
 5823:             $titles{$type} = $usertypes->{$type};
 5824:         }
 5825:     }
 5826:     return (\@alltypes,\%othertypes,\%titles);
 5827: }
 5828: 
 5829: sub loadbalance_rule_row {
 5830:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5831:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5832:     my @rulenames;
 5833:     my %ruletitles = &offloadtype_text();
 5834:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5835:         @rulenames = ('balancer','offloadedto','specific');
 5836:     } else {
 5837:         @rulenames = ('default','homeserver');
 5838:         if ($type eq '_LC_external') {
 5839:             push(@rulenames,'externalbalancer');
 5840:         } else {
 5841:             push(@rulenames,'specific');
 5842:         }
 5843:         push(@rulenames,'none');
 5844:     }
 5845:     my $style = $targets_div_style;
 5846:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5847:         $style = $homedom_div_style;
 5848:     }
 5849:     my $space;
 5850:     if ($islast && $num == 1) {
 5851:         $space = '<div display="inline-block">&nbsp;</div>';
 5852:     }
 5853:     my $output =
 5854:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5855:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5856:         '<td valaign="top">'.$space.
 5857:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5858:     for (my $i=0; $i<@rulenames; $i++) {
 5859:         my $rule = $rulenames[$i];
 5860:         my ($checked,$extra);
 5861:         if ($rulenames[$i] eq 'default') {
 5862:             $rule = '';
 5863:         }
 5864:         if ($rulenames[$i] eq 'specific') {
 5865:             if (ref($servers) eq 'HASH') {
 5866:                 my $default;
 5867:                 if (($current ne '') && (exists($servers->{$current}))) {
 5868:                     $checked = ' checked="checked"';
 5869:                 }
 5870:                 unless ($checked) {
 5871:                     $default = ' selected="selected"';
 5872:                 }
 5873:                 $extra =
 5874:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5875:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5876:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5877:                     '<option value=""'.$default.'></option>'."\n";
 5878:                 foreach my $server (sort(keys(%{$servers}))) {
 5879:                     if (ref($currbalancer) eq 'HASH') {
 5880:                         next if (exists($currbalancer->{$server}));
 5881:                     }
 5882:                     my $selected;
 5883:                     if ($server eq $current) {
 5884:                         $selected = ' selected="selected"';
 5885:                     }
 5886:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5887:                 }
 5888:                 $extra .= '</select>';
 5889:             }
 5890:         } elsif ($rule eq $current) {
 5891:             $checked = ' checked="checked"';
 5892:         }
 5893:         $output .= '<span class="LC_nobreak"><label>'.
 5894:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5895:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5896:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5897:                    ')"'.$checked.' />&nbsp;';
 5898:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5899:             $output .= $ruletitles{'particular'};
 5900:         } else {
 5901:             $output .= $ruletitles{$rulenames[$i]};
 5902:         }
 5903:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5904:     }
 5905:     $output .= '</div></td></tr>'."\n";
 5906:     return $output;
 5907: }
 5908: 
 5909: sub offloadtype_text {
 5910:     my %ruletitles = &Apache::lonlocal::texthash (
 5911:            'default'          => 'Offloads to default destinations',
 5912:            'homeserver'       => "Offloads to user's home server",
 5913:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5914:            'specific'         => 'Offloads to specific server',
 5915:            'none'             => 'No offload',
 5916:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5917:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5918:            'particular'       => 'Session hosted (after re-auth) on server:',
 5919:     );
 5920:     return %ruletitles;
 5921: }
 5922: 
 5923: sub sparestype_titles {
 5924:     my %typestitles = &Apache::lonlocal::texthash (
 5925:                           'primary' => 'primary',
 5926:                           'default' => 'default',
 5927:                       );
 5928:     return %typestitles;
 5929: }
 5930: 
 5931: sub contact_titles {
 5932:     my %titles = &Apache::lonlocal::texthash (
 5933:                    'supportemail'    => 'Support E-mail address',
 5934:                    'adminemail'      => 'Default Server Admin E-mail address',
 5935:                    'errormail'       => 'Error reports to be e-mailed to',
 5936:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5937:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 5938:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 5939:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5940:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5941:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5942:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5943:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 5944:                  );
 5945:     my %short_titles = &Apache::lonlocal::texthash (
 5946:                            adminemail   => 'Admin E-mail address',
 5947:                            supportemail => 'Support E-mail',
 5948:                        );   
 5949:     return (\%titles,\%short_titles);
 5950: }
 5951: 
 5952: sub helpform_fields {
 5953:     my %titles =  &Apache::lonlocal::texthash (
 5954:                        'username'   => 'Name',
 5955:                        'user'       => 'Username/domain',
 5956:                        'phone'      => 'Phone',
 5957:                        'cc'         => 'Cc e-mail',
 5958:                        'course'     => 'Course Details',
 5959:                        'section'    => 'Sections',
 5960:                        'screenshot' => 'File upload',
 5961:     );
 5962:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5963:     my %possoptions = (
 5964:                         username     => ['yes','no','req'],
 5965:                         phone        => ['yes','no','req'],
 5966:                         user         => ['yes','no'],
 5967:                         cc           => ['yes','no'],
 5968:                         course       => ['yes','no'],
 5969:                         section      => ['yes','no'],
 5970:                         screenshot   => ['yes','no'],
 5971:                       );
 5972:     my %fieldoptions = &Apache::lonlocal::texthash (
 5973:                          'yes'  => 'Optional',
 5974:                          'req'  => 'Required',
 5975:                          'no'   => "Not shown",
 5976:     );
 5977:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5978: }
 5979: 
 5980: sub tool_titles {
 5981:     my %titles = &Apache::lonlocal::texthash (
 5982:                      aboutme    => 'Personal web page',
 5983:                      blog       => 'Blog',
 5984:                      webdav     => 'WebDAV',
 5985:                      portfolio  => 'Portfolio',
 5986:                      official   => 'Official courses (with institutional codes)',
 5987:                      unofficial => 'Unofficial courses',
 5988:                      community  => 'Communities',
 5989:                      textbook   => 'Textbook courses',
 5990:                  );
 5991:     return %titles;
 5992: }
 5993: 
 5994: sub courserequest_titles {
 5995:     my %titles = &Apache::lonlocal::texthash (
 5996:                                    official   => 'Official',
 5997:                                    unofficial => 'Unofficial',
 5998:                                    community  => 'Communities',
 5999:                                    textbook   => 'Textbook',
 6000:                                    norequest  => 'Not allowed',
 6001:                                    approval   => 'Approval by Dom. Coord.',
 6002:                                    validate   => 'With validation',
 6003:                                    autolimit  => 'Numerical limit',
 6004:                                    unlimited  => '(blank for unlimited)',
 6005:                  );
 6006:     return %titles;
 6007: }
 6008: 
 6009: sub authorrequest_titles {
 6010:     my %titles = &Apache::lonlocal::texthash (
 6011:                                    norequest  => 'Not allowed',
 6012:                                    approval   => 'Approval by Dom. Coord.',
 6013:                                    automatic  => 'Automatic approval',
 6014:                  );
 6015:     return %titles;
 6016: }
 6017: 
 6018: sub courserequest_conditions {
 6019:     my %conditions = &Apache::lonlocal::texthash (
 6020:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 6021:        validate   => '(Processing of request subject to institutional validation).',
 6022:                  );
 6023:     return %conditions;
 6024: }
 6025: 
 6026: 
 6027: sub print_usercreation {
 6028:     my ($position,$dom,$settings,$rowtotal) = @_;
 6029:     my $numinrow = 4;
 6030:     my $datatable;
 6031:     if ($position eq 'top') {
 6032:         $$rowtotal ++;
 6033:         my $rowcount = 0;
 6034:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 6035:         if (ref($rules) eq 'HASH') {
 6036:             if (keys(%{$rules}) > 0) {
 6037:                 $datatable .= &user_formats_row('username',$settings,$rules,
 6038:                                                 $ruleorder,$numinrow,$rowcount);
 6039:                 $$rowtotal ++;
 6040:                 $rowcount ++;
 6041:             }
 6042:         }
 6043:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 6044:         if (ref($idrules) eq 'HASH') {
 6045:             if (keys(%{$idrules}) > 0) {
 6046:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 6047:                                                 $idruleorder,$numinrow,$rowcount);
 6048:                 $$rowtotal ++;
 6049:                 $rowcount ++;
 6050:             }
 6051:         }
 6052:         if ($rowcount == 0) {
 6053:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 6054:             $$rowtotal ++;
 6055:             $rowcount ++;
 6056:         }
 6057:     } elsif ($position eq 'middle') {
 6058:         my @creators = ('author','course','requestcrs');
 6059:         my ($rules,$ruleorder) =
 6060:             &Apache::lonnet::inst_userrules($dom,'username');
 6061:         my %lt = &usercreation_types();
 6062:         my %checked;
 6063:         if (ref($settings) eq 'HASH') {
 6064:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 6065:                 foreach my $item (@creators) {
 6066:                     $checked{$item} = $settings->{'cancreate'}{$item};
 6067:                 }
 6068:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 6069:                 foreach my $item (@creators) {
 6070:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 6071:                         $checked{$item} = 'none';
 6072:                     }
 6073:                 }
 6074:             }
 6075:         }
 6076:         my $rownum = 0;
 6077:         foreach my $item (@creators) {
 6078:             $rownum ++;
 6079:             if ($checked{$item} eq '') {
 6080:                 $checked{$item} = 'any';
 6081:             }
 6082:             my $css_class;
 6083:             if ($rownum%2) {
 6084:                 $css_class = '';
 6085:             } else {
 6086:                 $css_class = ' class="LC_odd_row" ';
 6087:             }
 6088:             $datatable .= '<tr'.$css_class.'>'.
 6089:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 6090:                          '</span></td><td align="right">';
 6091:             my @options = ('any');
 6092:             if (ref($rules) eq 'HASH') {
 6093:                 if (keys(%{$rules}) > 0) {
 6094:                     push(@options,('official','unofficial'));
 6095:                 }
 6096:             }
 6097:             push(@options,'none');
 6098:             foreach my $option (@options) {
 6099:                 my $type = 'radio';
 6100:                 my $check = ' ';
 6101:                 if ($checked{$item} eq $option) {
 6102:                     $check = ' checked="checked" ';
 6103:                 } 
 6104:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6105:                               '<input type="'.$type.'" name="can_createuser_'.
 6106:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 6107:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 6108:             }
 6109:             $datatable .= '</td></tr>';
 6110:         }
 6111:     } else {
 6112:         my @contexts = ('author','course','domain');
 6113:         my @authtypes = ('int','krb4','krb5','loc');
 6114:         my %checked;
 6115:         if (ref($settings) eq 'HASH') {
 6116:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 6117:                 foreach my $item (@contexts) {
 6118:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 6119:                         foreach my $auth (@authtypes) {
 6120:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 6121:                                 $checked{$item}{$auth} = ' checked="checked" ';
 6122:                             }
 6123:                         }
 6124:                     }
 6125:                 }
 6126:             }
 6127:         } else {
 6128:             foreach my $item (@contexts) {
 6129:                 foreach my $auth (@authtypes) {
 6130:                     $checked{$item}{$auth} = ' checked="checked" ';
 6131:                 }
 6132:             }
 6133:         }
 6134:         my %title = &context_names();
 6135:         my %authname = &authtype_names();
 6136:         my $rownum = 0;
 6137:         my $css_class; 
 6138:         foreach my $item (@contexts) {
 6139:             if ($rownum%2) {
 6140:                 $css_class = '';
 6141:             } else {
 6142:                 $css_class = ' class="LC_odd_row" ';
 6143:             }
 6144:             $datatable .=   '<tr'.$css_class.'>'.
 6145:                             '<td>'.$title{$item}.
 6146:                             '</td><td class="LC_left_item">'.
 6147:                             '<span class="LC_nobreak">';
 6148:             foreach my $auth (@authtypes) {
 6149:                 $datatable .= '<label>'. 
 6150:                               '<input type="checkbox" name="'.$item.'_auth" '.
 6151:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 6152:                               $authname{$auth}.'</label>&nbsp;';
 6153:             }
 6154:             $datatable .= '</span></td></tr>';
 6155:             $rownum ++;
 6156:         }
 6157:         $$rowtotal += $rownum;
 6158:     }
 6159:     return $datatable;
 6160: }
 6161: 
 6162: sub print_selfcreation {
 6163:     my ($position,$dom,$settings,$rowtotal) = @_;
 6164:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 6165:         $emaildomain,$datatable);
 6166:     if (ref($settings) eq 'HASH') {
 6167:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 6168:             $createsettings = $settings->{'cancreate'};
 6169:             if (ref($createsettings) eq 'HASH') {
 6170:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 6171:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 6172:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 6173:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 6174:                         @selfcreate = ('email','login','sso');
 6175:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 6176:                         @selfcreate = ($createsettings->{'selfcreate'});
 6177:                     }
 6178:                 }
 6179:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 6180:                     $processing = $createsettings->{'selfcreateprocessing'};
 6181:                 }
 6182:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 6183:                     $emailoptions = $createsettings->{'emailoptions'};
 6184:                 }
 6185:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 6186:                     $emailverified = $createsettings->{'emailverified'};
 6187:                 }
 6188:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 6189:                     $emaildomain = $createsettings->{'emaildomain'};
 6190:                 }
 6191:             }
 6192:         }
 6193:     }
 6194:     my %radiohash;
 6195:     my $numinrow = 4;
 6196:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 6197:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6198:     if ($position eq 'top') {
 6199:         my %choices = &Apache::lonlocal::texthash (
 6200:                                                       cancreate_login      => 'Institutional Login',
 6201:                                                       cancreate_sso        => 'Institutional Single Sign On',
 6202:                                                   );
 6203:         my @toggles = sort(keys(%choices));
 6204:         my %defaultchecked = (
 6205:                                'cancreate_login' => 'off',
 6206:                                'cancreate_sso'   => 'off',
 6207:                              );
 6208:         my ($onclick,$itemcount);
 6209:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6210:                                                      \%choices,$itemcount,$onclick);
 6211:         $$rowtotal += $itemcount;
 6212: 
 6213:         if (ref($usertypes) eq 'HASH') {
 6214:             if (keys(%{$usertypes}) > 0) {
 6215:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 6216:                                              $dom,$numinrow,$othertitle,
 6217:                                              'statustocreate',$rowtotal);
 6218:                 $$rowtotal ++;
 6219:             }
 6220:         }
 6221:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 6222:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6223:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 6224:         my $rem;
 6225:         my $numperrow = 2;
 6226:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 6227:         $datatable .= '<tr'.$css_class.'>'.
 6228:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 6229:                      '<td class="LC_left_item">'."\n".
 6230:                      '<table>'."\n";
 6231:         for (my $i=0; $i<@fields; $i++) {
 6232:             $rem = $i%($numperrow);
 6233:             if ($rem == 0) {
 6234:                 if ($i > 0) {
 6235:                     $datatable .= '</tr>';
 6236:                 }
 6237:                 $datatable .= '<tr>';
 6238:             }
 6239:             my $currval;
 6240:             if (ref($createsettings) eq 'HASH') {
 6241:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 6242:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 6243:                 }
 6244:             }
 6245:             $datatable .= '<td class="LC_left_item">'.
 6246:                           '<span class="LC_nobreak">'.
 6247:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 6248:                           'value="'.$currval.'" size="10" />&nbsp;'.
 6249:                           $fieldtitles{$fields[$i]}.'</span></td>';
 6250:         }
 6251:         my $colsleft = $numperrow - $rem;
 6252:         if ($colsleft > 1 ) {
 6253:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6254:                          '&nbsp;</td>';
 6255:         } elsif ($colsleft == 1) {
 6256:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6257:         }
 6258:         $datatable .= '</tr></table></td></tr>';
 6259:         $$rowtotal ++;
 6260:     } elsif ($position eq 'middle') {
 6261:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 6262:         my @posstypes;
 6263:         if (ref($types) eq 'ARRAY') {
 6264:             @posstypes = @{$types};
 6265:         }
 6266:         unless (grep(/^default$/,@posstypes)) {
 6267:             push(@posstypes,'default');
 6268:         }
 6269:         my %usertypeshash;
 6270:         if (ref($usertypes) eq 'HASH') {
 6271:             %usertypeshash = %{$usertypes};
 6272:         }
 6273:         $usertypeshash{'default'} = $othertitle;
 6274:         foreach my $status (@posstypes) {
 6275:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 6276:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 6277:             $$rowtotal ++;
 6278:         }
 6279:     } else {
 6280:         my %choices = &Apache::lonlocal::texthash (
 6281:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 6282:                                                   );
 6283:         my @toggles = sort(keys(%choices));
 6284:         my %defaultchecked = (
 6285:                                'cancreate_email' => 'off',
 6286:                              );
 6287:         my $customclass = 'LC_selfcreate_email';
 6288:         my $classprefix = 'LC_canmodify_emailusername_';
 6289:         my $optionsprefix = 'LC_options_emailusername_';
 6290:         my $display = 'none';
 6291:         my $rowstyle = 'display:none';
 6292:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 6293:             $display = 'block';
 6294:             $rowstyle = 'display:table-row';
 6295:         }
 6296:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 6297:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6298:                                                      \%choices,$$rowtotal,$onclick);
 6299:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 6300:                                          $rowstyle);
 6301:         $$rowtotal ++;
 6302:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 6303:                                       $rowstyle);
 6304:         $$rowtotal ++;
 6305:         my (@ordered,@posstypes,%usertypeshash);
 6306:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6307:         my ($emailrules,$emailruleorder) =
 6308:             &Apache::lonnet::inst_userrules($dom,'email');
 6309:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6310:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6311:         if (ref($types) eq 'ARRAY') {
 6312:             @posstypes = @{$types};
 6313:         }
 6314:         if (@posstypes) {
 6315:             unless (grep(/^default$/,@posstypes)) {
 6316:                 push(@posstypes,'default');
 6317:             }
 6318:             if (ref($usertypes) eq 'HASH') {
 6319:                 %usertypeshash = %{$usertypes};
 6320:             }
 6321:             my $currassign;
 6322:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 6323:                 $currassign = {
 6324:                                   selfassign => $domdefaults{'inststatusguest'},
 6325:                               };
 6326:                 @ordered = @{$domdefaults{'inststatusguest'}};
 6327:             } else {
 6328:                 $currassign = { selfassign => [] };
 6329:             }
 6330:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 6331:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 6332:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 6333:                                          $numinrow,$othertitle,'selfassign',
 6334:                                          $rowtotal,$onclicktypes,$customclass,
 6335:                                          $rowstyle);
 6336:             $$rowtotal ++;
 6337:             $usertypeshash{'default'} = $othertitle;
 6338:             foreach my $status (@posstypes) {
 6339:                 my $css_class;
 6340:                 if ($$rowtotal%2) {
 6341:                     $css_class = 'LC_odd_row ';
 6342:                 }
 6343:                 $css_class .= $customclass;
 6344:                 my $rowid = $optionsprefix.$status;
 6345:                 my $hidden = 1;
 6346:                 my $currstyle = 'display:none';
 6347:                 if (grep(/^\Q$status\E$/,@ordered)) {
 6348:                     $currstyle = $rowstyle;
 6349:                     $hidden = 0;
 6350:                 }
 6351:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6352:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 6353:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 6354:                 unless ($hidden) {
 6355:                     $$rowtotal ++;
 6356:                 }
 6357:             }
 6358:         } else {
 6359:             my $css_class;
 6360:             if ($$rowtotal%2) {
 6361:                 $css_class = 'LC_odd_row ';
 6362:             }
 6363:             $css_class .= $customclass;
 6364:             $usertypeshash{'default'} = $othertitle;
 6365:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6366:                                          $emailrules,$emailruleorder,$settings,'default','',
 6367:                                          $othertitle,$css_class,$rowstyle,$intdom);
 6368:             $$rowtotal ++;
 6369:         }
 6370:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 6371:         $numinrow = 1;
 6372:         if (@posstypes) {
 6373:             foreach my $status (@posstypes) {
 6374:                 my $rowid = $classprefix.$status;
 6375:                 my $datarowstyle = 'display:none';
 6376:                 if (grep(/^\Q$status\E$/,@ordered)) {
 6377:                     $datarowstyle = $rowstyle;
 6378:                 }
 6379:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 6380:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6381:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 6382:                 unless ($datarowstyle eq 'display:none') {
 6383:                     $$rowtotal ++;
 6384:                 }
 6385:             }
 6386:         } else {
 6387:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 6388:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6389:                                                    $infotitles,'',$customclass,$rowstyle);
 6390:         }
 6391:     }
 6392:     return $datatable;
 6393: }
 6394: 
 6395: sub selfcreate_javascript {
 6396:     return <<"ENDSCRIPT";
 6397: 
 6398: <script type="text/javascript">
 6399: // <![CDATA[
 6400: 
 6401: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 6402:     var x = document.getElementsByClassName(target);
 6403:     var insttypes = 0;
 6404:     var insttypeRegExp = new RegExp(prefix);
 6405:     if ((x.length != undefined) && (x.length > 0)) {
 6406:         if (form.elements[radio].length != undefined) {
 6407:             for (var i=0; i<form.elements[radio].length; i++) {
 6408:                 if (form.elements[radio][i].checked) {
 6409:                     if (form.elements[radio][i].value == 1) {
 6410:                         for (var j=0; j<x.length; j++) {
 6411:                             if (x[j].id == 'undefined') {
 6412:                                 x[j].style.display = 'table-row';
 6413:                             } else if (insttypeRegExp.test(x[j].id)) {
 6414:                                 insttypes ++;
 6415:                             } else {
 6416:                                 x[j].style.display = 'table-row';
 6417:                             }
 6418:                         }
 6419:                     } else {
 6420:                         for (var j=0; j<x.length; j++) {
 6421:                             x[j].style.display = 'none';
 6422:                         }
 6423:                     }
 6424:                     break;
 6425:                 }
 6426:             }
 6427:             if (insttypes > 0) {
 6428:                 toggleDataRow(form,checkbox,target,altprefix);
 6429:                 toggleDataRow(form,checkbox,target,prefix,1);
 6430:             }
 6431:         }
 6432:     }
 6433:     return;
 6434: }
 6435: 
 6436: function toggleDataRow(form,checkbox,target,prefix,docount) {
 6437:     if (form.elements[checkbox].length != undefined) {
 6438:         var count = 0;
 6439:         if (docount) {
 6440:             for (var i=0; i<form.elements[checkbox].length; i++) {
 6441:                 if (form.elements[checkbox][i].checked) {
 6442:                     count ++;
 6443:                 }
 6444:             }
 6445:         }
 6446:         for (var i=0; i<form.elements[checkbox].length; i++) {
 6447:             var type = form.elements[checkbox][i].value;
 6448:             if (document.getElementById(prefix+type)) {
 6449:                 if (form.elements[checkbox][i].checked) {
 6450:                     document.getElementById(prefix+type).style.display = 'table-row';
 6451:                     if (count % 2 == 1) {
 6452:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 6453:                     } else {
 6454:                         document.getElementById(prefix+type).className = target;
 6455:                     }
 6456:                     count ++;
 6457:                 } else {
 6458:                     document.getElementById(prefix+type).style.display = 'none';
 6459:                 }
 6460:             }
 6461:         }
 6462:     }
 6463:     return;
 6464: }
 6465: 
 6466: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 6467:     var caller = radio+'_'+status;
 6468:     if (form.elements[caller].length != undefined) {
 6469:         for (var i=0; i<form.elements[caller].length; i++) {
 6470:             if (form.elements[caller][i].checked) {
 6471:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 6472:                     var curr = form.elements[caller][i].value;
 6473:                     if (prefix) {
 6474:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 6475:                     }
 6476:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 6477:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 6478:                     if (curr == 'custom') {
 6479:                         if (prefix) {
 6480:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 6481:                         }
 6482:                     } else if (curr == 'inst') {
 6483:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 6484:                     } else if (curr == 'noninst') {
 6485:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 6486:                     }
 6487:                     break;
 6488:                 }
 6489:             }
 6490:         }
 6491:     }
 6492: }
 6493: 
 6494: // ]]>
 6495: </script>
 6496: 
 6497: ENDSCRIPT
 6498: }
 6499: 
 6500: sub noninst_users {
 6501:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 6502:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 6503:     my $class = 'LC_left_item';
 6504:     if ($css_class) {
 6505:         $css_class = ' class="'.$css_class.'"';
 6506:     }
 6507:     if ($rowid) {
 6508:         $rowid = ' id="'.$rowid.'"';
 6509:     }
 6510:     if ($rowstyle) {
 6511:         $rowstyle = ' style="'.$rowstyle.'"';
 6512:     }
 6513:     my ($output,$description);
 6514:     if ($type eq 'default') {
 6515:         $description = &mt('Requests for: [_1]',$typetitle);
 6516:     } else {
 6517:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 6518:     }
 6519:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 6520:               "<td>$description</td>\n".
 6521:               '<td class="'.$class.'" colspan="2">'.
 6522:               '<table><tr>';
 6523:     my %headers = &Apache::lonlocal::texthash(
 6524:               approve  => 'Processing',
 6525:               email    => 'E-mail',
 6526:               username => 'Username',
 6527:     );
 6528:     foreach my $item ('approve','email','username') {
 6529:         $output .= '<th>'.$headers{$item}.'</th>';
 6530:     }
 6531:     $output .= '</tr><tr>';
 6532:     foreach my $item ('approve','email','username') {
 6533:         $output .= '<td valign="top">';
 6534:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 6535:         if ($item eq 'approve') {
 6536:             %choices = &Apache::lonlocal::texthash (
 6537:                                                      automatic => 'Automatically approved',
 6538:                                                      approval  => 'Queued for approval',
 6539:                                                    );
 6540:             @options = ('automatic','approval');
 6541:             $hashref = $processing;
 6542:             $defoption = 'automatic';
 6543:             $name = 'cancreate_emailprocess_'.$type;
 6544:         } elsif ($item eq 'email') {
 6545:             %choices = &Apache::lonlocal::texthash (
 6546:                                                      any     => 'Any e-mail',
 6547:                                                      inst    => 'Institutional only',
 6548:                                                      noninst => 'Non-institutional only',
 6549:                                                      custom  => 'Custom restrictions',
 6550:                                                    );
 6551:             @options = ('any','inst','noninst');
 6552:             my $showcustom;
 6553:             if (ref($emailrules) eq 'HASH') {
 6554:                 if (keys(%{$emailrules}) > 0) {
 6555:                     push(@options,'custom');
 6556:                     $showcustom = 'cancreate_emailrule';
 6557:                     if (ref($settings) eq 'HASH') {
 6558:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 6559:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 6560:                                 if (exists($emailrules->{$rule})) {
 6561:                                     $hascustom ++;
 6562:                                 }
 6563:                             }
 6564:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 6565:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 6566:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 6567:                                     if (exists($emailrules->{$rule})) {
 6568:                                         $hascustom ++;
 6569:                                     }
 6570:                                 }
 6571:                             }
 6572:                         }
 6573:                     }
 6574:                 }
 6575:             }
 6576:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 6577:                                                      "'cancreate_emaildomain','$type'".');"';
 6578:             $hashref = $emailoptions;
 6579:             $defoption = 'any';
 6580:             $name = 'cancreate_emailoptions_'.$type;
 6581:         } elsif ($item eq 'username') {
 6582:             %choices = &Apache::lonlocal::texthash (
 6583:                                                      all    => 'Same as e-mail',
 6584:                                                      first  => 'Omit @domain',
 6585:                                                      free   => 'Free to choose',
 6586:                                                    );
 6587:             @options = ('all','first','free');
 6588:             $hashref = $emailverified;
 6589:             $defoption = 'all';
 6590:             $name = 'cancreate_usernameoptions_'.$type;
 6591:         }
 6592:         foreach my $option (@options) {
 6593:             my $checked;
 6594:             if (ref($hashref) eq 'HASH') {
 6595:                 if ($type eq '') {
 6596:                     if (!exists($hashref->{'default'})) {
 6597:                         if ($option eq $defoption) {
 6598:                             $checked = ' checked="checked"';
 6599:                         }
 6600:                     } else {
 6601:                         if ($hashref->{'default'} eq $option) {
 6602:                             $checked = ' checked="checked"';
 6603:                         }
 6604:                     }
 6605:                 } else {
 6606:                     if (!exists($hashref->{$type})) {
 6607:                         if ($option eq $defoption) {
 6608:                             $checked = ' checked="checked"';
 6609:                         }
 6610:                     } else {
 6611:                         if ($hashref->{$type} eq $option) {
 6612:                             $checked = ' checked="checked"';
 6613:                         }
 6614:                     }
 6615:                 }
 6616:             } elsif (($item eq 'email') && ($hascustom)) {
 6617:                 if ($option eq 'custom') {
 6618:                     $checked = ' checked="checked"';
 6619:                 }
 6620:             } elsif ($option eq $defoption) {
 6621:                 $checked = ' checked="checked"';
 6622:             }
 6623:             $output .= '<span class="LC_nobreak"><label>'.
 6624:                        '<input type="radio" name="'.$name.'"'.
 6625:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 6626:                        $choices{$option}.'</label></span><br />';
 6627:             if ($item eq 'email') {
 6628:                 if ($option eq 'custom') {
 6629:                     my $id = 'cancreate_emailrule_'.$type;
 6630:                     my $display = 'none';
 6631:                     if ($checked) {
 6632:                         $display = 'inline';
 6633:                     }
 6634:                     my $numinrow = 2;
 6635:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 6636:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 6637:                                &user_formats_row('email',$settings,$emailrules,
 6638:                                                  $emailruleorder,$numinrow,'',$type);
 6639:                               '</table></fieldset>';
 6640:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 6641:                     my %text = &Apache::lonlocal::texthash (
 6642:                                                              inst    => 'must end:',
 6643:                                                              noninst => 'cannot end:',
 6644:                                                            );
 6645:                     my $value;
 6646:                     if (ref($emaildomain) eq 'HASH') {
 6647:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 6648:                             $value = $emaildomain->{$type}->{$option};
 6649:                         }
 6650:                     }
 6651:                     if ($value eq '') {
 6652:                         $value = '@'.$intdom;
 6653:                     }
 6654:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 6655:                     my $display = 'none';
 6656:                     if ($checked) {
 6657:                         $display = 'inline';
 6658:                     }
 6659:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 6660:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 6661:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 6662:                                '</div>';
 6663:                 }
 6664:             }
 6665:         }
 6666:         $output .= '</td>'."\n";
 6667:     }
 6668:     $output .= "</tr></table></td></tr>\n";
 6669:     return $output;
 6670: }
 6671: 
 6672: sub captcha_choice {
 6673:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 6674:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 6675:         $vertext,$currver); 
 6676:     my %lt = &captcha_phrases();
 6677:     $keyentry = 'hidden';
 6678:     my $colspan=2;
 6679:     if ($context eq 'cancreate') {
 6680:         $rowname = &mt('CAPTCHA validation');
 6681:     } elsif ($context eq 'login') {
 6682:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 6683:     } elsif ($context eq 'passwords') {
 6684:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 6685:         $colspan=1;
 6686:     }
 6687:     if (ref($settings) eq 'HASH') {
 6688:         if ($settings->{'captcha'}) {
 6689:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6690:         } else {
 6691:             $checked{'original'} = ' checked="checked"';
 6692:         }
 6693:         if ($settings->{'captcha'} eq 'recaptcha') {
 6694:             $pubtext = $lt{'pub'};
 6695:             $privtext = $lt{'priv'};
 6696:             $keyentry = 'text';
 6697:             $vertext = $lt{'ver'};
 6698:             $currver = $settings->{'recaptchaversion'};
 6699:             if ($currver ne '2') {
 6700:                 $currver = 1;
 6701:             }
 6702:         }
 6703:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6704:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6705:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6706:         }
 6707:     } else {
 6708:         $checked{'original'} = ' checked="checked"';
 6709:     }
 6710:     my $css_class;
 6711:     if ($itemcount%2) {
 6712:         $css_class = 'LC_odd_row';
 6713:     }
 6714:     if ($customcss) {
 6715:         $css_class .= " $customcss";
 6716:     }
 6717:     $css_class =~ s/^\s+//;
 6718:     if ($css_class) {
 6719:         $css_class = ' class="'.$css_class.'"';
 6720:     }
 6721:     if ($rowstyle) {
 6722:         $css_class .= ' style="'.$rowstyle.'"';
 6723:     }
 6724:     my $output = '<tr'.$css_class.'>'.
 6725:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 6726:                  '<table><tr><td>'."\n";
 6727:     foreach my $option ('original','recaptcha','notused') {
 6728:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6729:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6730:                    $lt{$option}.'</label></span>';
 6731:         unless ($option eq 'notused') {
 6732:             $output .= ('&nbsp;'x2)."\n";
 6733:         }
 6734:     }
 6735: #
 6736: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6737: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6738: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6739: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6740: #
 6741:     $output .= '</td></tr>'."\n".
 6742:                '<tr><td class="LC_zero_height">'."\n".
 6743:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6744:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6745:                $currpub.'" size="40" /></span><br />'."\n".
 6746:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6747:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6748:                $currpriv.'" size="40" /></span><br />'.
 6749:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6750:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6751:                $currver.'" size="3" /></span><br />'.
 6752:                '</td></tr></table>'."\n".
 6753:                '</td></tr>';
 6754:     return $output;
 6755: }
 6756: 
 6757: sub user_formats_row {
 6758:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 6759:     my $output;
 6760:     my %text = (
 6761:                    'username' => 'new usernames',
 6762:                    'id'       => 'IDs',
 6763:                );
 6764:     unless ($type eq 'email') {
 6765:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6766:         $output = '<tr '.$css_class.'>'.
 6767:                   '<td><span class="LC_nobreak">'.
 6768:                   &mt("Format rules to check for $text{$type}: ").
 6769:                   '</td><td class="LC_left_item" colspan="2"><table>';
 6770:     }
 6771:     my $rem;
 6772:     if (ref($ruleorder) eq 'ARRAY') {
 6773:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6774:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6775:                 my $rem = $i%($numinrow);
 6776:                 if ($rem == 0) {
 6777:                     if ($i > 0) {
 6778:                         $output .= '</tr>';
 6779:                     }
 6780:                     $output .= '<tr>';
 6781:                 }
 6782:                 my $check = ' ';
 6783:                 if (ref($settings) eq 'HASH') {
 6784:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6785:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6786:                             $check = ' checked="checked" ';
 6787:                         }
 6788:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 6789:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 6790:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 6791:                                 $check = ' checked="checked" ';
 6792:                             }
 6793:                         }
 6794:                     }
 6795:                 }
 6796:                 my $name = $type.'_rule';
 6797:                 if ($type eq 'email') {
 6798:                     $name .= '_'.$status;
 6799:                 }
 6800:                 $output .= '<td class="LC_left_item">'.
 6801:                            '<span class="LC_nobreak"><label>'.
 6802:                            '<input type="checkbox" name="'.$name.'" '.
 6803:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6804:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6805:             }
 6806:         }
 6807:         $rem = @{$ruleorder}%($numinrow);
 6808:     }
 6809:     my $colsleft;
 6810:     if ($rem) {
 6811:         $colsleft = $numinrow - $rem;
 6812:     }
 6813:     if ($colsleft > 1 ) {
 6814:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6815:                    '&nbsp;</td>';
 6816:     } elsif ($colsleft == 1) {
 6817:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6818:     }
 6819:     $output .= '</tr></table>';
 6820:     unless ($type eq 'email') {
 6821:         $output .= '</td></tr>';
 6822:     }
 6823:     return $output;
 6824: }
 6825: 
 6826: sub usercreation_types {
 6827:     my %lt = &Apache::lonlocal::texthash (
 6828:                     author     => 'When adding a co-author',
 6829:                     course     => 'When adding a user to a course',
 6830:                     requestcrs => 'When requesting a course',
 6831:                     any        => 'Any',
 6832:                     official   => 'Institutional only ',
 6833:                     unofficial => 'Non-institutional only',
 6834:                     none       => 'None',
 6835:     );
 6836:     return %lt;
 6837: }
 6838: 
 6839: sub selfcreation_types {
 6840:     my %lt = &Apache::lonlocal::texthash (
 6841:                     selfcreate => 'User creates own account',
 6842:                     any        => 'Any',
 6843:                     official   => 'Institutional only ',
 6844:                     unofficial => 'Non-institutional only',
 6845:                     email      => 'E-mail address',
 6846:                     login      => 'Institutional Login',
 6847:                     sso        => 'SSO',
 6848:              );
 6849: }
 6850: 
 6851: sub authtype_names {
 6852:     my %lt = &Apache::lonlocal::texthash(
 6853:                       int    => 'Internal',
 6854:                       krb4   => 'Kerberos 4',
 6855:                       krb5   => 'Kerberos 5',
 6856:                       loc    => 'Local',
 6857:                   );
 6858:     return %lt;
 6859: }
 6860: 
 6861: sub context_names {
 6862:     my %context_title = &Apache::lonlocal::texthash(
 6863:        author => 'Creating users when an Author',
 6864:        course => 'Creating users when in a course',
 6865:        domain => 'Creating users when a Domain Coordinator',
 6866:     );
 6867:     return %context_title;
 6868: }
 6869: 
 6870: sub print_usermodification {
 6871:     my ($position,$dom,$settings,$rowtotal) = @_;
 6872:     my $numinrow = 4;
 6873:     my ($context,$datatable,$rowcount);
 6874:     if ($position eq 'top') {
 6875:         $rowcount = 0;
 6876:         $context = 'author'; 
 6877:         foreach my $role ('ca','aa') {
 6878:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6879:                                                    $numinrow,$rowcount);
 6880:             $$rowtotal ++;
 6881:             $rowcount ++;
 6882:         }
 6883:     } elsif ($position eq 'bottom') {
 6884:         $context = 'course';
 6885:         $rowcount = 0;
 6886:         foreach my $role ('st','ep','ta','in','cr') {
 6887:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6888:                                                    $numinrow,$rowcount);
 6889:             $$rowtotal ++;
 6890:             $rowcount ++;
 6891:         }
 6892:     }
 6893:     return $datatable;
 6894: }
 6895: 
 6896: sub print_defaults {
 6897:     my ($position,$dom,$settings,$rowtotal) = @_;
 6898:     my $rownum = 0;
 6899:     my ($datatable,$css_class,$titles);
 6900:     unless ($position eq 'bottom') {
 6901:         $titles = &defaults_titles($dom);
 6902:     }
 6903:     if ($position eq 'top') {
 6904:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6905:                      'datelocale_def','portal_def');
 6906:         my %defaults;
 6907:         if (ref($settings) eq 'HASH') {
 6908:             %defaults = %{$settings};
 6909:         } else {
 6910:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6911:             foreach my $item (@items) {
 6912:                 $defaults{$item} = $domdefaults{$item};
 6913:             }
 6914:         }
 6915:         foreach my $item (@items) {
 6916:             if ($rownum%2) {
 6917:                 $css_class = '';
 6918:             } else {
 6919:                 $css_class = ' class="LC_odd_row" ';
 6920:             }
 6921:             $datatable .= '<tr'.$css_class.'>'.
 6922:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6923:                           '</span></td><td class="LC_right_item" colspan="3">';
 6924:             if ($item eq 'auth_def') {
 6925:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6926:                 my %shortauth = (
 6927:                                  internal => 'int',
 6928:                                  krb4 => 'krb4',
 6929:                                  krb5 => 'krb5',
 6930:                                  localauth  => 'loc'
 6931:                                 );
 6932:                 my %authnames = &authtype_names();
 6933:                 foreach my $auth (@authtypes) {
 6934:                     my $checked = ' ';
 6935:                     if ($defaults{$item} eq $auth) {
 6936:                         $checked = ' checked="checked" ';
 6937:                     }
 6938:                     $datatable .= '<label><input type="radio" name="'.$item.
 6939:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6940:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6941:                 }
 6942:             } elsif ($item eq 'timezone_def') {
 6943:                 my $includeempty = 1;
 6944:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6945:             } elsif ($item eq 'datelocale_def') {
 6946:                 my $includeempty = 1;
 6947:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6948:             } elsif ($item eq 'lang_def') {
 6949:                 my $includeempty = 1;
 6950:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6951:             } else {
 6952:                 my $size;
 6953:                 if ($item eq 'portal_def') {
 6954:                     $size = ' size="25"';
 6955:                 }
 6956:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6957:                               $defaults{$item}.'"'.$size.' />';
 6958:             }
 6959:             $datatable .= '</td></tr>';
 6960:             $rownum ++;
 6961:         }
 6962:     } else {
 6963:         my %defaults;
 6964:         if (ref($settings) eq 'HASH') {
 6965:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6966:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6967:                 for (my $i=0; $i<$maxnum; $i++) {
 6968:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6969:                     my $item = $settings->{'inststatusorder'}->[$i];
 6970:                     my $title = $settings->{'inststatustypes'}->{$item};
 6971:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6972:                     $datatable .= '<tr'.$css_class.'>'.
 6973:                                   '<td><span class="LC_nobreak">'.
 6974:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6975:                     for (my $k=0; $k<=$maxnum; $k++) {
 6976:                         my $vpos = $k+1;
 6977:                         my $selstr;
 6978:                         if ($k == $i) {
 6979:                             $selstr = ' selected="selected" ';
 6980:                         }
 6981:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6982:                     }
 6983:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6984:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6985:                                   &mt('delete').'</span></td>'.
 6986:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6987:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6988:                                   '</span></td></tr>';
 6989:                 }
 6990:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6991:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6992:                 $datatable .= '<tr '.$css_class.'>'.
 6993:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6994:                 for (my $k=0; $k<=$maxnum; $k++) {
 6995:                     my $vpos = $k+1;
 6996:                     my $selstr;
 6997:                     if ($k == $maxnum) {
 6998:                         $selstr = ' selected="selected" ';
 6999:                     }
 7000:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7001:                 }
 7002:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 7003:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 7004:                               '&nbsp;'.&mt('(new)').
 7005:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 7006:                               &mt('Name displayed:').
 7007:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 7008:                               '</tr>'."\n";
 7009:                 $rownum ++;
 7010:             }
 7011:         }
 7012:     }
 7013:     $$rowtotal += $rownum;
 7014:     return $datatable;
 7015: }
 7016: 
 7017: sub get_languages_hash {
 7018:     my %langchoices;
 7019:     foreach my $id (&Apache::loncommon::languageids()) {
 7020:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 7021:         if ($code ne '') {
 7022:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 7023:         }
 7024:     }
 7025:     return %langchoices;
 7026: }
 7027: 
 7028: sub defaults_titles {
 7029:     my ($dom) = @_;
 7030:     my %titles = &Apache::lonlocal::texthash (
 7031:                    'auth_def'      => 'Default authentication type',
 7032:                    'auth_arg_def'  => 'Default authentication argument',
 7033:                    'lang_def'      => 'Default language',
 7034:                    'timezone_def'  => 'Default timezone',
 7035:                    'datelocale_def' => 'Default locale for dates',
 7036:                    'portal_def'     => 'Portal/Default URL',
 7037:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 7038:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 7039:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 7040:                  );
 7041:     if ($dom) {
 7042:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 7043:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 7044:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 7045:         $protocol = 'http' if ($protocol ne 'https');
 7046:         if ($uint_dom) {
 7047:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 7048:                                          $uint_dom);
 7049:         }
 7050:     }
 7051:     return (\%titles);
 7052: }
 7053: 
 7054: sub print_scantron {
 7055:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 7056:     if ($position eq 'top') {
 7057:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 7058:     } else {
 7059:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 7060:     }
 7061: }
 7062: 
 7063: sub scantron_javascript {
 7064:     return <<"ENDSCRIPT";
 7065: 
 7066: <script type="text/javascript">
 7067: // <![CDATA[
 7068: 
 7069: function toggleScantron(form) {
 7070:     var csvfieldset = new Array();
 7071:     if (document.getElementById('scantroncsv_cols')) {
 7072:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 7073:     }
 7074:     if (document.getElementById('scantroncsv_options')) {
 7075:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 7076:     }
 7077:     if (csvfieldset.length) {
 7078:         if (document.getElementById('scantronconfcsv')) {
 7079:             var scantroncsv = document.getElementById('scantronconfcsv');
 7080:             if (scantroncsv.checked) {
 7081:                 for (var i=0; i<csvfieldset.length; i++) {
 7082:                     csvfieldset[i].style.display = 'block';
 7083:                 }
 7084:             } else {
 7085:                 for (var i=0; i<csvfieldset.length; i++) {
 7086:                     csvfieldset[i].style.display = 'none';
 7087:                 }
 7088:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 7089:                 if (csvselects.length) {
 7090:                     for (var j=0; j<csvselects.length; j++) {
 7091:                         csvselects[j].selectedIndex = 0;
 7092:                     }
 7093:                 }
 7094:             }
 7095:         }
 7096:     }
 7097:     return;
 7098: }
 7099: // ]]>
 7100: </script>
 7101: 
 7102: ENDSCRIPT
 7103: 
 7104: }
 7105: 
 7106: sub print_scantronformat {
 7107:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 7108:     my $itemcount = 1;
 7109:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 7110:         %confhash);
 7111:     my $switchserver = &check_switchserver($dom,$confname);
 7112:     my %lt = &Apache::lonlocal::texthash (
 7113:                 default => 'Default bubblesheet format file error',
 7114:                 custom  => 'Custom bubblesheet format file error',
 7115:              );
 7116:     my %scantronfiles = (
 7117:         default => 'default.tab',
 7118:         custom => 'custom.tab',
 7119:     );
 7120:     foreach my $key (keys(%scantronfiles)) {
 7121:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 7122:                               .$scantronfiles{$key};
 7123:     }
 7124:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 7125:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 7126:         if (!$switchserver) {
 7127:             my $servadm = $r->dir_config('lonAdmEMail');
 7128:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 7129:             if ($configuserok eq 'ok') {
 7130:                 if ($author_ok eq 'ok') {
 7131:                     my %legacyfile = (
 7132:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 7133:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 7134:                     );
 7135:                     my %md5chk;
 7136:                     foreach my $type (keys(%legacyfile)) {
 7137:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 7138:                         chomp($md5chk{$type});
 7139:                     }
 7140:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 7141:                         foreach my $type (keys(%legacyfile)) {
 7142:                             ($scantronurls{$type},my $error) =
 7143:                                 &legacy_scantronformat($r,$dom,$confname,
 7144:                                                  $type,$legacyfile{$type},
 7145:                                                  $scantronurls{$type},
 7146:                                                  $scantronfiles{$type});
 7147:                             if ($error ne '') {
 7148:                                 $error{$type} = $error;
 7149:                             }
 7150:                         }
 7151:                         if (keys(%error) == 0) {
 7152:                             $is_custom = 1;
 7153:                             $confhash{'scantron'}{'scantronformat'} =
 7154:                                 $scantronurls{'custom'};
 7155:                             my $putresult =
 7156:                                 &Apache::lonnet::put_dom('configuration',
 7157:                                                          \%confhash,$dom);
 7158:                             if ($putresult ne 'ok') {
 7159:                                 $error{'custom'} =
 7160:                                     '<span class="LC_error">'.
 7161:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7162:                             }
 7163:                         }
 7164:                     } else {
 7165:                         ($scantronurls{'default'},my $error) =
 7166:                             &legacy_scantronformat($r,$dom,$confname,
 7167:                                           'default',$legacyfile{'default'},
 7168:                                           $scantronurls{'default'},
 7169:                                           $scantronfiles{'default'});
 7170:                         if ($error eq '') {
 7171:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 7172:                             my $putresult =
 7173:                                 &Apache::lonnet::put_dom('configuration',
 7174:                                                          \%confhash,$dom);
 7175:                             if ($putresult ne 'ok') {
 7176:                                 $error{'default'} =
 7177:                                     '<span class="LC_error">'.
 7178:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7179:                             }
 7180:                         } else {
 7181:                             $error{'default'} = $error;
 7182:                         }
 7183:                     }
 7184:                 }
 7185:             }
 7186:         } else {
 7187:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 7188:         }
 7189:     }
 7190:     if (ref($settings) eq 'HASH') {
 7191:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 7192:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 7193:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 7194:                 $scantronurl = '';
 7195:             } else {
 7196:                 $scantronurl = $settings->{'scantronformat'};
 7197:             }
 7198:             $is_custom = 1;
 7199:         } else {
 7200:             $scantronurl = $scantronurls{'default'};
 7201:         }
 7202:     } else {
 7203:         if ($is_custom) {
 7204:             $scantronurl = $scantronurls{'custom'};
 7205:         } else {
 7206:             $scantronurl = $scantronurls{'default'};
 7207:         }
 7208:     }
 7209:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7210:     $datatable .= '<tr'.$css_class.'>';
 7211:     if (!$is_custom) {
 7212:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 7213:                       '<span class="LC_nobreak">';
 7214:         if ($scantronurl) {
 7215:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 7216:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 7217:         } else {
 7218:             $datatable = &mt('File unavailable for display');
 7219:         }
 7220:         $datatable .= '</span></td>';
 7221:         if (keys(%error) == 0) { 
 7222:             $datatable .= '<td valign="bottom">';
 7223:             if (!$switchserver) {
 7224:                 $datatable .= &mt('Upload:').'<br />';
 7225:             }
 7226:         } else {
 7227:             my $errorstr;
 7228:             foreach my $key (sort(keys(%error))) {
 7229:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7230:             }
 7231:             $datatable .= '<td>'.$errorstr;
 7232:         }
 7233:     } else {
 7234:         if (keys(%error) > 0) {
 7235:             my $errorstr;
 7236:             foreach my $key (sort(keys(%error))) {
 7237:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7238:             } 
 7239:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 7240:         } elsif ($scantronurl) {
 7241:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 7242:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7243:             $datatable .= '<td><span class="LC_nobreak">'.
 7244:                           $link.
 7245:                           '<label><input type="checkbox" name="scantronformat_del"'.
 7246:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 7247:                           '<td><span class="LC_nobreak">&nbsp;'.
 7248:                           &mt('Replace:').'</span><br />';
 7249:         }
 7250:     }
 7251:     if (keys(%error) == 0) {
 7252:         if ($switchserver) {
 7253:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 7254:         } else {
 7255:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7256:                          '<input type="file" name="scantronformat" /></span>';
 7257:         }
 7258:     }
 7259:     $datatable .= '</td></tr>';
 7260:     $$rowtotal ++;
 7261:     return $datatable;
 7262: }
 7263: 
 7264: sub legacy_scantronformat {
 7265:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 7266:     my ($url,$error);
 7267:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 7268:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 7269:         (my $result,$url) =
 7270:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 7271:                          '','',$newfile);
 7272:         if ($result ne 'ok') {
 7273:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 7274:         }
 7275:     }
 7276:     return ($url,$error);
 7277: }
 7278: 
 7279: sub print_scantronconfig {
 7280:     my ($dom,$settings,$rowtotal) = @_;
 7281:     my $itemcount = 2;
 7282:     my $is_checked = ' checked="checked"';
 7283:     my %optionson = (
 7284:                      hdr => ' checked="checked"',
 7285:                      pad => ' checked="checked"',
 7286:                      rem => ' checked="checked"',
 7287:                     );
 7288:     my %optionsoff = (
 7289:                       hdr => '',
 7290:                       pad => '',
 7291:                       rem => '',
 7292:                      );
 7293:     my $currcsvsty = 'none';
 7294:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 7295:     my @fields = &scantroncsv_fields();
 7296:     my %titles = &scantronconfig_titles();
 7297:     if (ref($settings) eq 'HASH') {
 7298:         if (ref($settings->{config}) eq 'HASH') {
 7299:             if ($settings->{config}->{dat}) {
 7300:                 $checked{'dat'} = $is_checked;
 7301:             }
 7302:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 7303:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 7304:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 7305:                     if (keys(%csvfields) > 0) {
 7306:                         $checked{'csv'} = $is_checked;
 7307:                         $currcsvsty = 'block';
 7308:                     }
 7309:                 }
 7310:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 7311:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 7312:                     foreach my $option (keys(%optionson)) {
 7313:                         unless ($csvoptions{$option}) {
 7314:                             $optionsoff{$option} = $optionson{$option};
 7315:                             $optionson{$option} = '';
 7316:                         }
 7317:                     }
 7318:                 }
 7319:             }
 7320:         } else {
 7321:             $checked{'dat'} = $is_checked;
 7322:         }
 7323:     } else {
 7324:         $checked{'dat'} = $is_checked;
 7325:     }
 7326:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 7327:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 7328:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 7329:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 7330:     foreach my $item ('dat','csv') {
 7331:         my $id;
 7332:         if ($item eq 'csv') {
 7333:             $id = 'id="scantronconfcsv" ';
 7334:         }
 7335:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 7336:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 7337:         if ($item eq 'csv') {
 7338:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 7339:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 7340:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 7341:             foreach my $col (@fields) {
 7342:                 my $selnone;
 7343:                 if ($csvfields{$col} eq '') {
 7344:                     $selnone = ' selected="selected"';
 7345:                 }
 7346:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 7347:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 7348:                               '<option value=""'.$selnone.'></option>';
 7349:                 for (my $i=0; $i<20; $i++) {
 7350:                     my $shown = $i+1;
 7351:                     my $sel;
 7352:                     unless ($selnone) {
 7353:                         if (exists($csvfields{$col})) {
 7354:                             if ($csvfields{$col} == $i) {
 7355:                                 $sel = ' selected="selected"';
 7356:                             }
 7357:                         }
 7358:                     }
 7359:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 7360:                 }
 7361:                 $datatable .= '</select></td></tr>';
 7362:            }
 7363:            $datatable .= '</table></fieldset>'.
 7364:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 7365:                          '<legend>'.&mt('CSV Options').'</legend>';
 7366:            foreach my $option ('hdr','pad','rem') {
 7367:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 7368:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 7369:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 7370:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 7371:            }
 7372:            $datatable .= '</fieldset>';
 7373:            $itemcount ++;
 7374:         }
 7375:     }
 7376:     $datatable .= '</td></tr>';
 7377:     $$rowtotal ++;
 7378:     return $datatable;
 7379: }
 7380: 
 7381: sub scantronconfig_titles {
 7382:     return &Apache::lonlocal::texthash(
 7383:                                           dat => 'Standard format (.dat)',
 7384:                                           csv => 'Comma separated values (.csv)',
 7385:                                           hdr => 'Remove first line in file (contains column titles)',
 7386:                                           pad => 'Prepend 0s to PaperID',
 7387:                                           rem => 'Remove leading spaces (except Question Response columns)',
 7388:                                           CODE => 'CODE',
 7389:                                           ID   => 'Student ID',
 7390:                                           PaperID => 'Paper ID',
 7391:                                           FirstName => 'First Name',
 7392:                                           LastName => 'Last Name',
 7393:                                           FirstQuestion => 'First Question Response',
 7394:                                           Section => 'Section',
 7395:     );
 7396: }
 7397: 
 7398: sub scantroncsv_fields {
 7399:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 7400: }
 7401: 
 7402: sub print_coursecategories {
 7403:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 7404:     my $datatable;
 7405:     if ($position eq 'top') {
 7406:         my (%checked);
 7407:         my @catitems = ('unauth','auth');
 7408:         my @cattypes = ('std','domonly','codesrch','none');
 7409:         $checked{'unauth'} = 'std';
 7410:         $checked{'auth'} = 'std';
 7411:         if (ref($settings) eq 'HASH') {
 7412:             foreach my $type (@cattypes) {
 7413:                 if ($type eq $settings->{'unauth'}) {
 7414:                     $checked{'unauth'} = $type;
 7415:                 }
 7416:                 if ($type eq $settings->{'auth'}) {
 7417:                     $checked{'auth'} = $type;
 7418:                 }
 7419:             }
 7420:         }
 7421:         my %lt = &Apache::lonlocal::texthash (
 7422:                                                unauth   => 'Catalog type for unauthenticated users',
 7423:                                                auth     => 'Catalog type for authenticated users',
 7424:                                                none     => 'No catalog',
 7425:                                                std      => 'Standard catalog',
 7426:                                                domonly  => 'Domain-only catalog',
 7427:                                                codesrch => "Code search form",
 7428:                                              );
 7429:        my $itemcount = 0;
 7430:        foreach my $item (@catitems) {
 7431:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 7432:            $datatable .= '<tr '.$css_class.'>'.
 7433:                          '<td>'.$lt{$item}.'</td>'.
 7434:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 7435:            foreach my $type (@cattypes) {
 7436:                my $ischecked;
 7437:                if ($checked{$item} eq $type) {
 7438:                    $ischecked=' checked="checked"';
 7439:                }
 7440:                $datatable .= '<label>'.
 7441:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 7442:                              ' />'.$lt{$type}.'</label>&nbsp;';
 7443:            }
 7444:            $datatable .= '</span></td></tr>';
 7445:            $itemcount ++;
 7446:         }
 7447:         $$rowtotal += $itemcount;
 7448:     } elsif ($position eq 'middle') {
 7449:         my $toggle_cats_crs = ' ';
 7450:         my $toggle_cats_dom = ' checked="checked" ';
 7451:         my $can_cat_crs = ' ';
 7452:         my $can_cat_dom = ' checked="checked" ';
 7453:         my $toggle_catscomm_comm = ' ';
 7454:         my $toggle_catscomm_dom = ' checked="checked" ';
 7455:         my $can_catcomm_comm = ' ';
 7456:         my $can_catcomm_dom = ' checked="checked" ';
 7457: 
 7458:         if (ref($settings) eq 'HASH') {
 7459:             if ($settings->{'togglecats'} eq 'crs') {
 7460:                 $toggle_cats_crs = $toggle_cats_dom;
 7461:                 $toggle_cats_dom = ' ';
 7462:             }
 7463:             if ($settings->{'categorize'} eq 'crs') {
 7464:                 $can_cat_crs = $can_cat_dom;
 7465:                 $can_cat_dom = ' ';
 7466:             }
 7467:             if ($settings->{'togglecatscomm'} eq 'comm') {
 7468:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 7469:                 $toggle_catscomm_dom = ' ';
 7470:             }
 7471:             if ($settings->{'categorizecomm'} eq 'comm') {
 7472:                 $can_catcomm_comm = $can_catcomm_dom;
 7473:                 $can_catcomm_dom = ' ';
 7474:             }
 7475:         }
 7476:         my %title = &Apache::lonlocal::texthash (
 7477:                      togglecats     => 'Show/Hide a course in catalog',
 7478:                      togglecatscomm => 'Show/Hide a community in catalog',
 7479:                      categorize     => 'Assign a category to a course',
 7480:                      categorizecomm => 'Assign a category to a community',
 7481:                     );
 7482:         my %level = &Apache::lonlocal::texthash (
 7483:                      dom  => 'Set in Domain',
 7484:                      crs  => 'Set in Course',
 7485:                      comm => 'Set in Community',
 7486:                     );
 7487:         $datatable = '<tr class="LC_odd_row">'.
 7488:                   '<td>'.$title{'togglecats'}.'</td>'.
 7489:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7490:                   '<input type="radio" name="togglecats"'.
 7491:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7492:                   '<label><input type="radio" name="togglecats"'.
 7493:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7494:                   '</tr><tr>'.
 7495:                   '<td>'.$title{'categorize'}.'</td>'.
 7496:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7497:                   '<label><input type="radio" name="categorize"'.
 7498:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7499:                   '<label><input type="radio" name="categorize"'.
 7500:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7501:                   '</tr><tr class="LC_odd_row">'.
 7502:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 7503:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7504:                   '<input type="radio" name="togglecatscomm"'.
 7505:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7506:                   '<label><input type="radio" name="togglecatscomm"'.
 7507:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7508:                   '</tr><tr>'.
 7509:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 7510:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7511:                   '<label><input type="radio" name="categorizecomm"'.
 7512:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7513:                   '<label><input type="radio" name="categorizecomm"'.
 7514:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7515:                   '</tr>';
 7516:         $$rowtotal += 4;
 7517:     } else {
 7518:         my $css_class;
 7519:         my $itemcount = 1;
 7520:         my $cathash; 
 7521:         if (ref($settings) eq 'HASH') {
 7522:             $cathash = $settings->{'cats'};
 7523:         }
 7524:         if (ref($cathash) eq 'HASH') {
 7525:             my (@cats,@trails,%allitems,%idx,@jsarray);
 7526:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 7527:                                                    \%allitems,\%idx,\@jsarray);
 7528:             my $maxdepth = scalar(@cats);
 7529:             my $colattrib = '';
 7530:             if ($maxdepth > 2) {
 7531:                 $colattrib = ' colspan="2" ';
 7532:             }
 7533:             my @path;
 7534:             if (@cats > 0) {
 7535:                 if (ref($cats[0]) eq 'ARRAY') {
 7536:                     my $numtop = @{$cats[0]};
 7537:                     my $maxnum = $numtop;
 7538:                     my %default_names = (
 7539:                           instcode    => &mt('Official courses'),
 7540:                           communities => &mt('Communities'),
 7541:                     );
 7542: 
 7543:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 7544:                         ($cathash->{'instcode::0'} eq '') ||
 7545:                         (!grep(/^communities$/,@{$cats[0]})) || 
 7546:                         ($cathash->{'communities::0'} eq '')) {
 7547:                         $maxnum ++;
 7548:                     }
 7549:                     my $lastidx;
 7550:                     for (my $i=0; $i<$numtop; $i++) {
 7551:                         my $parent = $cats[0][$i];
 7552:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7553:                         my $item = &escape($parent).'::0';
 7554:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 7555:                         $lastidx = $idx{$item};
 7556:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7557:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 7558:                         for (my $k=0; $k<=$maxnum; $k++) {
 7559:                             my $vpos = $k+1;
 7560:                             my $selstr;
 7561:                             if ($k == $i) {
 7562:                                 $selstr = ' selected="selected" ';
 7563:                             }
 7564:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7565:                         }
 7566:                         $datatable .= '</select></span></td><td>';
 7567:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 7568:                             $datatable .=  '<span class="LC_nobreak">'
 7569:                                            .$default_names{$parent}.'</span>';
 7570:                             if ($parent eq 'instcode') {
 7571:                                 $datatable .= '<br /><span class="LC_nobreak">('
 7572:                                               .&mt('with institutional codes')
 7573:                                               .')</span></td><td'.$colattrib.'>';
 7574:                             } else {
 7575:                                 $datatable .= '<table><tr><td>';
 7576:                             }
 7577:                             $datatable .= '<span class="LC_nobreak">'
 7578:                                           .'<label><input type="radio" name="'
 7579:                                           .$parent.'" value="1" checked="checked" />'
 7580:                                           .&mt('Display').'</label>';
 7581:                             if ($parent eq 'instcode') {
 7582:                                 $datatable .= '&nbsp;';
 7583:                             } else {
 7584:                                 $datatable .= '</span></td></tr><tr><td>'
 7585:                                               .'<span class="LC_nobreak">';
 7586:                             }
 7587:                             $datatable .= '<label><input type="radio" name="'
 7588:                                           .$parent.'" value="0" />'
 7589:                                           .&mt('Do not display').'</label></span>';
 7590:                             if ($parent eq 'communities') {
 7591:                                 $datatable .= '</td></tr></table>';
 7592:                             }
 7593:                             $datatable .= '</td>';
 7594:                         } else {
 7595:                             $datatable .= $parent
 7596:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 7597:                                           .'<input type="checkbox" name="deletecategory" '
 7598:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 7599:                         }
 7600:                         my $depth = 1;
 7601:                         push(@path,$parent);
 7602:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 7603:                         pop(@path);
 7604:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 7605:                         $itemcount ++;
 7606:                     }
 7607:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7608:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 7609:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 7610:                     for (my $k=0; $k<=$maxnum; $k++) {
 7611:                         my $vpos = $k+1;
 7612:                         my $selstr;
 7613:                         if ($k == $numtop) {
 7614:                             $selstr = ' selected="selected" ';
 7615:                         }
 7616:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7617:                     }
 7618:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 7619:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 7620:                                   .'</tr>'."\n";
 7621:                     $itemcount ++;
 7622:                     foreach my $default ('instcode','communities') {
 7623:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 7624:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7625:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 7626:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 7627:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 7628:                             for (my $k=0; $k<=$maxnum; $k++) {
 7629:                                 my $vpos = $k+1;
 7630:                                 my $selstr;
 7631:                                 if ($k == $maxnum) {
 7632:                                     $selstr = ' selected="selected" ';
 7633:                                 }
 7634:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7635:                             }
 7636:                             $datatable .= '</select></span></td>'.
 7637:                                           '<td><span class="LC_nobreak">'.
 7638:                                           $default_names{$default}.'</span>';
 7639:                             if ($default eq 'instcode') {
 7640:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 7641:                                               .&mt('with institutional codes').')</span>';
 7642:                             }
 7643:                             $datatable .= '</td>'
 7644:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 7645:                                           .&mt('Display').'</label>&nbsp;'
 7646:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 7647:                                           .&mt('Do not display').'</label></span></td></tr>';
 7648:                         }
 7649:                     }
 7650:                 }
 7651:             } else {
 7652:                 $datatable .= &initialize_categories($itemcount);
 7653:             }
 7654:         } else {
 7655:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 7656:                           .&initialize_categories($itemcount);
 7657:         }
 7658:         $$rowtotal += $itemcount;
 7659:     }
 7660:     return $datatable;
 7661: }
 7662: 
 7663: sub print_serverstatuses {
 7664:     my ($dom,$settings,$rowtotal) = @_;
 7665:     my $datatable;
 7666:     my @pages = &serverstatus_pages();
 7667:     my (%namedaccess,%machineaccess);
 7668:     foreach my $type (@pages) {
 7669:         $namedaccess{$type} = '';
 7670:         $machineaccess{$type}= '';
 7671:     }
 7672:     if (ref($settings) eq 'HASH') {
 7673:         foreach my $type (@pages) {
 7674:             if (exists($settings->{$type})) {
 7675:                 if (ref($settings->{$type}) eq 'HASH') {
 7676:                     foreach my $key (keys(%{$settings->{$type}})) {
 7677:                         if ($key eq 'namedusers') {
 7678:                             $namedaccess{$type} = $settings->{$type}->{$key};
 7679:                         } elsif ($key eq 'machines') {
 7680:                             $machineaccess{$type} = $settings->{$type}->{$key};
 7681:                         }
 7682:                     }
 7683:                 }
 7684:             }
 7685:         }
 7686:     }
 7687:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7688:     my $rownum = 0;
 7689:     my $css_class;
 7690:     foreach my $type (@pages) {
 7691:         $rownum ++;
 7692:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 7693:         $datatable .= '<tr'.$css_class.'>'.
 7694:                       '<td><span class="LC_nobreak">'.
 7695:                       $titles->{$type}.'</span></td>'.
 7696:                       '<td class="LC_left_item">'.
 7697:                       '<input type="text" name="'.$type.'_namedusers" '.
 7698:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 7699:                       '<td class="LC_right_item">'.
 7700:                       '<span class="LC_nobreak">'.
 7701:                       '<input type="text" name="'.$type.'_machines" '.
 7702:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 7703:                       '</span></td></tr>'."\n";
 7704:     }
 7705:     $$rowtotal += $rownum;
 7706:     return $datatable;
 7707: }
 7708: 
 7709: sub serverstatus_pages {
 7710:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 7711:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 7712:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 7713:             'uniquecodes','diskusage','coursecatalog');
 7714: }
 7715: 
 7716: sub defaults_javascript {
 7717:     my ($settings) = @_;
 7718:     return unless (ref($settings) eq 'HASH');
 7719:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7720:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 7721:         if ($maxnum eq '') {
 7722:             $maxnum = 0;
 7723:         }
 7724:         $maxnum ++;
 7725:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 7726:         return <<"ENDSCRIPT";
 7727: <script type="text/javascript">
 7728: // <![CDATA[
 7729: function reorderTypes(form,caller) {
 7730:     var changedVal;
 7731: $jstext 
 7732:     var newpos = 'addinststatus_pos';
 7733:     var current = new Array;
 7734:     var maxh = $maxnum;
 7735:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7736:     var oldVal;
 7737:     if (caller == newpos) {
 7738:         changedVal = newitemVal;
 7739:     } else {
 7740:         var curritem = 'inststatus_pos_'+caller;
 7741:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7742:         current[newitemVal] = newpos;
 7743:     }
 7744:     for (var i=0; i<inststatuses.length; i++) {
 7745:         if (inststatuses[i] != caller) {
 7746:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7747:             if (form.elements[elementName]) {
 7748:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7749:                 current[currVal] = elementName;
 7750:             }
 7751:         }
 7752:     }
 7753:     for (var j=0; j<maxh; j++) {
 7754:         if (current[j] == undefined) {
 7755:             oldVal = j;
 7756:         }
 7757:     }
 7758:     if (oldVal < changedVal) {
 7759:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7760:            var elementName = current[k];
 7761:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7762:         }
 7763:     } else {
 7764:         for (var k=changedVal; k<oldVal; k++) {
 7765:             var elementName = current[k];
 7766:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7767:         }
 7768:     }
 7769:     return;
 7770: }
 7771: 
 7772: // ]]>
 7773: </script>
 7774: 
 7775: ENDSCRIPT
 7776:     }
 7777: }
 7778: 
 7779: sub passwords_javascript {
 7780:     my %intalert = &Apache::lonlocal::texthash (
 7781:         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.',
 7782:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
 7783:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
 7784:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
 7785:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
 7786:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
 7787:     );
 7788:     &js_escape(\%intalert);
 7789:     my $defmin = $Apache::lonnet::passwdmin;
 7790:     my $intauthjs = <<"ENDSCRIPT";
 7791: 
 7792: function warnIntAuth(field) {
 7793:     if (field.name == 'intauth_check') {
 7794:         if (field.value == '2') {
 7795:             alert('$intalert{authcheck}');
 7796:         }
 7797:     }
 7798:     if (field.name == 'intauth_cost') {
 7799:         field.value.replace(/\s/g,'');
 7800:         if (field.value != '') {
 7801:             var regexdigit=/^\\d+\$/;
 7802:             if (!regexdigit.test(field.value)) {
 7803:                 alert('$intalert{authcost}');
 7804:             }
 7805:         }
 7806:     }
 7807:     return;
 7808: }
 7809: 
 7810: function warnIntPass(field) {
 7811:     field.value.replace(/^\s+/,'');
 7812:     field.value.replace(/\s+\$/,'');
 7813:     var regexdigit=/^\\d+\$/;
 7814:     if (field.name == 'passwords_min') {
 7815:         if (field.value == '') {
 7816:             alert('$intalert{passmin}');
 7817:             field.value = '$defmin';
 7818:         } else {
 7819:             if (!regexdigit.test(field.value)) {
 7820:                 alert('$intalert{passmin}');
 7821:                 field.value = '$defmin';
 7822:             }
 7823:             var minval = parseInt(field.value,10);
 7824:             if (minval < $defmin) {
 7825:                 alert('$intalert{passmin}');
 7826:                 field.value = '$defmin';
 7827:             }
 7828:         }
 7829:     } else {
 7830:         if (field.value == '0') {
 7831:             field.value = '';
 7832:         }
 7833:         if (field.value != '') {
 7834:             if (field.name == 'passwords_expire') {
 7835:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/;
 7836:                 if (!regexpposnum.test(field.value)) {
 7837:                     alert('$intalert{passexp}');
 7838:                     field.value = '';
 7839:                 } else {
 7840:                     var expval = parseFloat(field.value);
 7841:                     if (expval == 0) {
 7842:                         alert('$intalert{passexp}');
 7843:                         field.value = '';
 7844:                     }
 7845:                 }
 7846:             } else {
 7847:                 if (!regexdigit.test(field.value)) {
 7848:                     if (field.name == 'passwords_max') {
 7849:                         alert('$intalert{passmax}');
 7850:                     } else {
 7851:                         if (field.name == 'passwords_numsaved') {
 7852:                             alert('$intalert{passnum}');
 7853:                         }
 7854:                     }
 7855:                     field.value = '';
 7856:                 }
 7857:             }
 7858:         }
 7859:     }
 7860:     return;
 7861: }
 7862: 
 7863: ENDSCRIPT
 7864:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7865: }
 7866: 
 7867: sub coursecategories_javascript {
 7868:     my ($settings) = @_;
 7869:     my ($output,$jstext,$cathash);
 7870:     if (ref($settings) eq 'HASH') {
 7871:         $cathash = $settings->{'cats'};
 7872:     }
 7873:     if (ref($cathash) eq 'HASH') {
 7874:         my (@cats,@jsarray,%idx);
 7875:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7876:         if (@jsarray > 0) {
 7877:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7878:             for (my $i=0; $i<@jsarray; $i++) {
 7879:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7880:                     my $catstr = join('","',@{$jsarray[$i]});
 7881:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7882:                 }
 7883:             }
 7884:         }
 7885:     } else {
 7886:         $jstext  = '    var categories = Array(1);'."\n".
 7887:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7888:     }
 7889:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7890:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7891:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7892:     &js_escape(\$instcode_reserved);
 7893:     &js_escape(\$communities_reserved);
 7894:     &js_escape(\$choose_again);
 7895:     $output = <<"ENDSCRIPT";
 7896: <script type="text/javascript">
 7897: // <![CDATA[
 7898: function reorderCats(form,parent,item,idx) {
 7899:     var changedVal;
 7900: $jstext
 7901:     var newpos = 'addcategory_pos';
 7902:     if (parent == '') {
 7903:         var has_instcode = 0;
 7904:         var maxtop = categories[idx].length;
 7905:         for (var j=0; j<maxtop; j++) {
 7906:             if (categories[idx][j] == 'instcode::0') {
 7907:                 has_instcode == 1;
 7908:             }
 7909:         }
 7910:         if (has_instcode == 0) {
 7911:             categories[idx][maxtop] = 'instcode_pos';
 7912:         }
 7913:     } else {
 7914:         newpos += '_'+parent;
 7915:     }
 7916:     var maxh = 1 + categories[idx].length;
 7917:     var current = new Array;
 7918:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7919:     if (item == newpos) {
 7920:         changedVal = newitemVal;
 7921:     } else {
 7922:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7923:         current[newitemVal] = newpos;
 7924:     }
 7925:     for (var i=0; i<categories[idx].length; i++) {
 7926:         var elementName = categories[idx][i];
 7927:         if (elementName != item) {
 7928:             if (form.elements[elementName]) {
 7929:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7930:                 current[currVal] = elementName;
 7931:             }
 7932:         }
 7933:     }
 7934:     var oldVal;
 7935:     for (var j=0; j<maxh; j++) {
 7936:         if (current[j] == undefined) {
 7937:             oldVal = j;
 7938:         }
 7939:     }
 7940:     if (oldVal < changedVal) {
 7941:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7942:            var elementName = current[k];
 7943:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7944:         }
 7945:     } else {
 7946:         for (var k=changedVal; k<oldVal; k++) {
 7947:             var elementName = current[k];
 7948:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7949:         }
 7950:     }
 7951:     return;
 7952: }
 7953: 
 7954: function categoryCheck(form) {
 7955:     if (form.elements['addcategory_name'].value == 'instcode') {
 7956:         alert('$instcode_reserved\\n$choose_again');
 7957:         return false;
 7958:     }
 7959:     if (form.elements['addcategory_name'].value == 'communities') {
 7960:         alert('$communities_reserved\\n$choose_again');
 7961:         return false;
 7962:     }
 7963:     return true;
 7964: }
 7965: 
 7966: // ]]>
 7967: </script>
 7968: 
 7969: ENDSCRIPT
 7970:     return $output;
 7971: }
 7972: 
 7973: sub initialize_categories {
 7974:     my ($itemcount) = @_;
 7975:     my ($datatable,$css_class,$chgstr);
 7976:     my %default_names = (
 7977:                       instcode    => 'Official courses (with institutional codes)',
 7978:                       communities => 'Communities',
 7979:                         );
 7980:     my $select0 = ' selected="selected"';
 7981:     my $select1 = '';
 7982:     foreach my $default ('instcode','communities') {
 7983:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7984:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 7985:         if ($default eq 'communities') {
 7986:             $select1 = $select0;
 7987:             $select0 = '';
 7988:         }
 7989:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7990:                      .'<select name="'.$default.'_pos">'
 7991:                      .'<option value="0"'.$select0.'>1</option>'
 7992:                      .'<option value="1"'.$select1.'>2</option>'
 7993:                      .'<option value="2">3</option></select>&nbsp;'
 7994:                      .$default_names{$default}
 7995:                      .'</span></td><td><span class="LC_nobreak">'
 7996:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7997:                      .&mt('Display').'</label>&nbsp;<label>'
 7998:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7999:                  .'</label></span></td></tr>';
 8000:         $itemcount ++;
 8001:     }
 8002:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8003:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 8004:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8005:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 8006:                   .'<option value="0">1</option>'
 8007:                   .'<option value="1">2</option>'
 8008:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 8009:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 8010:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 8011:                   .'</td></tr>';
 8012:     return $datatable;
 8013: }
 8014: 
 8015: sub build_category_rows {
 8016:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 8017:     my ($text,$name,$item,$chgstr);
 8018:     if (ref($cats) eq 'ARRAY') {
 8019:         my $maxdepth = scalar(@{$cats});
 8020:         if (ref($cats->[$depth]) eq 'HASH') {
 8021:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 8022:                 my $numchildren = @{$cats->[$depth]{$parent}};
 8023:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8024:                 $text .= '<td><table class="LC_data_table">';
 8025:                 my ($idxnum,$parent_name,$parent_item);
 8026:                 my $higher = $depth - 1;
 8027:                 if ($higher == 0) {
 8028:                     $parent_name = &escape($parent).'::'.$higher;
 8029:                 } else {
 8030:                     if (ref($path) eq 'ARRAY') {
 8031:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8032:                     }
 8033:                 }
 8034:                 $parent_item = 'addcategory_pos_'.$parent_name;
 8035:                 for (my $j=0; $j<=$numchildren; $j++) {
 8036:                     if ($j < $numchildren) {
 8037:                         $name = $cats->[$depth]{$parent}[$j];
 8038:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 8039:                         $idxnum = $idx->{$item};
 8040:                     } else {
 8041:                         $name = $parent_name;
 8042:                         $item = $parent_item;
 8043:                     }
 8044:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 8045:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 8046:                     for (my $i=0; $i<=$numchildren; $i++) {
 8047:                         my $vpos = $i+1;
 8048:                         my $selstr;
 8049:                         if ($j == $i) {
 8050:                             $selstr = ' selected="selected" ';
 8051:                         }
 8052:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 8053:                     }
 8054:                     $text .= '</select>&nbsp;';
 8055:                     if ($j < $numchildren) {
 8056:                         my $deeper = $depth+1;
 8057:                         $text .= $name.'&nbsp;'
 8058:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 8059:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 8060:                         if(ref($path) eq 'ARRAY') {
 8061:                             push(@{$path},$name);
 8062:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 8063:                             pop(@{$path});
 8064:                         }
 8065:                     } else {
 8066:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 8067:                         if ($j == $numchildren) {
 8068:                             $text .= $name;
 8069:                         } else {
 8070:                             $text .= $item;
 8071:                         }
 8072:                         $text .= '" value="" />';
 8073:                     }
 8074:                     $text .= '</td></tr>';
 8075:                 }
 8076:                 $text .= '</table></td>';
 8077:             } else {
 8078:                 my $higher = $depth-1;
 8079:                 if ($higher == 0) {
 8080:                     $name = &escape($parent).'::'.$higher;
 8081:                 } else {
 8082:                     if (ref($path) eq 'ARRAY') {
 8083:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8084:                     }
 8085:                 }
 8086:                 my $colspan;
 8087:                 if ($parent ne 'instcode') {
 8088:                     $colspan = $maxdepth - $depth - 1;
 8089:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 8090:                 }
 8091:             }
 8092:         }
 8093:     }
 8094:     return $text;
 8095: }
 8096: 
 8097: sub modifiable_userdata_row {
 8098:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 8099:         $rowid,$customcss,$rowstyle) = @_;
 8100:     my ($role,$rolename,$statustype);
 8101:     $role = $item;
 8102:     if ($context eq 'cancreate') {
 8103:         if ($item =~ /^(emailusername)_(.+)$/) {
 8104:             $role = $1;
 8105:             $statustype = $2;
 8106:             if (ref($usertypes) eq 'HASH') {
 8107:                 if ($usertypes->{$statustype}) {
 8108:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 8109:                 } else {
 8110:                     $rolename = &mt('Data provided by user');
 8111:                 }
 8112:             }
 8113:         }
 8114:     } elsif ($context eq 'selfcreate') {
 8115:         if (ref($usertypes) eq 'HASH') {
 8116:             $rolename = $usertypes->{$role};
 8117:         } else {
 8118:             $rolename = $role;
 8119:         }
 8120:     } else {
 8121:         if ($role eq 'cr') {
 8122:             $rolename = &mt('Custom role');
 8123:         } else {
 8124:             $rolename = &Apache::lonnet::plaintext($role);
 8125:         }
 8126:     }
 8127:     my (@fields,%fieldtitles);
 8128:     if (ref($fieldsref) eq 'ARRAY') {
 8129:         @fields = @{$fieldsref};
 8130:     } else {
 8131:         @fields = ('lastname','firstname','middlename','generation',
 8132:                    'permanentemail','id');
 8133:     }
 8134:     if ((ref($titlesref) eq 'HASH')) {
 8135:         %fieldtitles = %{$titlesref};
 8136:     } else {
 8137:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8138:     }
 8139:     my $output;
 8140:     my $css_class;
 8141:     if ($rowcount%2) {
 8142:         $css_class = 'LC_odd_row';
 8143:     }
 8144:     if ($customcss) {
 8145:         $css_class .= " $customcss";
 8146:     }
 8147:     $css_class =~ s/^\s+//;
 8148:     if ($css_class) {
 8149:         $css_class = ' class="'.$css_class.'"';
 8150:     }
 8151:     if ($rowstyle) {
 8152:         $css_class .= ' style="'.$rowstyle.'"';
 8153:     }
 8154:     if ($rowid) {
 8155:         $rowid = ' id="'.$rowid.'"';
 8156:     }
 8157: 
 8158:     $output = '<tr '.$css_class.$rowid.'>'.
 8159:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 8160:               '<td class="LC_left_item" colspan="2"><table>';
 8161:     my $rem;
 8162:     my %checks;
 8163:     if (ref($settings) eq 'HASH') {
 8164:         if (ref($settings->{$context}) eq 'HASH') {
 8165:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 8166:                 my $hashref = $settings->{$context}->{$role};
 8167:                 if ($role eq 'emailusername') {
 8168:                     if ($statustype) {
 8169:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 8170:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 8171:                             if (ref($hashref) eq 'HASH') { 
 8172:                                 foreach my $field (@fields) {
 8173:                                     if ($hashref->{$field}) {
 8174:                                         $checks{$field} = $hashref->{$field};
 8175:                                     }
 8176:                                 }
 8177:                             }
 8178:                         }
 8179:                     }
 8180:                 } else {
 8181:                     if (ref($hashref) eq 'HASH') {
 8182:                         foreach my $field (@fields) {
 8183:                             if ($hashref->{$field}) {
 8184:                                 $checks{$field} = ' checked="checked" ';
 8185:                             }
 8186:                         }
 8187:                     }
 8188:                 }
 8189:             }
 8190:         }
 8191:     }
 8192: 
 8193:     my $total = scalar(@fields);
 8194:     for (my $i=0; $i<$total; $i++) {
 8195:         $rem = $i%($numinrow);
 8196:         if ($rem == 0) {
 8197:             if ($i > 0) {
 8198:                 $output .= '</tr>';
 8199:             }
 8200:             $output .= '<tr>';
 8201:         }
 8202:         my $check = ' ';
 8203:         unless ($role eq 'emailusername') {
 8204:             if (exists($checks{$fields[$i]})) {
 8205:                 $check = $checks{$fields[$i]};
 8206:             } else {
 8207:                 if ($role eq 'st') {
 8208:                     if (ref($settings) ne 'HASH') {
 8209:                         $check = ' checked="checked" '; 
 8210:                     }
 8211:                 }
 8212:             }
 8213:         }
 8214:         $output .= '<td class="LC_left_item">'.
 8215:                    '<span class="LC_nobreak">';
 8216:         if ($role eq 'emailusername') {
 8217:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 8218:                 $checks{$fields[$i]} = 'omit';
 8219:             }
 8220:             foreach my $option ('required','optional','omit') {
 8221:                 my $checked='';
 8222:                 if ($checks{$fields[$i]} eq $option) {
 8223:                     $checked='checked="checked" ';
 8224:                 }
 8225:                 $output .= '<label>'.
 8226:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 8227:                            &mt($option).'</label>'.('&nbsp;' x2);
 8228:             }
 8229:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 8230:         } else {
 8231:             $output .= '<label>'.
 8232:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 8233:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 8234:                        '</label>';
 8235:         }
 8236:         $output .= '</span></td>';
 8237:     }
 8238:     $rem = $total%$numinrow;
 8239:     my $colsleft;
 8240:     if ($rem) {
 8241:         $colsleft = $numinrow - $rem;
 8242:     }
 8243:     if ($colsleft > 1) {
 8244:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8245:                    '&nbsp;</td>';
 8246:     } elsif ($colsleft == 1) {
 8247:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 8248:     }
 8249:     $output .= '</tr></table></td></tr>';
 8250:     return $output;
 8251: }
 8252: 
 8253: sub insttypes_row {
 8254:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 8255:         $customcss,$rowstyle) = @_;
 8256:     my %lt = &Apache::lonlocal::texthash (
 8257:                       cansearch => 'Users allowed to search',
 8258:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 8259:                       lockablenames => 'User preference to lock name',
 8260:                       selfassign    => 'Self-reportable affiliations',
 8261:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 8262:              );
 8263:     my $showdom;
 8264:     if ($context eq 'cansearch') {
 8265:         $showdom = ' ('.$dom.')';
 8266:     }
 8267:     my $class = 'LC_left_item';
 8268:     if ($context eq 'statustocreate') {
 8269:         $class = 'LC_right_item';
 8270:     }
 8271:     my $css_class;
 8272:     if ($$rowtotal%2) {
 8273:         $css_class = 'LC_odd_row';
 8274:     }
 8275:     if ($customcss) {
 8276:         $css_class .= ' '.$customcss;
 8277:     }
 8278:     $css_class =~ s/^\s+//;
 8279:     if ($css_class) {
 8280:         $css_class = ' class="'.$css_class.'"';
 8281:     }
 8282:     if ($rowstyle) {
 8283:         $css_class .= ' style="'.$rowstyle.'"';
 8284:     }
 8285:     if ($onclick) {
 8286:         $onclick = 'onclick="'.$onclick.'" ';
 8287:     }
 8288:     my $output = '<tr'.$css_class.'>'.
 8289:                  '<td>'.$lt{$context}.$showdom.
 8290:                  '</td><td class="'.$class.'" colspan="2"><table>';
 8291:     my $rem;
 8292:     if (ref($types) eq 'ARRAY') {
 8293:         for (my $i=0; $i<@{$types}; $i++) {
 8294:             if (defined($usertypes->{$types->[$i]})) {
 8295:                 my $rem = $i%($numinrow);
 8296:                 if ($rem == 0) {
 8297:                     if ($i > 0) {
 8298:                         $output .= '</tr>';
 8299:                     }
 8300:                     $output .= '<tr>';
 8301:                 }
 8302:                 my $check = ' ';
 8303:                 if (ref($settings) eq 'HASH') {
 8304:                     if (ref($settings->{$context}) eq 'ARRAY') {
 8305:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 8306:                             $check = ' checked="checked" ';
 8307:                         }
 8308:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 8309:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 8310:                             $check = ' checked="checked" ';
 8311:                         }
 8312:                     } elsif ($context eq 'statustocreate') {
 8313:                         $check = ' checked="checked" ';
 8314:                     }
 8315:                 }
 8316:                 $output .= '<td class="LC_left_item">'.
 8317:                            '<span class="LC_nobreak"><label>'.
 8318:                            '<input type="checkbox" name="'.$context.'" '.
 8319:                            'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
 8320:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 8321:             }
 8322:         }
 8323:         $rem = @{$types}%($numinrow);
 8324:     }
 8325:     my $colsleft = $numinrow - $rem;
 8326:     if ($context eq 'overrides') {
 8327:         if ($colsleft > 1) {
 8328:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8329:         } else {
 8330:             $output .= '<td class="LC_left_item">';
 8331:         }
 8332:         $output .= '&nbsp;';
 8333:     } else {
 8334:         if ($rem == 0) {
 8335:             $output .= '<tr>';
 8336:         }
 8337:         if ($colsleft > 1) {
 8338:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8339:         } else {
 8340:             $output .= '<td class="LC_left_item">';
 8341:         }
 8342:         my $defcheck = ' ';
 8343:         if (ref($settings) eq 'HASH') {  
 8344:             if (ref($settings->{$context}) eq 'ARRAY') {
 8345:                 if (grep(/^default$/,@{$settings->{$context}})) {
 8346:                     $defcheck = ' checked="checked" ';
 8347:                 }
 8348:             } elsif ($context eq 'statustocreate') {
 8349:                 $defcheck = ' checked="checked" ';
 8350:             }
 8351:         }
 8352:         $output .= '<span class="LC_nobreak"><label>'.
 8353:                    '<input type="checkbox" name="'.$context.'" '.
 8354:                    'value="default"'.$defcheck.$onclick.' />'.
 8355:                    $othertitle.'</label></span>';
 8356:     }
 8357:     $output .= '</td></tr></table></td></tr>';
 8358:     return $output;
 8359: }
 8360: 
 8361: sub sorted_searchtitles {
 8362:     my %searchtitles = &Apache::lonlocal::texthash(
 8363:                          'uname' => 'username',
 8364:                          'lastname' => 'last name',
 8365:                          'lastfirst' => 'last name, first name',
 8366:                      );
 8367:     my @titleorder = ('uname','lastname','lastfirst');
 8368:     return (\%searchtitles,\@titleorder);
 8369: }
 8370: 
 8371: sub sorted_searchtypes {
 8372:     my %srchtypes_desc = (
 8373:                            exact    => 'is exact match',
 8374:                            contains => 'contains ..',
 8375:                            begins   => 'begins with ..',
 8376:                          );
 8377:     my @srchtypeorder = ('exact','begins','contains');
 8378:     return (\%srchtypes_desc,\@srchtypeorder);
 8379: }
 8380: 
 8381: sub usertype_update_row {
 8382:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 8383:     my $datatable;
 8384:     my $numinrow = 4;
 8385:     foreach my $type (@{$types}) {
 8386:         if (defined($usertypes->{$type})) {
 8387:             $$rownums ++;
 8388:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 8389:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 8390:                           '</td><td class="LC_left_item"><table>';
 8391:             for (my $i=0; $i<@{$fields}; $i++) {
 8392:                 my $rem = $i%($numinrow);
 8393:                 if ($rem == 0) {
 8394:                     if ($i > 0) {
 8395:                         $datatable .= '</tr>';
 8396:                     }
 8397:                     $datatable .= '<tr>';
 8398:                 }
 8399:                 my $check = ' ';
 8400:                 if (ref($settings) eq 'HASH') {
 8401:                     if (ref($settings->{'fields'}) eq 'HASH') {
 8402:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 8403:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 8404:                                 $check = ' checked="checked" ';
 8405:                             }
 8406:                         }
 8407:                     }
 8408:                 }
 8409: 
 8410:                 if ($i == @{$fields}-1) {
 8411:                     my $colsleft = $numinrow - $rem;
 8412:                     if ($colsleft > 1) {
 8413:                         $datatable .= '<td colspan="'.$colsleft.'">';
 8414:                     } else {
 8415:                         $datatable .= '<td>';
 8416:                     }
 8417:                 } else {
 8418:                     $datatable .= '<td>';
 8419:                 }
 8420:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8421:                               '<input type="checkbox" name="updateable_'.$type.
 8422:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 8423:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 8424:             }
 8425:             $datatable .= '</tr></table></td></tr>';
 8426:         }
 8427:     }
 8428:     return $datatable;
 8429: }
 8430: 
 8431: sub modify_login {
 8432:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8433:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 8434:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 8435:     %title = ( coursecatalog => 'Display course catalog',
 8436:                adminmail => 'Display administrator E-mail address',
 8437:                helpdesk  => 'Display "Contact Helpdesk" link',
 8438:                newuser => 'Link for visitors to create a user account',
 8439:                loginheader => 'Log-in box header');
 8440:     @offon = ('off','on');
 8441:     if (ref($domconfig{login}) eq 'HASH') {
 8442:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 8443:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 8444:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 8445:             }
 8446:         }
 8447:     }
 8448:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 8449:                                            \%domconfig,\%loginhash);
 8450:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8451:     foreach my $item (@toggles) {
 8452:         $loginhash{login}{$item} = $env{'form.'.$item};
 8453:     }
 8454:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 8455:     if (ref($colchanges{'login'}) eq 'HASH') {  
 8456:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 8457:                                          \%loginhash);
 8458:     }
 8459: 
 8460:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8461:     my %domservers = &Apache::lonnet::get_servers($dom);
 8462:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 8463:     if (keys(%servers) > 1) {
 8464:         foreach my $lonhost (keys(%servers)) {
 8465:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 8466:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 8467:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 8468:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 8469:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 8470:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8471:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8472:                         $changes{'loginvia'}{$lonhost} = 1;
 8473:                     } else {
 8474:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 8475:                         $changes{'loginvia'}{$lonhost} = 1;
 8476:                     }
 8477:                 } else {
 8478:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8479:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8480:                         $changes{'loginvia'}{$lonhost} = 1;
 8481:                     }
 8482:                 }
 8483:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 8484:                     foreach my $item (@loginvia_attribs) {
 8485:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 8486:                     }
 8487:                 } else {
 8488:                     foreach my $item (@loginvia_attribs) {
 8489:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8490:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8491:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 8492:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8493:                                 $new = '/';
 8494:                             }
 8495:                         }
 8496:                         if (($item eq 'custompath') && 
 8497:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8498:                             $new = '';
 8499:                         }
 8500:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 8501:                             $changes{'loginvia'}{$lonhost} = 1;
 8502:                         }
 8503:                         if ($item eq 'exempt') {
 8504:                             $new = &check_exempt_addresses($new);
 8505:                         }
 8506:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8507:                     }
 8508:                 }
 8509:             } else {
 8510:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8511:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8512:                     $changes{'loginvia'}{$lonhost} = 1;
 8513:                     foreach my $item (@loginvia_attribs) {
 8514:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8515:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8516:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8517:                                 $new = '/';
 8518:                             }
 8519:                         }
 8520:                         if (($item eq 'custompath') && 
 8521:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8522:                             $new = '';
 8523:                         }
 8524:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8525:                     }
 8526:                 }
 8527:             }
 8528:         }
 8529:     }
 8530: 
 8531:     my $servadm = $r->dir_config('lonAdmEMail');
 8532:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 8533:     if (ref($domconfig{'login'}) eq 'HASH') {
 8534:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 8535:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 8536:                 if ($lang eq 'nolang') {
 8537:                     push(@currlangs,$lang);
 8538:                 } elsif (defined($langchoices{$lang})) {
 8539:                     push(@currlangs,$lang);
 8540:                 } else {
 8541:                     next;
 8542:                 }
 8543:             }
 8544:         }
 8545:     }
 8546:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 8547:     if (@currlangs > 0) {
 8548:         foreach my $lang (@currlangs) {
 8549:             if (grep(/^\Q$lang\E$/,@delurls)) {
 8550:                 $changes{'helpurl'}{$lang} = 1;
 8551:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 8552:                 $changes{'helpurl'}{$lang} = 1;
 8553:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 8554:                 push(@newlangs,$lang);
 8555:             } else {
 8556:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8557:             }
 8558:         }
 8559:     }
 8560:     unless (grep(/^nolang$/,@currlangs)) {
 8561:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 8562:             $changes{'helpurl'}{'nolang'} = 1;
 8563:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 8564:             push(@newlangs,'nolang');
 8565:         }
 8566:     }
 8567:     if ($env{'form.loginhelpurl_add_lang'}) {
 8568:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 8569:             ($env{'form.loginhelpurl_add_file.filename'})) {
 8570:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 8571:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 8572:         }
 8573:     }
 8574:     if ((@newlangs > 0) || ($addedfile)) {
 8575:         my $error;
 8576:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8577:         if ($configuserok eq 'ok') {
 8578:             if ($switchserver) {
 8579:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 8580:             } elsif ($author_ok eq 'ok') {
 8581:                 my @allnew = @newlangs;
 8582:                 if ($addedfile ne '') {
 8583:                     push(@allnew,$addedfile);
 8584:                 }
 8585:                 foreach my $lang (@allnew) {
 8586:                     my $formelem = 'loginhelpurl_'.$lang;
 8587:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 8588:                         $formelem = 'loginhelpurl_add_file';
 8589:                     }
 8590:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8591:                                                                "help/$lang",'','',$newfile{$lang});
 8592:                     if ($result eq 'ok') {
 8593:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 8594:                         $changes{'helpurl'}{$lang} = 1;
 8595:                     } else {
 8596:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 8597:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8598:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 8599:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 8600:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8601:                         }
 8602:                     }
 8603:                 }
 8604:             } else {
 8605:                 $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);
 8606:             }
 8607:         } else {
 8608:             $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);
 8609:         }
 8610:         if ($error) {
 8611:             &Apache::lonnet::logthis($error);
 8612:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8613:         }
 8614:     }
 8615: 
 8616:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 8617:     if (ref($domconfig{'login'}) eq 'HASH') {
 8618:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 8619:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 8620:                 if ($domservers{$lonhost}) {
 8621:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8622:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 8623:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 8624:                     }
 8625:                 }
 8626:             }
 8627:         }
 8628:     }
 8629:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 8630:     foreach my $lonhost (sort(keys(%domservers))) {
 8631:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8632:             $changes{'headtag'}{$lonhost} = 1;
 8633:         } else {
 8634:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 8635:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 8636:             }
 8637:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 8638:                 push(@newhosts,$lonhost);
 8639:             } elsif ($currheadtagurls{$lonhost}) {
 8640:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 8641:                 if ($currexempt{$lonhost}) {
 8642:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 8643:                         $changes{'headtag'}{$lonhost} = 1;
 8644:                     }
 8645:                 } elsif ($possexempt{$lonhost}) {
 8646:                     $changes{'headtag'}{$lonhost} = 1;
 8647:                 }
 8648:                 if ($possexempt{$lonhost}) {
 8649:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8650:                 }
 8651:             }
 8652:         }
 8653:     }
 8654:     if (@newhosts) {
 8655:         my $error;
 8656:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8657:         if ($configuserok eq 'ok') {
 8658:             if ($switchserver) {
 8659:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8660:             } elsif ($author_ok eq 'ok') {
 8661:                 foreach my $lonhost (@newhosts) {
 8662:                     my $formelem = 'loginheadtag_'.$lonhost;
 8663:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8664:                                                                           "login/headtag/$lonhost",'','',
 8665:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8666:                     if ($result eq 'ok') {
 8667:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8668:                         $changes{'headtag'}{$lonhost} = 1;
 8669:                         if ($possexempt{$lonhost}) {
 8670:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8671:                         }
 8672:                     } else {
 8673:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 8674:                                            $newheadtagurls{$lonhost},$result);
 8675:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8676:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 8677:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 8678:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 8679:                         }
 8680:                     }
 8681:                 }
 8682:             } else {
 8683:                 $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);
 8684:             }
 8685:         } else {
 8686:             $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);
 8687:         }
 8688:         if ($error) {
 8689:             &Apache::lonnet::logthis($error);
 8690:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8691:         }
 8692:     }
 8693:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 8694: 
 8695:     my $defaulthelpfile = '/adm/loginproblems.html';
 8696:     my $defaulttext = &mt('Default in use');
 8697: 
 8698:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 8699:                                              $dom);
 8700:     if ($putresult eq 'ok') {
 8701:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8702:         my %defaultchecked = (
 8703:                     'coursecatalog' => 'on',
 8704:                     'helpdesk'      => 'on',
 8705:                     'adminmail'     => 'off',
 8706:                     'newuser'       => 'off',
 8707:         );
 8708:         if (ref($domconfig{'login'}) eq 'HASH') {
 8709:             foreach my $item (@toggles) {
 8710:                 if ($defaultchecked{$item} eq 'on') { 
 8711:                     if (($domconfig{'login'}{$item} eq '0') &&
 8712:                         ($env{'form.'.$item} eq '1')) {
 8713:                         $changes{$item} = 1;
 8714:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8715:                               $domconfig{'login'}{$item} eq '1') &&
 8716:                              ($env{'form.'.$item} eq '0')) {
 8717:                         $changes{$item} = 1;
 8718:                     }
 8719:                 } elsif ($defaultchecked{$item} eq 'off') {
 8720:                     if (($domconfig{'login'}{$item} eq '1') &&
 8721:                         ($env{'form.'.$item} eq '0')) {
 8722:                         $changes{$item} = 1;
 8723:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8724:                               $domconfig{'login'}{$item} eq '0') &&
 8725:                              ($env{'form.'.$item} eq '1')) {
 8726:                         $changes{$item} = 1;
 8727:                     }
 8728:                 }
 8729:             }
 8730:         }
 8731:         if (keys(%changes) > 0 || $colchgtext) {
 8732:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8733:             if (ref($lastactref) eq 'HASH') {
 8734:                 $lastactref->{'domainconfig'} = 1;
 8735:             }
 8736:             $resulttext = &mt('Changes made:').'<ul>';
 8737:             foreach my $item (sort(keys(%changes))) {
 8738:                 if ($item eq 'loginvia') {
 8739:                     if (ref($changes{$item}) eq 'HASH') {
 8740:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 8741:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8742:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 8743:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 8744:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 8745:                                     $protocol = 'http' if ($protocol ne 'https');
 8746:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 8747: 
 8748:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 8749:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 8750:                                     } else {
 8751:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 8752:                                     }
 8753:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 8754:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 8755:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 8756:                                     }
 8757:                                     $resulttext .= '</li>';
 8758:                                 } else {
 8759:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 8760:                                 }
 8761:                             } else {
 8762:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 8763:                             }
 8764:                         }
 8765:                         $resulttext .= '</ul></li>';
 8766:                     }
 8767:                 } elsif ($item eq 'helpurl') {
 8768:                     if (ref($changes{$item}) eq 'HASH') {
 8769:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 8770:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 8771:                                 my ($chg,$link);
 8772:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 8773:                                 if ($lang eq 'nolang') {
 8774:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 8775:                                 } else {
 8776:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 8777:                                 }
 8778:                                 $resulttext .= '<li>'.$chg.'</li>';
 8779:                             } else {
 8780:                                 my $chg;
 8781:                                 if ($lang eq 'nolang') {
 8782:                                     $chg = &mt('custom log-in help file for no preferred language');
 8783:                                 } else {
 8784:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 8785:                                 }
 8786:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 8787:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 8788:                                                       '?inhibitmenu=yes',$chg,600,500).
 8789:                                                '</li>';
 8790:                             }
 8791:                         }
 8792:                     }
 8793:                 } elsif ($item eq 'headtag') {
 8794:                     if (ref($changes{$item}) eq 'HASH') {
 8795:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8796:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8797:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 8798:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8799:                                 $resulttext .= '<li><a href="'.
 8800:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 8801:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 8802:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 8803:                                 if ($possexempt{$lonhost}) {
 8804:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 8805:                                 } else {
 8806:                                     $resulttext .= &mt('included for any client IP');
 8807:                                 }
 8808:                                 $resulttext .= '</li>';
 8809:                             }
 8810:                         }
 8811:                     }
 8812:                 } elsif ($item eq 'captcha') {
 8813:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8814:                         my $chgtxt;
 8815:                         if ($loginhash{'login'}{$item} eq 'notused') {
 8816:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 8817:                         } else {
 8818:                             my %captchas = &captcha_phrases();
 8819:                             if ($captchas{$loginhash{'login'}{$item}}) {
 8820:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 8821:                             } else {
 8822:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 8823:                             }
 8824:                         }
 8825:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8826:                     }
 8827:                 } elsif ($item eq 'recaptchakeys') {
 8828:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8829:                         my ($privkey,$pubkey);
 8830:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 8831:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 8832:                             $privkey = $loginhash{'login'}{$item}{'private'};
 8833:                         }
 8834:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 8835:                         if (!$pubkey) {
 8836:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 8837:                         } else {
 8838:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8839:                         }
 8840:                         if (!$privkey) {
 8841:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 8842:                         } else {
 8843:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 8844:                         }
 8845:                         $chgtxt .= '</ul>';
 8846:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8847:                     }
 8848:                 } elsif ($item eq 'recaptchaversion') {
 8849:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8850:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 8851:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 8852:                                            '</li>';
 8853:                         }
 8854:                     }
 8855:                 } else {
 8856:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 8857:                 }
 8858:             }
 8859:             $resulttext .= $colchgtext.'</ul>';
 8860:         } else {
 8861:             $resulttext = &mt('No changes made to log-in page settings');
 8862:         }
 8863:     } else {
 8864:         $resulttext = '<span class="LC_error">'.
 8865: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8866:     }
 8867:     if ($errors) {
 8868:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8869:                        $errors.'</ul>';
 8870:     }
 8871:     return $resulttext;
 8872: }
 8873: 
 8874: sub check_exempt_addresses {
 8875:     my ($iplist) = @_;
 8876:     $iplist =~ s/^\s+//;
 8877:     $iplist =~ s/\s+$//;
 8878:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8879:     my (@okips,$new);
 8880:     foreach my $ip (@poss_ips) {
 8881:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8882:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8883:                 push(@okips,$ip);
 8884:             }
 8885:         }
 8886:     }
 8887:     if (@okips > 0) {
 8888:         $new = join(',',@okips);
 8889:     } else {
 8890:         $new = '';
 8891:     }
 8892:     return $new;
 8893: }
 8894: 
 8895: sub color_font_choices {
 8896:     my %choices =
 8897:         &Apache::lonlocal::texthash (
 8898:             img => "Header",
 8899:             bgs => "Background colors",
 8900:             links => "Link colors",
 8901:             images => "Images",
 8902:             font => "Font color",
 8903:             fontmenu => "Font menu",
 8904:             pgbg => "Page",
 8905:             tabbg => "Header",
 8906:             sidebg => "Border",
 8907:             link => "Link",
 8908:             alink => "Active link",
 8909:             vlink => "Visited link",
 8910:         );
 8911:     return %choices;
 8912: }
 8913: 
 8914: sub modify_rolecolors {
 8915:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8916:     my ($resulttext,%rolehash);
 8917:     $rolehash{'rolecolors'} = {};
 8918:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8919:         if ($domconfig{'rolecolors'} eq '') {
 8920:             $domconfig{'rolecolors'} = {};
 8921:         }
 8922:     }
 8923:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8924:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8925:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8926:                                              $dom);
 8927:     if ($putresult eq 'ok') {
 8928:         if (keys(%changes) > 0) {
 8929:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8930:             if (ref($lastactref) eq 'HASH') {
 8931:                 $lastactref->{'domainconfig'} = 1;
 8932:             }
 8933:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8934:                                              $rolehash{'rolecolors'});
 8935:         } else {
 8936:             $resulttext = &mt('No changes made to default color schemes');
 8937:         }
 8938:     } else {
 8939:         $resulttext = '<span class="LC_error">'.
 8940: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8941:     }
 8942:     if ($errors) {
 8943:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8944:                        $errors.'</ul>';
 8945:     }
 8946:     return $resulttext;
 8947: }
 8948: 
 8949: sub modify_colors {
 8950:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8951:     my (%changes,%choices);
 8952:     my @bgs;
 8953:     my @links = ('link','alink','vlink');
 8954:     my @logintext;
 8955:     my @images;
 8956:     my $servadm = $r->dir_config('lonAdmEMail');
 8957:     my $errors;
 8958:     my %defaults;
 8959:     foreach my $role (@{$roles}) {
 8960:         if ($role eq 'login') {
 8961:             %choices = &login_choices();
 8962:             @logintext = ('textcol','bgcol');
 8963:         } else {
 8964:             %choices = &color_font_choices();
 8965:         }
 8966:         if ($role eq 'login') {
 8967:             @images = ('img','logo','domlogo','login');
 8968:             @bgs = ('pgbg','mainbg','sidebg');
 8969:         } else {
 8970:             @images = ('img');
 8971:             @bgs = ('pgbg','tabbg','sidebg');
 8972:         }
 8973:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8974:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8975:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8976:         }
 8977:         if ($role eq 'login') {
 8978:             foreach my $item (@logintext) {
 8979:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8980:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8981:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8982:                 }
 8983:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8984:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8985:                 }
 8986:             }
 8987:         } else {
 8988:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8989:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8990:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8991:             }
 8992:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8993:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8994:             }
 8995:         }
 8996:         foreach my $item (@bgs) {
 8997:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8998:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8999:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9000:             }
 9001:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 9002:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9003:             }
 9004:         }
 9005:         foreach my $item (@links) {
 9006:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9007:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9008:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9009:             }
 9010:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 9011:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9012:             }
 9013:         }
 9014:         my ($configuserok,$author_ok,$switchserver) = 
 9015:             &config_check($dom,$confname,$servadm);
 9016:         my ($width,$height) = &thumb_dimensions();
 9017:         if (ref($domconfig->{$role}) ne 'HASH') {
 9018:             $domconfig->{$role} = {};
 9019:         }
 9020:         foreach my $img (@images) {
 9021:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 9022:                 if (defined($env{'form.login_showlogo_'.$img})) {
 9023:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 9024:                 } else { 
 9025:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 9026:                 }
 9027:             } 
 9028: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 9029: 		 && !defined($domconfig->{$role}{$img})
 9030: 		 && !$env{'form.'.$role.'_del_'.$img}
 9031: 		 && $env{'form.'.$role.'_import_'.$img}) {
 9032: 		# import the old configured image from the .tab setting
 9033: 		# if they haven't provided a new one 
 9034: 		$domconfig->{$role}{$img} = 
 9035: 		    $env{'form.'.$role.'_import_'.$img};
 9036: 	    }
 9037:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 9038:                 my $error;
 9039:                 if ($configuserok eq 'ok') {
 9040:                     if ($switchserver) {
 9041:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 9042:                     } else {
 9043:                         if ($author_ok eq 'ok') {
 9044:                             my ($result,$logourl) = 
 9045:                                 &publishlogo($r,'upload',$role.'_'.$img,
 9046:                                            $dom,$confname,$img,$width,$height);
 9047:                             if ($result eq 'ok') {
 9048:                                 $confhash->{$role}{$img} = $logourl;
 9049:                                 $changes{$role}{'images'}{$img} = 1;
 9050:                             } else {
 9051:                                 $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);
 9052:                             }
 9053:                         } else {
 9054:                             $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);
 9055:                         }
 9056:                     }
 9057:                 } else {
 9058:                     $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);
 9059:                 }
 9060:                 if ($error) {
 9061:                     &Apache::lonnet::logthis($error);
 9062:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9063:                 }
 9064:             } elsif ($domconfig->{$role}{$img} ne '') {
 9065:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 9066:                     my $error;
 9067:                     if ($configuserok eq 'ok') {
 9068: # is confname an author?
 9069:                         if ($switchserver eq '') {
 9070:                             if ($author_ok eq 'ok') {
 9071:                                 my ($result,$logourl) = 
 9072:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 9073:                                             $dom,$confname,$img,$width,$height);
 9074:                                 if ($result eq 'ok') {
 9075:                                     $confhash->{$role}{$img} = $logourl;
 9076: 				    $changes{$role}{'images'}{$img} = 1;
 9077:                                 }
 9078:                             }
 9079:                         }
 9080:                     }
 9081:                 }
 9082:             }
 9083:         }
 9084:         if (ref($domconfig) eq 'HASH') {
 9085:             if (ref($domconfig->{$role}) eq 'HASH') {
 9086:                 foreach my $img (@images) {
 9087:                     if ($domconfig->{$role}{$img} ne '') {
 9088:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9089:                             $confhash->{$role}{$img} = '';
 9090:                             $changes{$role}{'images'}{$img} = 1;
 9091:                         } else {
 9092:                             if ($confhash->{$role}{$img} eq '') {
 9093:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 9094:                             }
 9095:                         }
 9096:                     } else {
 9097:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9098:                             $confhash->{$role}{$img} = '';
 9099:                             $changes{$role}{'images'}{$img} = 1;
 9100:                         } 
 9101:                     }
 9102:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 9103:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 9104:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 9105:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 9106:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 9107:                             }
 9108:                         } else {
 9109:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9110:                                 $changes{$role}{'showlogo'}{$img} = 1;
 9111:                             }
 9112:                         }
 9113:                     }
 9114:                 }
 9115:                 if ($domconfig->{$role}{'font'} ne '') {
 9116:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 9117:                         $changes{$role}{'font'} = 1;
 9118:                     }
 9119:                 } else {
 9120:                     if ($confhash->{$role}{'font'}) {
 9121:                         $changes{$role}{'font'} = 1;
 9122:                     }
 9123:                 }
 9124:                 if ($role ne 'login') {
 9125:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 9126:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 9127:                             $changes{$role}{'fontmenu'} = 1;
 9128:                         }
 9129:                     } else {
 9130:                         if ($confhash->{$role}{'fontmenu'}) {
 9131:                             $changes{$role}{'fontmenu'} = 1;
 9132:                         }
 9133:                     }
 9134:                 }
 9135:                 foreach my $item (@bgs) {
 9136:                     if ($domconfig->{$role}{$item} ne '') {
 9137:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9138:                             $changes{$role}{'bgs'}{$item} = 1;
 9139:                         } 
 9140:                     } else {
 9141:                         if ($confhash->{$role}{$item}) {
 9142:                             $changes{$role}{'bgs'}{$item} = 1;
 9143:                         }
 9144:                     }
 9145:                 }
 9146:                 foreach my $item (@links) {
 9147:                     if ($domconfig->{$role}{$item} ne '') {
 9148:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9149:                             $changes{$role}{'links'}{$item} = 1;
 9150:                         }
 9151:                     } else {
 9152:                         if ($confhash->{$role}{$item}) {
 9153:                             $changes{$role}{'links'}{$item} = 1;
 9154:                         }
 9155:                     }
 9156:                 }
 9157:                 foreach my $item (@logintext) {
 9158:                     if ($domconfig->{$role}{$item} ne '') {
 9159:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9160:                             $changes{$role}{'logintext'}{$item} = 1;
 9161:                         }
 9162:                     } else {
 9163:                         if ($confhash->{$role}{$item}) {
 9164:                             $changes{$role}{'logintext'}{$item} = 1;
 9165:                         }
 9166:                     }
 9167:                 }
 9168:             } else {
 9169:                 &default_change_checker($role,\@images,\@links,\@bgs,
 9170:                                         \@logintext,$confhash,\%changes); 
 9171:             }
 9172:         } else {
 9173:             &default_change_checker($role,\@images,\@links,\@bgs,
 9174:                                     \@logintext,$confhash,\%changes); 
 9175:         }
 9176:     }
 9177:     return ($errors,%changes);
 9178: }
 9179: 
 9180: sub config_check {
 9181:     my ($dom,$confname,$servadm) = @_;
 9182:     my ($configuserok,$author_ok,$switchserver,%currroles);
 9183:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 9184:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 9185:                                                    $confname,$servadm);
 9186:     if ($configuserok eq 'ok') {
 9187:         $switchserver = &check_switchserver($dom,$confname);
 9188:         if ($switchserver eq '') {
 9189:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 9190:         }
 9191:     }
 9192:     return ($configuserok,$author_ok,$switchserver);
 9193: }
 9194: 
 9195: sub default_change_checker {
 9196:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 9197:     foreach my $item (@{$links}) {
 9198:         if ($confhash->{$role}{$item}) {
 9199:             $changes->{$role}{'links'}{$item} = 1;
 9200:         }
 9201:     }
 9202:     foreach my $item (@{$bgs}) {
 9203:         if ($confhash->{$role}{$item}) {
 9204:             $changes->{$role}{'bgs'}{$item} = 1;
 9205:         }
 9206:     }
 9207:     foreach my $item (@{$logintext}) {
 9208:         if ($confhash->{$role}{$item}) {
 9209:             $changes->{$role}{'logintext'}{$item} = 1;
 9210:         }
 9211:     }
 9212:     foreach my $img (@{$images}) {
 9213:         if ($env{'form.'.$role.'_del_'.$img}) {
 9214:             $confhash->{$role}{$img} = '';
 9215:             $changes->{$role}{'images'}{$img} = 1;
 9216:         }
 9217:         if ($role eq 'login') {
 9218:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9219:                 $changes->{$role}{'showlogo'}{$img} = 1;
 9220:             }
 9221:         }
 9222:     }
 9223:     if ($confhash->{$role}{'font'}) {
 9224:         $changes->{$role}{'font'} = 1;
 9225:     }
 9226: }
 9227: 
 9228: sub display_colorchgs {
 9229:     my ($dom,$changes,$roles,$confhash) = @_;
 9230:     my (%choices,$resulttext);
 9231:     if (!grep(/^login$/,@{$roles})) {
 9232:         $resulttext = &mt('Changes made:').'<br />';
 9233:     }
 9234:     foreach my $role (@{$roles}) {
 9235:         if ($role eq 'login') {
 9236:             %choices = &login_choices();
 9237:         } else {
 9238:             %choices = &color_font_choices();
 9239:         }
 9240:         if (ref($changes->{$role}) eq 'HASH') {
 9241:             if ($role ne 'login') {
 9242:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 9243:             }
 9244:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 9245:                 if ($role ne 'login') {
 9246:                     $resulttext .= '<ul>';
 9247:                 }
 9248:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 9249:                     if ($role ne 'login') {
 9250:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 9251:                     }
 9252:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 9253:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 9254:                             if ($confhash->{$role}{$key}{$item}) {
 9255:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 9256:                             } else {
 9257:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 9258:                             }
 9259:                         } elsif ($confhash->{$role}{$item} eq '') {
 9260:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 9261:                         } else {
 9262:                             my $newitem = $confhash->{$role}{$item};
 9263:                             if ($key eq 'images') {
 9264:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 9265:                             }
 9266:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 9267:                         }
 9268:                     }
 9269:                     if ($role ne 'login') {
 9270:                         $resulttext .= '</ul></li>';
 9271:                     }
 9272:                 } else {
 9273:                     if ($confhash->{$role}{$key} eq '') {
 9274:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 9275:                     } else {
 9276:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 9277:                     }
 9278:                 }
 9279:                 if ($role ne 'login') {
 9280:                     $resulttext .= '</ul>';
 9281:                 }
 9282:             }
 9283:         }
 9284:     }
 9285:     return $resulttext;
 9286: }
 9287: 
 9288: sub thumb_dimensions {
 9289:     return ('200','50');
 9290: }
 9291: 
 9292: sub check_dimensions {
 9293:     my ($inputfile) = @_;
 9294:     my ($fullwidth,$fullheight);
 9295:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 9296:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 9297:             my $imageinfo = <PIPE>;
 9298:             if (!close(PIPE)) {
 9299:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 9300:             }
 9301:             chomp($imageinfo);
 9302:             my ($fullsize) = 
 9303:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 9304:             if ($fullsize) {
 9305:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 9306:             }
 9307:         }
 9308:     }
 9309:     return ($fullwidth,$fullheight);
 9310: }
 9311: 
 9312: sub check_configuser {
 9313:     my ($uhome,$dom,$confname,$servadm) = @_;
 9314:     my ($configuserok,%currroles);
 9315:     if ($uhome eq 'no_host') {
 9316:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 9317:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
 9318:         $configuserok = 
 9319:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 9320:                              $configpass,'','','','','',undef,$servadm);
 9321:     } else {
 9322:         $configuserok = 'ok';
 9323:         %currroles = 
 9324:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 9325:     }
 9326:     return ($configuserok,%currroles);
 9327: }
 9328: 
 9329: sub check_authorstatus {
 9330:     my ($dom,$confname,%currroles) = @_;
 9331:     my $author_ok;
 9332:     if (!$currroles{':'.$dom.':au'}) {
 9333:         my $start = time;
 9334:         my $end = 0;
 9335:         $author_ok = 
 9336:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 9337:                                         'au',$end,$start,'','','domconfig');
 9338:     } else {
 9339:         $author_ok = 'ok';
 9340:     }
 9341:     return $author_ok;
 9342: }
 9343: 
 9344: sub publishlogo {
 9345:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 9346:     my ($output,$fname,$logourl);
 9347:     if ($action eq 'upload') {
 9348:         $fname=$env{'form.'.$formname.'.filename'};
 9349:         chop($env{'form.'.$formname});
 9350:     } else {
 9351:         ($fname) = ($formname =~ /([^\/]+)$/);
 9352:     }
 9353:     if ($savefileas ne '') {
 9354:         $fname = $savefileas;
 9355:     }
 9356:     $fname=&Apache::lonnet::clean_filename($fname);
 9357: # See if there is anything left
 9358:     unless ($fname) { return ('error: no uploaded file'); }
 9359:     $fname="$subdir/$fname";
 9360:     my $docroot=$r->dir_config('lonDocRoot');
 9361:     my $filepath="$docroot/priv";
 9362:     my $relpath = "$dom/$confname";
 9363:     my ($fnamepath,$file,$fetchthumb);
 9364:     $file=$fname;
 9365:     if ($fname=~m|/|) {
 9366:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 9367:     }
 9368:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 9369:     my $count;
 9370:     for ($count=5;$count<=$#parts;$count++) {
 9371:         $filepath.="/$parts[$count]";
 9372:         if ((-e $filepath)!=1) {
 9373:             mkdir($filepath,02770);
 9374:         }
 9375:     }
 9376:     # Check for bad extension and disallow upload
 9377:     if ($file=~/\.(\w+)$/ &&
 9378:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 9379:         $output = 
 9380:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 9381:     } elsif ($file=~/\.(\w+)$/ &&
 9382:         !defined(&Apache::loncommon::fileembstyle($1))) {
 9383:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 9384:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 9385:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 9386:     } elsif (-d "$filepath/$file") {
 9387:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 9388:     } else {
 9389:         my $source = $filepath.'/'.$file;
 9390:         my $logfile;
 9391:         if (!open($logfile,">>",$source.'.log')) {
 9392:             return (&mt('No write permission to Authoring Space'));
 9393:         }
 9394:         print $logfile
 9395: "\n================= Publish ".localtime()." ================\n".
 9396: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 9397: # Save the file
 9398:         if (!open(FH,">",$source)) {
 9399:             &Apache::lonnet::logthis('Failed to create '.$source);
 9400:             return (&mt('Failed to create file'));
 9401:         }
 9402:         if ($action eq 'upload') {
 9403:             if (!print FH ($env{'form.'.$formname})) {
 9404:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 9405:                 return (&mt('Failed to write file'));
 9406:             }
 9407:         } else {
 9408:             my $original = &Apache::lonnet::filelocation('',$formname);
 9409:             if(!copy($original,$source)) {
 9410:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 9411:                 return (&mt('Failed to write file'));
 9412:             }
 9413:         }
 9414:         close(FH);
 9415:         chmod(0660, $source); # Permissions to rw-rw---.
 9416: 
 9417:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 9418:         my $copyfile=$targetdir.'/'.$file;
 9419: 
 9420:         my @parts=split(/\//,$targetdir);
 9421:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 9422:         for (my $count=5;$count<=$#parts;$count++) {
 9423:             $path.="/$parts[$count]";
 9424:             if (!-e $path) {
 9425:                 print $logfile "\nCreating directory ".$path;
 9426:                 mkdir($path,02770);
 9427:             }
 9428:         }
 9429:         my $versionresult;
 9430:         if (-e $copyfile) {
 9431:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 9432:         } else {
 9433:             $versionresult = 'ok';
 9434:         }
 9435:         if ($versionresult eq 'ok') {
 9436:             if (copy($source,$copyfile)) {
 9437:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 9438:                 $output = 'ok';
 9439:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 9440:                 push(@{$modified_urls},[$copyfile,$source]);
 9441:                 my $metaoutput = 
 9442:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 9443:                 unless ($registered_cleanup) {
 9444:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9445:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9446:                     $registered_cleanup=1;
 9447:                 }
 9448:             } else {
 9449:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 9450:                 $output = &mt('Failed to copy file to RES space').", $!";
 9451:             }
 9452:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 9453:                 my $inputfile = $filepath.'/'.$file;
 9454:                 my $outfile = $filepath.'/'.'tn-'.$file;
 9455:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 9456:                 if ($fullwidth ne '' && $fullheight ne '') { 
 9457:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 9458:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 9459:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 9460:                         system({$args[0]} @args);
 9461:                         chmod(0660, $filepath.'/tn-'.$file);
 9462:                         if (-e $outfile) {
 9463:                             my $copyfile=$targetdir.'/tn-'.$file;
 9464:                             if (copy($outfile,$copyfile)) {
 9465:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 9466:                                 my $thumb_metaoutput = 
 9467:                                     &write_metadata($dom,$confname,$formname,
 9468:                                                     $targetdir,'tn-'.$file,$logfile);
 9469:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 9470:                                 unless ($registered_cleanup) {
 9471:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9472:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9473:                                     $registered_cleanup=1;
 9474:                                 }
 9475:                             } else {
 9476:                                 print $logfile "\nUnable to write ".$copyfile.
 9477:                                                ':'.$!."\n";
 9478:                             }
 9479:                         }
 9480:                     }
 9481:                 }
 9482:             }
 9483:         } else {
 9484:             $output = $versionresult;
 9485:         }
 9486:     }
 9487:     return ($output,$logourl);
 9488: }
 9489: 
 9490: sub logo_versioning {
 9491:     my ($targetdir,$file,$logfile) = @_;
 9492:     my $target = $targetdir.'/'.$file;
 9493:     my ($maxversion,$fn,$extn,$output);
 9494:     $maxversion = 0;
 9495:     if ($file =~ /^(.+)\.(\w+)$/) {
 9496:         $fn=$1;
 9497:         $extn=$2;
 9498:     }
 9499:     opendir(DIR,$targetdir);
 9500:     while (my $filename=readdir(DIR)) {
 9501:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 9502:             $maxversion=($1>$maxversion)?$1:$maxversion;
 9503:         }
 9504:     }
 9505:     $maxversion++;
 9506:     print $logfile "\nCreating old version ".$maxversion."\n";
 9507:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 9508:     if (copy($target,$copyfile)) {
 9509:         print $logfile "Copied old target to ".$copyfile."\n";
 9510:         $copyfile=$copyfile.'.meta';
 9511:         if (copy($target.'.meta',$copyfile)) {
 9512:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 9513:             $output = 'ok';
 9514:         } else {
 9515:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 9516:             $output = &mt('Failed to copy old meta').", $!, ";
 9517:         }
 9518:     } else {
 9519:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 9520:         $output = &mt('Failed to copy old target').", $!, ";
 9521:     }
 9522:     return $output;
 9523: }
 9524: 
 9525: sub write_metadata {
 9526:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 9527:     my (%metadatafields,%metadatakeys,$output);
 9528:     $metadatafields{'title'}=$formname;
 9529:     $metadatafields{'creationdate'}=time;
 9530:     $metadatafields{'lastrevisiondate'}=time;
 9531:     $metadatafields{'copyright'}='public';
 9532:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 9533:                                          $env{'user.domain'};
 9534:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 9535:     $metadatafields{'domain'}=$dom;
 9536:     {
 9537:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 9538:         my $mfh;
 9539:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 9540:             foreach (sort(keys(%metadatafields))) {
 9541:                 unless ($_=~/\./) {
 9542:                     my $unikey=$_;
 9543:                     $unikey=~/^([A-Za-z]+)/;
 9544:                     my $tag=$1;
 9545:                     $tag=~tr/A-Z/a-z/;
 9546:                     print $mfh "\n\<$tag";
 9547:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 9548:                         my $value=$metadatafields{$unikey.'.'.$_};
 9549:                         $value=~s/\"/\'\'/g;
 9550:                         print $mfh ' '.$_.'="'.$value.'"';
 9551:                     }
 9552:                     print $mfh '>'.
 9553:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 9554:                             .'</'.$tag.'>';
 9555:                 }
 9556:             }
 9557:             $output = 'ok';
 9558:             print $logfile "\nWrote metadata";
 9559:             close($mfh);
 9560:         } else {
 9561:             print $logfile "\nFailed to open metadata file";
 9562:             $output = &mt('Could not write metadata');
 9563:         }
 9564:     }
 9565:     return $output;
 9566: }
 9567: 
 9568: sub notifysubscribed {
 9569:     foreach my $targetsource (@{$modified_urls}){
 9570:         next unless (ref($targetsource) eq 'ARRAY');
 9571:         my ($target,$source)=@{$targetsource};
 9572:         if ($source ne '') {
 9573:             if (open(my $logfh,">>",$source.'.log')) {
 9574:                 print $logfh "\nCleanup phase: Notifications\n";
 9575:                 my @subscribed=&subscribed_hosts($target);
 9576:                 foreach my $subhost (@subscribed) {
 9577:                     print $logfh "\nNotifying host ".$subhost.':';
 9578:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 9579:                     print $logfh $reply;
 9580:                 }
 9581:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 9582:                 foreach my $subhost (@subscribedmeta) {
 9583:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 9584:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 9585:                                                         $subhost);
 9586:                     print $logfh $reply;
 9587:                 }
 9588:                 print $logfh "\n============ Done ============\n";
 9589:                 close($logfh);
 9590:             }
 9591:         }
 9592:     }
 9593:     return OK;
 9594: }
 9595: 
 9596: sub subscribed_hosts {
 9597:     my ($target) = @_;
 9598:     my @subscribed;
 9599:     if (open(my $fh,"<","$target.subscription")) {
 9600:         while (my $subline=<$fh>) {
 9601:             if ($subline =~ /^($match_lonid):/) {
 9602:                 my $host = $1;
 9603:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 9604:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 9605:                         push(@subscribed,$host);
 9606:                     }
 9607:                 }
 9608:             }
 9609:         }
 9610:     }
 9611:     return @subscribed;
 9612: }
 9613: 
 9614: sub check_switchserver {
 9615:     my ($dom,$confname) = @_;
 9616:     my ($allowed,$switchserver);
 9617:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 9618:     if ($home eq 'no_host') {
 9619:         $home = &Apache::lonnet::domain($dom,'primary');
 9620:     }
 9621:     my @ids=&Apache::lonnet::current_machine_ids();
 9622:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 9623:     if (!$allowed) {
 9624: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 9625:     }
 9626:     return $switchserver;
 9627: }
 9628: 
 9629: sub modify_quotas {
 9630:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9631:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 9632:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 9633:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 9634:         $validationfieldsref);
 9635:     if ($action eq 'quotas') {
 9636:         $context = 'tools'; 
 9637:     } else {
 9638:         $context = $action;
 9639:     }
 9640:     if ($context eq 'requestcourses') {
 9641:         @usertools = ('official','unofficial','community','textbook');
 9642:         @options =('norequest','approval','validate','autolimit');
 9643:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 9644:         %titles = &courserequest_titles();
 9645:         $toolregexp = join('|',@usertools);
 9646:         %conditions = &courserequest_conditions();
 9647:         $confname = $dom.'-domainconfig';
 9648:         my $servadm = $r->dir_config('lonAdmEMail');
 9649:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9650:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 9651:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 9652:     } elsif ($context eq 'requestauthor') {
 9653:         @usertools = ('author');
 9654:         %titles = &authorrequest_titles();
 9655:     } else {
 9656:         @usertools = ('aboutme','blog','webdav','portfolio');
 9657:         %titles = &tool_titles();
 9658:     }
 9659:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9660:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9661:     foreach my $key (keys(%env)) {
 9662:         if ($context eq 'requestcourses') {
 9663:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9664:                 my $item = $1;
 9665:                 my $type = $2;
 9666:                 if ($type =~ /^limit_(.+)/) {
 9667:                     $limithash{$item}{$1} = $env{$key};
 9668:                 } else {
 9669:                     $confhash{$item}{$type} = $env{$key};
 9670:                 }
 9671:             }
 9672:         } elsif ($context eq 'requestauthor') {
 9673:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 9674:                 $confhash{$1} = $env{$key};
 9675:             }
 9676:         } else {
 9677:             if ($key =~ /^form\.quota_(.+)$/) {
 9678:                 $confhash{'defaultquota'}{$1} = $env{$key};
 9679:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 9680:                 $confhash{'authorquota'}{$1} = $env{$key};
 9681:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 9682:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 9683:             }
 9684:         }
 9685:     }
 9686:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9687:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 9688:         @approvalnotify = sort(@approvalnotify);
 9689:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 9690:         my @crstypes = ('official','unofficial','community','textbook');
 9691:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 9692:         foreach my $type (@hasuniquecode) {
 9693:             if (grep(/^\Q$type\E$/,@crstypes)) {
 9694:                 $confhash{'uniquecode'}{$type} = 1;
 9695:             }
 9696:         }
 9697:         my (%newbook,%allpos);
 9698:         if ($context eq 'requestcourses') {
 9699:             foreach my $type ('textbooks','templates') {
 9700:                 @{$allpos{$type}} = (); 
 9701:                 my $invalid;
 9702:                 if ($type eq 'textbooks') {
 9703:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 9704:                 } else {
 9705:                     $invalid = &mt('Invalid LON-CAPA course for template');
 9706:                 }
 9707:                 if ($env{'form.'.$type.'_addbook'}) {
 9708:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 9709:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 9710:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 9711:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 9712:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9713:                         } else {
 9714:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 9715:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 9716:                             $position =~ s/\D+//g;
 9717:                             if ($position ne '') {
 9718:                                 $allpos{$type}[$position] = $newbook{$type};
 9719:                             }
 9720:                         }
 9721:                     } else {
 9722:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9723:                     }
 9724:                 }
 9725:             } 
 9726:         }
 9727:         if (ref($domconfig{$action}) eq 'HASH') {
 9728:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 9729:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 9730:                     $changes{'notify'}{'approval'} = 1;
 9731:                 }
 9732:             } else {
 9733:                 if ($confhash{'notify'}{'approval'}) {
 9734:                     $changes{'notify'}{'approval'} = 1;
 9735:                 }
 9736:             }
 9737:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 9738:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9739:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 9740:                         unless ($confhash{'uniquecode'}{$crstype}) {
 9741:                             $changes{'uniquecode'} = 1;
 9742:                         }
 9743:                     }
 9744:                     unless ($changes{'uniquecode'}) {
 9745:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 9746:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 9747:                                 $changes{'uniquecode'} = 1;
 9748:                             }
 9749:                         }
 9750:                     }
 9751:                } else {
 9752:                    $changes{'uniquecode'} = 1;
 9753:                }
 9754:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 9755:                 $changes{'uniquecode'} = 1;
 9756:             }
 9757:             if ($context eq 'requestcourses') {
 9758:                 foreach my $type ('textbooks','templates') {
 9759:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9760:                         my %deletions;
 9761:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 9762:                         if (@todelete) {
 9763:                             map { $deletions{$_} = 1; } @todelete;
 9764:                         }
 9765:                         my %imgdeletions;
 9766:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 9767:                         if (@todeleteimages) {
 9768:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9769:                         }
 9770:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 9771:                         for (my $i=0; $i<=$maxnum; $i++) {
 9772:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 9773:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 9774:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 9775:                                 if ($deletions{$key}) {
 9776:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 9777:                                         #FIXME need to obsolete item in RES space
 9778:                                     }
 9779:                                     next;
 9780:                                 } else {
 9781:                                     my $newpos = $env{'form.'.$itemid};
 9782:                                     $newpos =~ s/\D+//g;
 9783:                                     foreach my $item ('subject','title','publisher','author') {
 9784:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9785:                                                  ($type eq 'templates'));
 9786:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 9787:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 9788:                                             $changes{$type}{$key} = 1;
 9789:                                         }
 9790:                                     }
 9791:                                     $allpos{$type}[$newpos] = $key;
 9792:                                 }
 9793:                                 if ($imgdeletions{$key}) {
 9794:                                     $changes{$type}{$key} = 1;
 9795:                                     #FIXME need to obsolete item in RES space
 9796:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 9797:                                     my ($cdom,$cnum) = split(/_/,$key);
 9798:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9799:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9800:                                     } else {
 9801:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 9802:                                                                                       $cdom,$cnum,$type,$configuserok,
 9803:                                                                                       $switchserver,$author_ok);
 9804:                                         if ($imgurl) {
 9805:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 9806:                                             $changes{$type}{$key} = 1; 
 9807:                                         }
 9808:                                         if ($error) {
 9809:                                             &Apache::lonnet::logthis($error);
 9810:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9811:                                         }
 9812:                                     } 
 9813:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 9814:                                     $confhash{$type}{$key}{'image'} = 
 9815:                                         $domconfig{$action}{$type}{$key}{'image'};
 9816:                                 }
 9817:                             }
 9818:                         }
 9819:                     }
 9820:                 }
 9821:             }
 9822:         } else {
 9823:             if ($confhash{'notify'}{'approval'}) {
 9824:                 $changes{'notify'}{'approval'} = 1;
 9825:             }
 9826:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 9827:                 $changes{'uniquecode'} = 1;
 9828:             }
 9829:         }
 9830:         if ($context eq 'requestcourses') {
 9831:             foreach my $type ('textbooks','templates') {
 9832:                 if ($newbook{$type}) {
 9833:                     $changes{$type}{$newbook{$type}} = 1;
 9834:                     foreach my $item ('subject','title','publisher','author') {
 9835:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9836:                                  ($type eq 'template'));
 9837:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 9838:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 9839:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 9840:                         }
 9841:                     }
 9842:                     if ($type eq 'textbooks') {
 9843:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 9844:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 9845:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9846:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9847:                             } else {
 9848:                                 my ($imageurl,$error) =
 9849:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 9850:                                                             $configuserok,$switchserver,$author_ok);
 9851:                                 if ($imageurl) {
 9852:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 9853:                                 }
 9854:                                 if ($error) {
 9855:                                     &Apache::lonnet::logthis($error);
 9856:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9857:                                 }
 9858:                             }
 9859:                         }
 9860:                     }
 9861:                 }
 9862:                 if (@{$allpos{$type}} > 0) {
 9863:                     my $idx = 0;
 9864:                     foreach my $item (@{$allpos{$type}}) {
 9865:                         if ($item ne '') {
 9866:                             $confhash{$type}{$item}{'order'} = $idx;
 9867:                             if (ref($domconfig{$action}) eq 'HASH') {
 9868:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9869:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 9870:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 9871:                                             $changes{$type}{$item} = 1;
 9872:                                         }
 9873:                                     }
 9874:                                 }
 9875:                             }
 9876:                             $idx ++;
 9877:                         }
 9878:                     }
 9879:                 }
 9880:             }
 9881:             if (ref($validationitemsref) eq 'ARRAY') {
 9882:                 foreach my $item (@{$validationitemsref}) {
 9883:                     if ($item eq 'fields') {
 9884:                         my @changed;
 9885:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9886:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9887:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9888:                         }
 9889:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9890:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9891:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9892:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9893:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9894:                                 } else {
 9895:                                     @changed = @{$confhash{'validation'}{$item}};
 9896:                                 }
 9897:                             } else {
 9898:                                 @changed = @{$confhash{'validation'}{$item}};
 9899:                             }
 9900:                         } else {
 9901:                             @changed = @{$confhash{'validation'}{$item}};
 9902:                         }
 9903:                         if (@changed) {
 9904:                             if ($confhash{'validation'}{$item}) {
 9905:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9906:                             } else {
 9907:                                 $changes{'validation'}{$item} = &mt('None');
 9908:                             }
 9909:                         }
 9910:                     } else {
 9911:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9912:                         if ($item eq 'markup') {
 9913:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9914:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9915:                             }
 9916:                         }
 9917:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9918:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9919:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9920:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9921:                                 }
 9922:                             } else {
 9923:                                 if ($confhash{'validation'}{$item} ne '') {
 9924:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9925:                                 }
 9926:                             }
 9927:                         } else {
 9928:                             if ($confhash{'validation'}{$item} ne '') {
 9929:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9930:                             }
 9931:                         }
 9932:                     }
 9933:                 }
 9934:             }
 9935:             if ($env{'form.validationdc'}) {
 9936:                 my $newval = $env{'form.validationdc'};
 9937:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9938:                 if (exists($domcoords{$newval})) {
 9939:                     $confhash{'validation'}{'dc'} = $newval;
 9940:                 }
 9941:             }
 9942:             if (ref($confhash{'validation'}) eq 'HASH') {
 9943:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9944:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9945:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9946:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9947:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9948:                                     $changes{'validation'}{'dc'} = &mt('None');
 9949:                                 } else {
 9950:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9951:                                 }
 9952:                             }
 9953:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9954:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9955:                         }
 9956:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9957:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9958:                     }
 9959:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9960:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9961:                 }
 9962:             } else {
 9963:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9964:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9965:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9966:                             $changes{'validation'}{'dc'} = &mt('None');
 9967:                         }
 9968:                     }
 9969:                 }
 9970:             }
 9971:         }
 9972:     } else {
 9973:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9974:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9975:     }
 9976:     foreach my $item (@usertools) {
 9977:         foreach my $type (@{$types},'default','_LC_adv') {
 9978:             my $unset; 
 9979:             if ($context eq 'requestcourses') {
 9980:                 $unset = '0';
 9981:                 if ($type eq '_LC_adv') {
 9982:                     $unset = '';
 9983:                 }
 9984:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9985:                     $confhash{$item}{$type} .= '=';
 9986:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9987:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9988:                     }
 9989:                 }
 9990:             } elsif ($context eq 'requestauthor') {
 9991:                 $unset = '0';
 9992:                 if ($type eq '_LC_adv') {
 9993:                     $unset = '';
 9994:                 }
 9995:             } else {
 9996:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9997:                     $confhash{$item}{$type} = 1;
 9998:                 } else {
 9999:                     $confhash{$item}{$type} = 0;
10000:                 }
10001:             }
10002:             if (ref($domconfig{$action}) eq 'HASH') {
10003:                 if ($action eq 'requestauthor') {
10004:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
10005:                         $changes{$type} = 1;
10006:                     }
10007:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
10008:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
10009:                         $changes{$item}{$type} = 1;
10010:                     }
10011:                 } else {
10012:                     if ($context eq 'requestcourses') {
10013:                         if ($confhash{$item}{$type} ne $unset) {
10014:                             $changes{$item}{$type} = 1;
10015:                         }
10016:                     } else {
10017:                         if (!$confhash{$item}{$type}) {
10018:                             $changes{$item}{$type} = 1;
10019:                         }
10020:                     }
10021:                 }
10022:             } else {
10023:                 if ($context eq 'requestcourses') {
10024:                     if ($confhash{$item}{$type} ne $unset) {
10025:                         $changes{$item}{$type} = 1;
10026:                     }
10027:                 } elsif ($context eq 'requestauthor') {
10028:                     if ($confhash{$type} ne $unset) {
10029:                         $changes{$type} = 1;
10030:                     }
10031:                 } else {
10032:                     if (!$confhash{$item}{$type}) {
10033:                         $changes{$item}{$type} = 1;
10034:                     }
10035:                 }
10036:             }
10037:         }
10038:     }
10039:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10040:         if (ref($domconfig{'quotas'}) eq 'HASH') {
10041:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10042:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
10043:                     if (exists($confhash{'defaultquota'}{$key})) {
10044:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
10045:                             $changes{'defaultquota'}{$key} = 1;
10046:                         }
10047:                     } else {
10048:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
10049:                     }
10050:                 }
10051:             } else {
10052:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
10053:                     if (exists($confhash{'defaultquota'}{$key})) {
10054:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
10055:                             $changes{'defaultquota'}{$key} = 1;
10056:                         }
10057:                     } else {
10058:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
10059:                     }
10060:                 }
10061:             }
10062:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10063:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
10064:                     if (exists($confhash{'authorquota'}{$key})) {
10065:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
10066:                             $changes{'authorquota'}{$key} = 1;
10067:                         }
10068:                     } else {
10069:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
10070:                     }
10071:                 }
10072:             }
10073:         }
10074:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
10075:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
10076:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10077:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10078:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
10079:                             $changes{'defaultquota'}{$key} = 1;
10080:                         }
10081:                     } else {
10082:                         if (!exists($domconfig{'quotas'}{$key})) {
10083:                             $changes{'defaultquota'}{$key} = 1;
10084:                         }
10085:                     }
10086:                 } else {
10087:                     $changes{'defaultquota'}{$key} = 1;
10088:                 }
10089:             }
10090:         }
10091:         if (ref($confhash{'authorquota'}) eq 'HASH') {
10092:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
10093:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10094:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10095:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
10096:                             $changes{'authorquota'}{$key} = 1;
10097:                         }
10098:                     } else {
10099:                         $changes{'authorquota'}{$key} = 1;
10100:                     }
10101:                 } else {
10102:                     $changes{'authorquota'}{$key} = 1;
10103:                 }
10104:             }
10105:         }
10106:     }
10107: 
10108:     if ($context eq 'requestauthor') {
10109:         $domdefaults{'requestauthor'} = \%confhash;
10110:     } else {
10111:         foreach my $key (keys(%confhash)) {
10112:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
10113:                 $domdefaults{$key} = $confhash{$key};
10114:             }
10115:         }
10116:     }
10117: 
10118:     my %quotahash = (
10119:                       $action => { %confhash }
10120:                     );
10121:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
10122:                                              $dom);
10123:     if ($putresult eq 'ok') {
10124:         if (keys(%changes) > 0) {
10125:             my $cachetime = 24*60*60;
10126:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10127:             if (ref($lastactref) eq 'HASH') {
10128:                 $lastactref->{'domdefaults'} = 1;
10129:             }
10130:             $resulttext = &mt('Changes made:').'<ul>';
10131:             unless (($context eq 'requestcourses') ||
10132:                     ($context eq 'requestauthor')) {
10133:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
10134:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
10135:                     foreach my $type (@{$types},'default') {
10136:                         if (defined($changes{'defaultquota'}{$type})) {
10137:                             my $typetitle = $usertypes->{$type};
10138:                             if ($type eq 'default') {
10139:                                 $typetitle = $othertitle;
10140:                             }
10141:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
10142:                         }
10143:                     }
10144:                     $resulttext .= '</ul></li>';
10145:                 }
10146:                 if (ref($changes{'authorquota'}) eq 'HASH') {
10147:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
10148:                     foreach my $type (@{$types},'default') {
10149:                         if (defined($changes{'authorquota'}{$type})) {
10150:                             my $typetitle = $usertypes->{$type};
10151:                             if ($type eq 'default') {
10152:                                 $typetitle = $othertitle;
10153:                             }
10154:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
10155:                         }
10156:                     }
10157:                     $resulttext .= '</ul></li>';
10158:                 }
10159:             }
10160:             my %newenv;
10161:             foreach my $item (@usertools) {
10162:                 my (%haschgs,%inconf);
10163:                 if ($context eq 'requestauthor') {
10164:                     %haschgs = %changes;
10165:                     %inconf = %confhash;
10166:                 } else {
10167:                     if (ref($changes{$item}) eq 'HASH') {
10168:                         %haschgs = %{$changes{$item}};
10169:                     }
10170:                     if (ref($confhash{$item}) eq 'HASH') {
10171:                         %inconf = %{$confhash{$item}};
10172:                     }
10173:                 }
10174:                 if (keys(%haschgs) > 0) {
10175:                     my $newacc = 
10176:                         &Apache::lonnet::usertools_access($env{'user.name'},
10177:                                                           $env{'user.domain'},
10178:                                                           $item,'reload',$context);
10179:                     if (($context eq 'requestcourses') ||
10180:                         ($context eq 'requestauthor')) {
10181:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
10182:                             $newenv{'environment.canrequest.'.$item} = $newacc;
10183:                         }
10184:                     } else {
10185:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
10186:                             $newenv{'environment.availabletools.'.$item} = $newacc;
10187:                         }
10188:                     }
10189:                     unless ($context eq 'requestauthor') {
10190:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
10191:                     }
10192:                     foreach my $type (@{$types},'default','_LC_adv') {
10193:                         if ($haschgs{$type}) {
10194:                             my $typetitle = $usertypes->{$type};
10195:                             if ($type eq 'default') {
10196:                                 $typetitle = $othertitle;
10197:                             } elsif ($type eq '_LC_adv') {
10198:                                 $typetitle = 'LON-CAPA Advanced Users'; 
10199:                             }
10200:                             if ($inconf{$type}) {
10201:                                 if ($context eq 'requestcourses') {
10202:                                     my $cond;
10203:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
10204:                                         if ($1 eq '') {
10205:                                             $cond = &mt('(Automatic processing of any request).');
10206:                                         } else {
10207:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
10208:                                         }
10209:                                     } else { 
10210:                                         $cond = $conditions{$inconf{$type}};
10211:                                     }
10212:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
10213:                                 } elsif ($context eq 'requestauthor') {
10214:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
10215:                                                              $titles{$inconf{$type}},$typetitle);
10216: 
10217:                                 } else {
10218:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
10219:                                 }
10220:                             } else {
10221:                                 if ($type eq '_LC_adv') {
10222:                                     if ($inconf{$type} eq '0') {
10223:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10224:                                     } else { 
10225:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
10226:                                     }
10227:                                 } else {
10228:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10229:                                 }
10230:                             }
10231:                         }
10232:                     }
10233:                     unless ($context eq 'requestauthor') {
10234:                         $resulttext .= '</ul></li>';
10235:                     }
10236:                 }
10237:             }
10238:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
10239:                 if (ref($changes{'notify'}) eq 'HASH') {
10240:                     if ($changes{'notify'}{'approval'}) {
10241:                         if (ref($confhash{'notify'}) eq 'HASH') {
10242:                             if ($confhash{'notify'}{'approval'}) {
10243:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
10244:                             } else {
10245:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
10246:                             }
10247:                         }
10248:                     }
10249:                 }
10250:             }
10251:             if ($action eq 'requestcourses') {
10252:                 my @offon = ('off','on');
10253:                 if ($changes{'uniquecode'}) {
10254:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
10255:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
10256:                         $resulttext .= '<li>'.
10257:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
10258:                                        '</li>';
10259:                     } else {
10260:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
10261:                                        '</li>';
10262:                     }
10263:                 }
10264:                 foreach my $type ('textbooks','templates') {
10265:                     if (ref($changes{$type}) eq 'HASH') {
10266:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
10267:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
10268:                             my %coursehash = &Apache::lonnet::coursedescription($key);
10269:                             my $coursetitle = $coursehash{'description'};
10270:                             my $position = $confhash{$type}{$key}{'order'} + 1;
10271:                             $resulttext .= '<li>';
10272:                             foreach my $item ('subject','title','publisher','author') {
10273:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
10274:                                          ($type eq 'templates'));
10275:                                 my $name = $item.':';
10276:                                 $name =~ s/^(\w)/\U$1/;
10277:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
10278:                             }
10279:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
10280:                             if ($type eq 'textbooks') {
10281:                                 if ($confhash{$type}{$key}{'image'}) {
10282:                                     $resulttext .= ' '.&mt('Image: [_1]',
10283:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
10284:                                                    ' alt="Textbook cover" />').'<br />';
10285:                                 }
10286:                             }
10287:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
10288:                         }
10289:                         $resulttext .= '</ul></li>';
10290:                     }
10291:                 }
10292:                 if (ref($changes{'validation'}) eq 'HASH') {
10293:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
10294:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
10295:                         foreach my $item (@{$validationitemsref}) {
10296:                             if (exists($changes{'validation'}{$item})) {
10297:                                 if ($item eq 'markup') {
10298:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
10299:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
10300:                                 } else {
10301:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
10302:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
10303:                                 }
10304:                             }
10305:                         }
10306:                         if (exists($changes{'validation'}{'dc'})) {
10307:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
10308:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
10309:                         }
10310:                     }
10311:                 }
10312:             }
10313:             $resulttext .= '</ul>';
10314:             if (keys(%newenv)) {
10315:                 &Apache::lonnet::appenv(\%newenv);
10316:             }
10317:         } else {
10318:             if ($context eq 'requestcourses') {
10319:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
10320:             } elsif ($context eq 'requestauthor') {
10321:                 $resulttext = &mt('No changes made to rights to request author space.');
10322:             } else {
10323:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
10324:             }
10325:         }
10326:     } else {
10327:         $resulttext = '<span class="LC_error">'.
10328: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10329:     }
10330:     if ($errors) {
10331:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
10332:                        '<ul>'.$errors.'</ul></p>';
10333:     }
10334:     return $resulttext;
10335: }
10336: 
10337: sub process_textbook_image {
10338:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
10339:     my $filename = $env{'form.'.$caller.'.filename'};
10340:     my ($error,$url);
10341:     my ($width,$height) = (50,50);
10342:     if ($configuserok eq 'ok') {
10343:         if ($switchserver) {
10344:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
10345:                          $switchserver);
10346:         } elsif ($author_ok eq 'ok') {
10347:             my ($result,$imageurl) =
10348:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10349:                              "$type/$cdom/$cnum/cover",$width,$height);
10350:             if ($result eq 'ok') {
10351:                 $url = $imageurl;
10352:             } else {
10353:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10354:             }
10355:         } else {
10356:             $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);
10357:         }
10358:     } else {
10359:         $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);
10360:     }
10361:     return ($url,$error);
10362: }
10363: 
10364: sub modify_ltitools {
10365:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10366:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10367:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
10368:     my $confname = $dom.'-domainconfig';
10369:     my $servadm = $r->dir_config('lonAdmEMail');
10370:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10371:     my (%posslti,%possfield);
10372:     my @courseroles = ('cc','in','ta','ep','st');
10373:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
10374:     map { $posslti{$_} = 1; } @ltiroles;
10375:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
10376:     map { $possfield{$_} = 1; } @allfields;
10377:     my %lt = &ltitools_names();
10378:     if ($env{'form.ltitools_add'}) {
10379:         my $title = $env{'form.ltitools_add_title'};
10380:         $title =~ s/(`)/'/g;
10381:         ($newid,my $error) = &get_ltitools_id($dom,$title);
10382:         if ($newid) {
10383:             my $position = $env{'form.ltitools_add_pos'};
10384:             $position =~ s/\D+//g;
10385:             if ($position ne '') {
10386:                 $allpos[$position] = $newid;
10387:             }
10388:             $changes{$newid} = 1;
10389:             foreach my $item ('title','url','key','secret') {
10390:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
10391:                 if ($env{'form.ltitools_add_'.$item}) {
10392:                     if (($item eq 'key') || ($item eq 'secret')) {
10393:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10394:                     } else {
10395:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10396:                     }
10397:                 }
10398:             }
10399:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
10400:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
10401:             }
10402:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
10403:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
10404:             }
10405:             foreach my $item ('width','height','linktext','explanation') {
10406:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
10407:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
10408:                 if (($item eq 'width') || ($item eq 'height')) {
10409:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
10410:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
10411:                     }
10412:                 } else {
10413:                     if ($env{'form.ltitools_add_'.$item} ne '') {
10414:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
10415:                     }
10416:                 }
10417:             }
10418:             if ($env{'form.ltitools_add_target'} eq 'window') {
10419:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10420:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
10421:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10422:             } else {
10423:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
10424:             }
10425:             foreach my $item ('passback','roster') {
10426:                 if ($env{'form.ltitools_add_'.$item}) {
10427:                     $confhash{$newid}{$item} = 1;
10428:                 }
10429:             }
10430:             if ($env{'form.ltitools_add_image.filename'} ne '') {
10431:                 my ($imageurl,$error) =
10432:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
10433:                                             $configuserok,$switchserver,$author_ok);
10434:                 if ($imageurl) {
10435:                     $confhash{$newid}{'image'} = $imageurl;
10436:                 }
10437:                 if ($error) {
10438:                     &Apache::lonnet::logthis($error);
10439:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10440:                 }
10441:             }
10442:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
10443:             foreach my $field (@fields) {
10444:                 if ($possfield{$field}) {
10445:                     if ($field eq 'roles') {
10446:                         foreach my $role (@courseroles) {
10447:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
10448:                             if (($choice ne '') && ($posslti{$choice})) {
10449:                                 $confhash{$newid}{'roles'}{$role} = $choice;
10450:                                 if ($role eq 'cc') {
10451:                                     $confhash{$newid}{'roles'}{'co'} = $choice;
10452:                                 }
10453:                             }
10454:                         }
10455:                     } else {
10456:                         $confhash{$newid}{'fields'}{$field} = 1;
10457:                     }
10458:                 }
10459:             }
10460:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
10461:             foreach my $item (@courseconfig) {
10462:                 $confhash{$newid}{'crsconf'}{$item} = 1;
10463:             }
10464:             if ($env{'form.ltitools_add_custom'}) {
10465:                 my $name = $env{'form.ltitools_add_custom_name'};
10466:                 my $value = $env{'form.ltitools_add_custom_value'};
10467:                 $value =~ s/(`)/'/g;
10468:                 $name =~ s/(`)/'/g;
10469:                 $confhash{$newid}{'custom'}{$name} = $value;
10470:             }
10471:         } else {
10472:             my $error = &mt('Failed to acquire unique ID for new external tool');
10473:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10474:         }
10475:     }
10476:     if (ref($domconfig{$action}) eq 'HASH') {
10477:         my %deletions;
10478:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
10479:         if (@todelete) {
10480:             map { $deletions{$_} = 1; } @todelete;
10481:         }
10482:         my %customadds;
10483:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
10484:         if (@newcustom) {
10485:             map { $customadds{$_} = 1; } @newcustom;
10486:         }
10487:         my %imgdeletions;
10488:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
10489:         if (@todeleteimages) {
10490:             map { $imgdeletions{$_} = 1; } @todeleteimages;
10491:         }
10492:         my $maxnum = $env{'form.ltitools_maxnum'};
10493:         for (my $i=0; $i<=$maxnum; $i++) {
10494:             my $itemid = $env{'form.ltitools_id_'.$i};
10495:             $itemid =~ s/\D+//g;
10496:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10497:                 if ($deletions{$itemid}) {
10498:                     if ($domconfig{$action}{$itemid}{'image'}) {
10499:                         #FIXME need to obsolete item in RES space
10500:                     }
10501:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
10502:                     next;
10503:                 } else {
10504:                     my $newpos = $env{'form.ltitools_'.$itemid};
10505:                     $newpos =~ s/\D+//g;
10506:                     foreach my $item ('title','url') {
10507:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10508:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
10509:                             $changes{$itemid} = 1;
10510:                         }
10511:                     }
10512:                     foreach my $item ('key','secret') {
10513:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10514:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
10515:                             $changes{$itemid} = 1;
10516:                         }
10517:                     }
10518:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
10519:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
10520:                     }
10521:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
10522:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
10523:                     }
10524:                     foreach my $size ('width','height') {
10525:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
10526:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
10527:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
10528:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
10529:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10530:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
10531:                                     $changes{$itemid} = 1;
10532:                                 }
10533:                             } else {
10534:                                 $changes{$itemid} = 1;
10535:                             }
10536:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10537:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
10538:                                 $changes{$itemid} = 1;
10539:                             }
10540:                         }
10541:                     }
10542:                     foreach my $item ('linktext','explanation') {
10543:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
10544:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
10545:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
10546:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10547:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10548:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
10549:                                     $changes{$itemid} = 1;
10550:                                 }
10551:                             } else {
10552:                                 $changes{$itemid} = 1;
10553:                             }
10554:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10555:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
10556:                                 $changes{$itemid} = 1;
10557:                             }
10558:                         }
10559:                     }
10560:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
10561:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10562:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
10563:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10564:                     } else {
10565:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
10566:                     }
10567:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10568:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
10569:                             $changes{$itemid} = 1;
10570:                         }
10571:                     } else {
10572:                         $changes{$itemid} = 1;
10573:                     }
10574:                     foreach my $extra ('passback','roster') {
10575:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
10576:                             $confhash{$itemid}{$extra} = 1;
10577:                         }
10578:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
10579:                             $changes{$itemid} = 1;
10580:                         }
10581:                     }
10582:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
10583:                     foreach my $item ('label','title','target','linktext','explanation') {
10584:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
10585:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
10586:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
10587:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
10588:                                     $changes{$itemid} = 1;
10589:                                 }
10590:                             } else {
10591:                                 $changes{$itemid} = 1;
10592:                             }
10593:                         }
10594:                     }
10595:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
10596:                     foreach my $field (@fields) {
10597:                         if ($possfield{$field}) {
10598:                             if ($field eq 'roles') {
10599:                                 foreach my $role (@courseroles) {
10600:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
10601:                                     if (($choice ne '') && ($posslti{$choice})) {
10602:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
10603:                                         if ($role eq 'cc') {
10604:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
10605:                                         }
10606:                                     }
10607:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
10608:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
10609:                                             $changes{$itemid} = 1;
10610:                                         }
10611:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
10612:                                         $changes{$itemid} = 1;
10613:                                     }
10614:                                 }
10615:                             } else {
10616:                                 $confhash{$itemid}{'fields'}{$field} = 1;
10617:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
10618:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
10619:                                         $changes{$itemid} = 1;
10620:                                     }
10621:                                 } else {
10622:                                     $changes{$itemid} = 1;
10623:                                 }
10624:                             }
10625:                         }
10626:                     }
10627:                     $allpos[$newpos] = $itemid;
10628:                 }
10629:                 if ($imgdeletions{$itemid}) {
10630:                     $changes{$itemid} = 1;
10631:                     #FIXME need to obsolete item in RES space
10632:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
10633:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
10634:                                                                  $itemid,$configuserok,$switchserver,
10635:                                                                  $author_ok);
10636:                     if ($imgurl) {
10637:                         $confhash{$itemid}{'image'} = $imgurl;
10638:                         $changes{$itemid} = 1;
10639:                     }
10640:                     if ($error) {
10641:                         &Apache::lonnet::logthis($error);
10642:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10643:                     }
10644:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
10645:                     $confhash{$itemid}{'image'} =
10646:                        $domconfig{$action}{$itemid}{'image'};
10647:                 }
10648:                 if ($customadds{$i}) {
10649:                     my $name = $env{'form.ltitools_custom_name_'.$i};
10650:                     $name =~ s/(`)/'/g;
10651:                     $name =~ s/^\s+//;
10652:                     $name =~ s/\s+$//;
10653:                     my $value = $env{'form.ltitools_custom_value_'.$i};
10654:                     $value =~ s/(`)/'/g;
10655:                     $value =~ s/^\s+//;
10656:                     $value =~ s/\s+$//;
10657:                     if ($name ne '') {
10658:                         $confhash{$itemid}{'custom'}{$name} = $value;
10659:                         $changes{$itemid} = 1;
10660:                     }
10661:                 }
10662:                 my %customdels;
10663:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i);
10664:                 if (@customdeletions) {
10665:                     $changes{$itemid} = 1;
10666:                 }
10667:                 map { $customdels{$_} = 1; } @customdeletions;
10668:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
10669:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
10670:                         unless ($customdels{$key}) {
10671:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
10672:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i};
10673:                             }
10674:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
10675:                                 $changes{$itemid} = 1;
10676:                             }
10677:                         }
10678:                     }
10679:                 }
10680:                 unless ($changes{$itemid}) {
10681:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
10682:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
10683:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
10684:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
10685:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
10686:                                         $changes{$itemid} = 1;
10687:                                         last;
10688:                                     }
10689:                                 }
10690:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
10691:                                 $changes{$itemid} = 1;
10692:                             }
10693:                         }
10694:                         last if ($changes{$itemid});
10695:                     }
10696:                 }
10697:             }
10698:         }
10699:     }
10700:     if (@allpos > 0) {
10701:         my $idx = 0;
10702:         foreach my $itemid (@allpos) {
10703:             if ($itemid ne '') {
10704:                 $confhash{$itemid}{'order'} = $idx;
10705:                 if (ref($domconfig{$action}) eq 'HASH') {
10706:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10707:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
10708:                             $changes{$itemid} = 1;
10709:                         }
10710:                     }
10711:                 }
10712:                 $idx ++;
10713:             }
10714:         }
10715:     }
10716:     my %ltitoolshash = (
10717:                           $action => { %confhash }
10718:                        );
10719:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
10720:                                              $dom);
10721:     if ($putresult eq 'ok') {
10722:         my %ltienchash = (
10723:                              $action => { %encconfig }
10724:                          );
10725:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
10726:         if (keys(%changes) > 0) {
10727:             my $cachetime = 24*60*60;
10728:             my %ltiall = %confhash;
10729:             foreach my $id (keys(%ltiall)) {
10730:                 if (ref($encconfig{$id}) eq 'HASH') {
10731:                     foreach my $item ('key','secret') {
10732:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
10733:                     }
10734:                 }
10735:             }
10736:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
10737:             if (ref($lastactref) eq 'HASH') {
10738:                 $lastactref->{'ltitools'} = 1;
10739:             }
10740:             $resulttext = &mt('Changes made:').'<ul>';
10741:             my %bynum;
10742:             foreach my $itemid (sort(keys(%changes))) {
10743:                 my $position = $confhash{$itemid}{'order'};
10744:                 $bynum{$position} = $itemid;
10745:             }
10746:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
10747:                 my $itemid = $bynum{$pos};
10748:                 if (ref($confhash{$itemid}) ne 'HASH') {
10749:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
10750:                 } else {
10751:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
10752:                     if ($confhash{$itemid}{'image'}) {
10753:                         $resulttext .= '&nbsp;'.
10754:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
10755:                                        ' alt="'.&mt('Tool Provider icon').'" />';
10756:                     }
10757:                     $resulttext .= '</li><ul>';
10758:                     my $position = $pos + 1;
10759:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
10760:                     foreach my $item ('version','msgtype','url') {
10761:                         if ($confhash{$itemid}{$item} ne '') {
10762:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
10763:                         }
10764:                     }
10765:                     if ($encconfig{$itemid}{'key'} ne '') {
10766:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
10767:                     }
10768:                     if ($encconfig{$itemid}{'secret'} ne '') {
10769:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
10770:                         my $num = length($encconfig{$itemid}{'secret'});
10771:                         $resulttext .= ('*'x$num).'</li>';
10772:                     }
10773:                     $resulttext .= '<li>'.&mt('Configurable in course:');
10774:                     my @possconfig = ('label','title','target','linktext','explanation');
10775:                     my $numconfig = 0;
10776:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') {
10777:                         foreach my $item (@possconfig) {
10778:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
10779:                                 $numconfig ++;
10780:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
10781:                             }
10782:                         }
10783:                     }
10784:                     if (!$numconfig) {
10785:                         $resulttext .= &mt('None');
10786:                     }
10787:                     $resulttext .= '</li>';
10788:                     foreach my $item ('passback','roster') {
10789:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
10790:                         if ($confhash{$itemid}{$item}) {
10791:                             $resulttext .= &mt('Yes');
10792:                         } else {
10793:                             $resulttext .= &mt('No');
10794:                         }
10795:                         $resulttext .= '</li>';
10796:                     }
10797:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
10798:                         my $displaylist;
10799:                         if ($confhash{$itemid}{'display'}{'target'}) {
10800:                             $displaylist = &mt('Display target').':&nbsp;'.
10801:                                            $confhash{$itemid}{'display'}{'target'}.',';
10802:                         }
10803:                         foreach my $size ('width','height') {
10804:                             if ($confhash{$itemid}{'display'}{$size}) {
10805:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
10806:                                                 $confhash{$itemid}{'display'}{$size}.',';
10807:                             }
10808:                         }
10809:                         if ($displaylist) {
10810:                             $displaylist =~ s/,$//;
10811:                             $resulttext .= '<li>'.$displaylist.'</li>';
10812:                         }
10813:                         foreach my $item ('linktext','explanation') {
10814:                             if ($confhash{$itemid}{'display'}{$item}) {
10815:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
10816:                             }
10817:                         }
10818:                     }
10819:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
10820:                         my $fieldlist;
10821:                         foreach my $field (@allfields) {
10822:                             if ($confhash{$itemid}{'fields'}{$field}) {
10823:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
10824:                             }
10825:                         }
10826:                         if ($fieldlist) {
10827:                             $fieldlist =~ s/,$//;
10828:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
10829:                         }
10830:                     }
10831:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
10832:                         my $rolemaps;
10833:                         foreach my $role (@courseroles) {
10834:                             if ($confhash{$itemid}{'roles'}{$role}) {
10835:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
10836:                                              $confhash{$itemid}{'roles'}{$role}.',';
10837:                             }
10838:                         }
10839:                         if ($rolemaps) {
10840:                             $rolemaps =~ s/,$//;
10841:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
10842:                         }
10843:                     }
10844:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
10845:                         my $customlist;
10846:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
10847:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
10848:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
10849:                             }
10850:                         }
10851:                         if ($customlist) {
10852:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
10853:                         }
10854:                     }
10855:                     $resulttext .= '</ul></li>';
10856:                 }
10857:             }
10858:             $resulttext .= '</ul>';
10859:         } else {
10860:             $resulttext = &mt('No changes made.');
10861:         }
10862:     } else {
10863:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
10864:     }
10865:     if ($errors) {
10866:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10867:                        $errors.'</ul>';
10868:     }
10869:     return $resulttext;
10870: }
10871: 
10872: sub process_ltitools_image {
10873:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
10874:     my $filename = $env{'form.'.$caller.'.filename'};
10875:     my ($error,$url);
10876:     my ($width,$height) = (21,21);
10877:     if ($configuserok eq 'ok') {
10878:         if ($switchserver) {
10879:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
10880:                          $switchserver);
10881:         } elsif ($author_ok eq 'ok') {
10882:             my ($result,$imageurl,$madethumb) =
10883:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10884:                              "ltitools/$itemid/icon",$width,$height);
10885:             if ($result eq 'ok') {
10886:                 if ($madethumb) {
10887:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
10888:                     my $imagethumb = "$path/tn-".$imagefile;
10889:                     $url = $imagethumb;
10890:                 } else {
10891:                     $url = $imageurl;
10892:                 }
10893:             } else {
10894:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10895:             }
10896:         } else {
10897:             $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);
10898:         }
10899:     } else {
10900:         $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);
10901:     }
10902:     return ($url,$error);
10903: }
10904: 
10905: sub get_ltitools_id {
10906:     my ($cdom,$title) = @_;
10907:     # get lock on ltitools db
10908:     my $lockhash = {
10909:                       lock => $env{'user.name'}.
10910:                               ':'.$env{'user.domain'},
10911:                    };
10912:     my $tries = 0;
10913:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10914:     my ($id,$error);
10915: 
10916:     while (($gotlock ne 'ok') && ($tries<10)) {
10917:         $tries ++;
10918:         sleep (0.1);
10919:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10920:     }
10921:     if ($gotlock eq 'ok') {
10922:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
10923:         if ($currids{'lock'}) {
10924:             delete($currids{'lock'});
10925:             if (keys(%currids)) {
10926:                 my @curr = sort { $a <=> $b } keys(%currids);
10927:                 if ($curr[-1] =~ /^\d+$/) {
10928:                     $id = 1 + $curr[-1];
10929:                 }
10930:             } else {
10931:                 $id = 1;
10932:             }
10933:             if ($id) {
10934:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
10935:                     $error = 'nostore';
10936:                 }
10937:             } else {
10938:                 $error = 'nonumber';
10939:             }
10940:         }
10941:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
10942:     } else {
10943:         $error = 'nolock';
10944:     }
10945:     return ($id,$error);
10946: }
10947: 
10948: sub modify_autoenroll {
10949:     my ($dom,$lastactref,%domconfig) = @_;
10950:     my ($resulttext,%changes);
10951:     my %currautoenroll;
10952:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
10953:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
10954:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
10955:         }
10956:     }
10957:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
10958:     my %title = ( run => 'Auto-enrollment active',
10959:                   sender => 'Sender for notification messages',
10960:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
10961:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
10962:     my @offon = ('off','on');
10963:     my $sender_uname = $env{'form.sender_uname'};
10964:     my $sender_domain = $env{'form.sender_domain'};
10965:     if ($sender_domain eq '') {
10966:         $sender_uname = '';
10967:     } elsif ($sender_uname eq '') {
10968:         $sender_domain = '';
10969:     }
10970:     my $coowners = $env{'form.autoassign_coowners'};
10971:     my $failsafe = $env{'form.autoenroll_failsafe'};
10972:     $failsafe =~ s{^\s+|\s+$}{}g;
10973:     if ($failsafe =~ /\D/) {
10974:         undef($failsafe);
10975:     }
10976:     my %autoenrollhash =  (
10977:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
10978:                                        'sender_uname' => $sender_uname,
10979:                                        'sender_domain' => $sender_domain,
10980:                                        'co-owners' => $coowners,
10981:                                        'autofailsafe' => $failsafe,
10982:                                 }
10983:                      );
10984:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
10985:                                              $dom);
10986:     if ($putresult eq 'ok') {
10987:         if (exists($currautoenroll{'run'})) {
10988:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
10989:                  $changes{'run'} = 1;
10990:              }
10991:         } elsif ($autorun) {
10992:             if ($env{'form.autoenroll_run'} ne '1') {
10993:                  $changes{'run'} = 1;
10994:             }
10995:         }
10996:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
10997:             $changes{'sender'} = 1;
10998:         }
10999:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
11000:             $changes{'sender'} = 1;
11001:         }
11002:         if ($currautoenroll{'co-owners'} ne '') {
11003:             if ($currautoenroll{'co-owners'} ne $coowners) {
11004:                 $changes{'coowners'} = 1;
11005:             }
11006:         } elsif ($coowners) {
11007:             $changes{'coowners'} = 1;
11008:         }
11009:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
11010:             $changes{'autofailsafe'} = 1;
11011:         }
11012:         if (keys(%changes) > 0) {
11013:             $resulttext = &mt('Changes made:').'<ul>';
11014:             if ($changes{'run'}) {
11015:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
11016:             }
11017:             if ($changes{'sender'}) {
11018:                 if ($sender_uname eq '' || $sender_domain eq '') {
11019:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
11020:                 } else {
11021:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
11022:                 }
11023:             }
11024:             if ($changes{'coowners'}) {
11025:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
11026:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
11027:                 if (ref($lastactref) eq 'HASH') {
11028:                     $lastactref->{'domainconfig'} = 1;
11029:                 }
11030:             }
11031:             if ($changes{'autofailsafe'}) {
11032:                 if ($failsafe ne '') {
11033:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
11034:                 } else {
11035:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
11036:                 }
11037:                 &Apache::lonnet::get_domain_defaults($dom,1);
11038:                 if (ref($lastactref) eq 'HASH') {
11039:                     $lastactref->{'domdefaults'} = 1;
11040:                 }
11041:             }
11042:             $resulttext .= '</ul>';
11043:         } else {
11044:             $resulttext = &mt('No changes made to auto-enrollment settings');
11045:         }
11046:     } else {
11047:         $resulttext = '<span class="LC_error">'.
11048: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11049:     }
11050:     return $resulttext;
11051: }
11052: 
11053: sub modify_autoupdate {
11054:     my ($dom,%domconfig) = @_;
11055:     my ($resulttext,%currautoupdate,%fields,%changes);
11056:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
11057:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
11058:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
11059:         }
11060:     }
11061:     my @offon = ('off','on');
11062:     my %title = &Apache::lonlocal::texthash (
11063:                    run => 'Auto-update:',
11064:                    classlists => 'Updates to user information in classlists?'
11065:                 );
11066:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11067:     my %fieldtitles = &Apache::lonlocal::texthash (
11068:                         id => 'Student/Employee ID',
11069:                         permanentemail => 'E-mail address',
11070:                         lastname => 'Last Name',
11071:                         firstname => 'First Name',
11072:                         middlename => 'Middle Name',
11073:                         generation => 'Generation',
11074:                       );
11075:     $othertitle = &mt('All users');
11076:     if (keys(%{$usertypes}) >  0) {
11077:         $othertitle = &mt('Other users');
11078:     }
11079:     foreach my $key (keys(%env)) {
11080:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
11081:             my ($usertype,$item) = ($1,$2);
11082:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
11083:                 if ($usertype eq 'default') {   
11084:                     push(@{$fields{$1}},$2);
11085:                 } elsif (ref($types) eq 'ARRAY') {
11086:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
11087:                         push(@{$fields{$1}},$2);
11088:                     }
11089:                 }
11090:             }
11091:         }
11092:     }
11093:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
11094:     @lockablenames = sort(@lockablenames);
11095:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
11096:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11097:         if (@changed) {
11098:             $changes{'lockablenames'} = 1;
11099:         }
11100:     } else {
11101:         if (@lockablenames) {
11102:             $changes{'lockablenames'} = 1;
11103:         }
11104:     }
11105:     my %updatehash = (
11106:                       autoupdate => { run => $env{'form.autoupdate_run'},
11107:                                       classlists => $env{'form.classlists'},
11108:                                       fields => {%fields},
11109:                                       lockablenames => \@lockablenames,
11110:                                     }
11111:                      );
11112:     foreach my $key (keys(%currautoupdate)) {
11113:         if (($key eq 'run') || ($key eq 'classlists')) {
11114:             if (exists($updatehash{autoupdate}{$key})) {
11115:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
11116:                     $changes{$key} = 1;
11117:                 }
11118:             }
11119:         } elsif ($key eq 'fields') {
11120:             if (ref($currautoupdate{$key}) eq 'HASH') {
11121:                 foreach my $item (@{$types},'default') {
11122:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
11123:                         my $change = 0;
11124:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
11125:                             if (!exists($fields{$item})) {
11126:                                 $change = 1;
11127:                                 last;
11128:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
11129:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
11130:                                     $change = 1;
11131:                                     last;
11132:                                 }
11133:                             }
11134:                         }
11135:                         if ($change) {
11136:                             push(@{$changes{$key}},$item);
11137:                         }
11138:                     } 
11139:                 }
11140:             }
11141:         } elsif ($key eq 'lockablenames') {
11142:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
11143:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11144:                 if (@changed) {
11145:                     $changes{'lockablenames'} = 1;
11146:                 }
11147:             } else {
11148:                 if (@lockablenames) {
11149:                     $changes{'lockablenames'} = 1;
11150:                 }
11151:             }
11152:         }
11153:     }
11154:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
11155:         if (@lockablenames) {
11156:             $changes{'lockablenames'} = 1;
11157:         }
11158:     }
11159:     foreach my $item (@{$types},'default') {
11160:         if (defined($fields{$item})) {
11161:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
11162:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
11163:                     my $change = 0;
11164:                     if (ref($fields{$item}) eq 'ARRAY') {
11165:                         foreach my $type (@{$fields{$item}}) {
11166:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
11167:                                 $change = 1;
11168:                                 last;
11169:                             }
11170:                         }
11171:                     }
11172:                     if ($change) {
11173:                         push(@{$changes{'fields'}},$item);
11174:                     }
11175:                 } else {
11176:                     push(@{$changes{'fields'}},$item);
11177:                 }
11178:             } else {
11179:                 push(@{$changes{'fields'}},$item);
11180:             }
11181:         }
11182:     }
11183:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
11184:                                              $dom);
11185:     if ($putresult eq 'ok') {
11186:         if (keys(%changes) > 0) {
11187:             $resulttext = &mt('Changes made:').'<ul>';
11188:             foreach my $key (sort(keys(%changes))) {
11189:                 if ($key eq 'lockablenames') {
11190:                     $resulttext .= '<li>';
11191:                     if (@lockablenames) {
11192:                         $usertypes->{'default'} = $othertitle;
11193:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
11194:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
11195:                     } else {
11196:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
11197:                     }
11198:                     $resulttext .= '</li>';
11199:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
11200:                     foreach my $item (@{$changes{$key}}) {
11201:                         my @newvalues;
11202:                         foreach my $type (@{$fields{$item}}) {
11203:                             push(@newvalues,$fieldtitles{$type});
11204:                         }
11205:                         my $newvaluestr;
11206:                         if (@newvalues > 0) {
11207:                             $newvaluestr = join(', ',@newvalues);
11208:                         } else {
11209:                             $newvaluestr = &mt('none');
11210:                         }
11211:                         if ($item eq 'default') {
11212:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
11213:                         } else {
11214:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
11215:                         }
11216:                     }
11217:                 } else {
11218:                     my $newvalue;
11219:                     if ($key eq 'run') {
11220:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
11221:                     } else {
11222:                         $newvalue = $offon[$env{'form.'.$key}];
11223:                     }
11224:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
11225:                 }
11226:             }
11227:             $resulttext .= '</ul>';
11228:         } else {
11229:             $resulttext = &mt('No changes made to autoupdates');
11230:         }
11231:     } else {
11232:         $resulttext = '<span class="LC_error">'.
11233: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11234:     }
11235:     return $resulttext;
11236: }
11237: 
11238: sub modify_autocreate {
11239:     my ($dom,%domconfig) = @_;
11240:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
11241:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
11242:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
11243:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
11244:         }
11245:     }
11246:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
11247:                  req => 'Auto-creation of validated requests for official courses',
11248:                  xmldc => 'Identity of course creator of courses from XML files',
11249:                );
11250:     my @types = ('xml','req');
11251:     foreach my $item (@types) {
11252:         $newvals{$item} = $env{'form.autocreate_'.$item};
11253:         $newvals{$item} =~ s/\D//g;
11254:         $newvals{$item} = 0 if ($newvals{$item} eq '');
11255:     }
11256:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
11257:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
11258:     unless (exists($domcoords{$newvals{'xmldc'}})) {
11259:         $newvals{'xmldc'} = '';
11260:     } 
11261:     %autocreatehash =  (
11262:                         autocreate => { xml => $newvals{'xml'},
11263:                                         req => $newvals{'req'},
11264:                                       }
11265:                        );
11266:     if ($newvals{'xmldc'} ne '') {
11267:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
11268:     }
11269:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
11270:                                              $dom);
11271:     if ($putresult eq 'ok') {
11272:         my @items = @types;
11273:         if ($newvals{'xml'}) {
11274:             push(@items,'xmldc');
11275:         }
11276:         foreach my $item (@items) {
11277:             if (exists($currautocreate{$item})) {
11278:                 if ($currautocreate{$item} ne $newvals{$item}) {
11279:                     $changes{$item} = 1;
11280:                 }
11281:             } elsif ($newvals{$item}) {
11282:                 $changes{$item} = 1;
11283:             }
11284:         }
11285:         if (keys(%changes) > 0) {
11286:             my @offon = ('off','on'); 
11287:             $resulttext = &mt('Changes made:').'<ul>';
11288:             foreach my $item (@types) {
11289:                 if ($changes{$item}) {
11290:                     my $newtxt = $offon[$newvals{$item}];
11291:                     $resulttext .= '<li>'.
11292:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
11293:                                        '<b>','</b>').
11294:                                    '</li>';
11295:                 }
11296:             }
11297:             if ($changes{'xmldc'}) {
11298:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
11299:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
11300:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
11301:             }
11302:             $resulttext .= '</ul>';
11303:         } else {
11304:             $resulttext = &mt('No changes made to auto-creation settings');
11305:         }
11306:     } else {
11307:         $resulttext = '<span class="LC_error">'.
11308:             &mt('An error occurred: [_1]',$putresult).'</span>';
11309:     }
11310:     return $resulttext;
11311: }
11312: 
11313: sub modify_directorysrch {
11314:     my ($dom,$lastactref,%domconfig) = @_;
11315:     my ($resulttext,%changes);
11316:     my %currdirsrch;
11317:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
11318:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
11319:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
11320:         }
11321:     }
11322:     my %title = ( available => 'Institutional directory search available',
11323:                   localonly => 'Other domains can search institution',
11324:                   lcavailable => 'LON-CAPA directory search available',
11325:                   lclocalonly => 'Other domains can search LON-CAPA domain',
11326:                   searchby => 'Search types',
11327:                   searchtypes => 'Search latitude');
11328:     my @offon = ('off','on');
11329:     my @otherdoms = ('Yes','No');
11330: 
11331:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
11332:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
11333:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
11334: 
11335:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11336:     if (keys(%{$usertypes}) == 0) {
11337:         @cansearch = ('default');
11338:     } else {
11339:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
11340:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
11341:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
11342:                     push(@{$changes{'cansearch'}},$type);
11343:                 }
11344:             }
11345:             foreach my $type (@cansearch) {
11346:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
11347:                     push(@{$changes{'cansearch'}},$type);
11348:                 }
11349:             }
11350:         } else {
11351:             push(@{$changes{'cansearch'}},@cansearch);
11352:         }
11353:     }
11354: 
11355:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
11356:         foreach my $by (@{$currdirsrch{'searchby'}}) {
11357:             if (!grep(/^\Q$by\E$/,@searchby)) {
11358:                 push(@{$changes{'searchby'}},$by);
11359:             }
11360:         }
11361:         foreach my $by (@searchby) {
11362:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
11363:                 push(@{$changes{'searchby'}},$by);
11364:             }
11365:         }
11366:     } else {
11367:         push(@{$changes{'searchby'}},@searchby);
11368:     }
11369: 
11370:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
11371:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
11372:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
11373:                 push(@{$changes{'searchtypes'}},$type);
11374:             }
11375:         }
11376:         foreach my $type (@searchtypes) {
11377:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
11378:                 push(@{$changes{'searchtypes'}},$type);
11379:             }
11380:         }
11381:     } else {
11382:         if (exists($currdirsrch{'searchtypes'})) {
11383:             foreach my $type (@searchtypes) {  
11384:                 if ($type ne $currdirsrch{'searchtypes'}) { 
11385:                     push(@{$changes{'searchtypes'}},$type);
11386:                 }
11387:             }
11388:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
11389:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
11390:             }   
11391:         } else {
11392:             push(@{$changes{'searchtypes'}},@searchtypes); 
11393:         }
11394:     }
11395: 
11396:     my %dirsrch_hash =  (
11397:             directorysrch => { available => $env{'form.dirsrch_available'},
11398:                                cansearch => \@cansearch,
11399:                                localonly => $env{'form.dirsrch_instlocalonly'},
11400:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
11401:                                lcavailable => $env{'form.dirsrch_domavailable'},
11402:                                searchby => \@searchby,
11403:                                searchtypes => \@searchtypes,
11404:                              }
11405:             );
11406:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
11407:                                              $dom);
11408:     if ($putresult eq 'ok') {
11409:         if (exists($currdirsrch{'available'})) {
11410:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
11411:                  $changes{'available'} = 1;
11412:              }
11413:         } else {
11414:             if ($env{'form.dirsrch_available'} eq '1') {
11415:                 $changes{'available'} = 1;
11416:             }
11417:         }
11418:         if (exists($currdirsrch{'lcavailable'})) {
11419:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
11420:                 $changes{'lcavailable'} = 1;
11421:             }
11422:         } else {
11423:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
11424:                 $changes{'lcavailable'} = 1;
11425:             }
11426:         }
11427:         if (exists($currdirsrch{'localonly'})) {
11428:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
11429:                 $changes{'localonly'} = 1;
11430:             }
11431:         } else {
11432:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
11433:                 $changes{'localonly'} = 1;
11434:             }
11435:         }
11436:         if (exists($currdirsrch{'lclocalonly'})) {
11437:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
11438:                 $changes{'lclocalonly'} = 1;
11439:             }
11440:         } else {
11441:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
11442:                 $changes{'lclocalonly'} = 1;
11443:             }
11444:         }
11445:         if (keys(%changes) > 0) {
11446:             $resulttext = &mt('Changes made:').'<ul>';
11447:             if ($changes{'available'}) {
11448:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
11449:             }
11450:             if ($changes{'lcavailable'}) {
11451:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
11452:             }
11453:             if ($changes{'localonly'}) {
11454:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
11455:             }
11456:             if ($changes{'lclocalonly'}) {
11457:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
11458:             }
11459:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
11460:                 my $chgtext;
11461:                 if (ref($usertypes) eq 'HASH') {
11462:                     if (keys(%{$usertypes}) > 0) {
11463:                         foreach my $type (@{$types}) {
11464:                             if (grep(/^\Q$type\E$/,@cansearch)) {
11465:                                 $chgtext .= $usertypes->{$type}.'; ';
11466:                             }
11467:                         }
11468:                         if (grep(/^default$/,@cansearch)) {
11469:                             $chgtext .= $othertitle;
11470:                         } else {
11471:                             $chgtext =~ s/\; $//;
11472:                         }
11473:                         $resulttext .=
11474:                             '<li>'.
11475:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
11476:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
11477:                             '</li>';
11478:                     }
11479:                 }
11480:             }
11481:             if (ref($changes{'searchby'}) eq 'ARRAY') {
11482:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
11483:                 my $chgtext;
11484:                 foreach my $type (@{$titleorder}) {
11485:                     if (grep(/^\Q$type\E$/,@searchby)) {
11486:                         if (defined($searchtitles->{$type})) {
11487:                             $chgtext .= $searchtitles->{$type}.'; ';
11488:                         }
11489:                     }
11490:                 }
11491:                 $chgtext =~ s/\; $//;
11492:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
11493:             }
11494:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
11495:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
11496:                 my $chgtext;
11497:                 foreach my $type (@{$srchtypeorder}) {
11498:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
11499:                         if (defined($srchtypes_desc->{$type})) {
11500:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
11501:                         }
11502:                     }
11503:                 }
11504:                 $chgtext =~ s/\; $//;
11505:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
11506:             }
11507:             $resulttext .= '</ul>';
11508:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
11509:             if (ref($lastactref) eq 'HASH') {
11510:                 $lastactref->{'directorysrch'} = 1;
11511:             }
11512:         } else {
11513:             $resulttext = &mt('No changes made to directory search settings');
11514:         }
11515:     } else {
11516:         $resulttext = '<span class="LC_error">'.
11517:                       &mt('An error occurred: [_1]',$putresult).'</span>';
11518:     }
11519:     return $resulttext;
11520: }
11521: 
11522: sub modify_contacts {
11523:     my ($dom,$lastactref,%domconfig) = @_;
11524:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
11525:     if (ref($domconfig{'contacts'}) eq 'HASH') {
11526:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
11527:             $currsetting{$key} = $domconfig{'contacts'}{$key};
11528:         }
11529:     }
11530:     my (%others,%to,%bcc,%includestr,%includeloc);
11531:     my @contacts = ('supportemail','adminemail');
11532:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
11533:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
11534:     my @toggles = ('reporterrors','reportupdates');
11535:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
11536:     foreach my $type (@mailings) {
11537:         @{$newsetting{$type}} = 
11538:             &Apache::loncommon::get_env_multiple('form.'.$type);
11539:         foreach my $item (@contacts) {
11540:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
11541:                 $contacts_hash{contacts}{$type}{$item} = 1;
11542:             } else {
11543:                 $contacts_hash{contacts}{$type}{$item} = 0;
11544:             }
11545:         }
11546:         $others{$type} = $env{'form.'.$type.'_others'};
11547:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
11548:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11549:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
11550:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
11551:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
11552:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
11553:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
11554:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11555:             }
11556:         }
11557:     }
11558:     foreach my $item (@contacts) {
11559:         $to{$item} = $env{'form.'.$item};
11560:         $contacts_hash{'contacts'}{$item} = $to{$item};
11561:     }
11562:     foreach my $item (@toggles) {
11563:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
11564:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
11565:         }
11566:     }
11567:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
11568:         foreach my $field (@{$fields}) {
11569:             if (ref($possoptions->{$field}) eq 'ARRAY') {
11570:                 my $value = $env{'form.helpform_'.$field};
11571:                 $value =~ s/^\s+|\s+$//g;
11572:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
11573:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
11574:                     if ($field eq 'screenshot') {
11575:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
11576:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
11577:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
11578:                         }
11579:                     }
11580:                 }
11581:             }
11582:         }
11583:     }
11584:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11585:     my (@statuses,%usertypeshash,@overrides);
11586:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
11587:         @statuses = @{$types};
11588:         if (ref($usertypes) eq 'HASH') {
11589:             %usertypeshash = %{$usertypes};
11590:         }
11591:     }
11592:     if (@statuses) {
11593:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
11594:         foreach my $type (@possoverrides) {
11595:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
11596:                 push(@overrides,$type);
11597:             }
11598:         }
11599:         if (@overrides) {
11600:             foreach my $type (@overrides) {
11601:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
11602:                 foreach my $item (@contacts) {
11603:                     if (grep(/^\Q$item\E$/,@standard)) {
11604:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
11605:                         $newsetting{'override_'.$type}{$item} = 1;
11606:                     } else {
11607:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
11608:                         $newsetting{'override_'.$type}{$item} = 0;
11609:                     }
11610:                 }
11611:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
11612:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
11613:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
11614:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
11615:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
11616:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
11617:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
11618:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11619:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
11620:                 }
11621:             }    
11622:         }
11623:     }
11624:     if (keys(%currsetting) > 0) {
11625:         foreach my $item (@contacts) {
11626:             if ($to{$item} ne $currsetting{$item}) {
11627:                 $changes{$item} = 1;
11628:             }
11629:         }
11630:         foreach my $type (@mailings) {
11631:             foreach my $item (@contacts) {
11632:                 if (ref($currsetting{$type}) eq 'HASH') {
11633:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
11634:                         push(@{$changes{$type}},$item);
11635:                     }
11636:                 } else {
11637:                     push(@{$changes{$type}},@{$newsetting{$type}});
11638:                 }
11639:             }
11640:             if ($others{$type} ne $currsetting{$type}{'others'}) {
11641:                 push(@{$changes{$type}},'others');
11642:             }
11643:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11644:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
11645:                     push(@{$changes{$type}},'bcc'); 
11646:                 }
11647:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
11648:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
11649:                     push(@{$changes{$type}},'include');
11650:                 }
11651:             }
11652:         }
11653:         if (ref($fields) eq 'ARRAY') {
11654:             if (ref($currsetting{'helpform'}) eq 'HASH') {
11655:                 foreach my $field (@{$fields}) {
11656:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
11657:                         push(@{$changes{'helpform'}},$field);
11658:                     }
11659:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11660:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
11661:                             push(@{$changes{'helpform'}},'maxsize');
11662:                         }
11663:                     }
11664:                 }
11665:             } else {
11666:                 foreach my $field (@{$fields}) {
11667:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11668:                         push(@{$changes{'helpform'}},$field);
11669:                     }
11670:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11671:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11672:                             push(@{$changes{'helpform'}},'maxsize');
11673:                         }
11674:                     }
11675:                 }
11676:             }
11677:         }
11678:         if (@statuses) {
11679:             if (ref($currsetting{'overrides'}) eq 'HASH') {
11680:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
11681:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
11682:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
11683:                             foreach my $item (@contacts,'bcc','others','include') {
11684:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
11685:                                     push(@{$changes{'overrides'}},$key);
11686:                                     last;
11687:                                 }
11688:                             }
11689:                         } else {
11690:                             push(@{$changes{'overrides'}},$key);
11691:                         }
11692:                     }
11693:                 }
11694:                 foreach my $key (@overrides) {
11695:                     unless (exists($currsetting{'overrides'}{$key})) {
11696:                         push(@{$changes{'overrides'}},$key);
11697:                     }
11698:                 }
11699:             } else {
11700:                 foreach my $key (@overrides) {
11701:                     push(@{$changes{'overrides'}},$key);
11702:                 }
11703:             }
11704:         }
11705:     } else {
11706:         my %default;
11707:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
11708:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
11709:         $default{'errormail'} = 'adminemail';
11710:         $default{'packagesmail'} = 'adminemail';
11711:         $default{'helpdeskmail'} = 'supportemail';
11712:         $default{'otherdomsmail'} = 'supportemail';
11713:         $default{'lonstatusmail'} = 'adminemail';
11714:         $default{'requestsmail'} = 'adminemail';
11715:         $default{'updatesmail'} = 'adminemail';
11716:         $default{'hostipmail'} = 'adminemail';
11717:         foreach my $item (@contacts) {
11718:            if ($to{$item} ne $default{$item}) {
11719:                $changes{$item} = 1;
11720:            }
11721:         }
11722:         foreach my $type (@mailings) {
11723:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
11724:                 push(@{$changes{$type}},@{$newsetting{$type}});
11725:             }
11726:             if ($others{$type} ne '') {
11727:                 push(@{$changes{$type}},'others');
11728:             }
11729:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11730:                 if ($bcc{$type} ne '') {
11731:                     push(@{$changes{$type}},'bcc');
11732:                 }
11733:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
11734:                     push(@{$changes{$type}},'include');
11735:                 }
11736:             }
11737:         }
11738:         if (ref($fields) eq 'ARRAY') {
11739:             foreach my $field (@{$fields}) {
11740:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11741:                     push(@{$changes{'helpform'}},$field);
11742:                 }
11743:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11744:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11745:                         push(@{$changes{'helpform'}},'maxsize');
11746:                     }
11747:                 }
11748:             }
11749:         }
11750:     }
11751:     foreach my $item (@toggles) {
11752:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
11753:             $changes{$item} = 1;
11754:         } elsif ((!$env{'form.'.$item}) &&
11755:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
11756:             $changes{$item} = 1;
11757:         }
11758:     }
11759:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
11760:                                              $dom);
11761:     if ($putresult eq 'ok') {
11762:         if (keys(%changes) > 0) {
11763:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11764:             if (ref($lastactref) eq 'HASH') {
11765:                 $lastactref->{'domainconfig'} = 1;
11766:             }
11767:             my ($titles,$short_titles)  = &contact_titles();
11768:             $resulttext = &mt('Changes made:').'<ul>';
11769:             foreach my $item (@contacts) {
11770:                 if ($changes{$item}) {
11771:                     $resulttext .= '<li>'.$titles->{$item}.
11772:                                     &mt(' set to: ').
11773:                                     '<span class="LC_cusr_emph">'.
11774:                                     $to{$item}.'</span></li>';
11775:                 }
11776:             }
11777:             foreach my $type (@mailings) {
11778:                 if (ref($changes{$type}) eq 'ARRAY') {
11779:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11780:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
11781:                     } else {
11782:                         $resulttext .= '<li>'.$titles->{$type}.': ';
11783:                     }
11784:                     my @text;
11785:                     foreach my $item (@{$newsetting{$type}}) {
11786:                         push(@text,$short_titles->{$item});
11787:                     }
11788:                     if ($others{$type} ne '') {
11789:                         push(@text,$others{$type});
11790:                     }
11791:                     if (@text) {
11792:                         $resulttext .= '<span class="LC_cusr_emph">'.
11793:                                        join(', ',@text).'</span>';
11794:                     }
11795:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11796:                         if ($bcc{$type} ne '') {
11797:                             my $bcctext;
11798:                             if (@text) {
11799:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
11800:                             } else {
11801:                                 $bcctext = '(Bcc)';
11802:                             }
11803:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
11804:                         } elsif (!@text) {
11805:                             $resulttext .= &mt('No one');
11806:                         }
11807:                         if ($includestr{$type} ne '') {
11808:                             if ($includeloc{$type} eq 'b') {
11809:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
11810:                             } elsif ($includeloc{$type} eq 's') {
11811:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
11812:                             }
11813:                         }
11814:                     } elsif (!@text) {
11815:                         $resulttext .= &mt('No recipients');
11816:                     }
11817:                     $resulttext .= '</li>';
11818:                 }
11819:             }
11820:             if (ref($changes{'overrides'}) eq 'ARRAY') {
11821:                 my @deletions;
11822:                 foreach my $type (@{$changes{'overrides'}}) {
11823:                     if ($usertypeshash{$type}) {
11824:                         if (grep(/^\Q$type\E/,@overrides)) {
11825:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
11826:                                                       $usertypeshash{$type}).'<ul><li>';
11827:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
11828:                                 my @text;
11829:                                 foreach my $item (@contacts) {
11830:                                     if ($newsetting{'override_'.$type}{$item}) {
11831:                                         push(@text,$short_titles->{$item});
11832:                                     }
11833:                                 }
11834:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
11835:                                     push(@text,$newsetting{'override_'.$type}{'others'});
11836:                                 }
11837: 
11838:                                 if (@text) {
11839:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
11840:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
11841:                                 }
11842:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
11843:                                     my $bcctext;
11844:                                     if (@text) {
11845:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
11846:                                     } else {
11847:                                         $bcctext = '(Bcc)';
11848:                                     }
11849:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
11850:                                 } elsif (!@text) {
11851:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
11852:                                 }
11853:                                 $resulttext .= '</li>';
11854:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
11855:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
11856:                                     if ($loc eq 'b') {
11857:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
11858:                                     } elsif ($loc eq 's') {
11859:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
11860:                                     }
11861:                                 }
11862:                             }
11863:                             $resulttext .= '</li></ul></li>';
11864:                         } else {
11865:                             push(@deletions,$usertypeshash{$type});
11866:                         }
11867:                     }
11868:                 }
11869:                 if (@deletions) {
11870:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
11871:                                               join(', ',@deletions)).'</li>';
11872:                 }
11873:             }
11874:             my @offon = ('off','on');
11875:             if ($changes{'reporterrors'}) {
11876:                 $resulttext .= '<li>'.
11877:                                &mt('E-mail error reports to [_1] set to "'.
11878:                                    $offon[$env{'form.reporterrors'}].'".',
11879:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11880:                                        &mt('LON-CAPA core group - MSU'),600,500)).
11881:                                '</li>';
11882:             }
11883:             if ($changes{'reportupdates'}) {
11884:                 $resulttext .= '<li>'.
11885:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
11886:                                     $offon[$env{'form.reportupdates'}].'".',
11887:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11888:                                         &mt('LON-CAPA core group - MSU'),600,500)).
11889:                                 '</li>';
11890:             }
11891:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
11892:                 my (@optional,@required,@unused,$maxsizechg);
11893:                 foreach my $field (@{$changes{'helpform'}}) {
11894:                     if ($field eq 'maxsize') {
11895:                         $maxsizechg = 1;
11896:                         next;
11897:                     }
11898:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
11899:                         push(@optional,$field);
11900:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
11901:                         push(@unused,$field);
11902:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
11903:                         push(@required,$field);
11904:                     }
11905:                 }
11906:                 if (@optional) {
11907:                     $resulttext .= '<li>'.
11908:                                    &mt('Help form fields changed to "Optional": [_1].',
11909:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
11910:                                    '</li>';
11911:                 }
11912:                 if (@required) {
11913:                     $resulttext .= '<li>'.
11914:                                    &mt('Help form fields changed to "Required": [_1].',
11915:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
11916:                                    '</li>';
11917:                 }
11918:                 if (@unused) {
11919:                     $resulttext .= '<li>'.
11920:                                    &mt('Help form fields changed to "Not shown": [_1].',
11921:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
11922:                                    '</li>';
11923:                 }
11924:                 if ($maxsizechg) {
11925:                     $resulttext .= '<li>'.
11926:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
11927:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
11928:                                    '</li>';
11929:                 }
11930:             }
11931:             $resulttext .= '</ul>';
11932:         } else {
11933:             $resulttext = &mt('No changes made to contacts and form settings');
11934:         }
11935:     } else {
11936:         $resulttext = '<span class="LC_error">'.
11937:             &mt('An error occurred: [_1].',$putresult).'</span>';
11938:     }
11939:     return $resulttext;
11940: }
11941: 
11942: sub modify_passwords {
11943:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11944:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
11945:         $updatedefaults,$updateconf);
11946:     my $customfn = 'resetpw.html';
11947:     if (ref($domconfig{'passwords'}) eq 'HASH') {
11948:         %current = %{$domconfig{'passwords'}};
11949:     }
11950:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11951:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11952:     if (ref($types) eq 'ARRAY') {
11953:         @oktypes = @{$types};
11954:     }
11955:     push(@oktypes,'default');
11956: 
11957:     my %titles = &Apache::lonlocal::texthash (
11958:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
11959:         intauth_check  => 'Check bcrypt cost if authenticated',
11960:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
11961:         permanent      => 'Permanent e-mail address',
11962:         critical       => 'Critical notification address',
11963:         notify         => 'Notification address',
11964:         min            => 'Minimum password length',
11965:         max            => 'Maximum password length',
11966:         chars          => 'Required characters',
11967:         numsaved       => 'Number of previous passwords to save',
11968:         reset          => 'Resetting Forgotten Password',
11969:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
11970:         rules          => 'Rules for LON-CAPA Passwords',
11971:         crsownerchg    => 'Course Owner Changing Student Passwords',
11972:         username       => 'Username',
11973:         email          => 'E-mail address',
11974:     );
11975: 
11976: #
11977: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
11978: #
11979:     my (%curr_defaults,%save_defaults);
11980:     if (ref($domconfig{'defaults'}) eq 'HASH') {
11981:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
11982:             if ($key =~ /^intauth_(cost|check|switch)$/) {
11983:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
11984:             } else {
11985:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
11986:             }
11987:         }
11988:     }
11989:     my %staticdefaults = (
11990:         'resetlink'      => 2,
11991:         'resetcase'      => \@oktypes,
11992:         'resetprelink'   => 'both',
11993:         'resetemail'     => ['critical','notify','permanent'],
11994:         'intauth_cost'   => 10,
11995:         'intauth_check'  => 0,
11996:         'intauth_switch' => 0,
11997:     );
11998:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
11999:     foreach my $type (@oktypes) {
12000:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
12001:     }
12002:     my $linklife = $env{'form.passwords_link'};
12003:     $linklife =~ s/^\s+|\s+$//g;
12004:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
12005:         $newvalues{'resetlink'} = $linklife;
12006:         if ($current{'resetlink'}) {
12007:             if ($current{'resetlink'} ne $linklife) {
12008:                 $changes{'reset'} = 1;
12009:             }
12010:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12011:             if ($staticdefaults{'resetlink'} ne $linklife) {
12012:                 $changes{'reset'} = 1;
12013:             }
12014:         }
12015:     } elsif ($current{'resetlink'}) {
12016:         $changes{'reset'} = 1;
12017:     }
12018:     my @casesens;
12019:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
12020:     foreach my $case (sort(@posscase)) {
12021:         if (grep(/^\Q$case\E$/,@oktypes)) {
12022:             push(@casesens,$case);
12023:         }
12024:     }
12025:     $newvalues{'resetcase'} = \@casesens;
12026:     if (ref($current{'resetcase'}) eq 'ARRAY') {
12027:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
12028:         if (@diffs > 0) {
12029:             $changes{'reset'} = 1;
12030:         }
12031:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12032:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
12033:         if (@diffs > 0) {
12034:             $changes{'reset'} = 1;
12035:         }
12036:     }
12037:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
12038:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
12039:         if (exists($current{'resetprelink'})) {
12040:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
12041:                 $changes{'reset'} = 1;
12042:             }
12043:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12044:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
12045:                 $changes{'reset'} = 1;
12046:             }
12047:         }
12048:     } elsif ($current{'resetprelink'}) {
12049:         $changes{'reset'} = 1;
12050:     }
12051:     foreach my $type (@oktypes) {
12052:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
12053:         my @postlink;
12054:         foreach my $item (sort(@possplink)) {
12055:             if ($item =~ /^(email|username)$/) {
12056:                 push(@postlink,$item);
12057:             }
12058:         }
12059:         $newvalues{'resetpostlink'}{$type} = \@postlink;
12060:         unless ($changes{'reset'}) {
12061:             if (ref($current{'resetpostlink'}) eq 'HASH') {
12062:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
12063:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
12064:                     if (@diffs > 0) {
12065:                         $changes{'reset'} = 1;
12066:                     }
12067:                 } else {
12068:                     $changes{'reset'} = 1;
12069:                 }
12070:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12071:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
12072:                 if (@diffs > 0) {
12073:                     $changes{'reset'} = 1;
12074:                 }
12075:             }
12076:         }
12077:     }
12078:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
12079:     my @resetemail;
12080:     foreach my $item (sort(@possemailsrc)) {
12081:         if ($item =~ /^(permanent|critical|notify)$/) {
12082:             push(@resetemail,$item);
12083:         }
12084:     }
12085:     $newvalues{'resetemail'} = \@resetemail;
12086:     unless ($changes{'reset'}) {
12087:         if (ref($current{'resetemail'}) eq 'ARRAY') {
12088:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
12089:             if (@diffs > 0) {
12090:                 $changes{'reset'} = 1;
12091:             }
12092:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12093:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
12094:             if (@diffs > 0) {
12095:                 $changes{'reset'} = 1;
12096:             }
12097:         }
12098:     }
12099:     if ($env{'form.passwords_stdtext'} == 0) {
12100:         $newvalues{'resetremove'} = 1;
12101:         unless ($current{'resetremove'}) {
12102:             $changes{'reset'} = 1;
12103:         }
12104:     } elsif ($current{'resetremove'}) {
12105:         $changes{'reset'} = 1;
12106:     }
12107:     if ($env{'form.passwords_customfile.filename'} ne '') {
12108:         my $servadm = $r->dir_config('lonAdmEMail');
12109:         my $servadm = $r->dir_config('lonAdmEMail');
12110:         my ($configuserok,$author_ok,$switchserver) =
12111:             &config_check($dom,$confname,$servadm);
12112:         my $error;
12113:         if ($configuserok eq 'ok') {
12114:             if ($switchserver) {
12115:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
12116:             } else {
12117:                 if ($author_ok eq 'ok') {
12118:                     my ($result,$customurl) =
12119:                         &publishlogo($r,'upload','passwords_customfile',$dom,
12120:                                      $confname,'customtext/resetpw','','',$customfn);
12121:                     if ($result eq 'ok') {
12122:                         $newvalues{'resetcustom'} = $customurl;
12123:                         $changes{'reset'} = 1;
12124:                     } else {
12125:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
12126:                     }
12127:                 } else {
12128:                     $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);
12129:                 }
12130:             }
12131:         } else {
12132:             $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);
12133:         }
12134:         if ($error) {
12135:             &Apache::lonnet::logthis($error);
12136:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12137:         }
12138:     } elsif ($current{'resetcustom'}) {
12139:         if ($env{'form.passwords_custom_del'}) {
12140:             $changes{'reset'} = 1;
12141:         } else {
12142:             $newvalues{'resetcustom'} = $current{'resetcustom'};
12143:         }
12144:     }
12145:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
12146:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
12147:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
12148:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
12149:             $changes{'intauth'} = 1;
12150:         }
12151:     } else {
12152:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
12153:     }
12154:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
12155:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
12156:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
12157:             $changes{'intauth'} = 1;
12158:         }
12159:     } else {
12160:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
12161:     }
12162:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
12163:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
12164:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
12165:             $changes{'intauth'} = 1;
12166:         }
12167:     } else {
12168:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
12169:     }
12170:     foreach my $item ('cost','check','switch') {
12171:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
12172:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
12173:             $updatedefaults = 1;
12174:         }
12175:     }
12176:     foreach my $rule ('min','max','numsaved') {
12177:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
12178:         my $ruleok;
12179:         if ($rule eq 'min') {
12180:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
12181:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
12182:                     $ruleok = 1;
12183:                 }
12184:             }
12185:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
12186:                  ($env{'form.passwords_'.$rule} ne '0')) {
12187:             $ruleok = 1;
12188:         }
12189:         if ($ruleok) {
12190:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
12191:             if (exists($current{$rule})) {
12192:                 if ($newvalues{$rule} ne $current{$rule}) {
12193:                     $changes{'rules'} = 1;
12194:                 }
12195:             } elsif ($rule eq 'min') {
12196:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
12197:                     $changes{'rules'} = 1;
12198:                 }
12199:             } else {
12200:                 $changes{'rules'} = 1;
12201:             }
12202:         } elsif (exists($current{$rule})) {
12203:             $changes{'rules'} = 1;
12204:         }
12205:     }
12206:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
12207:     my @chars;
12208:     foreach my $item (sort(@posschars)) {
12209:         if ($item =~ /^(uc|lc|num|spec)$/) {
12210:             push(@chars,$item);
12211:         }
12212:     }
12213:     $newvalues{'chars'} = \@chars;
12214:     unless ($changes{'rules'}) {
12215:         if (ref($current{'chars'}) eq 'ARRAY') {
12216:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
12217:             if (@diffs > 0) {
12218:                 $changes{'rules'} = 1;
12219:             }
12220:         } else {
12221:             if (@chars > 0) {
12222:                 $changes{'rules'} = 1;
12223:             }
12224:         }
12225:     }
12226:     my %crsownerchg = (
12227:                         by => [],
12228:                         for => [],
12229:                       );
12230:     foreach my $item ('by','for') {
12231:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
12232:         foreach my $type (sort(@posstypes)) {
12233:             if (grep(/^\Q$type\E$/,@oktypes)) {
12234:                 push(@{$crsownerchg{$item}},$type);
12235:             }
12236:         }
12237:     }
12238:     $newvalues{'crsownerchg'} = \%crsownerchg;
12239:     if (ref($current{'crsownerchg'}) eq 'HASH') {
12240:         foreach my $item ('by','for') {
12241:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
12242:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
12243:                 if (@diffs > 0) {
12244:                     $changes{'crsownerchg'} = 1;
12245:                     last;
12246:                 }
12247:             }
12248:         }
12249:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
12250:         foreach my $item ('by','for') {
12251:             if (@{$crsownerchg{$item}} > 0) {
12252:                 $changes{'crsownerchg'} = 1;
12253:                 last;
12254:             }
12255:         }
12256:     }
12257: 
12258:     my %confighash = (
12259:                         defaults  => \%save_defaults,
12260:                         passwords => \%newvalues,
12261:                      );
12262:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
12263: 
12264:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
12265:     if ($putresult eq 'ok') {
12266:         if (keys(%changes) > 0) {
12267:             $resulttext = &mt('Changes made: ').'<ul>';
12268:             foreach my $key ('reset','intauth','rules','crsownerchg') {
12269:                 if ($changes{$key}) {
12270:                     unless ($key eq 'intauth') {
12271:                         $updateconf = 1;
12272:                     }
12273:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
12274:                     if ($key eq 'reset') {
12275:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
12276:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
12277:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
12278:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
12279:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
12280:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
12281:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
12282:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
12283:                             }
12284:                         } else {
12285:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
12286:                         }
12287:                         if ($confighash{'passwords'}{'resetlink'}) {
12288:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
12289:                         } else {
12290:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
12291:                                                   &mt('Will default to 2 hours').'</li>';
12292:                         }
12293:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
12294:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
12295:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
12296:                             } else {
12297:                                 my $casesens;
12298:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
12299:                                     if ($type eq 'default') {
12300:                                         $casesens .= $othertitle.', ';
12301:                                     } elsif ($usertypes->{$type} ne '') {
12302:                                         $casesens .= $usertypes->{$type}.', ';
12303:                                     }
12304:                                 }
12305:                                 $casesens =~ s/\Q, \E$//;
12306:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
12307:                             }
12308:                         } else {
12309:                             $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>';
12310:                         }
12311:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
12312:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
12313:                         } else {
12314:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
12315:                         }
12316:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
12317:                             my $output;
12318:                             if (ref($types) eq 'ARRAY') {
12319:                                 foreach my $type (@{$types}) {
12320:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
12321:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
12322:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
12323:                                         } else {
12324:                                             $output .= $usertypes->{$type}.' -- '.
12325:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
12326:                                         }
12327:                                     }
12328:                                 }
12329:                             }
12330:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
12331:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
12332:                                     $output .= $othertitle.' -- '.&mt('none');
12333:                                 } else {
12334:                                     $output .= $othertitle.' -- '.
12335:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
12336:                                 }
12337:                             }
12338:                             if ($output) {
12339:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
12340:                             } else {
12341:                                 $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>';
12342:                             }
12343:                         } else {
12344:                             $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>';
12345:                         }
12346:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
12347:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
12348:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
12349:                             } else {
12350:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
12351:                             }
12352:                         } else {
12353:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA usedfor verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
12354:                         }
12355:                         if ($confighash{'passwords'}{'resetremove'}) {
12356:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
12357:                         } else {
12358:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
12359:                         }
12360:                         if ($confighash{'passwords'}{'resetcustom'}) {
12361:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
12362:                                                                             &mt('custom text'),600,500,undef,undef,
12363:                                                                             undef,undef,'background-color:#ffffff');
12364:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
12365:                         } else {
12366:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
12367:                         }
12368:                     } elsif ($key eq 'intauth') {
12369:                         foreach my $item ('cost','switch','check') {
12370:                             my $value = $save_defaults{$key.'_'.$item};
12371:                             if ($item eq 'switch') {
12372:                                 my %optiondesc = &Apache::lonlocal::texthash (
12373:                                                      0 => 'No',
12374:                                                      1 => 'Yes',
12375:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
12376:                                                  );
12377:                                 if ($value =~ /^(0|1|2)$/) {
12378:                                     $value = $optiondesc{$value};
12379:                                 } else {
12380:                                     $value = &mt('none -- defaults to No');
12381:                                 }
12382:                             } elsif ($item eq 'check') {
12383:                                 my %optiondesc = &Apache::lonlocal::texthash (
12384:                                                      0 => 'No',
12385:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12386:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
12387:                                                  );
12388:                                 if ($value =~ /^(0|1|2)$/) {
12389:                                     $value = $optiondesc{$value};
12390:                                 } else {
12391:                                     $value = &mt('none -- defaults to No');
12392:                                 }
12393:                             }
12394:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
12395:                         }
12396:                     } elsif ($key eq 'rules') {
12397:                         foreach my $rule ('min','max','numsaved') {
12398:                             if ($confighash{'passwords'}{$rule} eq '') {
12399:                                 if ($rule eq 'min') {
12400:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
12401:                                                    ' '.&mt('Default of [_1] will be used',
12402:                                                            $Apache::lonnet::passwdmin).'</li>';
12403:                                 } else {
12404:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
12405:                                 }
12406:                             } else {
12407:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
12408:                             }
12409:                         }
12410:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
12411:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
12412:                                 my %rulenames = &Apache::lonlocal::texthash(
12413:                                                      uc => 'At least one upper case letter',
12414:                                                      lc => 'At least one lower case letter',
12415:                                                      num => 'At least one number',
12416:                                                      spec => 'At least one non-alphanumeric',
12417:                                                    );
12418:                                 my $needed = '<ul><li>'.
12419:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
12420:                                              '</li></ul>';
12421:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
12422:                             } else {
12423:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
12424:                             }
12425:                         } else {
12426:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
12427:                         }
12428:                     } elsif ($key eq 'crsownerchg') {
12429:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
12430:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
12431:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
12432:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
12433:                             } else {
12434:                                 my %crsownerstr;
12435:                                 foreach my $item ('by','for') {
12436:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
12437:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
12438:                                             if ($type eq 'default') {
12439:                                                 $crsownerstr{$item} .= $othertitle.', ';
12440:                                             } elsif ($usertypes->{$type} ne '') {
12441:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
12442:                                             }
12443:                                         }
12444:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
12445:                                     }
12446:                                 }
12447:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
12448:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
12449:                             }
12450:                         } else {
12451:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
12452:                         }
12453:                     }
12454:                     $resulttext .= '</ul></li>';
12455:                 }
12456:             }
12457:             $resulttext .= '</ul>';
12458:         } else {
12459:             $resulttext = &mt('No changes made to password settings');
12460:         }
12461:         my $cachetime = 24*60*60;
12462:         if ($updatedefaults) {
12463:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12464:             if (ref($lastactref) eq 'HASH') {
12465:                 $lastactref->{'domdefaults'} = 1;
12466:             }
12467:         }
12468:         if ($updateconf) {
12469:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
12470:             if (ref($lastactref) eq 'HASH') {
12471:                 $lastactref->{'passwdconf'} = 1;
12472:             }
12473:         }
12474:     } else {
12475:         $resulttext = '<span class="LC_error">'.
12476:             &mt('An error occurred: [_1]',$putresult).'</span>';
12477:     }
12478:     if ($errors) {
12479:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
12480:                        $errors.'</ul></p>';
12481:     }
12482:     return $resulttext;
12483: }
12484: 
12485: sub modify_usercreation {
12486:     my ($dom,%domconfig) = @_;
12487:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
12488:     my $warningmsg;
12489:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
12490:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
12491:             if ($key eq 'cancreate') {
12492:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
12493:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
12494:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
12495:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12496:                         } else {
12497:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12498:                         }
12499:                     }
12500:                 }
12501:             } elsif ($key eq 'email_rule') {
12502:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
12503:             } else {
12504:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
12505:             }
12506:         }
12507:     }
12508:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
12509:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
12510:     my @contexts = ('author','course','requestcrs');
12511:     foreach my $item(@contexts) {
12512:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
12513:     }
12514:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
12515:         foreach my $item (@contexts) {
12516:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
12517:                 push(@{$changes{'cancreate'}},$item);
12518:             }
12519:         }
12520:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
12521:         foreach my $item (@contexts) {
12522:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
12523:                 if ($cancreate{$item} ne 'any') {
12524:                     push(@{$changes{'cancreate'}},$item);
12525:                 }
12526:             } else {
12527:                 if ($cancreate{$item} ne 'none') {
12528:                     push(@{$changes{'cancreate'}},$item);
12529:                 }
12530:             }
12531:         }
12532:     } else {
12533:         foreach my $item (@contexts)  {
12534:             push(@{$changes{'cancreate'}},$item);
12535:         }
12536:     }
12537: 
12538:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
12539:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
12540:             if (!grep(/^\Q$type\E$/,@username_rule)) {
12541:                 push(@{$changes{'username_rule'}},$type);
12542:             }
12543:         }
12544:         foreach my $type (@username_rule) {
12545:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
12546:                 push(@{$changes{'username_rule'}},$type);
12547:             }
12548:         }
12549:     } else {
12550:         push(@{$changes{'username_rule'}},@username_rule);
12551:     }
12552: 
12553:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
12554:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
12555:             if (!grep(/^\Q$type\E$/,@id_rule)) {
12556:                 push(@{$changes{'id_rule'}},$type);
12557:             }
12558:         }
12559:         foreach my $type (@id_rule) {
12560:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
12561:                 push(@{$changes{'id_rule'}},$type);
12562:             }
12563:         }
12564:     } else {
12565:         push(@{$changes{'id_rule'}},@id_rule);
12566:     }
12567: 
12568:     my @authen_contexts = ('author','course','domain');
12569:     my @authtypes = ('int','krb4','krb5','loc');
12570:     my %authhash;
12571:     foreach my $item (@authen_contexts) {
12572:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
12573:         foreach my $auth (@authtypes) {
12574:             if (grep(/^\Q$auth\E$/,@authallowed)) {
12575:                 $authhash{$item}{$auth} = 1;
12576:             } else {
12577:                 $authhash{$item}{$auth} = 0;
12578:             }
12579:         }
12580:     }
12581:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
12582:         foreach my $item (@authen_contexts) {
12583:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
12584:                 foreach my $auth (@authtypes) {
12585:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
12586:                         push(@{$changes{'authtypes'}},$item);
12587:                         last;
12588:                     }
12589:                 }
12590:             }
12591:         }
12592:     } else {
12593:         foreach my $item (@authen_contexts) {
12594:             push(@{$changes{'authtypes'}},$item);
12595:         }
12596:     }
12597: 
12598:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
12599:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
12600:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
12601:     $save_usercreate{'id_rule'} = \@id_rule;
12602:     $save_usercreate{'username_rule'} = \@username_rule,
12603:     $save_usercreate{'authtypes'} = \%authhash;
12604: 
12605:     my %usercreation_hash =  (
12606:         usercreation     => \%save_usercreate,
12607:     );
12608: 
12609:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
12610:                                              $dom);
12611: 
12612:     if ($putresult eq 'ok') {
12613:         if (keys(%changes) > 0) {
12614:             $resulttext = &mt('Changes made:').'<ul>';
12615:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
12616:                 my %lt = &usercreation_types();
12617:                 foreach my $type (@{$changes{'cancreate'}}) {
12618:                     my $chgtext = $lt{$type}.', ';
12619:                     if ($cancreate{$type} eq 'none') {
12620:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
12621:                     } elsif ($cancreate{$type} eq 'any') {
12622:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
12623:                     } elsif ($cancreate{$type} eq 'official') {
12624:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
12625:                     } elsif ($cancreate{$type} eq 'unofficial') {
12626:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
12627:                     }
12628:                     $resulttext .= '<li>'.$chgtext.'</li>';
12629:                 }
12630:             }
12631:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
12632:                 my ($rules,$ruleorder) = 
12633:                     &Apache::lonnet::inst_userrules($dom,'username');
12634:                 my $chgtext = '<ul>';
12635:                 foreach my $type (@username_rule) {
12636:                     if (ref($rules->{$type}) eq 'HASH') {
12637:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
12638:                     }
12639:                 }
12640:                 $chgtext .= '</ul>';
12641:                 if (@username_rule > 0) {
12642:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
12643:                 } else {
12644:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
12645:                 }
12646:             }
12647:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
12648:                 my ($idrules,$idruleorder) = 
12649:                     &Apache::lonnet::inst_userrules($dom,'id');
12650:                 my $chgtext = '<ul>';
12651:                 foreach my $type (@id_rule) {
12652:                     if (ref($idrules->{$type}) eq 'HASH') {
12653:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
12654:                     }
12655:                 }
12656:                 $chgtext .= '</ul>';
12657:                 if (@id_rule > 0) {
12658:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
12659:                 } else {
12660:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
12661:                 }
12662:             }
12663:             my %authname = &authtype_names();
12664:             my %context_title = &context_names();
12665:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
12666:                 my $chgtext = '<ul>';
12667:                 foreach my $type (@{$changes{'authtypes'}}) {
12668:                     my @allowed;
12669:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
12670:                     foreach my $auth (@authtypes) {
12671:                         if ($authhash{$type}{$auth}) {
12672:                             push(@allowed,$authname{$auth});
12673:                         }
12674:                     }
12675:                     if (@allowed > 0) {
12676:                         $chgtext .= join(', ',@allowed).'</li>';
12677:                     } else {
12678:                         $chgtext .= &mt('none').'</li>';
12679:                     }
12680:                 }
12681:                 $chgtext .= '</ul>';
12682:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
12683:                 $resulttext .= '</li>';
12684:             }
12685:             $resulttext .= '</ul>';
12686:         } else {
12687:             $resulttext = &mt('No changes made to user creation settings');
12688:         }
12689:     } else {
12690:         $resulttext = '<span class="LC_error">'.
12691:             &mt('An error occurred: [_1]',$putresult).'</span>';
12692:     }
12693:     if ($warningmsg ne '') {
12694:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
12695:     }
12696:     return $resulttext;
12697: }
12698: 
12699: sub modify_selfcreation {
12700:     my ($dom,$lastactref,%domconfig) = @_;
12701:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
12702:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
12703:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12704:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
12705:     if (ref($typesref) eq 'ARRAY') {
12706:         @types = @{$typesref};
12707:     }
12708:     if (ref($usertypesref) eq 'HASH') {
12709:         %usertypes = %{$usertypesref};
12710:     }
12711:     $usertypes{'default'} = $othertitle;
12712: #
12713: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
12714: #
12715:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
12716:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
12717:             if ($key eq 'cancreate') {
12718:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
12719:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
12720:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
12721:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
12722:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
12723:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
12724:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
12725:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
12726:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12727:                         } else {
12728:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12729:                         }
12730:                     }
12731:                 }
12732:             } elsif ($key eq 'email_rule') {
12733:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
12734:             } else {
12735:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
12736:             }
12737:         }
12738:     }
12739: #
12740: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
12741: #
12742:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
12743:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
12744:             if ($key eq 'selfcreate') {
12745:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
12746:             } else {
12747:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
12748:             }
12749:         }
12750:     }
12751: #
12752: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
12753: #
12754:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12755:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
12756:             if ($key eq 'inststatusguest') {
12757:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
12758:             } else {
12759:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
12760:             }
12761:         }
12762:     }
12763: 
12764:     my @contexts = ('selfcreate');
12765:     @{$cancreate{'selfcreate'}} = ();
12766:     %{$cancreate{'emailusername'}} = ();
12767:     if (@types) {
12768:         @{$cancreate{'statustocreate'}} = ();
12769:     }
12770:     %{$cancreate{'selfcreateprocessing'}} = ();
12771:     %{$cancreate{'shibenv'}} = ();
12772:     %{$cancreate{'emailverified'}} = ();
12773:     %{$cancreate{'emailoptions'}} = ();
12774:     %{$cancreate{'emaildomain'}} = ();
12775:     my %selfcreatetypes = (
12776:                              sso   => 'users authenticated by institutional single sign on',
12777:                              login => 'users authenticated by institutional log-in',
12778:                              email => 'users verified by e-mail',
12779:                           );
12780: #
12781: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
12782: # is permitted.
12783: #
12784: 
12785:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
12786: 
12787:     my (@statuses,%email_rule);
12788:     foreach my $item ('login','sso','email') {
12789:         if ($item eq 'email') {
12790:             if ($env{'form.cancreate_email'}) {
12791:                 if (@types) {
12792:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
12793:                     foreach my $status (@poss_statuses) {
12794:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
12795:                             push(@statuses,$status);
12796:                         }
12797:                     }
12798:                     $save_inststatus{'inststatusguest'} = \@statuses;
12799:                 } else {
12800:                     push(@statuses,'default');
12801:                 }
12802:                 if (@statuses) {
12803:                     my %curr_rule;
12804:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
12805:                         foreach my $type (@statuses) {
12806:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
12807:                         }
12808:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
12809:                         foreach my $type (@statuses) {
12810:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
12811:                         }
12812:                     }
12813:                     push(@{$cancreate{'selfcreate'}},'email');
12814:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
12815:                     my %curremaildom;
12816:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
12817:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
12818:                     }
12819:                     foreach my $type (@statuses) {
12820:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
12821:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
12822:                         }
12823:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
12824:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
12825:                         }
12826:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
12827: #
12828: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
12829: #
12830:                             my $chosen = $1;
12831:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
12832:                                 my $emaildom;
12833:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
12834:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
12835:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
12836:                                     if (ref($curremaildom{$type}) eq 'HASH') {
12837:                                         if (exists($curremaildom{$type}{$chosen})) {
12838:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
12839:                                                 push(@{$changes{'cancreate'}},'emaildomain');
12840:                                             }
12841:                                         } elsif ($emaildom ne '') {
12842:                                             push(@{$changes{'cancreate'}},'emaildomain');
12843:                                         }
12844:                                     } elsif ($emaildom ne '') {
12845:                                         push(@{$changes{'cancreate'}},'emaildomain');
12846:                                     }
12847:                                 }
12848:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
12849:                             } elsif ($chosen eq 'custom') {
12850:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
12851:                                 $email_rule{$type} = [];
12852:                                 if (ref($emailrules) eq 'HASH') {
12853:                                     foreach my $rule (@possemail_rules) {
12854:                                         if (exists($emailrules->{$rule})) {
12855:                                             push(@{$email_rule{$type}},$rule);
12856:                                         }
12857:                                     }
12858:                                 }
12859:                                 if (@{$email_rule{$type}}) {
12860:                                     $cancreate{'emailoptions'}{$type} = 'custom';
12861:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
12862:                                         if (@{$curr_rule{$type}} > 0) {
12863:                                             foreach my $rule (@{$curr_rule{$type}}) {
12864:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
12865:                                                     push(@{$changes{'email_rule'}},$type);
12866:                                                 }
12867:                                             }
12868:                                         }
12869:                                         foreach my $type (@{$email_rule{$type}}) {
12870:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
12871:                                                 push(@{$changes{'email_rule'}},$type);
12872:                                             }
12873:                                         }
12874:                                     } else {
12875:                                         push(@{$changes{'email_rule'}},$type);
12876:                                     }
12877:                                 }
12878:                             } else {
12879:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
12880:                             }
12881:                         }
12882:                     }
12883:                     if (@types) {
12884:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
12885:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
12886:                             if (@changed) {
12887:                                 push(@{$changes{'inststatus'}},'inststatusguest');
12888:                             }
12889:                         } else {
12890:                             push(@{$changes{'inststatus'}},'inststatusguest');
12891:                         }
12892:                     }
12893:                 } else {
12894:                     delete($env{'form.cancreate_email'});
12895:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
12896:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
12897:                             push(@{$changes{'inststatus'}},'inststatusguest');
12898:                         }
12899:                     }
12900:                 }
12901:             } else {
12902:                 $save_inststatus{'inststatusguest'} = [];
12903:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
12904:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
12905:                         push(@{$changes{'inststatus'}},'inststatusguest');
12906:                     }
12907:                 }
12908:             }
12909:         } else {
12910:             if ($env{'form.cancreate_'.$item}) {
12911:                 push(@{$cancreate{'selfcreate'}},$item);
12912:             }
12913:         }
12914:     }
12915:     my (%userinfo,%savecaptcha);
12916:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
12917: #
12918: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
12919: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
12920: #
12921: 
12922:     if ($env{'form.cancreate_email'}) {
12923:         push(@contexts,'emailusername');
12924:         if (@statuses) {
12925:             foreach my $type (@statuses) {
12926:                 if (ref($infofields) eq 'ARRAY') {
12927:                     foreach my $field (@{$infofields}) {
12928:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
12929:                             $cancreate{'emailusername'}{$type}{$field} = $1;
12930:                         }
12931:                     }
12932:                 }
12933:             }
12934:         }
12935: #
12936: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
12937: # queued requests for self-creation of account verified by e-mail.
12938: #
12939: 
12940:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
12941:         @approvalnotify = sort(@approvalnotify);
12942:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
12943:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
12944:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
12945:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
12946:                     push(@{$changes{'cancreate'}},'notify');
12947:                 }
12948:             } else {
12949:                 if ($cancreate{'notify'}{'approval'}) {
12950:                     push(@{$changes{'cancreate'}},'notify');
12951:                 }
12952:             }
12953:         } elsif ($cancreate{'notify'}{'approval'}) {
12954:             push(@{$changes{'cancreate'}},'notify');
12955:         }
12956: 
12957:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
12958:     }
12959: #  
12960: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
12961: # institutional log-in.
12962: #
12963:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
12964:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
12965:                ($domdefaults{'auth_def'} eq 'localauth'))) {
12966:             $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.').' '.
12967:                           &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.');
12968:         }
12969:     }
12970:     my @fields = ('lastname','firstname','middlename','generation',
12971:                   'permanentemail','id');
12972:     my @shibfields = (@fields,'inststatus');
12973:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12974: #
12975: # Where usernames may created for institutional log-in and/or institutional single sign on:
12976: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
12977: # may self-create accounts 
12978: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
12979: # which the user may supply, if institutional data is unavailable.
12980: #
12981:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
12982:         if (@types) {
12983:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
12984:             push(@contexts,'statustocreate');
12985:             foreach my $type (@types) {
12986:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
12987:                 foreach my $field (@fields) {
12988:                     if (grep(/^\Q$field\E$/,@modifiable)) {
12989:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
12990:                     } else {
12991:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
12992:                     }
12993:                 }
12994:             }
12995:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
12996:                 foreach my $type (@types) {
12997:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
12998:                         foreach my $field (@fields) {
12999:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
13000:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
13001:                                 push(@{$changes{'selfcreate'}},$type);
13002:                                 last;
13003:                             }
13004:                         }
13005:                     }
13006:                 }
13007:             } else {
13008:                 foreach my $type (@types) {
13009:                     push(@{$changes{'selfcreate'}},$type);
13010:                 }
13011:             }
13012:         }
13013:         foreach my $field (@shibfields) {
13014:             if ($env{'form.shibenv_'.$field} ne '') {
13015:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
13016:             }
13017:         }
13018:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13019:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
13020:                 foreach my $field (@shibfields) {
13021:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
13022:                         push(@{$changes{'cancreate'}},'shibenv');
13023:                     }
13024:                 }
13025:             } else {
13026:                 foreach my $field (@shibfields) {
13027:                     if ($env{'form.shibenv_'.$field}) {
13028:                         push(@{$changes{'cancreate'}},'shibenv');
13029:                         last;
13030:                     }
13031:                 }
13032:             }
13033:         }
13034:     }
13035:     foreach my $item (@contexts) {
13036:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
13037:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
13038:                 if (ref($cancreate{$item}) eq 'ARRAY') {
13039:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
13040:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13041:                             push(@{$changes{'cancreate'}},$item);
13042:                         }
13043:                     }
13044:                 }
13045:             }
13046:             if (ref($cancreate{$item}) eq 'ARRAY') {
13047:                 foreach my $type (@{$cancreate{$item}}) {
13048:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
13049:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13050:                             push(@{$changes{'cancreate'}},$item);
13051:                         }
13052:                     }
13053:                 }
13054:             }
13055:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
13056:             if (ref($cancreate{$item}) eq 'HASH') {
13057:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
13058:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13059:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
13060:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13061:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13062:                                     push(@{$changes{'cancreate'}},$item);
13063:                                 }
13064:                             }
13065:                         }
13066:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13067:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13068:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13069:                                 push(@{$changes{'cancreate'}},$item);
13070:                             }
13071:                         }
13072:                     }
13073:                 }
13074:                 foreach my $type (keys(%{$cancreate{$item}})) {
13075:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13076:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13077:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13078:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13079:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13080:                                         push(@{$changes{'cancreate'}},$item);
13081:                                     }
13082:                                 }
13083:                             } else {
13084:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13085:                                     push(@{$changes{'cancreate'}},$item);
13086:                                 }
13087:                             }
13088:                         }
13089:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13090:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13091:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13092:                                 push(@{$changes{'cancreate'}},$item);
13093:                             }
13094:                         }
13095:                     }
13096:                 }
13097:             }
13098:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
13099:             if (ref($cancreate{$item}) eq 'ARRAY') {
13100:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
13101:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13102:                         push(@{$changes{'cancreate'}},$item);
13103:                     }
13104:                 }
13105:             }
13106:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13107:             if (ref($cancreate{$item}) eq 'HASH') {
13108:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13109:                     push(@{$changes{'cancreate'}},$item);
13110:                 }
13111:             }
13112:         } elsif ($item eq 'emailusername') {
13113:             if (ref($cancreate{$item}) eq 'HASH') {
13114:                 foreach my $type (keys(%{$cancreate{$item}})) {
13115:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13116:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13117:                             if ($cancreate{$item}{$type}{$field}) {
13118:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13119:                                     push(@{$changes{'cancreate'}},$item);
13120:                                 }
13121:                                 last;
13122:                             }
13123:                         }
13124:                     }
13125:                 }
13126:             }
13127:         }
13128:     }
13129: #
13130: # Populate %save_usercreate hash with updates to self-creation configuration.
13131: #
13132:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
13133:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
13134:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
13135:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
13136:     if (ref($cancreate{'notify'}) eq 'HASH') {
13137:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
13138:     }
13139:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
13140:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
13141:     }
13142:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
13143:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
13144:     }
13145:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
13146:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
13147:     }
13148:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
13149:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
13150:     }
13151:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13152:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
13153:     }
13154:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
13155:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
13156:     }
13157:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
13158:     $save_usercreate{'email_rule'} = \%email_rule;
13159: 
13160:     my %userconfig_hash = (
13161:             usercreation     => \%save_usercreate,
13162:             usermodification => \%save_usermodify,
13163:             inststatus       => \%save_inststatus,
13164:     );
13165: 
13166:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
13167:                                              $dom);
13168: #
13169: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
13170: #
13171:     if ($putresult eq 'ok') {
13172:         if (keys(%changes) > 0) {
13173:             $resulttext = &mt('Changes made:').'<ul>';
13174:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
13175:                 my %lt = &selfcreation_types();
13176:                 foreach my $type (@{$changes{'cancreate'}}) {
13177:                     my $chgtext = '';
13178:                     if ($type eq 'selfcreate') {
13179:                         if (@{$cancreate{$type}} == 0) {
13180:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
13181:                         } else {
13182:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
13183:                                         '<ul>';
13184:                             foreach my $case (@{$cancreate{$type}}) {
13185:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
13186:                             }
13187:                             $chgtext .= '</ul>';
13188:                             if (ref($cancreate{$type}) eq 'ARRAY') {
13189:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
13190:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13191:                                         if (@{$cancreate{'statustocreate'}} == 0) {
13192:                                             $chgtext .= '<span class="LC_warning">'.
13193:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
13194:                                                         '</span><br />';
13195:                                         }
13196:                                     }
13197:                                 }
13198:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
13199:                                     if (!@statuses) {
13200:                                         $chgtext .= '<span class="LC_warning">'.
13201:                                                     &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.").
13202:                                                     '</span><br />';
13203: 
13204:                                     }
13205:                                 }
13206:                             }
13207:                         }
13208:                     } elsif ($type eq 'shibenv') {
13209:                         if (keys(%{$cancreate{$type}}) == 0) {
13210:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
13211:                         } else {
13212:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
13213:                                         '<ul>';
13214:                             foreach my $field (@shibfields) {
13215:                                 next if ($cancreate{$type}{$field} eq '');
13216:                                 if ($field eq 'inststatus') {
13217:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
13218:                                 } else {
13219:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
13220:                                 }
13221:                             }
13222:                             $chgtext .= '</ul>';
13223:                         }
13224:                     } elsif ($type eq 'statustocreate') {
13225:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
13226:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
13227:                             if (@{$cancreate{'selfcreate'}} > 0) {
13228:                                 if (@{$cancreate{'statustocreate'}} == 0) {
13229:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
13230:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
13231:                                         $chgtext .= '<br />'.
13232:                                                     '<span class="LC_warning">'.
13233:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
13234:                                                     '</span>';
13235:                                     }
13236:                                 } elsif (keys(%usertypes) > 0) {
13237:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13238:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
13239:                                     } else {
13240:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
13241:                                     }
13242:                                     $chgtext .= '<ul>';
13243:                                     foreach my $case (@{$cancreate{$type}}) {
13244:                                         if ($case eq 'default') {
13245:                                             $chgtext .= '<li>'.$othertitle.'</li>';
13246:                                         } else {
13247:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
13248:                                         }
13249:                                     }
13250:                                     $chgtext .= '</ul>';
13251:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13252:                                         $chgtext .= '<span class="LC_warning">'.
13253:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
13254:                                                     '</span>';
13255:                                     }
13256:                                 }
13257:                             } else {
13258:                                 if (@{$cancreate{$type}} == 0) {
13259:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
13260:                                 } else {
13261:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
13262:                                 }
13263:                             }
13264:                             $chgtext .= '<br />';
13265:                         }
13266:                     } elsif ($type eq 'selfcreateprocessing') {
13267:                         my %choices = &Apache::lonlocal::texthash (
13268:                                                                     automatic => 'Automatic approval',
13269:                                                                     approval  => 'Queued for approval',
13270:                                                                   );
13271:                         if (@types) {
13272:                             if (@statuses) {
13273:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
13274:                                             '<ul>';
13275:                                 foreach my $status (@statuses) {
13276:                                     if ($status eq 'default') {
13277:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13278:                                     } else {
13279:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13280:                                     }
13281:                                 }
13282:                                 $chgtext .= '</ul>';
13283:                             }
13284:                         } else {
13285:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
13286:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
13287:                         }
13288:                     } elsif ($type eq 'emailverified') {
13289:                         my %options = &Apache::lonlocal::texthash (
13290:                                                                     all   => 'Same as e-mail',
13291:                                                                     first => 'Omit @domain',
13292:                                                                     free  => 'Free to choose',
13293:                                                                   );
13294:                         if (@types) {
13295:                             if (@statuses) {
13296:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
13297:                                             '<ul>';
13298:                                 foreach my $status (@statuses) {
13299:                                     if ($status eq 'default') {
13300:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
13301:                                     } else {
13302:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
13303:                                     }
13304:                                 }
13305:                                 $chgtext .= '</ul>';
13306:                             }
13307:                         } else {
13308:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
13309:                                             $options{$cancreate{'emailverified'}{'default'}});
13310:                         }
13311:                     } elsif ($type eq 'emailoptions') {
13312:                         my %options = &Apache::lonlocal::texthash (
13313:                                                                     any     => 'Any e-mail',
13314:                                                                     inst    => 'Institutional only',
13315:                                                                     noninst => 'Non-institutional only',
13316:                                                                     custom  => 'Custom restrictions',
13317:                                                                   );
13318:                         if (@types) {
13319:                             if (@statuses) {
13320:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
13321:                                             '<ul>';
13322:                                 foreach my $status (@statuses) {
13323:                                     if ($type eq 'default') {
13324:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
13325:                                     } else {
13326:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
13327:                                     }
13328:                                 }
13329:                                 $chgtext .= '</ul>';
13330:                             }
13331:                         } else {
13332:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
13333:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
13334:                             } else {
13335:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
13336:                                                 $options{$cancreate{'emailoptions'}{'default'}});
13337:                             }
13338:                         }
13339:                     } elsif ($type eq 'emaildomain') {
13340:                         my $output;
13341:                         if (@statuses) {
13342:                             foreach my $type (@statuses) {
13343:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
13344:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
13345:                                         if ($type eq 'default') {
13346:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13347:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
13348:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
13349:                                             } else {
13350:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
13351:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
13352:                                             }
13353:                                         } else {
13354:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13355:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
13356:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
13357:                                             } else {
13358:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
13359:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
13360:                                             }
13361:                                         }
13362:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
13363:                                         if ($type eq 'default') {
13364:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13365:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
13366:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
13367:                                             } else {
13368:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
13369:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
13370:                                             }
13371:                                         } else {
13372:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13373:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
13374:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
13375:                                             } else {
13376:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
13377:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
13378:                                             }
13379:                                         }
13380:                                     }
13381:                                 }
13382:                             }
13383:                         }
13384:                         if ($output ne '') {
13385:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
13386:                                         '<ul>'.$output.'</ul>';
13387:                         }
13388:                     } elsif ($type eq 'captcha') {
13389:                         if ($savecaptcha{$type} eq 'notused') {
13390:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
13391:                         } else {
13392:                             my %captchas = &captcha_phrases();
13393:                             if ($captchas{$savecaptcha{$type}}) {
13394:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
13395:                             } else {
13396:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
13397:                             }
13398:                         }
13399:                     } elsif ($type eq 'recaptchakeys') {
13400:                         my ($privkey,$pubkey);
13401:                         if (ref($savecaptcha{$type}) eq 'HASH') {
13402:                             $pubkey = $savecaptcha{$type}{'public'};
13403:                             $privkey = $savecaptcha{$type}{'private'};
13404:                         }
13405:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
13406:                         if (!$pubkey) {
13407:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
13408:                         } else {
13409:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
13410:                         }
13411:                         if (!$privkey) {
13412:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
13413:                         } else {
13414:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
13415:                         }
13416:                         $chgtext .= '</ul>';
13417:                     } elsif ($type eq 'recaptchaversion') {
13418:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
13419:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
13420:                         }
13421:                     } elsif ($type eq 'emailusername') {
13422:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
13423:                             if (@statuses) {
13424:                                 foreach my $type (@statuses) {
13425:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
13426:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
13427:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
13428:                                                     '<ul>';
13429:                                             foreach my $field (@{$infofields}) {
13430:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
13431:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
13432:                                                 }
13433:                                             }
13434:                                             $chgtext .= '</ul>';
13435:                                         } else {
13436:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
13437:                                         }
13438:                                     } else {
13439:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
13440:                                     }
13441:                                 }
13442:                             }
13443:                         }
13444:                     } elsif ($type eq 'notify') {
13445:                         my $numapprove = 0;
13446:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
13447:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
13448:                                 if ($cancreate{'notify'}{'approval'}) {
13449:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
13450:                                     $numapprove ++;
13451:                                 }
13452:                             }
13453:                         }
13454:                         unless ($numapprove) {
13455:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
13456:                         }
13457:                     }
13458:                     if ($chgtext) {
13459:                         $resulttext .= '<li>'.$chgtext.'</li>';
13460:                     }
13461:                 }
13462:             }
13463:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
13464:                 my ($emailrules,$emailruleorder) =
13465:                     &Apache::lonnet::inst_userrules($dom,'email');
13466:                 foreach my $type (@{$changes{'email_rule'}}) {
13467:                     if (ref($email_rule{$type}) eq 'ARRAY') {
13468:                         my $chgtext = '<ul>';
13469:                         foreach my $rule (@{$email_rule{$type}}) {
13470:                             if (ref($emailrules->{$rule}) eq 'HASH') {
13471:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
13472:                             }
13473:                         }
13474:                         $chgtext .= '</ul>';
13475:                         my $typename;
13476:                         if (@types) {
13477:                             if ($type eq 'default') {
13478:                                 $typename = $othertitle;
13479:                             } else {
13480:                                 $typename = $usertypes{$type};
13481:                             }
13482:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
13483:                         }
13484:                         if (@{$email_rule{$type}} > 0) {
13485:                             $resulttext .= '<li>'.
13486:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
13487:                                                $usertypes{$type}).
13488:                                            $chgtext.
13489:                                            '</li>';
13490:                         } else {
13491:                             $resulttext .= '<li>'.
13492:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
13493:                                            '</li>'.
13494:                                            &mt('(Affiliation: [_1])',$typename);
13495:                         }
13496:                     }
13497:                 }
13498:             }
13499:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
13500:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
13501:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
13502:                         my $chgtext = '<ul>';
13503:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
13504:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
13505:                         }
13506:                         $chgtext .= '</ul>';
13507:                         $resulttext .= '<li>'.
13508:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
13509:                                           $chgtext.
13510:                                        '</li>';
13511:                     } else {
13512:                         $resulttext .= '<li>'.
13513:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
13514:                                        '</li>';
13515:                     }
13516:                 }
13517:             }
13518:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
13519:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
13520:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13521:                 foreach my $type (@{$changes{'selfcreate'}}) {
13522:                     my $typename = $type;
13523:                     if (keys(%usertypes) > 0) {
13524:                         if ($usertypes{$type} ne '') {
13525:                             $typename = $usertypes{$type};
13526:                         }
13527:                     }
13528:                     my @modifiable;
13529:                     $resulttext .= '<li>'.
13530:                                     &mt('Self-creation of account by users with status: [_1]',
13531:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
13532:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
13533:                     foreach my $field (@fields) {
13534:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
13535:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
13536:                         }
13537:                     }
13538:                     if (@modifiable > 0) {
13539:                         $resulttext .= join(', ',@modifiable);
13540:                     } else {
13541:                         $resulttext .= &mt('none');
13542:                     }
13543:                     $resulttext .= '</li>';
13544:                 }
13545:                 $resulttext .= '</ul></li>';
13546:             }
13547:             $resulttext .= '</ul>';
13548:             my $cachetime = 24*60*60;
13549:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
13550:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13551:             if (ref($lastactref) eq 'HASH') {
13552:                 $lastactref->{'domdefaults'} = 1;
13553:             }
13554:         } else {
13555:             $resulttext = &mt('No changes made to self-creation settings');
13556:         }
13557:     } else {
13558:         $resulttext = '<span class="LC_error">'.
13559:             &mt('An error occurred: [_1]',$putresult).'</span>';
13560:     }
13561:     if ($warningmsg ne '') {
13562:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
13563:     }
13564:     return $resulttext;
13565: }
13566: 
13567: sub process_captcha {
13568:     my ($container,$changes,$newsettings,$currsettings) = @_;
13569:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
13570:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
13571:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
13572:         $newsettings->{'captcha'} = 'original';
13573:     }
13574:     my %current;
13575:     if (ref($currsettings) eq 'HASH') {
13576:         %current = %{$currsettings};
13577:     }
13578:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
13579:         if ($container eq 'cancreate') {
13580:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13581:                 push(@{$changes->{'cancreate'}},'captcha');
13582:             } elsif (!defined($changes->{'cancreate'})) {
13583:                 $changes->{'cancreate'} = ['captcha'];
13584:             }
13585:         } elsif ($container eq 'passwords') {
13586:             $changes->{'reset'} = 1;
13587:         } else {
13588:             $changes->{'captcha'} = 1;
13589:         }
13590:     }
13591:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
13592:     if ($newsettings->{'captcha'} eq 'recaptcha') {
13593:         $newpub = $env{'form.'.$container.'_recaptchapub'};
13594:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
13595:         $newpub =~ s/[^\w\-]//g;
13596:         $newpriv =~ s/[^\w\-]//g;
13597:         $newsettings->{'recaptchakeys'} = {
13598:                                              public  => $newpub,
13599:                                              private => $newpriv,
13600:                                           };
13601:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
13602:         $newversion =~ s/\D//g;
13603:         if ($newversion ne '2') {
13604:             $newversion = 1;
13605:         }
13606:         $newsettings->{'recaptchaversion'} = $newversion;
13607:     }
13608:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
13609:         $currpub = $current{'recaptchakeys'}{'public'};
13610:         $currpriv = $current{'recaptchakeys'}{'private'};
13611:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
13612:             $newsettings->{'recaptchakeys'} = {
13613:                                                  public  => '',
13614:                                                  private => '',
13615:                                               }
13616:         }
13617:     }
13618:     if ($current{'captcha'} eq 'recaptcha') {
13619:         $currversion = $current{'recaptchaversion'};
13620:         if ($currversion ne '2') {
13621:             $currversion = 1;
13622:         }
13623:     }
13624:     if ($currversion ne $newversion) {
13625:         if ($container eq 'cancreate') {
13626:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13627:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
13628:             } elsif (!defined($changes->{'cancreate'})) {
13629:                 $changes->{'cancreate'} = ['recaptchaversion'];
13630:             }
13631:         } elsif ($container eq 'passwords') {
13632:             $changes->{'reset'} = 1;
13633:         } else {
13634:             $changes->{'recaptchaversion'} = 1;
13635:         }
13636:     }
13637:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
13638:         if ($container eq 'cancreate') {
13639:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13640:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
13641:             } elsif (!defined($changes->{'cancreate'})) {
13642:                 $changes->{'cancreate'} = ['recaptchakeys'];
13643:             }
13644:         } elsif ($container eq 'passwords') {
13645:             $changes->{'reset'} = 1;
13646:         } else {
13647:             $changes->{'recaptchakeys'} = 1;
13648:         }
13649:     }
13650:     return;
13651: }
13652: 
13653: sub modify_usermodification {
13654:     my ($dom,%domconfig) = @_;
13655:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
13656:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
13657:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
13658:             if ($key eq 'selfcreate') {
13659:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
13660:             } else {  
13661:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
13662:             }
13663:         }
13664:     }
13665:     my @contexts = ('author','course');
13666:     my %context_title = (
13667:                            author => 'In author context',
13668:                            course => 'In course context',
13669:                         );
13670:     my @fields = ('lastname','firstname','middlename','generation',
13671:                   'permanentemail','id');
13672:     my %roles = (
13673:                   author => ['ca','aa'],
13674:                   course => ['st','ep','ta','in','cr'],
13675:                 );
13676:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13677:     foreach my $context (@contexts) {
13678:         foreach my $role (@{$roles{$context}}) {
13679:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
13680:             foreach my $item (@fields) {
13681:                 if (grep(/^\Q$item\E$/,@modifiable)) {
13682:                     $modifyhash{$context}{$role}{$item} = 1;
13683:                 } else {
13684:                     $modifyhash{$context}{$role}{$item} = 0;
13685:                 }
13686:             }
13687:         }
13688:         if (ref($curr_usermodification{$context}) eq 'HASH') {
13689:             foreach my $role (@{$roles{$context}}) {
13690:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
13691:                     foreach my $field (@fields) {
13692:                         if ($modifyhash{$context}{$role}{$field} ne 
13693:                                 $curr_usermodification{$context}{$role}{$field}) {
13694:                             push(@{$changes{$context}},$role);
13695:                             last;
13696:                         }
13697:                     }
13698:                 }
13699:             }
13700:         } else {
13701:             foreach my $context (@contexts) {
13702:                 foreach my $role (@{$roles{$context}}) {
13703:                     push(@{$changes{$context}},$role);
13704:                 }
13705:             }
13706:         }
13707:     }
13708:     my %usermodification_hash =  (
13709:                                    usermodification => \%modifyhash,
13710:                                  );
13711:     my $putresult = &Apache::lonnet::put_dom('configuration',
13712:                                              \%usermodification_hash,$dom);
13713:     if ($putresult eq 'ok') {
13714:         if (keys(%changes) > 0) {
13715:             $resulttext = &mt('Changes made: ').'<ul>';
13716:             foreach my $context (@contexts) {
13717:                 if (ref($changes{$context}) eq 'ARRAY') {
13718:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
13719:                     if (ref($changes{$context}) eq 'ARRAY') {
13720:                         foreach my $role (@{$changes{$context}}) {
13721:                             my $rolename;
13722:                             if ($role eq 'cr') {
13723:                                 $rolename = &mt('Custom');
13724:                             } else {
13725:                                 $rolename = &Apache::lonnet::plaintext($role);
13726:                             }
13727:                             my @modifiable;
13728:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
13729:                             foreach my $field (@fields) {
13730:                                 if ($modifyhash{$context}{$role}{$field}) {
13731:                                     push(@modifiable,$fieldtitles{$field});
13732:                                 }
13733:                             }
13734:                             if (@modifiable > 0) {
13735:                                 $resulttext .= join(', ',@modifiable);
13736:                             } else {
13737:                                 $resulttext .= &mt('none'); 
13738:                             }
13739:                             $resulttext .= '</li>';
13740:                         }
13741:                         $resulttext .= '</ul></li>';
13742:                     }
13743:                 }
13744:             }
13745:             $resulttext .= '</ul>';
13746:         } else {
13747:             $resulttext = &mt('No changes made to user modification settings');
13748:         }
13749:     } else {
13750:         $resulttext = '<span class="LC_error">'.
13751:             &mt('An error occurred: [_1]',$putresult).'</span>';
13752:     }
13753:     return $resulttext;
13754: }
13755: 
13756: sub modify_defaults {
13757:     my ($dom,$lastactref,%domconfig) = @_;
13758:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
13759:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13760:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
13761:                  'portal_def');
13762:     my @authtypes = ('internal','krb4','krb5','localauth');
13763:     foreach my $item (@items) {
13764:         $newvalues{$item} = $env{'form.'.$item};
13765:         if ($item eq 'auth_def') {
13766:             if ($newvalues{$item} ne '') {
13767:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
13768:                     push(@errors,$item);
13769:                 }
13770:             }
13771:         } elsif ($item eq 'lang_def') {
13772:             if ($newvalues{$item} ne '') {
13773:                 if ($newvalues{$item} =~ /^(\w+)/) {
13774:                     my $langcode = $1;
13775:                     if ($langcode ne 'x_chef') {
13776:                         if (code2language($langcode) eq '') {
13777:                             push(@errors,$item);
13778:                         }
13779:                     }
13780:                 } else {
13781:                     push(@errors,$item);
13782:                 }
13783:             }
13784:         } elsif ($item eq 'timezone_def') {
13785:             if ($newvalues{$item} ne '') {
13786:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
13787:                     push(@errors,$item);   
13788:                 }
13789:             }
13790:         } elsif ($item eq 'datelocale_def') {
13791:             if ($newvalues{$item} ne '') {
13792:                 my @datelocale_ids = DateTime::Locale->ids();
13793:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
13794:                     push(@errors,$item);
13795:                 }
13796:             }
13797:         } elsif ($item eq 'portal_def') {
13798:             if ($newvalues{$item} ne '') {
13799:                 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])\/?$/) {
13800:                     push(@errors,$item);
13801:                 }
13802:             }
13803:         }
13804:         if (grep(/^\Q$item\E$/,@errors)) {
13805:             $newvalues{$item} = $domdefaults{$item};
13806:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
13807:             $changes{$item} = 1;
13808:         }
13809:         $domdefaults{$item} = $newvalues{$item};
13810:     }
13811:     my %staticdefaults = (
13812:                            'intauth_cost'   => 10,
13813:                            'intauth_check'  => 0,
13814:                            'intauth_switch' => 0,
13815:                          );
13816:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
13817:         if (exists($domdefaults{$item})) {
13818:             $newvalues{$item} = $domdefaults{$item};
13819:         } else {
13820:             $newvalues{$item} = $staticdefaults{$item};
13821:         }
13822:     }
13823:     my %defaults_hash = (
13824:                          defaults => \%newvalues,
13825:                         );
13826:     my $title = &defaults_titles();
13827: 
13828:     my $currinststatus;
13829:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
13830:         $currinststatus = $domconfig{'inststatus'};
13831:     } else {
13832:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13833:         $currinststatus = {
13834:                              inststatustypes => $usertypes,
13835:                              inststatusorder => $types,
13836:                              inststatusguest => [],
13837:                           };
13838:     }
13839:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
13840:     my @allpos;
13841:     my %alltypes;
13842:     my @inststatusguest;
13843:     if (ref($currinststatus) eq 'HASH') {
13844:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
13845:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
13846:                 unless (grep(/^\Q$type\E$/,@todelete)) {
13847:                     push(@inststatusguest,$type);
13848:                 }
13849:             }
13850:         }
13851:     }
13852:     my ($currtitles,$currorder);
13853:     if (ref($currinststatus) eq 'HASH') {
13854:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
13855:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
13856:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
13857:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
13858:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
13859:                     }
13860:                 }
13861:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
13862:                     my $position = $env{'form.inststatus_pos_'.$type};
13863:                     $position =~ s/\D+//g;
13864:                     $allpos[$position] = $type;
13865:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
13866:                     $alltypes{$type} =~ s/`//g;
13867:                 }
13868:             }
13869:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
13870:             $currtitles =~ s/,$//;
13871:         }
13872:     }
13873:     if ($env{'form.addinststatus'}) {
13874:         my $newtype = $env{'form.addinststatus'};
13875:         $newtype =~ s/\W//g;
13876:         unless (exists($alltypes{$newtype})) {
13877:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
13878:             $alltypes{$newtype} =~ s/`//g; 
13879:             my $position = $env{'form.addinststatus_pos'};
13880:             $position =~ s/\D+//g;
13881:             if ($position ne '') {
13882:                 $allpos[$position] = $newtype;
13883:             }
13884:         }
13885:     }
13886:     my @orderedstatus;
13887:     foreach my $type (@allpos) {
13888:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
13889:             push(@orderedstatus,$type);
13890:         }
13891:     }
13892:     foreach my $type (keys(%alltypes)) {
13893:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
13894:             delete($alltypes{$type});
13895:         }
13896:     }
13897:     $defaults_hash{'inststatus'} = {
13898:                                      inststatustypes => \%alltypes,
13899:                                      inststatusorder => \@orderedstatus,
13900:                                      inststatusguest => \@inststatusguest,
13901:                                    };
13902:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
13903:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
13904:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
13905:         }
13906:     }
13907:     if ($currorder ne join(',',@orderedstatus)) {
13908:         $changes{'inststatus'}{'inststatusorder'} = 1;
13909:     }
13910:     my $newtitles;
13911:     foreach my $item (@orderedstatus) {
13912:         $newtitles .= $alltypes{$item}.',';
13913:     }
13914:     $newtitles =~ s/,$//;
13915:     if ($currtitles ne $newtitles) {
13916:         $changes{'inststatus'}{'inststatustypes'} = 1;
13917:     }
13918:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
13919:                                              $dom);
13920:     if ($putresult eq 'ok') {
13921:         if (keys(%changes) > 0) {
13922:             $resulttext = &mt('Changes made:').'<ul>';
13923:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
13924:             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";
13925:             foreach my $item (sort(keys(%changes))) {
13926:                 if ($item eq 'inststatus') {
13927:                     if (ref($changes{'inststatus'}) eq 'HASH') {
13928:                         if (@orderedstatus) {
13929:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
13930:                             foreach my $type (@orderedstatus) { 
13931:                                 $resulttext .= $alltypes{$type}.', ';
13932:                             }
13933:                             $resulttext =~ s/, $//;
13934:                             $resulttext .= '</li>';
13935:                         } else {
13936:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
13937:                         }
13938:                     }
13939:                 } else {
13940:                     my $value = $env{'form.'.$item};
13941:                     if ($value eq '') {
13942:                         $value = &mt('none');
13943:                     } elsif ($item eq 'auth_def') {
13944:                         my %authnames = &authtype_names();
13945:                         my %shortauth = (
13946:                                           internal   => 'int',
13947:                                           krb4       => 'krb4',
13948:                                           krb5       => 'krb5',
13949:                                           localauth  => 'loc',
13950:                         );
13951:                         $value = $authnames{$shortauth{$value}};
13952:                     }
13953:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
13954:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
13955:                 }
13956:             }
13957:             $resulttext .= '</ul>';
13958:             $mailmsgtext .= "\n";
13959:             my $cachetime = 24*60*60;
13960:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13961:             if (ref($lastactref) eq 'HASH') {
13962:                 $lastactref->{'domdefaults'} = 1;
13963:             }
13964:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
13965:                 my $notify = 1;
13966:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
13967:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
13968:                         $notify = 0;
13969:                     }
13970:                 }
13971:                 if ($notify) {
13972:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
13973:                                                "LON-CAPA Domain Settings Change - $dom",
13974:                                                $mailmsgtext);
13975:                 }
13976:             }
13977:         } else {
13978:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
13979:         }
13980:     } else {
13981:         $resulttext = '<span class="LC_error">'.
13982:             &mt('An error occurred: [_1]',$putresult).'</span>';
13983:     }
13984:     if (@errors > 0) {
13985:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
13986:         foreach my $item (@errors) {
13987:             $resulttext .= ' "'.$title->{$item}.'",';
13988:         }
13989:         $resulttext =~ s/,$//;
13990:     }
13991:     return $resulttext;
13992: }
13993: 
13994: sub modify_scantron {
13995:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13996:     my ($resulttext,%confhash,%changes,$errors);
13997:     my $custom = 'custom.tab';
13998:     my $default = 'default.tab';
13999:     my $servadm = $r->dir_config('lonAdmEMail');
14000:     my ($configuserok,$author_ok,$switchserver) =
14001:         &config_check($dom,$confname,$servadm);
14002:     if ($env{'form.scantronformat.filename'} ne '') {
14003:         my $error;
14004:         if ($configuserok eq 'ok') {
14005:             if ($switchserver) {
14006:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
14007:             } else {
14008:                 if ($author_ok eq 'ok') {
14009:                     my ($result,$scantronurl) =
14010:                         &publishlogo($r,'upload','scantronformat',$dom,
14011:                                      $confname,'scantron','','',$custom);
14012:                     if ($result eq 'ok') {
14013:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
14014:                         $changes{'scantronformat'} = 1;
14015:                     } else {
14016:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
14017:                     }
14018:                 } else {
14019:                     $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);
14020:                 }
14021:             }
14022:         } else {
14023:             $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);
14024:         }
14025:         if ($error) {
14026:             &Apache::lonnet::logthis($error);
14027:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14028:         }
14029:     }
14030:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14031:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
14032:             if ($env{'form.scantronformat_del'}) {
14033:                 $confhash{'scantron'}{'scantronformat'} = '';
14034:                 $changes{'scantronformat'} = 1;
14035:             } else {
14036:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
14037:             }
14038:         }
14039:     }
14040:     my @options = ('hdr','pad','rem');
14041:     my @fields = &scantroncsv_fields();
14042:     my %titles = &scantronconfig_titles();
14043:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
14044:     my ($newdat,$currdat,%newcol,%currcol);
14045:     if (grep(/^dat$/,@formats)) {
14046:         $confhash{'scantron'}{config}{dat} = 1;
14047:         $newdat = 1;
14048:     } else {
14049:         $newdat = 0;
14050:     }
14051:     if (grep(/^csv$/,@formats)) {
14052:         my %bynum;
14053:         foreach my $field (@fields) {
14054:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
14055:                 my $posscol = $1;
14056:                 if (($posscol < 20) && (!$bynum{$posscol})) {
14057:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
14058:                     $bynum{$posscol} = $field;
14059:                     $newcol{$field} = $posscol;
14060:                 }
14061:             }
14062:         }
14063:         if (keys(%newcol)) {
14064:             foreach my $option (@options) {
14065:                 if ($env{'form.scantroncsv_'.$option}) {
14066:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
14067:                 }
14068:             }
14069:         }
14070:     }
14071:     $currdat = 1;
14072:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14073:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
14074:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
14075:                 $currdat = 0;
14076:             }
14077:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
14078:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
14079:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
14080:                 }
14081:             }
14082:         }
14083:     }
14084:     if ($currdat != $newdat) {
14085:         $changes{'config'} = 1;
14086:     } else {
14087:         foreach my $field (@fields) {
14088:             if ($currcol{$field} ne '') {
14089:                 if ($currcol{$field} ne $newcol{$field}) {
14090:                     $changes{'config'} = 1;
14091:                     last;
14092:                 }
14093:             } elsif ($newcol{$field} ne '') {
14094:                 $changes{'config'} = 1;
14095:                 last;
14096:             }
14097:         }
14098:     }
14099:     if (keys(%confhash) > 0) {
14100:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
14101:                                                  $dom);
14102:         if ($putresult eq 'ok') {
14103:             if (keys(%changes) > 0) {
14104:                 if (ref($confhash{'scantron'}) eq 'HASH') {
14105:                     $resulttext = &mt('Changes made:').'<ul>';
14106:                     if ($changes{'scantronformat'}) {
14107:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
14108:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
14109:                         } else {
14110:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
14111:                         }
14112:                     }
14113:                     if ($changes{'config'}) {
14114:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
14115:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
14116:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
14117:                             }
14118:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
14119:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
14120:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
14121:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
14122:                                         foreach my $field (@fields) {
14123:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
14124:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
14125:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
14126:                                             }
14127:                                         }
14128:                                         $resulttext .= '</ul></li>';
14129:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
14130:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
14131:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
14132:                                                 foreach my $option (@options) {
14133:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
14134:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
14135:                                                     }
14136:                                                 }
14137:                                                 $resulttext .= '</ul></li>';
14138:                                             }
14139:                                         }
14140:                                     }
14141:                                 }
14142:                             }
14143:                         } else {
14144:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
14145:                         }
14146:                     }
14147:                     $resulttext .= '</ul>';
14148:                 } else {
14149:                     $resulttext = &mt('Changes made to bubblesheet format file.');
14150:                 }
14151:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14152:                 if (ref($lastactref) eq 'HASH') {
14153:                     $lastactref->{'domainconfig'} = 1;
14154:                 }
14155:             } else {
14156:                 $resulttext = &mt('No changes made to bubblesheet format settings');
14157:             }
14158:         } else {
14159:             $resulttext = '<span class="LC_error">'.
14160:                 &mt('An error occurred: [_1]',$putresult).'</span>';
14161:         }
14162:     } else {
14163:         $resulttext = &mt('No changes made to bubblesheet format settings');
14164:     }
14165:     if ($errors) {
14166:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14167:                        $errors.'</ul>';
14168:     }
14169:     return $resulttext;
14170: }
14171: 
14172: sub modify_coursecategories {
14173:     my ($dom,$lastactref,%domconfig) = @_;
14174:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
14175:         $cathash);
14176:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
14177:     my @catitems = ('unauth','auth');
14178:     my @cattypes = ('std','domonly','codesrch','none');
14179:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
14180:         $cathash = $domconfig{'coursecategories'}{'cats'};
14181:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
14182:             $changes{'togglecats'} = 1;
14183:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
14184:         }
14185:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
14186:             $changes{'categorize'} = 1;
14187:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
14188:         }
14189:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
14190:             $changes{'togglecatscomm'} = 1;
14191:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
14192:         }
14193:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
14194:             $changes{'categorizecomm'} = 1;
14195:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
14196:         }
14197:         foreach my $item (@catitems) {
14198:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14199:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
14200:                     $changes{$item} = 1;
14201:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14202:                 }
14203:             }
14204:         }
14205:     } else {
14206:         $changes{'togglecats'} = 1;
14207:         $changes{'categorize'} = 1;
14208:         $changes{'togglecatscomm'} = 1;
14209:         $changes{'categorizecomm'} = 1;
14210:         $domconfig{'coursecategories'} = {
14211:                                              togglecats => $env{'form.togglecats'},
14212:                                              categorize => $env{'form.categorize'},
14213:                                              togglecatscomm => $env{'form.togglecatscomm'},
14214:                                              categorizecomm => $env{'form.categorizecomm'},
14215:                                          };
14216:         foreach my $item (@catitems) {
14217:             if ($env{'form.coursecat_'.$item} ne 'std') {
14218:                 $changes{$item} = 1;
14219:             }
14220:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14221:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14222:             }
14223:         }
14224:     }
14225:     if (ref($cathash) eq 'HASH') {
14226:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
14227:             push (@deletecategory,'instcode::0');
14228:         }
14229:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
14230:             push(@deletecategory,'communities::0');
14231:         }
14232:     }
14233:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
14234:     if (ref($cathash) eq 'HASH') {
14235:         if (@deletecategory > 0) {
14236:             #FIXME Need to remove category from all courses using a deleted category 
14237:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
14238:             foreach my $item (@deletecategory) {
14239:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
14240:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
14241:                     $deletions{$item} = 1;
14242:                     &recurse_cat_deletes($item,$cathash,\%deletions);
14243:                 }
14244:             }
14245:         }
14246:         foreach my $item (keys(%{$cathash})) {
14247:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14248:             if ($cathash->{$item} ne $env{'form.'.$item}) {
14249:                 $reorderings{$item} = 1;
14250:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
14251:             }
14252:             if ($env{'form.addcategory_name_'.$item} ne '') {
14253:                 my $newcat = $env{'form.addcategory_name_'.$item};
14254:                 my $newdepth = $depth+1;
14255:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14256:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
14257:                 $adds{$newitem} = 1; 
14258:             }
14259:             if ($env{'form.subcat_'.$item} ne '') {
14260:                 my $newcat = $env{'form.subcat_'.$item};
14261:                 my $newdepth = $depth+1;
14262:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14263:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
14264:                 $adds{$newitem} = 1;
14265:             }
14266:         }
14267:     }
14268:     if ($env{'form.instcode'} eq '1') {
14269:         if (ref($cathash) eq 'HASH') {
14270:             my $newitem = 'instcode::0';
14271:             if ($cathash->{$newitem} eq '') {  
14272:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14273:                 $adds{$newitem} = 1;
14274:             }
14275:         } else {
14276:             my $newitem = 'instcode::0';
14277:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14278:             $adds{$newitem} = 1;
14279:         }
14280:     }
14281:     if ($env{'form.communities'} eq '1') {
14282:         if (ref($cathash) eq 'HASH') {
14283:             my $newitem = 'communities::0';
14284:             if ($cathash->{$newitem} eq '') {
14285:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
14286:                 $adds{$newitem} = 1;
14287:             }
14288:         } else {
14289:             my $newitem = 'communities::0';
14290:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
14291:             $adds{$newitem} = 1;
14292:         }
14293:     }
14294:     if ($env{'form.addcategory_name'} ne '') {
14295:         if (($env{'form.addcategory_name'} ne 'instcode') &&
14296:             ($env{'form.addcategory_name'} ne 'communities')) {
14297:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
14298:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
14299:             $adds{$newitem} = 1;
14300:         }
14301:     }
14302:     my $putresult;
14303:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14304:         if (keys(%deletions) > 0) {
14305:             foreach my $key (keys(%deletions)) {
14306:                 if ($predelallitems{$key} ne '') {
14307:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
14308:                 }
14309:             }
14310:         }
14311:         my (@chkcats,@chktrails,%chkallitems);
14312:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
14313:         if (ref($chkcats[0]) eq 'ARRAY') {
14314:             my $depth = 0;
14315:             my $chg = 0;
14316:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
14317:                 my $name = $chkcats[0][$i];
14318:                 my $item;
14319:                 if ($name eq '') {
14320:                     $chg ++;
14321:                 } else {
14322:                     $item = &escape($name).'::0';
14323:                     if ($chg) {
14324:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
14325:                     }
14326:                     $depth ++; 
14327:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
14328:                     $depth --;
14329:                 }
14330:             }
14331:         }
14332:     }
14333:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14334:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
14335:         if ($putresult eq 'ok') {
14336:             my %title = (
14337:                          togglecats     => 'Show/Hide a course in catalog',
14338:                          categorize     => 'Assign a category to a course',
14339:                          togglecatscomm => 'Show/Hide a community in catalog',
14340:                          categorizecomm => 'Assign a category to a community',
14341:                         );
14342:             my %level = (
14343:                          dom  => 'set in Domain ("Modify Course/Community")',
14344:                          crs  => 'set in Course ("Course Configuration")',
14345:                          comm => 'set in Community ("Community Configuration")',
14346:                          none     => 'No catalog',
14347:                          std      => 'Standard catalog',
14348:                          domonly  => 'Domain-only catalog',
14349:                          codesrch => 'Code search form',
14350:                         );
14351:             $resulttext = &mt('Changes made:').'<ul>';
14352:             if ($changes{'togglecats'}) {
14353:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
14354:             }
14355:             if ($changes{'categorize'}) {
14356:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
14357:             }
14358:             if ($changes{'togglecatscomm'}) {
14359:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
14360:             }
14361:             if ($changes{'categorizecomm'}) {
14362:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
14363:             }
14364:             if ($changes{'unauth'}) {
14365:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
14366:             }
14367:             if ($changes{'auth'}) {
14368:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
14369:             }
14370:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14371:                 my $cathash;
14372:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
14373:                     $cathash = $domconfig{'coursecategories'}{'cats'};
14374:                 } else {
14375:                     $cathash = {};
14376:                 } 
14377:                 my (@cats,@trails,%allitems);
14378:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
14379:                 if (keys(%deletions) > 0) {
14380:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
14381:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
14382:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
14383:                     }
14384:                     $resulttext .= '</ul></li>';
14385:                 }
14386:                 if (keys(%reorderings) > 0) {
14387:                     my %sort_by_trail;
14388:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
14389:                     foreach my $key (keys(%reorderings)) {
14390:                         if ($allitems{$key} ne '') {
14391:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
14392:                         }
14393:                     }
14394:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
14395:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
14396:                     }
14397:                     $resulttext .= '</ul></li>';
14398:                 }
14399:                 if (keys(%adds) > 0) {
14400:                     my %sort_by_trail;
14401:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
14402:                     foreach my $key (keys(%adds)) {
14403:                         if ($allitems{$key} ne '') {
14404:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
14405:                         }
14406:                     }
14407:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
14408:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
14409:                     }
14410:                     $resulttext .= '</ul></li>';
14411:                 }
14412:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
14413:                 if (ref($lastactref) eq 'HASH') {
14414:                     $lastactref->{'cats'} = 1;
14415:                 }
14416:             }
14417:             $resulttext .= '</ul>';
14418:             if ($changes{'unauth'} || $changes{'auth'}) {
14419:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
14420:                 if ($changes{'auth'}) {
14421:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
14422:                 }
14423:                 if ($changes{'unauth'}) {
14424:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
14425:                 }
14426:                 my $cachetime = 24*60*60;
14427:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14428:                 if (ref($lastactref) eq 'HASH') {
14429:                     $lastactref->{'domdefaults'} = 1;
14430:                 }
14431:             }
14432:         } else {
14433:             $resulttext = '<span class="LC_error">'.
14434:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14435:         }
14436:     } else {
14437:         $resulttext = &mt('No changes made to course and community categories');
14438:     }
14439:     return $resulttext;
14440: }
14441: 
14442: sub modify_serverstatuses {
14443:     my ($dom,%domconfig) = @_;
14444:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
14445:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
14446:         %currserverstatus = %{$domconfig{'serverstatuses'}};
14447:     }
14448:     my @pages = &serverstatus_pages();
14449:     foreach my $type (@pages) {
14450:         $newserverstatus{$type}{'namedusers'} = '';
14451:         $newserverstatus{$type}{'machines'} = '';
14452:         if (defined($env{'form.'.$type.'_namedusers'})) {
14453:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
14454:             my @okusers;
14455:             foreach my $user (@users) {
14456:                 my ($uname,$udom) = split(/:/,$user);
14457:                 if (($udom =~ /^$match_domain$/) &&   
14458:                     (&Apache::lonnet::domain($udom)) &&
14459:                     ($uname =~ /^$match_username$/)) {
14460:                     if (!grep(/^\Q$user\E/,@okusers)) {
14461:                         push(@okusers,$user);
14462:                     }
14463:                 }
14464:             }
14465:             if (@okusers > 0) {
14466:                  @okusers = sort(@okusers);
14467:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
14468:             }
14469:         }
14470:         if (defined($env{'form.'.$type.'_machines'})) {
14471:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
14472:             my @okmachines;
14473:             foreach my $ip (@machines) {
14474:                 my @parts = split(/\./,$ip);
14475:                 next if (@parts < 4);
14476:                 my $badip = 0;
14477:                 for (my $i=0; $i<4; $i++) {
14478:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
14479:                         $badip = 1;
14480:                         last;
14481:                     }
14482:                 }
14483:                 if (!$badip) {
14484:                     push(@okmachines,$ip);     
14485:                 }
14486:             }
14487:             @okmachines = sort(@okmachines);
14488:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
14489:         }
14490:     }
14491:     my %serverstatushash =  (
14492:                                 serverstatuses => \%newserverstatus,
14493:                             );
14494:     foreach my $type (@pages) {
14495:         foreach my $setting ('namedusers','machines') {
14496:             my (@current,@new);
14497:             if (ref($currserverstatus{$type}) eq 'HASH') {
14498:                 if ($currserverstatus{$type}{$setting} ne '') { 
14499:                     @current = split(/,/,$currserverstatus{$type}{$setting});
14500:                 }
14501:             }
14502:             if ($newserverstatus{$type}{$setting} ne '') {
14503:                 @new = split(/,/,$newserverstatus{$type}{$setting});
14504:             }
14505:             if (@current > 0) {
14506:                 if (@new > 0) {
14507:                     foreach my $item (@current) {
14508:                         if (!grep(/^\Q$item\E$/,@new)) {
14509:                             $changes{$type}{$setting} = 1;
14510:                             last;
14511:                         }
14512:                     }
14513:                     foreach my $item (@new) {
14514:                         if (!grep(/^\Q$item\E$/,@current)) {
14515:                             $changes{$type}{$setting} = 1;
14516:                             last;
14517:                         }
14518:                     }
14519:                 } else {
14520:                     $changes{$type}{$setting} = 1;
14521:                 }
14522:             } elsif (@new > 0) {
14523:                 $changes{$type}{$setting} = 1;
14524:             }
14525:         }
14526:     }
14527:     if (keys(%changes) > 0) {
14528:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
14529:         my $putresult = &Apache::lonnet::put_dom('configuration',
14530:                                                  \%serverstatushash,$dom);
14531:         if ($putresult eq 'ok') {
14532:             $resulttext .= &mt('Changes made:').'<ul>';
14533:             foreach my $type (@pages) {
14534:                 if (ref($changes{$type}) eq 'HASH') {
14535:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
14536:                     if ($changes{$type}{'namedusers'}) {
14537:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
14538:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
14539:                         } else {
14540:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
14541:                         }
14542:                     }
14543:                     if ($changes{$type}{'machines'}) {
14544:                         if ($newserverstatus{$type}{'machines'} eq '') {
14545:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
14546:                         } else {
14547:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
14548:                         }
14549: 
14550:                     }
14551:                     $resulttext .= '</ul></li>';
14552:                 }
14553:             }
14554:             $resulttext .= '</ul>';
14555:         } else {
14556:             $resulttext = '<span class="LC_error">'.
14557:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
14558: 
14559:         }
14560:     } else {
14561:         $resulttext = &mt('No changes made to access to server status pages');
14562:     }
14563:     return $resulttext;
14564: }
14565: 
14566: sub modify_helpsettings {
14567:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
14568:     my ($resulttext,$errors,%changes,%helphash);
14569:     my %defaultchecked = ('submitbugs' => 'on');
14570:     my @offon = ('off','on');
14571:     my @toggles = ('submitbugs');
14572:     my %current = ('submitbugs' => '',
14573:                    'adhoc'      => {},
14574:                   );
14575:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
14576:         %current = %{$domconfig{'helpsettings'}};
14577:     }
14578:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14579:     foreach my $item (@toggles) {
14580:         if ($defaultchecked{$item} eq 'on') { 
14581:             if ($current{$item} eq '') {
14582:                 if ($env{'form.'.$item} eq '0') {
14583:                     $changes{$item} = 1;
14584:                 }
14585:             } elsif ($current{$item} ne $env{'form.'.$item}) {
14586:                 $changes{$item} = 1;
14587:             }
14588:         } elsif ($defaultchecked{$item} eq 'off') {
14589:             if ($current{$item} eq '') {
14590:                 if ($env{'form.'.$item} eq '1') {
14591:                     $changes{$item} = 1;
14592:                 }
14593:             } elsif ($current{$item} ne $env{'form.'.$item}) {
14594:                 $changes{$item} = 1;
14595:             }
14596:         }
14597:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
14598:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
14599:         }
14600:     }
14601:     my $maxnum = $env{'form.helproles_maxnum'};
14602:     my $confname = $dom.'-domainconfig';
14603:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
14604:     my (@allpos,%newsettings,%changedprivs,$newrole);
14605:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14606:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
14607:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
14608:     my %lt = &Apache::lonlocal::texthash(
14609:                     s      => 'system',
14610:                     d      => 'domain',
14611:                     order  => 'Display order',
14612:                     access => 'Role usage',
14613:                     all    => 'All with domain helpdesk or helpdesk assistant role',
14614:                     dh     => 'All with domain helpdesk role',
14615:                     da     => 'All with domain helpdesk assistant role',
14616:                     none   => 'None',
14617:                     status => 'Determined based on institutional status',
14618:                     inc    => 'Include all, but exclude specific personnel',
14619:                     exc    => 'Exclude all, but include specific personnel',
14620:     );
14621:     for (my $num=0; $num<=$maxnum; $num++) {
14622:         my ($prefix,$identifier,$rolename,%curr);
14623:         if ($num == $maxnum) {
14624:             next unless ($env{'form.newcusthelp'} == $maxnum);
14625:             $identifier = 'custhelp'.$num;
14626:             $prefix = 'helproles_'.$num;
14627:             $rolename = $env{'form.custhelpname'.$num};
14628:             $rolename=~s/[^A-Za-z0-9]//gs;
14629:             next if ($rolename eq '');
14630:             next if (exists($existing{'rolesdef_'.$rolename}));
14631:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
14632:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
14633:                                                      $newprivs{'c'},$confname,$dom);
14634:             if ($result ne 'ok') {
14635:                 $errors .= '<li><span class="LC_error">'.
14636:                            &mt('An error occurred storing the new custom role: [_1]',
14637:                            $result).'</span></li>';
14638:                 next;
14639:             } else {
14640:                 $changedprivs{$rolename} = \%newprivs;
14641:                 $newrole = $rolename;
14642:             }
14643:         } else {
14644:             $prefix = 'helproles_'.$num;
14645:             $rolename = $env{'form.'.$prefix};
14646:             next if ($rolename eq '');
14647:             next unless (exists($existing{'rolesdef_'.$rolename}));
14648:             $identifier = 'custhelp'.$num;
14649:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
14650:             my %currprivs;
14651:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
14652:                 split(/\_/,$existing{'rolesdef_'.$rolename});
14653:             foreach my $level ('c','d','s') {
14654:                 if ($newprivs{$level} ne $currprivs{$level}) {
14655:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
14656:                                                              $newprivs{'c'},$confname,$dom);
14657:                     if ($result ne 'ok') {
14658:                         $errors .= '<li><span class="LC_error">'.
14659:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
14660:                                        $rolename,$result).'</span></li>';
14661:                     } else {
14662:                         $changedprivs{$rolename} = \%newprivs;
14663:                     }
14664:                     last;
14665:                 }
14666:             }
14667:             if (ref($current{'adhoc'}) eq 'HASH') {
14668:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
14669:                     %curr = %{$current{'adhoc'}{$rolename}};
14670:                 }
14671:             }
14672:         }
14673:         my $newpos = $env{'form.'.$prefix.'_pos'};
14674:         $newpos =~ s/\D+//g;
14675:         $allpos[$newpos] = $rolename;
14676:         my $newdesc = $env{'form.'.$prefix.'_desc'};
14677:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
14678:         if ($curr{'desc'}) {
14679:             if ($curr{'desc'} ne $newdesc) {
14680:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
14681:                 $newsettings{$rolename}{'desc'} = $newdesc;
14682:             }
14683:         } elsif ($newdesc ne '') {
14684:             $changes{'customrole'}{$rolename}{'desc'} = 1;
14685:             $newsettings{$rolename}{'desc'} = $newdesc;
14686:         }
14687:         my $access = $env{'form.'.$prefix.'_access'};
14688:         if (grep(/^\Q$access\E$/,@accesstypes)) {
14689:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
14690:             if ($access eq 'status') {
14691:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
14692:                 if (scalar(@statuses) == 0) {
14693:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
14694:                 } else {
14695:                     my (@shownstatus,$numtypes);
14696:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
14697:                     if (ref($types) eq 'ARRAY') {
14698:                         $numtypes = scalar(@{$types});
14699:                         foreach my $type (sort(@statuses)) {
14700:                             if ($type eq 'default') {
14701:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
14702:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
14703:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
14704:                                 push(@shownstatus,$usertypes->{$type});
14705:                             }
14706:                         }
14707:                     }
14708:                     if (grep(/^default$/,@statuses)) {
14709:                         push(@shownstatus,$othertitle);
14710:                     }
14711:                     if (scalar(@shownstatus) == 1+$numtypes) {
14712:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
14713:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
14714:                     } else {
14715:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
14716:                         if (ref($curr{'status'}) eq 'ARRAY') {
14717:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
14718:                             if (@diffs) {
14719:                                 $changes{'customrole'}{$rolename}{$access} = 1;
14720:                             }
14721:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
14722:                             $changes{'customrole'}{$rolename}{$access} = 1;
14723:                         }
14724:                     }
14725:                 }
14726:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
14727:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
14728:                 my @newspecstaff;
14729:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
14730:                 foreach my $person (sort(@personnel)) {
14731:                     if ($domhelpdesk{$person}) {
14732:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
14733:                     }
14734:                 }
14735:                 if (ref($curr{$access}) eq 'ARRAY') {
14736:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
14737:                     if (@diffs) {
14738:                         $changes{'customrole'}{$rolename}{$access} = 1;
14739:                     }
14740:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
14741:                     $changes{'customrole'}{$rolename}{$access} = 1;
14742:                 }
14743:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
14744:                     my ($uname,$udom) = split(/:/,$person);
14745:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
14746:                 }
14747:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
14748:             }
14749:         } else {
14750:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
14751:         }
14752:         unless ($curr{'access'} eq $access) {
14753:             $changes{'customrole'}{$rolename}{'access'} = 1;
14754:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
14755:         }
14756:     }
14757:     if (@allpos > 0) {
14758:         my $idx = 0;
14759:         foreach my $rolename (@allpos) {
14760:             if ($rolename ne '') {
14761:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
14762:                 if (ref($current{'adhoc'}) eq 'HASH') {
14763:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
14764:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
14765:                             $changes{'customrole'}{$rolename}{'order'} = 1;
14766:                             $newsettings{$rolename}{'order'} = $idx+1;
14767:                         }
14768:                     }
14769:                 }
14770:                 $idx ++;
14771:             }
14772:         }
14773:     }
14774:     my $putresult;
14775:     if (keys(%changes) > 0) {
14776:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
14777:         if ($putresult eq 'ok') {
14778:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
14779:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
14780:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
14781:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
14782:                 }
14783:             }
14784:             my $cachetime = 24*60*60;
14785:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14786:             if (ref($lastactref) eq 'HASH') {
14787:                 $lastactref->{'domdefaults'} = 1;
14788:             }
14789:         } else {
14790:             $errors .= '<li><span class="LC_error">'.
14791:                        &mt('An error occurred storing the settings: [_1]',
14792:                            $putresult).'</span></li>';
14793:         }
14794:     }
14795:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
14796:         $resulttext = &mt('Changes made:').'<ul>';
14797:         my (%shownprivs,@levelorder);
14798:         @levelorder = ('c','d','s');
14799:         if ((keys(%changes)) && ($putresult eq 'ok')) {
14800:             foreach my $item (sort(keys(%changes))) {
14801:                 if ($item eq 'submitbugs') {
14802:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
14803:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
14804:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
14805:                 } elsif ($item eq 'customrole') {
14806:                     if (ref($changes{'customrole'}) eq 'HASH') {
14807:                         my @keyorder = ('order','desc','access','status','exc','inc');
14808:                         my %keytext = &Apache::lonlocal::texthash(
14809:                                                                    order  => 'Order',
14810:                                                                    desc   => 'Role description',
14811:                                                                    access => 'Role usage',
14812:                                                                    status => 'Allowed institutional types',
14813:                                                                    exc    => 'Allowed personnel',
14814:                                                                    inc    => 'Disallowed personnel',
14815:                         );
14816:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
14817:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
14818:                                 if ($role eq $newrole) {
14819:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
14820:                                                               $role).'<ul>';
14821:                                 } else {
14822:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
14823:                                                               $role).'<ul>';
14824:                                 }
14825:                                 foreach my $key (@keyorder) {
14826:                                     if ($changes{'customrole'}{$role}{$key}) {
14827:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
14828:                                                                   $keytext{$key},$newsettings{$role}{$key}).
14829:                                                        '</li>';
14830:                                     }
14831:                                 }
14832:                                 if (ref($changedprivs{$role}) eq 'HASH') {
14833:                                     $shownprivs{$role} = 1;
14834:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
14835:                                     foreach my $level (@levelorder) {
14836:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
14837:                                             next if ($item eq '');
14838:                                             my ($priv) = split(/\&/,$item,2);
14839:                                             if (&Apache::lonnet::plaintext($priv)) {
14840:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
14841:                                                 unless ($level eq 'c') {
14842:                                                     $resulttext .= ' ('.$lt{$level}.')';
14843:                                                 }
14844:                                                 $resulttext .= '</li>';
14845:                                             }
14846:                                         }
14847:                                     }
14848:                                     $resulttext .= '</ul>';
14849:                                 }
14850:                                 $resulttext .= '</ul></li>';
14851:                             }
14852:                         }
14853:                     }
14854:                 }
14855:             }
14856:         }
14857:         if (keys(%changedprivs)) {
14858:             foreach my $role (sort(keys(%changedprivs))) {
14859:                 unless ($shownprivs{$role}) {
14860:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
14861:                                               $role).'<ul>'.
14862:                                    '<li>'.&mt('Privileges set to :').'<ul>';
14863:                     foreach my $level (@levelorder) {
14864:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
14865:                             next if ($item eq '');
14866:                             my ($priv) = split(/\&/,$item,2);
14867:                             if (&Apache::lonnet::plaintext($priv)) {
14868:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
14869:                                 unless ($level eq 'c') {
14870:                                     $resulttext .= ' ('.$lt{$level}.')';
14871:                                 }
14872:                                 $resulttext .= '</li>';
14873:                             }
14874:                         }
14875:                     }
14876:                     $resulttext .= '</ul></li></ul></li>';
14877:                 }
14878:             }
14879:         }
14880:         $resulttext .= '</ul>';
14881:     } else {
14882:         $resulttext = &mt('No changes made to help settings');
14883:     }
14884:     if ($errors) {
14885:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
14886:                                     $errors.'</ul>';
14887:     }
14888:     return $resulttext;
14889: }
14890: 
14891: sub modify_coursedefaults {
14892:     my ($dom,$lastactref,%domconfig) = @_;
14893:     my ($resulttext,$errors,%changes,%defaultshash);
14894:     my %defaultchecked = (
14895:                            'uselcmath'       => 'on',
14896:                            'usejsme'         => 'on'
14897:                          );
14898:     my @toggles = ('uselcmath','usejsme');
14899:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
14900:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
14901:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
14902:     my @types = ('official','unofficial','community','textbook');
14903:     my %staticdefaults = (
14904:                            anonsurvey_threshold => 10,
14905:                            uploadquota          => 500,
14906:                            postsubmit           => 60,
14907:                            mysqltables          => 172800,
14908:                          );
14909:     my %texoptions = (
14910:                         MathJax  => 'MathJax',
14911:                         mimetex  => &mt('Convert to Images'),
14912:                         tth      => &mt('TeX to HTML'),
14913:                      );
14914:     $defaultshash{'coursedefaults'} = {};
14915: 
14916:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
14917:         if ($domconfig{'coursedefaults'} eq '') {
14918:             $domconfig{'coursedefaults'} = {};
14919:         }
14920:     }
14921: 
14922:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
14923:         foreach my $item (@toggles) {
14924:             if ($defaultchecked{$item} eq 'on') {
14925:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
14926:                     ($env{'form.'.$item} eq '0')) {
14927:                     $changes{$item} = 1;
14928:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
14929:                     $changes{$item} = 1;
14930:                 }
14931:             } elsif ($defaultchecked{$item} eq 'off') {
14932:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
14933:                     ($env{'form.'.$item} eq '1')) {
14934:                     $changes{$item} = 1;
14935:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
14936:                     $changes{$item} = 1;
14937:                 }
14938:             }
14939:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
14940:         }
14941:         foreach my $item (@numbers) {
14942:             my ($currdef,$newdef);
14943:             $newdef = $env{'form.'.$item};
14944:             if ($item eq 'anonsurvey_threshold') {
14945:                 $currdef = $domconfig{'coursedefaults'}{$item};
14946:                 $newdef =~ s/\D//g;
14947:                 if ($newdef eq '' || $newdef < 1) {
14948:                     $newdef = 1;
14949:                 }
14950:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
14951:             } else {
14952:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
14953:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
14954:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
14955:                 }
14956:                 $newdef =~ s/[^\w.\-]//g;
14957:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
14958:             }
14959:             if ($currdef ne $newdef) {
14960:                 if ($item eq 'anonsurvey_threshold') {
14961:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
14962:                         $changes{$item} = 1;
14963:                     }
14964:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
14965:                     my $setting = $1;
14966:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
14967:                         $changes{$setting} = 1;
14968:                     }
14969:                 }
14970:             }
14971:         }
14972:         my $texengine;
14973:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
14974:             $texengine = $env{'form.texengine'};
14975:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
14976:             if ($currdef eq '') {
14977:                 unless ($texengine eq $Apache::lonnet::deftex) {
14978:                     $changes{'texengine'} = 1;
14979:                 }
14980:             } elsif ($currdef ne $texengine) {
14981:                 $changes{'texengine'} = 1;
14982:             }
14983:         }
14984:         if ($texengine ne '') {
14985:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
14986:         }
14987:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
14988:         my @currclonecode;
14989:         if (ref($currclone) eq 'HASH') {
14990:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
14991:                 @currclonecode = @{$currclone->{'instcode'}};
14992:             }
14993:         }
14994:         my $newclone;
14995:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
14996:             $newclone = $env{'form.canclone'};
14997:         }
14998:         if ($newclone eq 'instcode') {
14999:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
15000:             my (%codedefaults,@code_order,@clonecode);
15001:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
15002:                                                     \@code_order);
15003:             foreach my $item (@code_order) {
15004:                 if (grep(/^\Q$item\E$/,@newcodes)) {
15005:                     push(@clonecode,$item);
15006:                 }
15007:             }
15008:             if (@clonecode) {
15009:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
15010:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
15011:                 if (@diffs) {
15012:                     $changes{'canclone'} = 1;
15013:                 }
15014:             } else {
15015:                 $newclone eq '';
15016:             }
15017:         } elsif ($newclone ne '') {
15018:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
15019:         }
15020:         if ($newclone ne $currclone) {
15021:             $changes{'canclone'} = 1;
15022:         }
15023:         my %credits;
15024:         foreach my $type (@types) {
15025:             unless ($type eq 'community') {
15026:                 $credits{$type} = $env{'form.'.$type.'_credits'};
15027:                 $credits{$type} =~ s/[^\d.]+//g;
15028:             }
15029:         }
15030:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
15031:             ($env{'form.coursecredits'} eq '1')) {
15032:             $changes{'coursecredits'} = 1;
15033:             foreach my $type (keys(%credits)) {
15034:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15035:             }
15036:         } else {
15037:             if ($env{'form.coursecredits'} eq '1') {
15038:                 foreach my $type (@types) {
15039:                     unless ($type eq 'community') {
15040:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
15041:                             $changes{'coursecredits'} = 1;
15042:                         }
15043:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15044:                     }
15045:                 }
15046:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15047:                 foreach my $type (@types) {
15048:                     unless ($type eq 'community') {
15049:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
15050:                             $changes{'coursecredits'} = 1;
15051:                             last;
15052:                         }
15053:                     }
15054:                 }
15055:             }
15056:         }
15057:         if ($env{'form.postsubmit'} eq '1') {
15058:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
15059:             my %currtimeout;
15060:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15061:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
15062:                     $changes{'postsubmit'} = 1;
15063:                 }
15064:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15065:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
15066:                 }
15067:             } else {
15068:                 $changes{'postsubmit'} = 1;
15069:             }
15070:             foreach my $type (@types) {
15071:                 my $timeout = $env{'form.'.$type.'_timeout'};
15072:                 $timeout =~ s/\D//g;
15073:                 if ($timeout == $staticdefaults{'postsubmit'}) {
15074:                     $timeout = '';
15075:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
15076:                     $timeout = '0';
15077:                 }
15078:                 unless ($timeout eq '') {
15079:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
15080:                 }
15081:                 if (exists($currtimeout{$type})) {
15082:                     if ($timeout ne $currtimeout{$type}) {
15083:                         $changes{'postsubmit'} = 1;
15084:                     }
15085:                 } elsif ($timeout ne '') {
15086:                     $changes{'postsubmit'} = 1;
15087:                 }
15088:             }
15089:         } else {
15090:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
15091:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15092:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
15093:                     $changes{'postsubmit'} = 1;
15094:                 }
15095:             } else {
15096:                 $changes{'postsubmit'} = 1;
15097:             }
15098:         }
15099:     }
15100:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15101:                                              $dom);
15102:     if ($putresult eq 'ok') {
15103:         if (keys(%changes) > 0) {
15104:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15105:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
15106:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
15107:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
15108:                 foreach my $item ('uselcmath','usejsme','texengine') {
15109:                     if ($changes{$item}) {
15110:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
15111:                     }
15112:                 }
15113:                 if ($changes{'coursecredits'}) {
15114:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15115:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
15116:                             $domdefaults{$type.'credits'} =
15117:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
15118:                         }
15119:                     }
15120:                 }
15121:                 if ($changes{'postsubmit'}) {
15122:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15123:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
15124:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15125:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
15126:                                 $domdefaults{$type.'postsubtimeout'} =
15127:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15128:                             }
15129:                         }
15130:                     }
15131:                 }
15132:                 if ($changes{'uploadquota'}) {
15133:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15134:                         foreach my $type (@types) {
15135:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
15136:                         }
15137:                     }
15138:                 }
15139:                 if ($changes{'canclone'}) {
15140:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15141:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15142:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
15143:                             if (@clonecodes) {
15144:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
15145:                             }
15146:                         }
15147:                     } else {
15148:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
15149:                     }
15150:                 }
15151:                 my $cachetime = 24*60*60;
15152:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15153:                 if (ref($lastactref) eq 'HASH') {
15154:                     $lastactref->{'domdefaults'} = 1;
15155:                 }
15156:             }
15157:             $resulttext = &mt('Changes made:').'<ul>';
15158:             foreach my $item (sort(keys(%changes))) {
15159:                 if ($item eq 'uselcmath') {
15160:                     if ($env{'form.'.$item} eq '1') {
15161:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
15162:                     } else {
15163:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
15164:                     }
15165:                 } elsif ($item eq 'usejsme') {
15166:                     if ($env{'form.'.$item} eq '1') {
15167:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
15168:                     } else {
15169:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
15170:                     }
15171:                 } elsif ($item eq 'texengine') {
15172:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
15173:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
15174:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
15175:                     }
15176:                 } elsif ($item eq 'anonsurvey_threshold') {
15177:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
15178:                 } elsif ($item eq 'uploadquota') {
15179:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15180:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
15181:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
15182:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
15183:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
15184: 
15185:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
15186:                                        '</ul>'.
15187:                                        '</li>';
15188:                     } else {
15189:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
15190:                     }
15191:                 } elsif ($item eq 'mysqltables') {
15192:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
15193:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
15194:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
15195:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
15196:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
15197:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
15198:                                        '</ul>'.
15199:                                        '</li>';
15200:                     } else {
15201:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
15202:                     }
15203:                 } elsif ($item eq 'postsubmit') {
15204:                     if ($domdefaults{'postsubmit'} eq 'off') {
15205:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
15206:                     } else {
15207:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
15208:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15209:                             $resulttext .= &mt('durations:').'<ul>';
15210:                             foreach my $type (@types) {
15211:                                 $resulttext .= '<li>';
15212:                                 my $timeout;
15213:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15214:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15215:                                 }
15216:                                 my $display;
15217:                                 if ($timeout eq '0') {
15218:                                     $display = &mt('unlimited');
15219:                                 } elsif ($timeout eq '') {
15220:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
15221:                                 } else {
15222:                                     $display = &mt('[quant,_1,second]',$timeout);
15223:                                 }
15224:                                 if ($type eq 'community') {
15225:                                     $resulttext .= &mt('Communities');
15226:                                 } elsif ($type eq 'official') {
15227:                                     $resulttext .= &mt('Official courses');
15228:                                 } elsif ($type eq 'unofficial') {
15229:                                     $resulttext .= &mt('Unofficial courses');
15230:                                 } elsif ($type eq 'textbook') {
15231:                                     $resulttext .= &mt('Textbook courses');
15232:                                 }
15233:                                 $resulttext .= ' -- '.$display.'</li>';
15234:                             }
15235:                             $resulttext .= '</ul>';
15236:                         }
15237:                         $resulttext .= '</li>';
15238:                     }
15239:                 } elsif ($item eq 'coursecredits') {
15240:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15241:                         if (($domdefaults{'officialcredits'} eq '') &&
15242:                             ($domdefaults{'unofficialcredits'} eq '') &&
15243:                             ($domdefaults{'textbookcredits'} eq '')) {
15244:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15245:                         } else {
15246:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
15247:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
15248:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
15249:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
15250:                                            '</ul>'.
15251:                                            '</li>';
15252:                         }
15253:                     } else {
15254:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15255:                     }
15256:                 } elsif ($item eq 'canclone') {
15257:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15258:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15259:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
15260:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
15261:                         }
15262:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
15263:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
15264:                     } else {
15265:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
15266:                     }
15267:                 }
15268:             }
15269:             $resulttext .= '</ul>';
15270:         } else {
15271:             $resulttext = &mt('No changes made to course defaults');
15272:         }
15273:     } else {
15274:         $resulttext = '<span class="LC_error">'.
15275:             &mt('An error occurred: [_1]',$putresult).'</span>';
15276:     }
15277:     return $resulttext;
15278: }
15279: 
15280: sub modify_selfenrollment {
15281:     my ($dom,$lastactref,%domconfig) = @_;
15282:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
15283:     my @types = ('official','unofficial','community','textbook');
15284:     my %titles = &tool_titles();
15285:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
15286:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
15287:     $ordered{'default'} = ['types','registered','approval','limit'];
15288: 
15289:     my (%roles,%shown,%toplevel);
15290:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
15291: 
15292:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
15293:         if ($domconfig{'selfenrollment'} eq '') {
15294:             $domconfig{'selfenrollment'} = {};
15295:         }
15296:     }
15297:     %toplevel = (
15298:                   admin      => 'Configuration Rights',
15299:                   default    => 'Default settings',
15300:                   validation => 'Validation of self-enrollment requests',
15301:                 );
15302:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
15303: 
15304:     if (ref($ordered{'admin'}) eq 'ARRAY') {
15305:         foreach my $item (@{$ordered{'admin'}}) {
15306:             foreach my $type (@types) {
15307:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
15308:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
15309:                 } else {
15310:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
15311:                 }
15312:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
15313:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
15314:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
15315:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
15316:                             push(@{$changes{'admin'}{$type}},$item);
15317:                         }
15318:                     } else {
15319:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
15320:                             push(@{$changes{'admin'}{$type}},$item);
15321:                         }
15322:                     }
15323:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
15324:                     push(@{$changes{'admin'}{$type}},$item);
15325:                 }
15326:             }
15327:         }
15328:     }
15329: 
15330:     foreach my $item (@{$ordered{'default'}}) {
15331:         foreach my $type (@types) {
15332:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
15333:             if ($item eq 'types') {
15334:                 unless (($value eq 'all') || ($value eq 'dom')) {
15335:                     $value = '';
15336:                 }
15337:             } elsif ($item eq 'registered') {
15338:                 unless ($value eq '1') {
15339:                     $value = 0;
15340:                 }
15341:             } elsif ($item eq 'approval') {
15342:                 unless ($value =~ /^[012]$/) {
15343:                     $value = 0;
15344:                 }
15345:             } else {
15346:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
15347:                     $value = 'none';
15348:                 }
15349:             }
15350:             $selfenrollhash{'default'}{$type}{$item} = $value;
15351:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
15352:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
15353:                     if ($selfenrollhash{'default'}{$type}{$item} ne
15354:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
15355:                          push(@{$changes{'default'}{$type}},$item);
15356:                     }
15357:                 } else {
15358:                     push(@{$changes{'default'}{$type}},$item);
15359:                 }
15360:             } else {
15361:                 push(@{$changes{'default'}{$type}},$item);
15362:             }
15363:             if ($item eq 'limit') {
15364:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
15365:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
15366:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
15367:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
15368:                     }
15369:                 } else {
15370:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
15371:                 }
15372:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
15373:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
15374:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
15375:                          push(@{$changes{'default'}{$type}},'cap');
15376:                     }
15377:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
15378:                     push(@{$changes{'default'}{$type}},'cap');
15379:                 }
15380:             }
15381:         }
15382:     }
15383: 
15384:     foreach my $item (@{$itemsref}) {
15385:         if ($item eq 'fields') {
15386:             my @changed;
15387:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
15388:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
15389:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
15390:             }
15391:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
15392:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
15393:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
15394:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
15395:                 } else {
15396:                     @changed = @{$selfenrollhash{'validation'}{$item}};
15397:                 }
15398:             } else {
15399:                 @changed = @{$selfenrollhash{'validation'}{$item}};
15400:             }
15401:             if (@changed) {
15402:                 if ($selfenrollhash{'validation'}{$item}) { 
15403:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
15404:                 } else {
15405:                     $changes{'validation'}{$item} = &mt('None');
15406:                 }
15407:             }
15408:         } else {
15409:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
15410:             if ($item eq 'markup') {
15411:                if ($env{'form.selfenroll_validation_'.$item}) {
15412:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
15413:                }
15414:             }
15415:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
15416:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
15417:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
15418:                 }
15419:             }
15420:         }
15421:     }
15422: 
15423:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
15424:                                              $dom);
15425:     if ($putresult eq 'ok') {
15426:         if (keys(%changes) > 0) {
15427:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15428:             $resulttext = &mt('Changes made:').'<ul>';
15429:             foreach my $key ('admin','default','validation') {
15430:                 if (ref($changes{$key}) eq 'HASH') {
15431:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
15432:                     if ($key eq 'validation') {
15433:                         foreach my $item (@{$itemsref}) {
15434:                             if (exists($changes{$key}{$item})) {
15435:                                 if ($item eq 'markup') {
15436:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
15437:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
15438:                                 } else {  
15439:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
15440:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
15441:                                 }
15442:                             }
15443:                         }
15444:                     } else {
15445:                         foreach my $type (@types) {
15446:                             if ($type eq 'community') {
15447:                                 $roles{'1'} = &mt('Community personnel');
15448:                             } else {
15449:                                 $roles{'1'} = &mt('Course personnel');
15450:                             }
15451:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
15452:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
15453:                                     if ($key eq 'admin') {
15454:                                         my @mgrdc = ();
15455:                                         if (ref($ordered{$key}) eq 'ARRAY') {
15456:                                             foreach my $item (@{$ordered{'admin'}}) {
15457:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
15458:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
15459:                                                         push(@mgrdc,$item);
15460:                                                     }
15461:                                                 }
15462:                                             }
15463:                                             if (@mgrdc) {
15464:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
15465:                                             } else {
15466:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
15467:                                             }
15468:                                         }
15469:                                     } else {
15470:                                         if (ref($ordered{$key}) eq 'ARRAY') {
15471:                                             foreach my $item (@{$ordered{$key}}) {
15472:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
15473:                                                     $domdefaults{$type.'selfenroll'.$item} =
15474:                                                         $selfenrollhash{$key}{$type}{$item};
15475:                                                 }
15476:                                             }
15477:                                         }
15478:                                     }
15479:                                 }
15480:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
15481:                                 foreach my $item (@{$ordered{$key}}) {
15482:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
15483:                                         $resulttext .= '<li>';
15484:                                         if ($key eq 'admin') {
15485:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
15486:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
15487:                                         } else {
15488:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
15489:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
15490:                                         }
15491:                                         $resulttext .= '</li>';
15492:                                     }
15493:                                 }
15494:                                 $resulttext .= '</ul></li>';
15495:                             }
15496:                         }
15497:                         $resulttext .= '</ul></li>'; 
15498:                     }
15499:                 }
15500:             }
15501:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
15502:                 my $cachetime = 24*60*60;
15503:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15504:                 if (ref($lastactref) eq 'HASH') {
15505:                     $lastactref->{'domdefaults'} = 1;
15506:                 }
15507:             }
15508:             $resulttext .= '</ul>';
15509:         } else {
15510:             $resulttext = &mt('No changes made to self-enrollment settings');
15511:         }
15512:     } else {
15513:         $resulttext = '<span class="LC_error">'.
15514:             &mt('An error occurred: [_1]',$putresult).'</span>';
15515:     }
15516:     return $resulttext;
15517: }
15518: 
15519: sub modify_usersessions {
15520:     my ($dom,$lastactref,%domconfig) = @_;
15521:     my @hostingtypes = ('version','excludedomain','includedomain');
15522:     my @offloadtypes = ('primary','default');
15523:     my %types = (
15524:                   remote => \@hostingtypes,
15525:                   hosted => \@hostingtypes,
15526:                   spares => \@offloadtypes,
15527:                 );
15528:     my @prefixes = ('remote','hosted','spares');
15529:     my @lcversions = &Apache::lonnet::all_loncaparevs();
15530:     my (%by_ip,%by_location,@intdoms);
15531:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
15532:     my @locations = sort(keys(%by_location));
15533:     my (%defaultshash,%changes);
15534:     foreach my $prefix (@prefixes) {
15535:         $defaultshash{'usersessions'}{$prefix} = {};
15536:     }
15537:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15538:     my $resulttext;
15539:     my %iphost = &Apache::lonnet::get_iphost();
15540:     foreach my $prefix (@prefixes) {
15541:         next if ($prefix eq 'spares');
15542:         foreach my $type (@{$types{$prefix}}) {
15543:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
15544:             if ($type eq 'version') {
15545:                 my $value = $env{'form.'.$prefix.'_'.$type};
15546:                 my $okvalue;
15547:                 if ($value ne '') {
15548:                     if (grep(/^\Q$value\E$/,@lcversions)) {
15549:                         $okvalue = $value;
15550:                     }
15551:                 }
15552:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
15553:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
15554:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
15555:                             if ($inuse == 0) {
15556:                                 $changes{$prefix}{$type} = 1;
15557:                             } else {
15558:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
15559:                                     $changes{$prefix}{$type} = 1;
15560:                                 }
15561:                                 if ($okvalue ne '') {
15562:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15563:                                 } 
15564:                             }
15565:                         } else {
15566:                             if (($inuse == 1) && ($okvalue ne '')) {
15567:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15568:                                 $changes{$prefix}{$type} = 1;
15569:                             }
15570:                         }
15571:                     } else {
15572:                         if (($inuse == 1) && ($okvalue ne '')) {
15573:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15574:                             $changes{$prefix}{$type} = 1;
15575:                         }
15576:                     }
15577:                 } else {
15578:                     if (($inuse == 1) && ($okvalue ne '')) {
15579:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15580:                         $changes{$prefix}{$type} = 1;
15581:                     }
15582:                 }
15583:             } else {
15584:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
15585:                 my @okvals;
15586:                 foreach my $val (@vals) {
15587:                     if ($val =~ /:/) {
15588:                         my @items = split(/:/,$val);
15589:                         foreach my $item (@items) {
15590:                             if (ref($by_location{$item}) eq 'ARRAY') {
15591:                                 push(@okvals,$item);
15592:                             }
15593:                         }
15594:                     } else {
15595:                         if (ref($by_location{$val}) eq 'ARRAY') {
15596:                             push(@okvals,$val);
15597:                         }
15598:                     }
15599:                 }
15600:                 @okvals = sort(@okvals);
15601:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
15602:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
15603:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
15604:                             if ($inuse == 0) {
15605:                                 $changes{$prefix}{$type} = 1; 
15606:                             } else {
15607:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15608:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
15609:                                 if (@changed > 0) {
15610:                                     $changes{$prefix}{$type} = 1;
15611:                                 }
15612:                             }
15613:                         } else {
15614:                             if ($inuse == 1) {
15615:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15616:                                 $changes{$prefix}{$type} = 1;
15617:                             }
15618:                         } 
15619:                     } else {
15620:                         if ($inuse == 1) {
15621:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15622:                             $changes{$prefix}{$type} = 1;
15623:                         }
15624:                     }
15625:                 } else {
15626:                     if ($inuse == 1) {
15627:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15628:                         $changes{$prefix}{$type} = 1;
15629:                     }
15630:                 }
15631:             }
15632:         }
15633:     }
15634: 
15635:     my @alldoms = &Apache::lonnet::all_domains();
15636:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15637:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
15638:     my $savespares;
15639: 
15640:     foreach my $lonhost (sort(keys(%servers))) {
15641:         my $serverhomeID =
15642:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
15643:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
15644:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
15645:         my %spareschg;
15646:         foreach my $type (@{$types{'spares'}}) {
15647:             my @okspares;
15648:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
15649:             foreach my $server (@checked) {
15650:                 if (&Apache::lonnet::hostname($server) ne '') {
15651:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
15652:                         unless (grep(/^\Q$server\E$/,@okspares)) {
15653:                             push(@okspares,$server);
15654:                         }
15655:                     }
15656:                 }
15657:             }
15658:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
15659:             my $newspare;
15660:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
15661:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
15662:                     $newspare = $new;
15663:                 }
15664:             }
15665:             my @spares;
15666:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
15667:                 @spares = sort(@okspares,$newspare);
15668:             } else {
15669:                 @spares = sort(@okspares);
15670:             }
15671:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
15672:             if (ref($spareid{$lonhost}) eq 'HASH') {
15673:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
15674:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
15675:                     if (@diffs > 0) {
15676:                         $spareschg{$type} = 1;
15677:                     }
15678:                 }
15679:             }
15680:         }
15681:         if (keys(%spareschg) > 0) {
15682:             $changes{'spares'}{$lonhost} = \%spareschg;
15683:         }
15684:     }
15685:     $defaultshash{'usersessions'}{'offloadnow'} = {};
15686:     $defaultshash{'usersessions'}{'offloadoth'} = {};
15687:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
15688:     my @okoffload;
15689:     if (@offloadnow) {
15690:         foreach my $server (@offloadnow) {
15691:             if (&Apache::lonnet::hostname($server) ne '') {
15692:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
15693:                     push(@okoffload,$server);
15694:                 }
15695:             }
15696:         }
15697:         if (@okoffload) {
15698:             foreach my $lonhost (@okoffload) {
15699:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
15700:             }
15701:         }
15702:     }
15703:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
15704:     my @okoffloadoth;
15705:     if (@offloadoth) {
15706:         foreach my $server (@offloadoth) {
15707:             if (&Apache::lonnet::hostname($server) ne '') {
15708:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
15709:                     push(@okoffloadoth,$server);
15710:                 }
15711:             }
15712:         }
15713:         if (@okoffloadoth) {
15714:             foreach my $lonhost (@okoffloadoth) {
15715:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
15716:             }
15717:         }
15718:     }
15719:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
15720:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
15721:             if (ref($changes{'spares'}) eq 'HASH') {
15722:                 if (keys(%{$changes{'spares'}}) > 0) {
15723:                     $savespares = 1;
15724:                 }
15725:             }
15726:         } else {
15727:             $savespares = 1;
15728:         }
15729:         foreach my $offload ('offloadnow','offloadoth') {
15730:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
15731:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
15732:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
15733:                         $changes{$offload} = 1;
15734:                         last;
15735:                     }
15736:                 }
15737:                 unless ($changes{$offload}) {
15738:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
15739:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
15740:                             $changes{$offload} = 1;
15741:                             last;
15742:                         }
15743:                     }
15744:                 }
15745:             } else {
15746:                 if (($offload eq 'offloadnow') && (@okoffload)) {
15747:                      $changes{'offloadnow'} = 1;
15748:                 }
15749:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
15750:                     $changes{'offloadoth'} = 1;
15751:                 }
15752:             }
15753:         }
15754:     } else {
15755:         if (@okoffload) {
15756:             $changes{'offloadnow'} = 1;
15757:         }
15758:         if (@okoffloadoth) {
15759:             $changes{'offloadoth'} = 1;
15760:         }
15761:     }
15762:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
15763:     if ((keys(%changes) > 0) || ($savespares)) {
15764:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15765:                                                  $dom);
15766:         if ($putresult eq 'ok') {
15767:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
15768:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
15769:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
15770:                 }
15771:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
15772:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
15773:                 }
15774:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
15775:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
15776:                 }
15777:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
15778:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
15779:                 }
15780:             }
15781:             my $cachetime = 24*60*60;
15782:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15783:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
15784:             if (ref($lastactref) eq 'HASH') {
15785:                 $lastactref->{'domdefaults'} = 1;
15786:                 $lastactref->{'usersessions'} = 1;
15787:             }
15788:             if (keys(%changes) > 0) {
15789:                 my %lt = &usersession_titles();
15790:                 $resulttext = &mt('Changes made:').'<ul>';
15791:                 foreach my $prefix (@prefixes) {
15792:                     if (ref($changes{$prefix}) eq 'HASH') {
15793:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
15794:                         if ($prefix eq 'spares') {
15795:                             if (ref($changes{$prefix}) eq 'HASH') {
15796:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
15797:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
15798:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
15799:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
15800:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
15801:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
15802:                                         foreach my $type (@{$types{$prefix}}) {
15803:                                             if ($changes{$prefix}{$lonhost}{$type}) {
15804:                                                 my $offloadto = &mt('None');
15805:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
15806:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
15807:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
15808:                                                     }
15809:                                                 }
15810:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
15811:                                             }
15812:                                         }
15813:                                     }
15814:                                     $resulttext .= '</li>';
15815:                                 }
15816:                             }
15817:                         } else {
15818:                             foreach my $type (@{$types{$prefix}}) {
15819:                                 if (defined($changes{$prefix}{$type})) {
15820:                                     my $newvalue;
15821:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
15822:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
15823:                                             if ($type eq 'version') {
15824:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
15825:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
15826:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
15827:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
15828:                                                 }
15829:                                             }
15830:                                         }
15831:                                     }
15832:                                     if ($newvalue eq '') {
15833:                                         if ($type eq 'version') {
15834:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
15835:                                         } else {
15836:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
15837:                                         }
15838:                                     } else {
15839:                                         if ($type eq 'version') {
15840:                                             $newvalue .= ' '.&mt('(or later)'); 
15841:                                         }
15842:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
15843:                                     }
15844:                                 }
15845:                             }
15846:                         }
15847:                         $resulttext .= '</ul>';
15848:                     }
15849:                 }
15850:                 if ($changes{'offloadnow'}) {
15851:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
15852:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
15853:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
15854:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
15855:                                 $resulttext .= '<li>'.$lonhost.'</li>';
15856:                             }
15857:                             $resulttext .= '</ul>';
15858:                         } else {
15859:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
15860:                         }
15861:                     } else {
15862:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
15863:                     }
15864:                 }
15865:                 if ($changes{'offloadoth'}) {
15866:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
15867:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
15868:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
15869:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
15870:                                 $resulttext .= '<li>'.$lonhost.'</li>';
15871:                             }
15872:                             $resulttext .= '</ul>';
15873:                         } else {
15874:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
15875:                         }
15876:                     } else {
15877:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
15878:                     }
15879:                 }
15880:                 $resulttext .= '</ul>';
15881:             } else {
15882:                 $resulttext = $nochgmsg;
15883:             }
15884:         } else {
15885:             $resulttext = '<span class="LC_error">'.
15886:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15887:         }
15888:     } else {
15889:         $resulttext = $nochgmsg;
15890:     }
15891:     return $resulttext;
15892: }
15893: 
15894: sub modify_loadbalancing {
15895:     my ($dom,%domconfig) = @_;
15896:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
15897:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
15898:     my ($othertitle,$usertypes,$types) =
15899:         &Apache::loncommon::sorted_inst_types($dom);
15900:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15901:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
15902:     my @sparestypes = ('primary','default');
15903:     my %typetitles = &sparestype_titles();
15904:     my $resulttext;
15905:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
15906:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
15907:         %existing = %{$domconfig{'loadbalancing'}};
15908:     }
15909:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
15910:                               \%currtargets,\%currrules,\%currcookies);
15911:     my ($saveloadbalancing,%defaultshash,%changes);
15912:     my ($alltypes,$othertypes,$titles) =
15913:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
15914:     my %ruletitles = &offloadtype_text();
15915:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
15916:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
15917:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
15918:         if ($balancer eq '') {
15919:             next;
15920:         }
15921:         if (!exists($servers{$balancer})) {
15922:             if (exists($currbalancer{$balancer})) {
15923:                 push(@{$changes{'delete'}},$balancer);
15924:             }
15925:             next;
15926:         }
15927:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
15928:             push(@{$changes{'delete'}},$balancer);
15929:             next;
15930:         }
15931:         if (!exists($currbalancer{$balancer})) {
15932:             push(@{$changes{'add'}},$balancer);
15933:         }
15934:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
15935:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
15936:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
15937:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
15938:             $saveloadbalancing = 1;
15939:         }
15940:         foreach my $sparetype (@sparestypes) {
15941:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
15942:             my @offloadto;
15943:             foreach my $target (@targets) {
15944:                 if (($servers{$target}) && ($target ne $balancer)) {
15945:                     if ($sparetype eq 'default') {
15946:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
15947:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
15948:                         }
15949:                     }
15950:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
15951:                         push(@offloadto,$target);
15952:                     }
15953:                 }
15954:             }
15955:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
15956:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
15957:                     push(@offloadto,$balancer);
15958:                 }
15959:             }
15960:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
15961:         }
15962:         if ($env{'form.loadbalancing_cookie_'.$i}) {
15963:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
15964:             if (exists($currbalancer{$balancer})) {
15965:                 unless ($currcookies{$balancer}) {
15966:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
15967:                 }
15968:             }
15969:         } elsif (exists($currbalancer{$balancer})) {
15970:             if ($currcookies{$balancer}) {
15971:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
15972:             }
15973:         }
15974:         if (ref($currtargets{$balancer}) eq 'HASH') {
15975:             foreach my $sparetype (@sparestypes) {
15976:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
15977:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
15978:                     if (@targetdiffs > 0) {
15979:                         $changes{'curr'}{$balancer}{'targets'} = 1;
15980:                     }
15981:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15982:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15983:                         $changes{'curr'}{$balancer}{'targets'} = 1;
15984:                     }
15985:                 }
15986:             }
15987:         } else {
15988:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
15989:                 foreach my $sparetype (@sparestypes) {
15990:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15991:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15992:                             $changes{'curr'}{$balancer}{'targets'} = 1;
15993:                         }
15994:                     }
15995:                 }
15996:             }
15997:         }
15998:         my $ishomedom;
15999:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
16000:             $ishomedom = 1;
16001:         }
16002:         if (ref($alltypes) eq 'ARRAY') {
16003:             foreach my $type (@{$alltypes}) {
16004:                 my $rule;
16005:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
16006:                          (!$ishomedom)) {
16007:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
16008:                 }
16009:                 if ($rule eq 'specific') {
16010:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
16011:                     if (exists($servers{$specifiedhost})) {
16012:                         $rule = $specifiedhost;
16013:                     }
16014:                 }
16015:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
16016:                 if (ref($currrules{$balancer}) eq 'HASH') {
16017:                     if ($rule ne $currrules{$balancer}{$type}) {
16018:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
16019:                     }
16020:                 } elsif ($rule ne '') {
16021:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
16022:                 }
16023:             }
16024:         }
16025:     }
16026:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
16027:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
16028:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
16029:             $defaultshash{'loadbalancing'} = {};
16030:         }
16031:         my $putresult = &Apache::lonnet::put_dom('configuration',
16032:                                                  \%defaultshash,$dom);
16033:         if ($putresult eq 'ok') {
16034:             if (keys(%changes) > 0) {
16035:                 my %toupdate;
16036:                 if (ref($changes{'delete'}) eq 'ARRAY') {
16037:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
16038:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
16039:                         $toupdate{$balancer} = 1;
16040:                     }
16041:                 }
16042:                 if (ref($changes{'add'}) eq 'ARRAY') {
16043:                     foreach my $balancer (sort(@{$changes{'add'}})) {
16044:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
16045:                         $toupdate{$balancer} = 1;
16046:                     }
16047:                 }
16048:                 if (ref($changes{'curr'}) eq 'HASH') {
16049:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
16050:                         $toupdate{$balancer} = 1;
16051:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
16052:                             if ($changes{'curr'}{$balancer}{'targets'}) {
16053:                                 my %offloadstr;
16054:                                 foreach my $sparetype (@sparestypes) {
16055:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16056:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
16057:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
16058:                                         }
16059:                                     }
16060:                                 }
16061:                                 if (keys(%offloadstr) == 0) {
16062:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
16063:                                 } else {
16064:                                     my $showoffload;
16065:                                     foreach my $sparetype (@sparestypes) {
16066:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
16067:                                         if (defined($offloadstr{$sparetype})) {
16068:                                             $showoffload .= $offloadstr{$sparetype};
16069:                                         } else {
16070:                                             $showoffload .= &mt('None');
16071:                                         }
16072:                                         $showoffload .= ('&nbsp;'x3);
16073:                                     }
16074:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
16075:                                 }
16076:                             }
16077:                         }
16078:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
16079:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
16080:                                 foreach my $type (@{$alltypes}) {
16081:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
16082:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
16083:                                         my $balancetext;
16084:                                         if ($rule eq '') {
16085:                                             $balancetext =  $ruletitles{'default'};
16086:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
16087:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
16088:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
16089:                                                 foreach my $sparetype (@sparestypes) {
16090:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16091:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
16092:                                                     }
16093:                                                 }
16094:                                                 foreach my $item (@{$alltypes}) {
16095:                                                     next if ($item =~  /^_LC_ipchange/);
16096:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
16097:                                                     if ($hasrule eq 'homeserver') {
16098:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
16099:                                                     } else {
16100:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
16101:                                                             if ($servers{$hasrule}) {
16102:                                                                 $toupdate{$hasrule} = 1;
16103:                                                             }
16104:                                                         }
16105:                                                     }
16106:                                                 }
16107:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
16108:                                                     $balancetext =  $ruletitles{$rule};
16109:                                                 } else {
16110:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
16111:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
16112:                                                     if ($receiver) {
16113:                                                         $toupdate{$receiver};
16114:                                                     }
16115:                                                 }
16116:                                             } else {
16117:                                                 $balancetext =  $ruletitles{$rule};
16118:                                             }
16119:                                         } else {
16120:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
16121:                                         }
16122:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
16123:                                     }
16124:                                 }
16125:                             }
16126:                         }
16127:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
16128:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
16129:                                                       $balancer).'</li>';
16130:                         }
16131:                         if (keys(%toupdate)) {
16132:                             my %thismachine;
16133:                             my $updatedhere;
16134:                             my $cachetime = 60*60*24;
16135:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
16136:                             foreach my $lonhost (keys(%toupdate)) {
16137:                                 if ($thismachine{$lonhost}) {
16138:                                     unless ($updatedhere) {
16139:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
16140:                                                                       $defaultshash{'loadbalancing'},
16141:                                                                       $cachetime);
16142:                                         $updatedhere = 1;
16143:                                     }
16144:                                 } else {
16145:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
16146:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
16147:                                 }
16148:                             }
16149:                         }
16150:                     }
16151:                 }
16152:                 if ($resulttext ne '') {
16153:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
16154:                 } else {
16155:                     $resulttext = $nochgmsg;
16156:                 }
16157:             } else {
16158:                 $resulttext = $nochgmsg;
16159:             }
16160:         } else {
16161:             $resulttext = '<span class="LC_error">'.
16162:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16163:         }
16164:     } else {
16165:         $resulttext = $nochgmsg;
16166:     }
16167:     return $resulttext;
16168: }
16169: 
16170: sub recurse_check {
16171:     my ($chkcats,$categories,$depth,$name) = @_;
16172:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
16173:         my $chg = 0;
16174:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
16175:             my $category = $chkcats->[$depth]{$name}[$j];
16176:             my $item;
16177:             if ($category eq '') {
16178:                 $chg ++;
16179:             } else {
16180:                 my $deeper = $depth + 1;
16181:                 $item = &escape($category).':'.&escape($name).':'.$depth;
16182:                 if ($chg) {
16183:                     $categories->{$item} -= $chg;
16184:                 }
16185:                 &recurse_check($chkcats,$categories,$deeper,$category);
16186:                 $deeper --;
16187:             }
16188:         }
16189:     }
16190:     return;
16191: }
16192: 
16193: sub recurse_cat_deletes {
16194:     my ($item,$coursecategories,$deletions) = @_;
16195:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
16196:     my $subdepth = $depth + 1;
16197:     if (ref($coursecategories) eq 'HASH') {
16198:         foreach my $subitem (keys(%{$coursecategories})) {
16199:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
16200:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
16201:                 delete($coursecategories->{$subitem});
16202:                 $deletions->{$subitem} = 1;
16203:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
16204:             }
16205:         }
16206:     }
16207:     return;
16208: }
16209: 
16210: sub active_dc_picker {
16211:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
16212:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16213:     my @domcoord = keys(%domcoords);
16214:     if (keys(%currhash)) {
16215:         foreach my $dc (keys(%currhash)) {
16216:             unless (exists($domcoords{$dc})) {
16217:                 push(@domcoord,$dc);
16218:             }
16219:         }
16220:     }
16221:     @domcoord = sort(@domcoord);
16222:     my $numdcs = scalar(@domcoord);
16223:     my $rows = 0;
16224:     my $table;
16225:     if ($numdcs > 1) {
16226:         $table = '<table>';
16227:         for (my $i=0; $i<@domcoord; $i++) {
16228:             my $rem = $i%($numinrow);
16229:             if ($rem == 0) {
16230:                 if ($i > 0) {
16231:                     $table .= '</tr>';
16232:                 }
16233:                 $table .= '<tr>';
16234:                 $rows ++;
16235:             }
16236:             my $check = '';
16237:             if ($inputtype eq 'radio') {
16238:                 if (keys(%currhash) == 0) {
16239:                     if (!$i) {
16240:                         $check = ' checked="checked"';
16241:                     }
16242:                 } elsif (exists($currhash{$domcoord[$i]})) {
16243:                     $check = ' checked="checked"';
16244:                 }
16245:             } else {
16246:                 if (exists($currhash{$domcoord[$i]})) {
16247:                     $check = ' checked="checked"';
16248:                 }
16249:             }
16250:             if ($i == @domcoord - 1) {
16251:                 my $colsleft = $numinrow - $rem;
16252:                 if ($colsleft > 1) {
16253:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
16254:                 } else {
16255:                     $table .= '<td class="LC_left_item">';
16256:                 }
16257:             } else {
16258:                 $table .= '<td class="LC_left_item">';
16259:             }
16260:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
16261:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
16262:             $table .= '<span class="LC_nobreak"><label>'.
16263:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
16264:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
16265:             if ($user ne $dcname.':'.$dcdom) {
16266:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16267:             }
16268:             $table .= '</label></span></td>';
16269:         }
16270:         $table .= '</tr></table>';
16271:     } elsif ($numdcs == 1) {
16272:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
16273:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
16274:         if ($inputtype eq 'radio') {
16275:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
16276:             if ($user ne $dcname.':'.$dcdom) {
16277:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16278:             }
16279:         } else {
16280:             my $check;
16281:             if (exists($currhash{$domcoord[0]})) {
16282:                 $check = ' checked="checked"';
16283:             }
16284:             $table = '<span class="LC_nobreak"><label>'.
16285:                      '<input type="checkbox" name="'.$name.'" '.
16286:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
16287:             if ($user ne $dcname.':'.$dcdom) {
16288:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16289:             }
16290:             $table .= '</label></span>';
16291:             $rows ++;
16292:         }
16293:     }
16294:     return ($numdcs,$table,$rows);
16295: }
16296: 
16297: sub usersession_titles {
16298:     return &Apache::lonlocal::texthash(
16299:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
16300:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
16301:                spares => 'Servers offloaded to, when busy',
16302:                version => 'LON-CAPA version requirement',
16303:                excludedomain => 'Allow all, but exclude specific domains',
16304:                includedomain => 'Deny all, but include specific domains',
16305:                primary => 'Primary (checked first)',
16306:                default => 'Default',
16307:            );
16308: }
16309: 
16310: sub id_for_thisdom {
16311:     my (%servers) = @_;
16312:     my %altids;
16313:     foreach my $server (keys(%servers)) {
16314:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
16315:         if ($serverhome ne $server) {
16316:             $altids{$serverhome} = $server;
16317:         }
16318:     }
16319:     return %altids;
16320: }
16321: 
16322: sub count_servers {
16323:     my ($currbalancer,%servers) = @_;
16324:     my (@spares,$numspares);
16325:     foreach my $lonhost (sort(keys(%servers))) {
16326:         next if ($currbalancer eq $lonhost);
16327:         push(@spares,$lonhost);
16328:     }
16329:     if ($currbalancer) {
16330:         $numspares = scalar(@spares);
16331:     } else {
16332:         $numspares = scalar(@spares) - 1;
16333:     }
16334:     return ($numspares,@spares);
16335: }
16336: 
16337: sub lonbalance_targets_js {
16338:     my ($dom,$types,$servers,$settings) = @_;
16339:     my $select = &mt('Select');
16340:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
16341:     if (ref($servers) eq 'HASH') {
16342:         $alltargets = join("','",sort(keys(%{$servers})));
16343:         my @homedoms;
16344:         foreach my $server (sort(keys(%{$servers}))) {
16345:             if (&Apache::lonnet::host_domain($server) eq $dom) {
16346:                 push(@homedoms,'1');
16347:             } else {
16348:                 push(@homedoms,'0');
16349:             }
16350:         }
16351:         $allishome = join("','",@homedoms);
16352:     }
16353:     if (ref($types) eq 'ARRAY') {
16354:         if (@{$types} > 0) {
16355:             @alltypes = @{$types};
16356:         }
16357:     }
16358:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
16359:     $allinsttypes = join("','",@alltypes);
16360:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
16361:     if (ref($settings) eq 'HASH') {
16362:         %existing = %{$settings};
16363:     }
16364:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
16365:                               \%currtargets,\%currrules,\%currcookies);
16366:     my $balancers = join("','",sort(keys(%currbalancer)));
16367:     return <<"END";
16368: 
16369: <script type="text/javascript">
16370: // <![CDATA[
16371: 
16372: currBalancers = new Array('$balancers');
16373: 
16374: function toggleTargets(balnum) {
16375:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
16376:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
16377:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
16378:     var prevbalancer = prevhostitem.value;
16379:     var baltotal = document.getElementById('loadbalancing_total').value;
16380:     prevhostitem.value = balancer;
16381:     if (prevbalancer != '') {
16382:         var prevIdx = currBalancers.indexOf(prevbalancer);
16383:         if (prevIdx != -1) {
16384:             currBalancers.splice(prevIdx,1);
16385:         }
16386:     }
16387:     if (balancer == '') {
16388:         hideSpares(balnum);
16389:     } else {
16390:         var currIdx = currBalancers.indexOf(balancer);
16391:         if (currIdx == -1) {
16392:             currBalancers.push(balancer);
16393:         }
16394:         var homedoms = new Array('$allishome');
16395:         var ishomedom = homedoms[lonhostitem.selectedIndex];
16396:         showSpares(balancer,ishomedom,balnum);
16397:     }
16398:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
16399:     return;
16400: }
16401: 
16402: function showSpares(balancer,ishomedom,balnum) {
16403:     var alltargets = new Array('$alltargets');
16404:     var insttypes = new Array('$allinsttypes');
16405:     var offloadtypes = new Array('primary','default');
16406: 
16407:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
16408:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
16409:  
16410:     for (var i=0; i<offloadtypes.length; i++) {
16411:         var count = 0;
16412:         for (var j=0; j<alltargets.length; j++) {
16413:             if (alltargets[j] != balancer) {
16414:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
16415:                 item.value = alltargets[j];
16416:                 item.style.textAlign='left';
16417:                 item.style.textFace='normal';
16418:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
16419:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
16420:                     item.disabled = '';
16421:                 } else {
16422:                     item.disabled = 'disabled';
16423:                     item.checked = false;
16424:                 }
16425:                 count ++;
16426:             }
16427:         }
16428:     }
16429:     for (var k=0; k<insttypes.length; k++) {
16430:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
16431:             if (ishomedom == 1) {
16432:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
16433:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
16434:             } else {
16435:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
16436:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
16437:             }
16438:         } else {
16439:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
16440:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
16441:         }
16442:         if ((insttypes[k] != '_LC_external') && 
16443:             ((insttypes[k] != '_LC_internetdom') ||
16444:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
16445:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
16446:             item.options.length = 0;
16447:             item.options[0] = new Option("","",true,true);
16448:             var idx = 0;
16449:             for (var m=0; m<alltargets.length; m++) {
16450:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
16451:                     idx ++;
16452:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
16453:                 }
16454:             }
16455:         }
16456:     }
16457:     return;
16458: }
16459: 
16460: function hideSpares(balnum) {
16461:     var alltargets = new Array('$alltargets');
16462:     var insttypes = new Array('$allinsttypes');
16463:     var offloadtypes = new Array('primary','default');
16464: 
16465:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
16466:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
16467: 
16468:     var total = alltargets.length - 1;
16469:     for (var i=0; i<offloadtypes; i++) {
16470:         for (var j=0; j<total; j++) {
16471:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
16472:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
16473:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
16474:         }
16475:     }
16476:     for (var k=0; k<insttypes.length; k++) {
16477:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
16478:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
16479:         if (insttypes[k] != '_LC_external') {
16480:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
16481:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
16482:         }
16483:     }
16484:     return;
16485: }
16486: 
16487: function checkOffloads(item,balnum,type) {
16488:     var alltargets = new Array('$alltargets');
16489:     var offloadtypes = new Array('primary','default');
16490:     if (item.checked) {
16491:         var total = alltargets.length - 1;
16492:         var other;
16493:         if (type == offloadtypes[0]) {
16494:             other = offloadtypes[1];
16495:         } else {
16496:             other = offloadtypes[0];
16497:         }
16498:         for (var i=0; i<total; i++) {
16499:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
16500:             if (server == item.value) {
16501:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
16502:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
16503:                 }
16504:             }
16505:         }
16506:     }
16507:     return;
16508: }
16509: 
16510: function singleServerToggle(balnum,type) {
16511:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
16512:     if (offloadtoSelIdx == 0) {
16513:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
16514:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
16515: 
16516:     } else {
16517:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
16518:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
16519:     }
16520:     return;
16521: }
16522: 
16523: function balanceruleChange(formname,balnum,type) {
16524:     if (type == '_LC_external') {
16525:         return;
16526:     }
16527:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
16528:     for (var i=0; i<typesRules.length; i++) {
16529:         if (formname.elements[typesRules[i]].checked) {
16530:             if (formname.elements[typesRules[i]].value != 'specific') {
16531:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
16532:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
16533:             } else {
16534:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
16535:             }
16536:         }
16537:     }
16538:     return;
16539: }
16540: 
16541: function balancerDeleteChange(balnum) {
16542:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
16543:     var baltotal = document.getElementById('loadbalancing_total').value;
16544:     var addtarget;
16545:     var removetarget;
16546:     var action = 'delete';
16547:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
16548:         var lonhost = hostitem.value;
16549:         var currIdx = currBalancers.indexOf(lonhost);
16550:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
16551:             if (currIdx != -1) {
16552:                 currBalancers.splice(currIdx,1);
16553:             }
16554:             addtarget = lonhost;
16555:         } else {
16556:             if (currIdx == -1) {
16557:                 currBalancers.push(lonhost);
16558:             }
16559:             removetarget = lonhost;
16560:             action = 'undelete';
16561:         }
16562:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
16563:     }
16564:     return;
16565: }
16566: 
16567: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
16568:     if (baltotal > 1) {
16569:         var offloadtypes = new Array('primary','default');
16570:         var alltargets = new Array('$alltargets');
16571:         var insttypes = new Array('$allinsttypes');
16572:         for (var i=0; i<baltotal; i++) {
16573:             if (i != balnum) {
16574:                 for (var j=0; j<offloadtypes.length; j++) {
16575:                     var total = alltargets.length - 1;
16576:                     for (var k=0; k<total; k++) {
16577:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
16578:                         var server = serveritem.value;
16579:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
16580:                             if (server == addtarget) {
16581:                                 serveritem.disabled = '';
16582:                             }
16583:                         }
16584:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
16585:                             if (server == removetarget) {
16586:                                 serveritem.disabled = 'disabled';
16587:                                 serveritem.checked = false;
16588:                             }
16589:                         }
16590:                     }
16591:                 }
16592:                 for (var j=0; j<insttypes.length; j++) {
16593:                     if (insttypes[j] != '_LC_external') {
16594:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
16595:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
16596:                             var currSel = singleserver.selectedIndex;
16597:                             var currVal = singleserver.options[currSel].value;
16598:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
16599:                                 var numoptions = singleserver.options.length;
16600:                                 var needsnew = 1;
16601:                                 for (var k=0; k<numoptions; k++) {
16602:                                     if (singleserver.options[k] == addtarget) {
16603:                                         needsnew = 0;
16604:                                         break;
16605:                                     }
16606:                                 }
16607:                                 if (needsnew == 1) {
16608:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
16609:                                 }
16610:                             }
16611:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
16612:                                 singleserver.options.length = 0;
16613:                                 if ((currVal) && (currVal != removetarget)) {
16614:                                     singleserver.options[0] = new Option("","",false,false);
16615:                                 } else {
16616:                                     singleserver.options[0] = new Option("","",true,true);
16617:                                 }
16618:                                 var idx = 0;
16619:                                 for (var m=0; m<alltargets.length; m++) {
16620:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
16621:                                         idx ++;
16622:                                         if (currVal == alltargets[m]) {
16623:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
16624:                                         } else {
16625:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
16626:                                         }
16627:                                     }
16628:                                 }
16629:                             }
16630:                         }
16631:                     }
16632:                 }
16633:             }
16634:         }
16635:     }
16636:     return;
16637: }
16638: 
16639: // ]]>
16640: </script>
16641: 
16642: END
16643: }
16644: 
16645: sub new_spares_js {
16646:     my @sparestypes = ('primary','default');
16647:     my $types = join("','",@sparestypes);
16648:     my $select = &mt('Select');
16649:     return <<"END";
16650: 
16651: <script type="text/javascript">
16652: // <![CDATA[
16653: 
16654: function updateNewSpares(formname,lonhost) {
16655:     var types = new Array('$types');
16656:     var include = new Array();
16657:     var exclude = new Array();
16658:     for (var i=0; i<types.length; i++) {
16659:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
16660:         for (var j=0; j<spareboxes.length; j++) {
16661:             if (formname.elements[spareboxes[j]].checked) {
16662:                 exclude.push(formname.elements[spareboxes[j]].value);
16663:             } else {
16664:                 include.push(formname.elements[spareboxes[j]].value);
16665:             }
16666:         }
16667:     }
16668:     for (var i=0; i<types.length; i++) {
16669:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
16670:         var selIdx = newSpare.selectedIndex;
16671:         var currnew = newSpare.options[selIdx].value;
16672:         var okSpares = new Array();
16673:         for (var j=0; j<newSpare.options.length; j++) {
16674:             var possible = newSpare.options[j].value;
16675:             if (possible != '') {
16676:                 if (exclude.indexOf(possible) == -1) {
16677:                     okSpares.push(possible);
16678:                 } else {
16679:                     if (currnew == possible) {
16680:                         selIdx = 0;
16681:                     }
16682:                 }
16683:             }
16684:         }
16685:         for (var k=0; k<include.length; k++) {
16686:             if (okSpares.indexOf(include[k]) == -1) {
16687:                 okSpares.push(include[k]);
16688:             }
16689:         }
16690:         okSpares.sort();
16691:         newSpare.options.length = 0;
16692:         if (selIdx == 0) {
16693:             newSpare.options[0] = new Option("$select","",true,true);
16694:         } else {
16695:             newSpare.options[0] = new Option("$select","",false,false);
16696:         }
16697:         for (var m=0; m<okSpares.length; m++) {
16698:             var idx = m+1;
16699:             var selThis = 0;
16700:             if (selIdx != 0) {
16701:                 if (okSpares[m] == currnew) {
16702:                     selThis = 1;
16703:                 }
16704:             }
16705:             if (selThis == 1) {
16706:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
16707:             } else {
16708:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
16709:             }
16710:         }
16711:     }
16712:     return;
16713: }
16714: 
16715: function checkNewSpares(lonhost,type) {
16716:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
16717:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
16718:     if (chosen != '') { 
16719:         var othertype;
16720:         var othernewSpare;
16721:         if (type == 'primary') {
16722:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
16723:         }
16724:         if (type == 'default') {
16725:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
16726:         }
16727:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
16728:             othernewSpare.selectedIndex = 0;
16729:         }
16730:     }
16731:     return;
16732: }
16733: 
16734: // ]]>
16735: </script>
16736: 
16737: END
16738: 
16739: }
16740: 
16741: sub common_domprefs_js {
16742:     return <<"END";
16743: 
16744: <script type="text/javascript">
16745: // <![CDATA[
16746: 
16747: function getIndicesByName(formname,item) {
16748:     var group = new Array();
16749:     for (var i=0;i<formname.elements.length;i++) {
16750:         if (formname.elements[i].name == item) {
16751:             group.push(formname.elements[i].id);
16752:         }
16753:     }
16754:     return group;
16755: }
16756: 
16757: // ]]>
16758: </script>
16759: 
16760: END
16761: 
16762: }
16763: 
16764: sub recaptcha_js {
16765:     my %lt = &captcha_phrases();
16766:     return <<"END";
16767: 
16768: <script type="text/javascript">
16769: // <![CDATA[
16770: 
16771: function updateCaptcha(caller,context) {
16772:     var privitem;
16773:     var pubitem;
16774:     var privtext;
16775:     var pubtext;
16776:     var versionitem;
16777:     var versiontext;
16778:     if (document.getElementById(context+'_recaptchapub')) {
16779:         pubitem = document.getElementById(context+'_recaptchapub');
16780:     } else {
16781:         return;
16782:     }
16783:     if (document.getElementById(context+'_recaptchapriv')) {
16784:         privitem = document.getElementById(context+'_recaptchapriv');
16785:     } else {
16786:         return;
16787:     }
16788:     if (document.getElementById(context+'_recaptchapubtxt')) {
16789:         pubtext = document.getElementById(context+'_recaptchapubtxt');
16790:     } else {
16791:         return;
16792:     }
16793:     if (document.getElementById(context+'_recaptchaprivtxt')) {
16794:         privtext = document.getElementById(context+'_recaptchaprivtxt');
16795:     } else {
16796:         return;
16797:     }
16798:     if (document.getElementById(context+'_recaptchaversion')) {
16799:         versionitem = document.getElementById(context+'_recaptchaversion');
16800:     } else {
16801:         return;
16802:     }
16803:     if (document.getElementById(context+'_recaptchavertxt')) {
16804:         versiontext = document.getElementById(context+'_recaptchavertxt');
16805:     } else {
16806:         return;
16807:     }
16808:     if (caller.checked) {
16809:         if (caller.value == 'recaptcha') {
16810:             pubitem.type = 'text';
16811:             privitem.type = 'text';
16812:             pubitem.size = '40';
16813:             privitem.size = '40';
16814:             pubtext.innerHTML = "$lt{'pub'}";
16815:             privtext.innerHTML = "$lt{'priv'}";
16816:             versionitem.type = 'text';
16817:             versionitem.size = '3';
16818:             versiontext.innerHTML = "$lt{'ver'}";
16819:         } else {
16820:             pubitem.type = 'hidden';
16821:             privitem.type = 'hidden';
16822:             versionitem.type = 'hidden';
16823:             pubtext.innerHTML = '';
16824:             privtext.innerHTML = '';
16825:             versiontext.innerHTML = '';
16826:         }
16827:     }
16828:     return;
16829: }
16830: 
16831: // ]]>
16832: </script>
16833: 
16834: END
16835: 
16836: }
16837: 
16838: sub toggle_display_js {
16839:     return <<"END";
16840: 
16841: <script type="text/javascript">
16842: // <![CDATA[
16843: 
16844: function toggleDisplay(domForm,caller) {
16845:     if (document.getElementById(caller)) {
16846:         var divitem = document.getElementById(caller);
16847:         var optionsElement = domForm.coursecredits;
16848:         var checkval = 1;
16849:         var dispval = 'block';
16850:         var selfcreateRegExp = /^cancreate_emailverified/;
16851:         if (caller == 'emailoptions') {
16852:             optionsElement = domForm.cancreate_email; 
16853:         }
16854:         if (caller == 'studentsubmission') {
16855:             optionsElement = domForm.postsubmit;
16856:         }
16857:         if (caller == 'cloneinstcode') {
16858:             optionsElement = domForm.canclone;
16859:             checkval = 'instcode';
16860:         }
16861:         if (selfcreateRegExp.test(caller)) {
16862:             optionsElement = domForm.elements[caller];
16863:             checkval = 'other';
16864:             dispval = 'inline'
16865:         }
16866:         if (optionsElement.length) {
16867:             var currval;
16868:             for (var i=0; i<optionsElement.length; i++) {
16869:                 if (optionsElement[i].checked) {
16870:                    currval = optionsElement[i].value;
16871:                 }
16872:             }
16873:             if (currval == checkval) {
16874:                 divitem.style.display = dispval;
16875:             } else {
16876:                 divitem.style.display = 'none';
16877:             }
16878:         }
16879:     }
16880:     return;
16881: }
16882: 
16883: // ]]>
16884: </script>
16885: 
16886: END
16887: 
16888: }
16889: 
16890: sub captcha_phrases {
16891:     return &Apache::lonlocal::texthash (
16892:                  priv => 'Private key',
16893:                  pub  => 'Public key',
16894:                  original  => 'original (CAPTCHA)',
16895:                  recaptcha => 'successor (ReCAPTCHA)',
16896:                  notused   => 'unused',
16897:                  ver => 'ReCAPTCHA version (1 or 2)',
16898:     );
16899: }
16900: 
16901: sub devalidate_remote_domconfs {
16902:     my ($dom,$cachekeys) = @_;
16903:     return unless (ref($cachekeys) eq 'HASH');
16904:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16905:     my %thismachine;
16906:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
16907:     my @posscached = ('domainconfig','domdefaults','usersessions',
16908:                       'ltitools','directorysrch','passwdconf','cats');
16909:     if (keys(%servers)) {
16910:         foreach my $server (keys(%servers)) {
16911:             next if ($thismachine{$server});
16912:             my @cached;
16913:             foreach my $name (@posscached) {
16914:                 if ($cachekeys->{$name}) {
16915:                     push(@cached,&escape($name).':'.&escape($dom));
16916:                 }
16917:             }
16918:             if (@cached) {
16919:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
16920:             }
16921:         }
16922:     }
16923:     return;
16924: }
16925: 
16926: 1;

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