File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.102.2.11: download - view: text, annotated - select for diffs
Sun Mar 7 00:32:21 2021 UTC (3 years, 2 months ago) by raeburn
Branches: version_2_11_3_msu
- For 2.11.3 (modified)
  Include changes in rev. 1.380

    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.11 2021/03/07 00:32:21 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ##############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, and textbook).  In each case the radio buttons 
  108: allow the selection of one of four values:
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use File::Copy;
  174: use Locale::Language;
  175: use DateTime::TimeZone;
  176: use DateTime::Locale;
  177: 
  178: my $registered_cleanup;
  179: my $modified_urls;
  180: 
  181: sub handler {
  182:     my $r=shift;
  183:     if ($r->header_only) {
  184:         &Apache::loncommon::content_type($r,'text/html');
  185:         $r->send_http_header;
  186:         return OK;
  187:     }
  188: 
  189:     my $context = 'domain';
  190:     my $dom = $env{'request.role.domain'};
  191:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  192:     if (&Apache::lonnet::allowed('mau',$dom)) {
  193:         &Apache::loncommon::content_type($r,'text/html');
  194:         $r->send_http_header;
  195:     } else {
  196:         $env{'user.error.msg'}=
  197:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  198:         return HTTP_NOT_ACCEPTABLE;
  199:     }
  200: 
  201:     $registered_cleanup=0;
  202:     @{$modified_urls}=();
  203: 
  204:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  205:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  206:                                             ['phase','actions']);
  207:     my $phase = 'pickactions';
  208:     if ( exists($env{'form.phase'}) ) {
  209:         $phase = $env{'form.phase'};
  210:     }
  211:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  212:     my %domconfig =
  213:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  214:                 'quotas','autoenroll','autoupdate','autocreate',
  215:                 'directorysrch','usercreation','usermodification',
  216:                 'contacts','defaults','scantron','coursecategories',
  217:                 'serverstatuses','requestcourses','helpsettings',
  218:                 'coursedefaults','usersessions','loadbalancing',
  219:                 'requestauthor','selfenrollment','inststatus',
  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 => 'Nightly status check e-mail',
  353:                                 col2 => 'Settings',},
  354:                                {col1 => 'Ask helpdesk form settings',
  355:                                 col2 => 'Value',},],
  356:                     print => \&print_contacts,
  357:                     modify => \&modify_contacts,
  358:                   },
  359:         'usercreation' => 
  360:                   { text => 'User creation',
  361:                     help => 'Domain_Configuration_User_Creation',
  362:                     header => [{col1 => 'Format rule type',
  363:                                 col2 => 'Format rules in force'},
  364:                                {col1 => 'User account creation',
  365:                                 col2 => 'Usernames which may be created',},
  366:                                {col1 => 'Context',
  367:                                 col2 => 'Assignable authentication types'}],
  368:                     print => \&print_usercreation,
  369:                     modify => \&modify_usercreation,
  370:                   },
  371:         'selfcreation' => 
  372:                   { text => 'Users self-creating accounts',
  373:                     help => 'Domain_Configuration_Self_Creation', 
  374:                     header => [{col1 => 'Self-creation with institutional username',
  375:                                 col2 => 'Enabled?'},
  376:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  377:                                 col2 => 'Information user can enter'},
  378:                                {col1 => 'Self-creation with e-mail verification',
  379:                                 col2 => 'Settings'}],
  380:                     print => \&print_selfcreation,
  381:                     modify => \&modify_selfcreation,
  382:                   },
  383:         'usermodification' =>
  384:                   { text => 'User modification',
  385:                     help => 'Domain_Configuration_User_Modification',
  386:                     header => [{col1 => 'Target user has role',
  387:                                 col2 => 'User information updatable in author context'},
  388:                                {col1 => 'Target user has role',
  389:                                 col2 => 'User information updatable in course context'}],
  390:                     print => \&print_usermodification,
  391:                     modify => \&modify_usermodification,
  392:                   },
  393:         'scantron' =>
  394:                   { text => 'Bubblesheet format',
  395:                     help => 'Domain_Configuration_Scantron_Format',
  396:                     header => [ {col1 => 'Bubblesheet format file',
  397:                                  col2 => ''},
  398:                                 {col1 => 'Bubblesheet data upload formats',
  399:                                  col2 => 'Settings'}],
  400:                     print => \&print_scantron,
  401:                     modify => \&modify_scantron,
  402:                   },
  403:         'requestcourses' => 
  404:                  {text => 'Request creation of courses',
  405:                   help => 'Domain_Configuration_Request_Courses',
  406:                   header => [{col1 => 'User affiliation',
  407:                               col2 => 'Availability/Processing of requests',},
  408:                              {col1 => 'Setting',
  409:                               col2 => 'Value'},
  410:                              {col1 => 'Available textbooks',
  411:                               col2 => ''},
  412:                              {col1 => 'Available templates',
  413:                               col2 => ''},
  414:                              {col1 => 'Validation (not official courses)',
  415:                               col2 => 'Value'},],
  416:                   print => \&print_quotas,
  417:                   modify => \&modify_quotas,
  418:                  },
  419:         'requestauthor' =>
  420:                  {text => 'Request Authoring Space',
  421:                   help => 'Domain_Configuration_Request_Author',
  422:                   header => [{col1 => 'User affiliation',
  423:                               col2 => 'Availability/Processing of requests',},
  424:                              {col1 => 'Setting',
  425:                               col2 => 'Value'}],
  426:                   print => \&print_quotas,
  427:                   modify => \&modify_quotas,
  428:                  },
  429:         'coursecategories' =>
  430:                   { text => 'Cataloging of courses/communities',
  431:                     help => 'Domain_Configuration_Cataloging_Courses',
  432:                     header => [{col1 => 'Catalog type/availability',
  433:                                 col2 => '',},
  434:                                {col1 => 'Category settings for standard catalog',
  435:                                 col2 => '',},
  436:                                {col1 => 'Categories',
  437:                                 col2 => '',
  438:                                }],
  439:                     print => \&print_coursecategories,
  440:                     modify => \&modify_coursecategories,
  441:                   },
  442:         'serverstatuses' =>
  443:                  {text   => 'Access to server status pages',
  444:                   help   => 'Domain_Configuration_Server_Status',
  445:                   header => [{col1 => 'Status Page',
  446:                               col2 => 'Other named users',
  447:                               col3 => 'Specific IPs',
  448:                             }],
  449:                   print => \&print_serverstatuses,
  450:                   modify => \&modify_serverstatuses,
  451:                  },
  452:         'helpsettings' =>
  453:                  {text   => 'Support settings',
  454:                   help   => 'Domain_Configuration_Help_Settings',
  455:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  456:                               col2 => 'Value'},
  457:                              {col1 => 'Helpdesk Roles',
  458:                               col2 => 'Settings'},],
  459:                   print  => \&print_helpsettings,
  460:                   modify => \&modify_helpsettings,
  461:                  },
  462:         'coursedefaults' => 
  463:                  {text => 'Course/Community defaults',
  464:                   help => 'Domain_Configuration_Course_Defaults',
  465:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  466:                               col2 => 'Value',},
  467:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  468:                               col2 => 'Value',},],
  469:                   print => \&print_coursedefaults,
  470:                   modify => \&modify_coursedefaults,
  471:                  },
  472:         'selfenrollment' => 
  473:                  {text   => 'Self-enrollment in Course/Community',
  474:                   help   => 'Domain_Configuration_Selfenrollment',
  475:                   header => [{col1 => 'Configuration Rights',
  476:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  477:                              {col1 => 'Defaults',
  478:                               col2 => 'Value'},
  479:                              {col1 => 'Self-enrollment validation (optional)',
  480:                               col2 => 'Value'},],
  481:                   print => \&print_selfenrollment,
  482:                   modify => \&modify_selfenrollment,
  483:                  },
  484:         'usersessions' =>
  485:                  {text  => 'User session hosting/offloading',
  486:                   help  => 'Domain_Configuration_User_Sessions',
  487:                   header => [{col1 => 'Domain server',
  488:                               col2 => 'Servers to offload sessions to when busy'},
  489:                              {col1 => 'Hosting of users from other domains',
  490:                               col2 => 'Rules'},
  491:                              {col1 => "Hosting domain's own users elsewhere",
  492:                               col2 => 'Rules'}],
  493:                   print => \&print_usersessions,
  494:                   modify => \&modify_usersessions,
  495:                  },
  496:         'loadbalancing' =>
  497:                  {text  => 'Dedicated Load Balancer(s)',
  498:                   help  => 'Domain_Configuration_Load_Balancing',
  499:                   header => [{col1 => 'Balancers',
  500:                               col2 => 'Default destinations',
  501:                               col3 => 'User affiliation',
  502:                               col4 => 'Overrides'},
  503:                             ],
  504:                   print => \&print_loadbalancing,
  505:                   modify => \&modify_loadbalancing,
  506:                  },
  507:         'ltitools' =>
  508:                  {text => 'External Tools (LTI)',
  509:                   help => 'Domain_Configuration_LTI_Tools',
  510:                   header => [{col1 => 'Setting',
  511:                               col2 => 'Value',}],
  512:                   print => \&print_ltitools,
  513:                   modify => \&modify_ltitools,
  514:                  },
  515:     );
  516:     if (keys(%servers) > 1) {
  517:         $prefs{'login'}  = { text   => 'Log-in page options',
  518:                              help   => 'Domain_Configuration_Login_Page',
  519:                             header => [{col1 => 'Log-in Service',
  520:                                         col2 => 'Server Setting',},
  521:                                        {col1 => 'Log-in Page Items',
  522:                                         col2 => ''},
  523:                                        {col1 => 'Log-in Help',
  524:                                         col2 => 'Value'},
  525:                                        {col1 => 'Custom HTML in document head',
  526:                                         col2 => 'Value'}],
  527:                             print => \&print_login,
  528:                             modify => \&modify_login,
  529:                            };
  530:     }
  531: 
  532:     my @roles = ('student','coordinator','author','admin');
  533:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  534:     &Apache::lonhtmlcommon::add_breadcrumb
  535:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  536:       text=>"Settings to display/modify"});
  537:     my $confname = $dom.'-domainconfig';
  538: 
  539:     if ($phase eq 'process') {
  540:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  541:                                                               \%prefs,\%domconfig,$confname,\@roles);
  542:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  543:             $r->rflush();
  544:             &devalidate_remote_domconfs($dom,$result);
  545:         }
  546:     } elsif ($phase eq 'display') {
  547:         my $js = &recaptcha_js().
  548:                  &toggle_display_js();
  549:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  550:             my ($othertitle,$usertypes,$types) =
  551:                 &Apache::loncommon::sorted_inst_types($dom);
  552:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  553:                                           $domconfig{'loadbalancing'}).
  554:                    &new_spares_js().
  555:                    &common_domprefs_js().
  556:                    &Apache::loncommon::javascript_array_indexof();
  557:         }
  558:         if (grep(/^requestcourses$/,@actions)) {
  559:             my $javascript_validations;
  560:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  561:             $js .= <<END;
  562: <script type="text/javascript">
  563: $javascript_validations
  564: </script>
  565: $coursebrowserjs
  566: END
  567:         }
  568:         if (grep(/^selfcreation$/,@actions)) {
  569:             $js .= &selfcreate_javascript();
  570:         }
  571:         if (grep(/^contacts$/,@actions)) {
  572:             $js .= &contacts_javascript();
  573:         }
  574:         if (grep(/^scantron$/,@actions)) {
  575:             $js .= &scantron_javascript();
  576:         }
  577:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  578:     } else {
  579: # check if domconfig user exists for the domain.
  580:         my $servadm = $r->dir_config('lonAdmEMail');
  581:         my ($configuserok,$author_ok,$switchserver) =
  582:             &config_check($dom,$confname,$servadm);
  583:         unless ($configuserok eq 'ok') {
  584:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  585:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  586:                           $confname).
  587:                       '<br />'
  588:             );
  589:             if ($switchserver) {
  590:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  591:                           '<br />'.
  592:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  593:                           '<br />'.
  594:                           &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).
  595:                           '<br />'.
  596:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  597:                 );
  598:             } else {
  599:                 $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.').
  600:                           '<br />'.
  601:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  602:                 );
  603:             }
  604:             $r->print(&Apache::loncommon::end_page());
  605:             return OK;
  606:         }
  607:         if (keys(%domconfig) == 0) {
  608:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  609:             my @ids=&Apache::lonnet::current_machine_ids();
  610:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  611:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  612:                 my @loginimages = ('img','logo','domlogo','login');
  613:                 my $custom_img_count = 0;
  614:                 foreach my $img (@loginimages) {
  615:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  616:                         $custom_img_count ++;
  617:                     }
  618:                 }
  619:                 foreach my $role (@roles) {
  620:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  621:                         $custom_img_count ++;
  622:                     }
  623:                 }
  624:                 if ($custom_img_count > 0) {
  625:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  626:                     my $switch_server = &check_switchserver($dom,$confname);
  627:                     $r->print(
  628:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  629:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  630:     &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 />'.
  631:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  632:                     if ($switch_server) {
  633:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  634:                     }
  635:                     $r->print(&Apache::loncommon::end_page());
  636:                     return OK;
  637:                 }
  638:             }
  639:         }
  640:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  641:     }
  642:     return OK;
  643: }
  644: 
  645: sub process_changes {
  646:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  647:     my %domconfig;
  648:     if (ref($values) eq 'HASH') {
  649:         %domconfig = %{$values};
  650:     }
  651:     my $output;
  652:     if ($action eq 'login') {
  653:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  654:     } elsif ($action eq 'rolecolors') {
  655:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  656:                                      $lastactref,%domconfig);
  657:     } elsif ($action eq 'quotas') {
  658:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  659:     } elsif ($action eq 'autoenroll') {
  660:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  661:     } elsif ($action eq 'autoupdate') {
  662:         $output = &modify_autoupdate($dom,%domconfig);
  663:     } elsif ($action eq 'autocreate') {
  664:         $output = &modify_autocreate($dom,%domconfig);
  665:     } elsif ($action eq 'directorysrch') {
  666:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  667:     } elsif ($action eq 'usercreation') {
  668:         $output = &modify_usercreation($dom,%domconfig);
  669:     } elsif ($action eq 'selfcreation') {
  670:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  671:     } elsif ($action eq 'usermodification') {
  672:         $output = &modify_usermodification($dom,%domconfig);
  673:     } elsif ($action eq 'contacts') {
  674:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  675:     } elsif ($action eq 'defaults') {
  676:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  677:     } elsif ($action eq 'scantron') {
  678:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  679:     } elsif ($action eq 'coursecategories') {
  680:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  681:     } elsif ($action eq 'serverstatuses') {
  682:         $output = &modify_serverstatuses($dom,%domconfig);
  683:     } elsif ($action eq 'requestcourses') {
  684:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  685:     } elsif ($action eq 'requestauthor') {
  686:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  687:     } elsif ($action eq 'helpsettings') {
  688:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  689:     } elsif ($action eq 'coursedefaults') {
  690:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  691:     } elsif ($action eq 'selfenrollment') {
  692:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  693:     } elsif ($action eq 'usersessions') {
  694:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  695:     } elsif ($action eq 'loadbalancing') {
  696:         $output = &modify_loadbalancing($dom,%domconfig);
  697:     } elsif ($action eq 'passwords') {
  698:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  699:     } elsif ($action eq 'ltitools') {
  700:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  701:     }
  702:     return $output;
  703: }
  704: 
  705: sub print_config_box {
  706:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  707:     my $rowtotal = 0;
  708:     my $output;
  709:     if ($action eq 'coursecategories') {
  710:         $output = &coursecategories_javascript($settings);
  711:     } elsif ($action eq 'defaults') {
  712:         $output = &defaults_javascript($settings); 
  713:     } elsif ($action eq 'passwords') {
  714:         $output = &passwords_javascript();
  715:     } elsif ($action eq 'helpsettings') {
  716:         my (%privs,%levelscurrent);
  717:         my %full=();
  718:         my %levels=(
  719:                      course => {},
  720:                      domain => {},
  721:                      system => {},
  722:                    );
  723:         my $context = 'domain';
  724:         my $crstype = 'Course';
  725:         my $formname = 'display';
  726:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  727:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  728:         $output =
  729:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  730:                                                       \@templateroles);
  731:     } elsif ($action eq 'ltitools') {
  732:         $output .= &ltitools_javascript($settings);
  733:     }
  734:     $output .=
  735:          '<table class="LC_nested_outer">
  736:           <tr>
  737:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  738:            &mt($item->{text}).'&nbsp;'.
  739:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  740:           '</tr>';
  741:     $rowtotal ++;
  742:     my $numheaders = 1;
  743:     if (ref($item->{'header'}) eq 'ARRAY') {
  744:         $numheaders = scalar(@{$item->{'header'}});
  745:     }
  746:     if ($numheaders > 1) {
  747:         my $colspan = '';
  748:         my $rightcolspan = '';
  749:         my $leftnobr = '';
  750:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  751:             ($action eq 'directorysrch') ||
  752:             (($action eq 'login') && ($numheaders < 4))) {
  753:             $colspan = ' colspan="2"';
  754:         }
  755:         if ($action eq 'usersessions') {
  756:             $rightcolspan = ' colspan="3"'; 
  757:         }
  758:         if ($action eq 'passwords') {
  759:             $leftnobr = ' LC_nobreak';
  760:         }
  761:         $output .= '
  762:           <tr>
  763:            <td>
  764:             <table class="LC_nested">
  765:              <tr class="LC_info_row">
  766:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  767:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  768:              </tr>';
  769:         $rowtotal ++;
  770:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  771:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  772:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  773:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  774:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  775:         } elsif ($action eq 'passwords') {
  776:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  777:         } elsif ($action eq 'coursecategories') {
  778:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  779:         } elsif ($action eq 'scantron') {
  780:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  781:         } elsif ($action eq 'login') {
  782:             if ($numheaders == 4) {
  783:                 $colspan = ' colspan="2"';
  784:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  785:             } else {
  786:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  787:             }
  788:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  789:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  790:         } elsif ($action eq 'rolecolors') {
  791:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  792:         }
  793:         $output .= '
  794:            </table>
  795:           </td>
  796:          </tr>
  797:          <tr>
  798:            <td>
  799:             <table class="LC_nested">
  800:              <tr class="LC_info_row">
  801:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  802:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  803:              </tr>';
  804:             $rowtotal ++;
  805:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  806:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  807:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  808:             ($action eq 'contacts') || ($action eq 'passwords')) {
  809:             if ($action eq 'coursecategories') {
  810:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  811:                 $colspan = ' colspan="2"';
  812:             } elsif ($action eq 'passwords') {
  813:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  814:             } else {
  815:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  816:             }
  817:             $output .= '
  818:            </table>
  819:           </td>
  820:          </tr>
  821:          <tr>
  822:            <td>
  823:             <table class="LC_nested">
  824:              <tr class="LC_info_row">
  825:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  826:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  827:              </tr>'."\n";
  828:             if ($action eq 'coursecategories') {
  829:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  830:             } elsif (($action eq 'contacts') || ($action eq 'passwords')) {
  831:                 if ($action eq 'passwords') {
  832:                     $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
  833:                 } else {
  834:                     $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
  835:                 }
  836:                 $output .= '
  837:              </tr>
  838:             </table>
  839:            </td>
  840:           </tr>
  841:           <tr>
  842:            <td>
  843:             <table class="LC_nested">
  844:              <tr class="LC_info_row">
  845:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  846:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
  847:                 if ($action eq 'passwords') {
  848:                     $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
  849:                 } else {
  850:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  851:                 }
  852:                 $output .= '
  853:             </table>
  854:           </td>
  855:          </tr>
  856:          <tr>';
  857:             } else {
  858:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  859:             }
  860:             $rowtotal ++;
  861:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  862:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  863:                  ($action eq 'helpsettings')) {
  864:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  865:         } elsif ($action eq 'scantron') {
  866:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
  867:         } elsif ($action eq 'login') {
  868:             if ($numheaders == 4) {
  869:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  870:            </table>
  871:           </td>
  872:          </tr>
  873:          <tr>
  874:            <td>
  875:             <table class="LC_nested">
  876:              <tr class="LC_info_row">
  877:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  878:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  879:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  880:                 $rowtotal ++;
  881:             } else {
  882:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  883:             }
  884:             $output .= '
  885:            </table>
  886:           </td>
  887:          </tr>
  888:          <tr>
  889:            <td>
  890:             <table class="LC_nested">
  891:              <tr class="LC_info_row">';
  892:             if ($numheaders == 4) {
  893:                 $output .= '
  894:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  895:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  896:              </tr>';
  897:             } else {
  898:                 $output .= '
  899:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  900:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  901:              </tr>';
  902:             }
  903:             $rowtotal ++;
  904:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  905:         } elsif ($action eq 'requestcourses') {
  906:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  907:             $rowtotal ++;
  908:             $output .= &print_studentcode($settings,\$rowtotal).'
  909:            </table>
  910:           </td>
  911:          </tr>
  912:          <tr>
  913:            <td>
  914:             <table class="LC_nested">
  915:              <tr class="LC_info_row">
  916:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  917:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  918:                        &textbookcourses_javascript($settings).
  919:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  920:             </table>
  921:            </td>
  922:           </tr>
  923:          <tr>
  924:            <td>
  925:             <table class="LC_nested">
  926:              <tr class="LC_info_row">
  927:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  928:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  929:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  930:             </table>
  931:            </td>
  932:           </tr>
  933:           <tr>
  934:            <td>
  935:             <table class="LC_nested">
  936:              <tr class="LC_info_row">
  937:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  938:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  939:              </tr>'.
  940:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  941:         } elsif ($action eq 'requestauthor') {
  942:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  943:             $rowtotal ++;
  944:         } elsif ($action eq 'rolecolors') {
  945:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  946:            </table>
  947:           </td>
  948:          </tr>
  949:          <tr>
  950:            <td>
  951:             <table class="LC_nested">
  952:              <tr class="LC_info_row">
  953:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  954:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  955:               <td class="LC_right_item" valign="top">'.
  956:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  957:              </tr>'.
  958:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  959:            </table>
  960:           </td>
  961:          </tr>
  962:          <tr>
  963:            <td>
  964:             <table class="LC_nested">
  965:              <tr class="LC_info_row">
  966:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  967:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  968:              </tr>'.
  969:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  970:             $rowtotal += 2;
  971:         }
  972:     } else {
  973:         $output .= '
  974:           <tr>
  975:            <td>
  976:             <table class="LC_nested">
  977:              <tr class="LC_info_row">';
  978:         if ($action eq 'login') {
  979:             $output .= '  
  980:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  981:         } elsif ($action eq 'serverstatuses') {
  982:             $output .= '
  983:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  984:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  985: 
  986:         } else {
  987:             $output .= '
  988:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  989:         }
  990:         if (defined($item->{'header'}->[0]->{'col3'})) {
  991:             $output .= '<td class="LC_left_item" valign="top">'.
  992:                        &mt($item->{'header'}->[0]->{'col2'});
  993:             if ($action eq 'serverstatuses') {
  994:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  995:             } 
  996:         } else {
  997:             $output .= '<td class="LC_right_item" valign="top">'.
  998:                        &mt($item->{'header'}->[0]->{'col2'});
  999:         }
 1000:         $output .= '</td>';
 1001:         if ($item->{'header'}->[0]->{'col3'}) {
 1002:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1003:                 $output .= '<td class="LC_left_item" valign="top">'.
 1004:                             &mt($item->{'header'}->[0]->{'col3'});
 1005:             } else {
 1006:                 $output .= '<td class="LC_right_item" valign="top">'.
 1007:                            &mt($item->{'header'}->[0]->{'col3'});
 1008:             }
 1009:             if ($action eq 'serverstatuses') {
 1010:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1011:             }
 1012:             $output .= '</td>';
 1013:         }
 1014:         if ($item->{'header'}->[0]->{'col4'}) {
 1015:             $output .= '<td class="LC_right_item" valign="top">'.
 1016:                        &mt($item->{'header'}->[0]->{'col4'});
 1017:         }
 1018:         $output .= '</tr>';
 1019:         $rowtotal ++;
 1020:         if ($action eq 'quotas') {
 1021:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1022:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1023:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') ||
 1024:                  ($action eq 'ltitools')) {
 1025:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1026:         }
 1027:     }
 1028:     $output .= '
 1029:    </table>
 1030:   </td>
 1031:  </tr>
 1032: </table><br />';
 1033:     return ($output,$rowtotal);
 1034: }
 1035: 
 1036: sub print_login {
 1037:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1038:     my ($css_class,$datatable);
 1039:     my %choices = &login_choices();
 1040: 
 1041:     if ($caller eq 'service') {
 1042:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1043:         my $choice = $choices{'disallowlogin'};
 1044:         $css_class = ' class="LC_odd_row"';
 1045:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1046:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1047:                       '<th>'.$choices{'server'}.'</th>'.
 1048:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1049:                       '<th>'.$choices{'custompath'}.'</th>'.
 1050:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1051:         my %disallowed;
 1052:         if (ref($settings) eq 'HASH') {
 1053:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1054:                %disallowed = %{$settings->{'loginvia'}};
 1055:             }
 1056:         }
 1057:         foreach my $lonhost (sort(keys(%servers))) {
 1058:             my $direct = 'selected="selected"';
 1059:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1060:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1061:                     $direct = '';
 1062:                 }
 1063:             }
 1064:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1065:                           '<td><select name="'.$lonhost.'_server">'.
 1066:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1067:                           '</option>';
 1068:             foreach my $hostid (sort(keys(%servers))) {
 1069:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1070:                 my $selected = '';
 1071:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1072:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1073:                         $selected = 'selected="selected"';
 1074:                     }
 1075:                 }
 1076:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1077:                               $servers{$hostid}.'</option>';
 1078:             }
 1079:             $datatable .= '</select></td>'.
 1080:                           '<td><select name="'.$lonhost.'_serverpath">';
 1081:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1082:                 my $pathname = $path;
 1083:                 if ($path eq 'custom') {
 1084:                     $pathname = &mt('Custom Path').' ->';
 1085:                 }
 1086:                 my $selected = '';
 1087:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1088:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1089:                         $selected = 'selected="selected"';
 1090:                     }
 1091:                 } elsif ($path eq '') {
 1092:                     $selected = 'selected="selected"';
 1093:                 }
 1094:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1095:             }
 1096:             $datatable .= '</select></td>';
 1097:             my ($custom,$exempt);
 1098:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1099:                 $custom = $disallowed{$lonhost}{'custompath'};
 1100:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1101:             }
 1102:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1103:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1104:                           '</tr>';
 1105:         }
 1106:         $datatable .= '</table></td></tr>';
 1107:         return $datatable;
 1108:     } elsif ($caller eq 'page') {
 1109:         my %defaultchecked = ( 
 1110:                                'coursecatalog' => 'on',
 1111:                                'helpdesk'      => 'on',
 1112:                                'adminmail'     => 'off',
 1113:                                'newuser'       => 'off',
 1114:                              );
 1115:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1116:         my (%checkedon,%checkedoff);
 1117:         foreach my $item (@toggles) {
 1118:             if ($defaultchecked{$item} eq 'on') { 
 1119:                 $checkedon{$item} = ' checked="checked" ';
 1120:                 $checkedoff{$item} = ' ';
 1121:             } elsif ($defaultchecked{$item} eq 'off') {
 1122:                 $checkedoff{$item} = ' checked="checked" ';
 1123:                 $checkedon{$item} = ' ';
 1124:             }
 1125:         }
 1126:         my @images = ('img','logo','domlogo','login');
 1127:         my @logintext = ('textcol','bgcol');
 1128:         my @bgs = ('pgbg','mainbg','sidebg');
 1129:         my @links = ('link','alink','vlink');
 1130:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1131:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1132:         my (%is_custom,%designs);
 1133:         my %defaults = (
 1134:                        font => $defaultdesign{'login.font'},
 1135:                        );
 1136:         foreach my $item (@images) {
 1137:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1138:             $defaults{'showlogo'}{$item} = 1;
 1139:         }
 1140:         foreach my $item (@bgs) {
 1141:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1142:         }
 1143:         foreach my $item (@logintext) {
 1144:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1145:         }
 1146:         foreach my $item (@links) {
 1147:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1148:         }
 1149:         if (ref($settings) eq 'HASH') {
 1150:             foreach my $item (@toggles) {
 1151:                 if ($settings->{$item} eq '1') {
 1152:                     $checkedon{$item} =  ' checked="checked" ';
 1153:                     $checkedoff{$item} = ' ';
 1154:                 } elsif ($settings->{$item} eq '0') {
 1155:                     $checkedoff{$item} =  ' checked="checked" ';
 1156:                     $checkedon{$item} = ' ';
 1157:                 }
 1158:             }
 1159:             foreach my $item (@images) {
 1160:                 if (defined($settings->{$item})) {
 1161:                     $designs{$item} = $settings->{$item};
 1162:                     $is_custom{$item} = 1;
 1163:                 }
 1164:                 if (defined($settings->{'showlogo'}{$item})) {
 1165:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1166:                 }
 1167:             }
 1168:             foreach my $item (@logintext) {
 1169:                 if ($settings->{$item} ne '') {
 1170:                     $designs{'logintext'}{$item} = $settings->{$item};
 1171:                     $is_custom{$item} = 1;
 1172:                 }
 1173:             }
 1174:             if ($settings->{'font'} ne '') {
 1175:                 $designs{'font'} = $settings->{'font'};
 1176:                 $is_custom{'font'} = 1;
 1177:             }
 1178:             foreach my $item (@bgs) {
 1179:                 if ($settings->{$item} ne '') {
 1180:                     $designs{'bgs'}{$item} = $settings->{$item};
 1181:                     $is_custom{$item} = 1;
 1182:                 }
 1183:             }
 1184:             foreach my $item (@links) {
 1185:                 if ($settings->{$item} ne '') {
 1186:                     $designs{'links'}{$item} = $settings->{$item};
 1187:                     $is_custom{$item} = 1;
 1188:                 }
 1189:             }
 1190:         } else {
 1191:             if ($designhash{$dom.'.login.font'} ne '') {
 1192:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1193:                 $is_custom{'font'} = 1;
 1194:             }
 1195:             foreach my $item (@images) {
 1196:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1197:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1198:                     $is_custom{$item} = 1;
 1199:                 }
 1200:             }
 1201:             foreach my $item (@bgs) {
 1202:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1203:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1204:                     $is_custom{$item} = 1;
 1205:                 }
 1206:             }
 1207:             foreach my $item (@links) {
 1208:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1209:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1210:                     $is_custom{$item} = 1;
 1211:                 }
 1212:             }
 1213:         }
 1214:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1215:                                                       logo => 'Institution Logo',
 1216:                                                       domlogo => 'Domain Logo',
 1217:                                                       login => 'Login box');
 1218:         my $itemcount = 1;
 1219:         foreach my $item (@toggles) {
 1220:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1221:             $datatable .=  
 1222:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1223:                 '</td><td>'.
 1224:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1225:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1226:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1227:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1228:                 '</tr>';
 1229:             $itemcount ++;
 1230:         }
 1231:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1232:         $datatable .= '</tr></table></td></tr>';
 1233:     } elsif ($caller eq 'help') {
 1234:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1235:         my $switchserver = &check_switchserver($dom,$confname);
 1236:         my $itemcount = 1;
 1237:         $defaulturl = '/adm/loginproblems.html';
 1238:         $defaulttype = 'default';
 1239:         %lt = &Apache::lonlocal::texthash (
 1240:                      del     => 'Delete?',
 1241:                      rep     => 'Replace:',
 1242:                      upl     => 'Upload:',
 1243:                      default => 'Default',
 1244:                      custom  => 'Custom',
 1245:                                              );
 1246:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1247:         my @currlangs;
 1248:         if (ref($settings) eq 'HASH') {
 1249:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1250:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1251:                     next if ($settings->{'helpurl'}{$key} eq '');
 1252:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1253:                     $type{$key} = 'custom';
 1254:                     unless ($key eq 'nolang') {
 1255:                         push(@currlangs,$key);
 1256:                     }
 1257:                 }
 1258:             } elsif ($settings->{'helpurl'} ne '') {
 1259:                 $type{'nolang'} = 'custom';
 1260:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1261:             }
 1262:         }
 1263:         foreach my $lang ('nolang',sort(@currlangs)) {
 1264:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1265:             $datatable .= '<tr'.$css_class.'>';
 1266:             if ($url{$lang} eq '') {
 1267:                 $url{$lang} = $defaulturl;
 1268:             }
 1269:             if ($type{$lang} eq '') {
 1270:                 $type{$lang} = $defaulttype;
 1271:             }
 1272:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1273:             if ($lang eq 'nolang') {
 1274:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1275:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1276:             } else {
 1277:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1278:                                   $langchoices{$lang},
 1279:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1280:             }
 1281:             $datatable .= '</span></td>'."\n".
 1282:                           '<td class="LC_left_item">';
 1283:             if ($type{$lang} eq 'custom') {
 1284:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1285:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1286:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1287:             } else {
 1288:                 $datatable .= $lt{'upl'};
 1289:             }
 1290:             $datatable .='<br />';
 1291:             if ($switchserver) {
 1292:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1293:             } else {
 1294:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1295:             }
 1296:             $datatable .= '</td></tr>';
 1297:             $itemcount ++;
 1298:         }
 1299:         my @addlangs;
 1300:         foreach my $lang (sort(keys(%langchoices))) {
 1301:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1302:             push(@addlangs,$lang);
 1303:         }
 1304:         if (@addlangs > 0) {
 1305:             my %toadd;
 1306:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1307:             $toadd{''} = &mt('Select');
 1308:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1309:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1310:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1311:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1312:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1313:             if ($switchserver) {
 1314:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1315:             } else {
 1316:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1317:             }
 1318:             $datatable .= '</td></tr>';
 1319:             $itemcount ++;
 1320:         }
 1321:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1322:     } elsif ($caller eq 'headtag') {
 1323:         my %domservers = &Apache::lonnet::get_servers($dom);
 1324:         my $choice = $choices{'headtag'};
 1325:         $css_class = ' class="LC_odd_row"';
 1326:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1327:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1328:                       '<th>'.$choices{'current'}.'</th>'.
 1329:                       '<th>'.$choices{'action'}.'</th>'.
 1330:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1331:         my (%currurls,%currexempt);
 1332:         if (ref($settings) eq 'HASH') {
 1333:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1334:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1335:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1336:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1337:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1338:                     }
 1339:                 }
 1340:             }
 1341:         }
 1342:         my %lt = &Apache::lonlocal::texthash(
 1343:                                                del  => 'Delete?',
 1344:                                                rep  => 'Replace:',
 1345:                                                upl  => 'Upload:',
 1346:                                                curr => 'View contents',
 1347:                                                none => 'None',
 1348:         );
 1349:         my $switchserver = &check_switchserver($dom,$confname);
 1350:         foreach my $lonhost (sort(keys(%domservers))) {
 1351:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1352:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1353:             if ($currurls{$lonhost}) {
 1354:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1355:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1356:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1357:                               '">'.$lt{'curr'}.'</a></td>'.
 1358:                               '<td><span class="LC_nobreak"><label>'.
 1359:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1360:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1361:             } else {
 1362:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1363:             }
 1364:             $datatable .='<br />';
 1365:             if ($switchserver) {
 1366:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1367:             } else {
 1368:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1369:             }
 1370:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1371:         }
 1372:         $datatable .= '</table></td></tr>';
 1373:     }
 1374:     return $datatable;
 1375: }
 1376: 
 1377: sub login_choices {
 1378:     my %choices =
 1379:         &Apache::lonlocal::texthash (
 1380:             coursecatalog => 'Display Course/Community Catalog link?',
 1381:             adminmail     => "Display Administrator's E-mail Address?",
 1382:             helpdesk      => 'Display "Contact Helpdesk" link',
 1383:             disallowlogin => "Login page requests redirected",
 1384:             hostid        => "Server",
 1385:             server        => "Redirect to:",
 1386:             serverpath    => "Path",
 1387:             custompath    => "Custom", 
 1388:             exempt        => "Exempt IP(s)",
 1389:             directlogin   => "No redirect",
 1390:             newuser       => "Link to create a user account",
 1391:             img           => "Header",
 1392:             logo          => "Main Logo",
 1393:             domlogo       => "Domain Logo",
 1394:             login         => "Log-in Header", 
 1395:             textcol       => "Text color",
 1396:             bgcol         => "Box color",
 1397:             bgs           => "Background colors",
 1398:             links         => "Link colors",
 1399:             font          => "Font color",
 1400:             pgbg          => "Header",
 1401:             mainbg        => "Page",
 1402:             sidebg        => "Login box",
 1403:             link          => "Link",
 1404:             alink         => "Active link",
 1405:             vlink         => "Visited link",
 1406:             headtag       => "Custom markup",
 1407:             action        => "Action",
 1408:             current       => "Current",
 1409:         );
 1410:     return %choices;
 1411: }
 1412: 
 1413: sub print_rolecolors {
 1414:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1415:     my %choices = &color_font_choices();
 1416:     my @bgs = ('pgbg','tabbg','sidebg');
 1417:     my @links = ('link','alink','vlink');
 1418:     my @images = ('img');
 1419:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1420:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1421:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1422:     my (%is_custom,%designs);
 1423:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1424:     if (ref($settings) eq 'HASH') {
 1425:         if (ref($settings->{$role}) eq 'HASH') {
 1426:             if ($settings->{$role}->{'img'} ne '') {
 1427:                 $designs{'img'} = $settings->{$role}->{'img'};
 1428:                 $is_custom{'img'} = 1;
 1429:             }
 1430:             if ($settings->{$role}->{'font'} ne '') {
 1431:                 $designs{'font'} = $settings->{$role}->{'font'};
 1432:                 $is_custom{'font'} = 1;
 1433:             }
 1434:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1435:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1436:                 $is_custom{'fontmenu'} = 1;
 1437:             }
 1438:             foreach my $item (@bgs) {
 1439:                 if ($settings->{$role}->{$item} ne '') {
 1440:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1441:                     $is_custom{$item} = 1;
 1442:                 }
 1443:             }
 1444:             foreach my $item (@links) {
 1445:                 if ($settings->{$role}->{$item} ne '') {
 1446:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1447:                     $is_custom{$item} = 1;
 1448:                 }
 1449:             }
 1450:         }
 1451:     } else {
 1452:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1453:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1454:             $is_custom{'img'} = 1;
 1455:         }
 1456:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1457:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1458:             $is_custom{'fontmenu'} = 1; 
 1459:         }
 1460:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1461:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1462:             $is_custom{'font'} = 1;
 1463:         }
 1464:         foreach my $item (@bgs) {
 1465:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1466:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1467:                 $is_custom{$item} = 1;
 1468:             
 1469:             }
 1470:         }
 1471:         foreach my $item (@links) {
 1472:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1473:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1474:                 $is_custom{$item} = 1;
 1475:             }
 1476:         }
 1477:     }
 1478:     my $itemcount = 1;
 1479:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1480:     $datatable .= '</tr></table></td></tr>';
 1481:     return $datatable;
 1482: }
 1483: 
 1484: sub role_defaults {
 1485:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1486:     my %defaults;
 1487:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1488:         return %defaults;
 1489:     }
 1490:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1491:     if ($role eq 'login') {
 1492:         %defaults = (
 1493:                        font => $defaultdesign{$role.'.font'},
 1494:                     );
 1495:         if (ref($logintext) eq 'ARRAY') {
 1496:             foreach my $item (@{$logintext}) {
 1497:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1498:             }
 1499:         }
 1500:         foreach my $item (@{$images}) {
 1501:             $defaults{'showlogo'}{$item} = 1;
 1502:         }
 1503:     } else {
 1504:         %defaults = (
 1505:                        img => $defaultdesign{$role.'.img'},
 1506:                        font => $defaultdesign{$role.'.font'},
 1507:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1508:                     );
 1509:     }
 1510:     foreach my $item (@{$bgs}) {
 1511:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1512:     }
 1513:     foreach my $item (@{$links}) {
 1514:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1515:     }
 1516:     foreach my $item (@{$images}) {
 1517:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1518:     }
 1519:     return %defaults;
 1520: }
 1521: 
 1522: sub display_color_options {
 1523:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1524:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1525:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1526:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1527:     my $datatable = '<tr'.$css_class.'>'.
 1528:         '<td>'.$choices->{'font'}.'</td>';
 1529:     if (!$is_custom->{'font'}) {
 1530:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1531:     } else {
 1532:         $datatable .= '<td>&nbsp;</td>';
 1533:     }
 1534:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1535: 
 1536:     $datatable .= '<td><span class="LC_nobreak">'.
 1537:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1538:                   ' value="'.$current_color.'" />&nbsp;'.
 1539:                   '&nbsp;</span></td></tr>';
 1540:     unless ($role eq 'login') { 
 1541:         $datatable .= '<tr'.$css_class.'>'.
 1542:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1543:         if (!$is_custom->{'fontmenu'}) {
 1544:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1545:         } else {
 1546:             $datatable .= '<td>&nbsp;</td>';
 1547:         }
 1548: 	$current_color = $designs->{'fontmenu'} ?
 1549: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1550:         $datatable .= '<td><span class="LC_nobreak">'.
 1551:                       '<input class="colorchooser" type="text" size="10" name="'
 1552: 		      .$role.'_fontmenu"'.
 1553:                       ' value="'.$current_color.'" />&nbsp;'.
 1554:                       '&nbsp;</span></td></tr>';
 1555:     }
 1556:     my $switchserver = &check_switchserver($dom,$confname);
 1557:     foreach my $img (@{$images}) {
 1558: 	$itemcount ++;
 1559:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1560:         $datatable .= '<tr'.$css_class.'>'.
 1561:                       '<td>'.$choices->{$img};
 1562:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1563:         if ($role eq 'login') {
 1564:             if ($img eq 'login') {
 1565:                 $login_hdr_pick =
 1566:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1567:                 $logincolors =
 1568:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1569:                                        $designs,$defaults);
 1570:             } elsif ($img ne 'domlogo') {
 1571:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1572:             }
 1573:         }
 1574:         $datatable .= '</td>';
 1575:         if ($designs->{$img} ne '') {
 1576:             $imgfile = $designs->{$img};
 1577: 	    $img_import = ($imgfile =~ m{^/adm/});
 1578:         } else {
 1579:             $imgfile = $defaults->{$img};
 1580:         }
 1581:         if ($imgfile) {
 1582:             my ($showfile,$fullsize);
 1583:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1584:                 my $urldir = $1;
 1585:                 my $filename = $2;
 1586:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1587:                 if (@info) {
 1588:                     my $thumbfile = 'tn-'.$filename;
 1589:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1590:                     if (@thumb) {
 1591:                         $showfile = $urldir.'/'.$thumbfile;
 1592:                     } else {
 1593:                         $showfile = $imgfile;
 1594:                     }
 1595:                 } else {
 1596:                     $showfile = '';
 1597:                 }
 1598:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1599:                 $showfile = $imgfile;
 1600:                 my $imgdir = $1;
 1601:                 my $filename = $2;
 1602:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1603:                     $showfile = "/$imgdir/tn-".$filename;
 1604:                 } else {
 1605:                     my $input = $londocroot.$imgfile;
 1606:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1607:                     if (!-e $output) {
 1608:                         my ($width,$height) = &thumb_dimensions();
 1609:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1610:                         if ($fullwidth ne '' && $fullheight ne '') {
 1611:                             if ($fullwidth > $width && $fullheight > $height) { 
 1612:                                 my $size = $width.'x'.$height;
 1613:                                 my @args = ('convert','-sample',$size,$input,$output);
 1614:                                 system({$args[0]} @args);
 1615:                                 $showfile = "/$imgdir/tn-".$filename;
 1616:                             }
 1617:                         }
 1618:                     }
 1619:                 }
 1620:             }
 1621:             if ($showfile) {
 1622:                 if ($showfile =~ m{^/(adm|res)/}) {
 1623:                     if ($showfile =~ m{^/res/}) {
 1624:                         my $local_showfile =
 1625:                             &Apache::lonnet::filelocation('',$showfile);
 1626:                         &Apache::lonnet::repcopy($local_showfile);
 1627:                     }
 1628:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1629:                 }
 1630:                 if ($imgfile) {
 1631:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1632:                         if ($imgfile =~ m{^/res/}) {
 1633:                             my $local_imgfile =
 1634:                                 &Apache::lonnet::filelocation('',$imgfile);
 1635:                             &Apache::lonnet::repcopy($local_imgfile);
 1636:                         }
 1637:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1638:                     } else {
 1639:                         $fullsize = $imgfile;
 1640:                     }
 1641:                 }
 1642:                 $datatable .= '<td>';
 1643:                 if ($img eq 'login') {
 1644:                     $datatable .= $login_hdr_pick;
 1645:                 } 
 1646:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1647:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1648:             } else {
 1649:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1650:                               &mt('Upload:').'<br />';
 1651:             }
 1652:         } else {
 1653:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1654:                           &mt('Upload:').'<br />';
 1655:         }
 1656:         if ($switchserver) {
 1657:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1658:         } else {
 1659:             if ($img ne 'login') { # suppress file selection for Log-in header
 1660:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1661:             }
 1662:         }
 1663:         $datatable .= '</td></tr>';
 1664:     }
 1665:     $itemcount ++;
 1666:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1667:     $datatable .= '<tr'.$css_class.'>'.
 1668:                   '<td>'.$choices->{'bgs'}.'</td>';
 1669:     my $bgs_def;
 1670:     foreach my $item (@{$bgs}) {
 1671:         if (!$is_custom->{$item}) {
 1672:             $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>';
 1673:         }
 1674:     }
 1675:     if ($bgs_def) {
 1676:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1677:     } else {
 1678:         $datatable .= '<td>&nbsp;</td>';
 1679:     }
 1680:     $datatable .= '<td class="LC_right_item">'.
 1681:                   '<table border="0"><tr>';
 1682: 
 1683:     foreach my $item (@{$bgs}) {
 1684:         $datatable .= '<td align="center">'.$choices->{$item};
 1685: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1686:         if ($designs->{'bgs'}{$item}) {
 1687:             $datatable .= '&nbsp;';
 1688:         }
 1689:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1690:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1691:     }
 1692:     $datatable .= '</tr></table></td></tr>';
 1693:     $itemcount ++;
 1694:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1695:     $datatable .= '<tr'.$css_class.'>'.
 1696:                   '<td>'.$choices->{'links'}.'</td>';
 1697:     my $links_def;
 1698:     foreach my $item (@{$links}) {
 1699:         if (!$is_custom->{$item}) {
 1700:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1701:         }
 1702:     }
 1703:     if ($links_def) {
 1704:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1705:     } else {
 1706:         $datatable .= '<td>&nbsp;</td>';
 1707:     }
 1708:     $datatable .= '<td class="LC_right_item">'.
 1709:                   '<table border="0"><tr>';
 1710:     foreach my $item (@{$links}) {
 1711: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1712:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1713:         if ($designs->{'links'}{$item}) {
 1714:             $datatable.='&nbsp;';
 1715:         }
 1716:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1717:                       '" /></td>';
 1718:     }
 1719:     $$rowtotal += $itemcount;
 1720:     return $datatable;
 1721: }
 1722: 
 1723: sub logo_display_options {
 1724:     my ($img,$defaults,$designs) = @_;
 1725:     my $checkedon;
 1726:     if (ref($defaults) eq 'HASH') {
 1727:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1728:             if ($defaults->{'showlogo'}{$img}) {
 1729:                 $checkedon = 'checked="checked" ';     
 1730:             }
 1731:         } 
 1732:     }
 1733:     if (ref($designs) eq 'HASH') {
 1734:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1735:             if (defined($designs->{'showlogo'}{$img})) {
 1736:                 if ($designs->{'showlogo'}{$img} == 0) {
 1737:                     $checkedon = '';
 1738:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1739:                     $checkedon = 'checked="checked" ';
 1740:                 }
 1741:             }
 1742:         }
 1743:     }
 1744:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1745:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1746:            &mt('show').'</label>'."\n";
 1747: }
 1748: 
 1749: sub login_header_options  {
 1750:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1751:     my $output = '';
 1752:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1753:         $output .= &mt('Text default(s):').'<br />';
 1754:         if (!$is_custom->{'textcol'}) {
 1755:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1756:                        '&nbsp;&nbsp;&nbsp;';
 1757:         }
 1758:         if (!$is_custom->{'bgcol'}) {
 1759:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1760:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1761:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1762:         }
 1763:         $output .= '<br />';
 1764:     }
 1765:     $output .='<br />';
 1766:     return $output;
 1767: }
 1768: 
 1769: sub login_text_colors {
 1770:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1771:     my $color_menu = '<table border="0"><tr>';
 1772:     foreach my $item (@{$logintext}) {
 1773:         $color_menu .= '<td align="center">'.$choices->{$item};
 1774:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1775:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1776:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1777:     }
 1778:     $color_menu .= '</tr></table><br />';
 1779:     return $color_menu;
 1780: }
 1781: 
 1782: sub image_changes {
 1783:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1784:     my $output;
 1785:     if ($img eq 'login') {
 1786:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1787:     } elsif (!$is_custom) {
 1788:         if ($img ne 'domlogo') {
 1789:             $output = &mt('Default image:').'<br />';
 1790:         } else {
 1791:             $output = &mt('Default in use:').'<br />';
 1792:         }
 1793:     }
 1794:     if ($img ne 'login') {
 1795:         if ($img_import) {
 1796:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1797:         }
 1798:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1799:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1800:         if ($is_custom) {
 1801:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1802:                        '<input type="checkbox" name="'.
 1803:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1804:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1805:         } else {
 1806:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1807:         }
 1808:     }
 1809:     return $output;
 1810: }
 1811: 
 1812: sub print_quotas {
 1813:     my ($dom,$settings,$rowtotal,$action) = @_;
 1814:     my $context;
 1815:     if ($action eq 'quotas') {
 1816:         $context = 'tools';
 1817:     } else {
 1818:         $context = $action;
 1819:     }
 1820:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1821:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1822:     my $typecount = 0;
 1823:     my ($css_class,%titles);
 1824:     if ($context eq 'requestcourses') {
 1825:         @usertools = ('official','unofficial','community','textbook');
 1826:         @options =('norequest','approval','validate','autolimit');
 1827:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1828:         %titles = &courserequest_titles();
 1829:     } elsif ($context eq 'requestauthor') {
 1830:         @usertools = ('author');
 1831:         @options = ('norequest','approval','automatic');
 1832:         %titles = &authorrequest_titles();
 1833:     } else {
 1834:         @usertools = ('aboutme','blog','webdav','portfolio');
 1835:         %titles = &tool_titles();
 1836:     }
 1837:     if (ref($types) eq 'ARRAY') {
 1838:         foreach my $type (@{$types}) {
 1839:             my ($currdefquota,$currauthorquota);
 1840:             unless (($context eq 'requestcourses') ||
 1841:                     ($context eq 'requestauthor')) {
 1842:                 if (ref($settings) eq 'HASH') {
 1843:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1844:                         $currdefquota = $settings->{defaultquota}->{$type};
 1845:                     } else {
 1846:                         $currdefquota = $settings->{$type};
 1847:                     }
 1848:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1849:                         $currauthorquota = $settings->{authorquota}->{$type};
 1850:                     }
 1851:                 }
 1852:             }
 1853:             if (defined($usertypes->{$type})) {
 1854:                 $typecount ++;
 1855:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1856:                 $datatable .= '<tr'.$css_class.'>'.
 1857:                               '<td>'.$usertypes->{$type}.'</td>'.
 1858:                               '<td class="LC_left_item">';
 1859:                 if ($context eq 'requestcourses') {
 1860:                     $datatable .= '<table><tr>';
 1861:                 }
 1862:                 my %cell;  
 1863:                 foreach my $item (@usertools) {
 1864:                     if ($context eq 'requestcourses') {
 1865:                         my ($curroption,$currlimit);
 1866:                         if (ref($settings) eq 'HASH') {
 1867:                             if (ref($settings->{$item}) eq 'HASH') {
 1868:                                 $curroption = $settings->{$item}->{$type};
 1869:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1870:                                     $currlimit = $1; 
 1871:                                 }
 1872:                             }
 1873:                         }
 1874:                         if (!$curroption) {
 1875:                             $curroption = 'norequest';
 1876:                         }
 1877:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1878:                         foreach my $option (@options) {
 1879:                             my $val = $option;
 1880:                             if ($option eq 'norequest') {
 1881:                                 $val = 0;  
 1882:                             }
 1883:                             if ($option eq 'validate') {
 1884:                                 my $canvalidate = 0;
 1885:                                 if (ref($validations{$item}) eq 'HASH') { 
 1886:                                     if ($validations{$item}{$type}) {
 1887:                                         $canvalidate = 1;
 1888:                                     }
 1889:                                 }
 1890:                                 next if (!$canvalidate);
 1891:                             }
 1892:                             my $checked = '';
 1893:                             if ($option eq $curroption) {
 1894:                                 $checked = ' checked="checked"';
 1895:                             } elsif ($option eq 'autolimit') {
 1896:                                 if ($curroption =~ /^autolimit/) {
 1897:                                     $checked = ' checked="checked"';
 1898:                                 }                       
 1899:                             } 
 1900:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1901:                                   '<input type="radio" name="crsreq_'.$item.
 1902:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1903:                                   $titles{$option}.'</label>';
 1904:                             if ($option eq 'autolimit') {
 1905:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1906:                                                 $item.'_limit_'.$type.'" size="1" '.
 1907:                                                 'value="'.$currlimit.'" />';
 1908:                             }
 1909:                             $cell{$item} .= '</span> ';
 1910:                             if ($option eq 'autolimit') {
 1911:                                 $cell{$item} .= $titles{'unlimited'};
 1912:                             }
 1913:                         }
 1914:                     } elsif ($context eq 'requestauthor') {
 1915:                         my $curroption;
 1916:                         if (ref($settings) eq 'HASH') {
 1917:                             $curroption = $settings->{$type};
 1918:                         }
 1919:                         if (!$curroption) {
 1920:                             $curroption = 'norequest';
 1921:                         }
 1922:                         foreach my $option (@options) {
 1923:                             my $val = $option;
 1924:                             if ($option eq 'norequest') {
 1925:                                 $val = 0;
 1926:                             }
 1927:                             my $checked = '';
 1928:                             if ($option eq $curroption) {
 1929:                                 $checked = ' checked="checked"';
 1930:                             }
 1931:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1932:                                   '<input type="radio" name="authorreq_'.$type.
 1933:                                   '" value="'.$val.'"'.$checked.' />'.
 1934:                                   $titles{$option}.'</label></span>&nbsp; ';
 1935:                         }
 1936:                     } else {
 1937:                         my $checked = 'checked="checked" ';
 1938:                         if (ref($settings) eq 'HASH') {
 1939:                             if (ref($settings->{$item}) eq 'HASH') {
 1940:                                 if ($settings->{$item}->{$type} == 0) {
 1941:                                     $checked = '';
 1942:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1943:                                     $checked =  'checked="checked" ';
 1944:                                 }
 1945:                             }
 1946:                         }
 1947:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1948:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1949:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1950:                                       '</label></span>&nbsp; ';
 1951:                     }
 1952:                 }
 1953:                 if ($context eq 'requestcourses') {
 1954:                     $datatable .= '</tr><tr>';
 1955:                     foreach my $item (@usertools) {
 1956:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1957:                     }
 1958:                     $datatable .= '</tr></table>';
 1959:                 }
 1960:                 $datatable .= '</td>';
 1961:                 unless (($context eq 'requestcourses') ||
 1962:                         ($context eq 'requestauthor')) {
 1963:                     $datatable .= 
 1964:                               '<td class="LC_right_item">'.
 1965:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1966:                               '<input type="text" name="quota_'.$type.
 1967:                               '" value="'.$currdefquota.
 1968:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1969:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1970:                               '<input type="text" name="authorquota_'.$type.
 1971:                               '" value="'.$currauthorquota.
 1972:                               '" size="5" /></span></td>';
 1973:                 }
 1974:                 $datatable .= '</tr>';
 1975:             }
 1976:         }
 1977:     }
 1978:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1979:         $defaultquota = '20';
 1980:         $authorquota = '500';
 1981:         if (ref($settings) eq 'HASH') {
 1982:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1983:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1984:             } elsif (defined($settings->{'default'})) {
 1985:                 $defaultquota = $settings->{'default'};
 1986:             }
 1987:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1988:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1989:             }
 1990:         }
 1991:     }
 1992:     $typecount ++;
 1993:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1994:     $datatable .= '<tr'.$css_class.'>'.
 1995:                   '<td>'.$othertitle.'</td>'.
 1996:                   '<td class="LC_left_item">';
 1997:     if ($context eq 'requestcourses') {
 1998:         $datatable .= '<table><tr>';
 1999:     }
 2000:     my %defcell;
 2001:     foreach my $item (@usertools) {
 2002:         if ($context eq 'requestcourses') {
 2003:             my ($curroption,$currlimit);
 2004:             if (ref($settings) eq 'HASH') {
 2005:                 if (ref($settings->{$item}) eq 'HASH') {
 2006:                     $curroption = $settings->{$item}->{'default'};
 2007:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2008:                         $currlimit = $1;
 2009:                     }
 2010:                 }
 2011:             }
 2012:             if (!$curroption) {
 2013:                 $curroption = 'norequest';
 2014:             }
 2015:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2016:             foreach my $option (@options) {
 2017:                 my $val = $option;
 2018:                 if ($option eq 'norequest') {
 2019:                     $val = 0;
 2020:                 }
 2021:                 if ($option eq 'validate') {
 2022:                     my $canvalidate = 0;
 2023:                     if (ref($validations{$item}) eq 'HASH') {
 2024:                         if ($validations{$item}{'default'}) {
 2025:                             $canvalidate = 1;
 2026:                         }
 2027:                     }
 2028:                     next if (!$canvalidate);
 2029:                 }
 2030:                 my $checked = '';
 2031:                 if ($option eq $curroption) {
 2032:                     $checked = ' checked="checked"';
 2033:                 } elsif ($option eq 'autolimit') {
 2034:                     if ($curroption =~ /^autolimit/) {
 2035:                         $checked = ' checked="checked"';
 2036:                     }
 2037:                 }
 2038:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2039:                                   '<input type="radio" name="crsreq_'.$item.
 2040:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2041:                                   $titles{$option}.'</label>';
 2042:                 if ($option eq 'autolimit') {
 2043:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2044:                                        $item.'_limit_default" size="1" '.
 2045:                                        'value="'.$currlimit.'" />';
 2046:                 }
 2047:                 $defcell{$item} .= '</span> ';
 2048:                 if ($option eq 'autolimit') {
 2049:                     $defcell{$item} .= $titles{'unlimited'};
 2050:                 }
 2051:             }
 2052:         } elsif ($context eq 'requestauthor') {
 2053:             my $curroption;
 2054:             if (ref($settings) eq 'HASH') {
 2055:                 $curroption = $settings->{'default'};
 2056:             }
 2057:             if (!$curroption) {
 2058:                 $curroption = 'norequest';
 2059:             }
 2060:             foreach my $option (@options) {
 2061:                 my $val = $option;
 2062:                 if ($option eq 'norequest') {
 2063:                     $val = 0;
 2064:                 }
 2065:                 my $checked = '';
 2066:                 if ($option eq $curroption) {
 2067:                     $checked = ' checked="checked"';
 2068:                 }
 2069:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2070:                               '<input type="radio" name="authorreq_default"'.
 2071:                               ' value="'.$val.'"'.$checked.' />'.
 2072:                               $titles{$option}.'</label></span>&nbsp; ';
 2073:             }
 2074:         } else {
 2075:             my $checked = 'checked="checked" ';
 2076:             if (ref($settings) eq 'HASH') {
 2077:                 if (ref($settings->{$item}) eq 'HASH') {
 2078:                     if ($settings->{$item}->{'default'} == 0) {
 2079:                         $checked = '';
 2080:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2081:                         $checked = 'checked="checked" ';
 2082:                     }
 2083:                 }
 2084:             }
 2085:             $datatable .= '<span class="LC_nobreak"><label>'.
 2086:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2087:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2088:                           '</label></span>&nbsp; ';
 2089:         }
 2090:     }
 2091:     if ($context eq 'requestcourses') {
 2092:         $datatable .= '</tr><tr>';
 2093:         foreach my $item (@usertools) {
 2094:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2095:         }
 2096:         $datatable .= '</tr></table>';
 2097:     }
 2098:     $datatable .= '</td>';
 2099:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2100:         $datatable .= '<td class="LC_right_item">'.
 2101:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2102:                       '<input type="text" name="defaultquota" value="'.
 2103:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2104:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2105:                       '<input type="text" name="authorquota" value="'.
 2106:                       $authorquota.'" size="5" /></span></td>';
 2107:     }
 2108:     $datatable .= '</tr>';
 2109:     $typecount ++;
 2110:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2111:     $datatable .= '<tr'.$css_class.'>'.
 2112:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2113:     if ($context eq 'requestcourses') {
 2114:         $datatable .= &mt('(overrides affiliation, if set)').
 2115:                       '</td>'.
 2116:                       '<td class="LC_left_item">'.
 2117:                       '<table><tr>';
 2118:     } else {
 2119:         $datatable .= &mt('(overrides affiliation, if checked)').
 2120:                       '</td>'.
 2121:                       '<td class="LC_left_item" colspan="2">'.
 2122:                       '<br />';
 2123:     }
 2124:     my %advcell;
 2125:     foreach my $item (@usertools) {
 2126:         if ($context eq 'requestcourses') {
 2127:             my ($curroption,$currlimit);
 2128:             if (ref($settings) eq 'HASH') {
 2129:                 if (ref($settings->{$item}) eq 'HASH') {
 2130:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2131:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2132:                         $currlimit = $1;
 2133:                     }
 2134:                 }
 2135:             }
 2136:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2137:             my $checked = '';
 2138:             if ($curroption eq '') {
 2139:                 $checked = ' checked="checked"';
 2140:             }
 2141:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2142:                                '<input type="radio" name="crsreq_'.$item.
 2143:                                '__LC_adv" value=""'.$checked.' />'.
 2144:                                &mt('No override set').'</label></span>&nbsp; ';
 2145:             foreach my $option (@options) {
 2146:                 my $val = $option;
 2147:                 if ($option eq 'norequest') {
 2148:                     $val = 0;
 2149:                 }
 2150:                 if ($option eq 'validate') {
 2151:                     my $canvalidate = 0;
 2152:                     if (ref($validations{$item}) eq 'HASH') {
 2153:                         if ($validations{$item}{'_LC_adv'}) {
 2154:                             $canvalidate = 1;
 2155:                         }
 2156:                     }
 2157:                     next if (!$canvalidate);
 2158:                 }
 2159:                 my $checked = '';
 2160:                 if ($val eq $curroption) {
 2161:                     $checked = ' checked="checked"';
 2162:                 } elsif ($option eq 'autolimit') {
 2163:                     if ($curroption =~ /^autolimit/) {
 2164:                         $checked = ' checked="checked"';
 2165:                     }
 2166:                 }
 2167:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2168:                                   '<input type="radio" name="crsreq_'.$item.
 2169:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2170:                                   $titles{$option}.'</label>';
 2171:                 if ($option eq 'autolimit') {
 2172:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2173:                                        $item.'_limit__LC_adv" size="1" '.
 2174:                                        'value="'.$currlimit.'" />';
 2175:                 }
 2176:                 $advcell{$item} .= '</span> ';
 2177:                 if ($option eq 'autolimit') {
 2178:                     $advcell{$item} .= $titles{'unlimited'};
 2179:                 }
 2180:             }
 2181:         } elsif ($context eq 'requestauthor') {
 2182:             my $curroption;
 2183:             if (ref($settings) eq 'HASH') {
 2184:                 $curroption = $settings->{'_LC_adv'};
 2185:             }
 2186:             my $checked = '';
 2187:             if ($curroption eq '') {
 2188:                 $checked = ' checked="checked"';
 2189:             }
 2190:             $datatable .= '<span class="LC_nobreak"><label>'.
 2191:                           '<input type="radio" name="authorreq__LC_adv"'.
 2192:                           ' value=""'.$checked.' />'.
 2193:                           &mt('No override set').'</label></span>&nbsp; ';
 2194:             foreach my $option (@options) {
 2195:                 my $val = $option;
 2196:                 if ($option eq 'norequest') {
 2197:                     $val = 0;
 2198:                 }
 2199:                 my $checked = '';
 2200:                 if ($val eq $curroption) {
 2201:                     $checked = ' checked="checked"';
 2202:                 }
 2203:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2204:                               '<input type="radio" name="authorreq__LC_adv"'.
 2205:                               ' value="'.$val.'"'.$checked.' />'.
 2206:                               $titles{$option}.'</label></span>&nbsp; ';
 2207:             }
 2208:         } else {
 2209:             my $checked = 'checked="checked" ';
 2210:             if (ref($settings) eq 'HASH') {
 2211:                 if (ref($settings->{$item}) eq 'HASH') {
 2212:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2213:                         $checked = '';
 2214:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2215:                         $checked = 'checked="checked" ';
 2216:                     }
 2217:                 }
 2218:             }
 2219:             $datatable .= '<span class="LC_nobreak"><label>'.
 2220:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2221:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2222:                           '</label></span>&nbsp; ';
 2223:         }
 2224:     }
 2225:     if ($context eq 'requestcourses') {
 2226:         $datatable .= '</tr><tr>';
 2227:         foreach my $item (@usertools) {
 2228:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2229:         }
 2230:         $datatable .= '</tr></table>';
 2231:     }
 2232:     $datatable .= '</td></tr>';
 2233:     $$rowtotal += $typecount;
 2234:     return $datatable;
 2235: }
 2236: 
 2237: sub print_requestmail {
 2238:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2239:     my ($now,$datatable,%currapp);
 2240:     $now = time;
 2241:     if (ref($settings) eq 'HASH') {
 2242:         if (ref($settings->{'notify'}) eq 'HASH') {
 2243:             if ($settings->{'notify'}{'approval'} ne '') {
 2244:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2245:             }
 2246:         }
 2247:     }
 2248:     my $numinrow = 2;
 2249:     my $css_class;
 2250:     if ($$rowtotal%2) {
 2251:         $css_class = 'LC_odd_row';
 2252:     }
 2253:     if ($customcss) {
 2254:         $css_class .= " $customcss";
 2255:     }
 2256:     $css_class =~ s/^\s+//;
 2257:     if ($css_class) {
 2258:         $css_class = ' class="'.$css_class.'"';
 2259:     }
 2260:     if ($rowstyle) {
 2261:         $css_class .= ' style="'.$rowstyle.'"';
 2262:     }
 2263:     my $text;
 2264:     if ($action eq 'requestcourses') {
 2265:         $text = &mt('Receive notification of course requests requiring approval');
 2266:     } elsif ($action eq 'requestauthor') {
 2267:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2268:     } else {
 2269:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2270:     }
 2271:     $datatable = '<tr'.$css_class.'>'.
 2272:                  ' <td>'.$text.'</td>'.
 2273:                  ' <td class="LC_left_item">';
 2274:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2275:                                                  $action.'notifyapproval',%currapp);
 2276:     if ($numdc > 0) {
 2277:         $datatable .= $table;
 2278:     } else {
 2279:         $datatable .= &mt('There are no active Domain Coordinators');
 2280:     }
 2281:     $datatable .='</td></tr>';
 2282:     return $datatable;
 2283: }
 2284: 
 2285: sub print_studentcode {
 2286:     my ($settings,$rowtotal) = @_;
 2287:     my $rownum = 0; 
 2288:     my ($output,%current);
 2289:     my @crstypes = ('official','unofficial','community','textbook');
 2290:     if (ref($settings) eq 'HASH') {
 2291:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2292:             foreach my $type (@crstypes) {
 2293:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2294:             }
 2295:         }
 2296:     }
 2297:     $output .= '<tr>'.
 2298:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2299:                '<td class="LC_left_item">';
 2300:     foreach my $type (@crstypes) {
 2301:         my $check = ' ';
 2302:         if ($current{$type}) {
 2303:             $check = ' checked="checked" ';
 2304:         }
 2305:         $output .= '<span class="LC_nobreak"><label>'.
 2306:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2307:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2308:     }
 2309:     $output .= '</td></tr>';
 2310:     $$rowtotal ++;
 2311:     return $output;
 2312: }
 2313: 
 2314: sub print_textbookcourses {
 2315:     my ($dom,$type,$settings,$rowtotal) = @_;
 2316:     my $rownum = 0;
 2317:     my $css_class;
 2318:     my $itemcount = 1;
 2319:     my $maxnum = 0;
 2320:     my $bookshash;
 2321:     if (ref($settings) eq 'HASH') {
 2322:         $bookshash = $settings->{$type};
 2323:     }
 2324:     my %ordered;
 2325:     if (ref($bookshash) eq 'HASH') {
 2326:         foreach my $item (keys(%{$bookshash})) {
 2327:             if (ref($bookshash->{$item}) eq 'HASH') {
 2328:                 my $num = $bookshash->{$item}{'order'};
 2329:                 $ordered{$num} = $item;
 2330:             }
 2331:         }
 2332:     }
 2333:     my $confname = $dom.'-domainconfig';
 2334:     my $switchserver = &check_switchserver($dom,$confname);
 2335:     my $maxnum = scalar(keys(%ordered));
 2336:     my $datatable;
 2337:     if (keys(%ordered)) {
 2338:         my @items = sort { $a <=> $b } keys(%ordered);
 2339:         for (my $i=0; $i<@items; $i++) {
 2340:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2341:             my $key = $ordered{$items[$i]};
 2342:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2343:             my $coursetitle = $coursehash{'description'};
 2344:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2345:             if (ref($bookshash->{$key}) eq 'HASH') {
 2346:                 $subject = $bookshash->{$key}->{'subject'};
 2347:                 $title = $bookshash->{$key}->{'title'};
 2348:                 if ($type eq 'textbooks') {
 2349:                     $publisher = $bookshash->{$key}->{'publisher'};
 2350:                     $author = $bookshash->{$key}->{'author'};
 2351:                     $image = $bookshash->{$key}->{'image'};
 2352:                     if ($image ne '') {
 2353:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2354:                         my $imagethumb = "$path/tn-".$imagefile;
 2355:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2356:                     }
 2357:                 }
 2358:             }
 2359:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2360:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2361:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2362:             for (my $k=0; $k<=$maxnum; $k++) {
 2363:                 my $vpos = $k+1;
 2364:                 my $selstr;
 2365:                 if ($k == $i) {
 2366:                     $selstr = ' selected="selected" ';
 2367:                 }
 2368:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2369:             }
 2370:             $datatable .= '</select>'.('&nbsp;'x2).
 2371:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2372:                 &mt('Delete?').'</label></span></td>'.
 2373:                 '<td colspan="2">'.
 2374:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2375:                 ('&nbsp;'x2).
 2376:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2377:             if ($type eq 'textbooks') {
 2378:                 $datatable .= ('&nbsp;'x2).
 2379:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2380:                               ('&nbsp;'x2).
 2381:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2382:                               ('&nbsp;'x2).
 2383:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2384:                 if ($image) {
 2385:                     $datatable .= '<span class="LC_nobreak">'.
 2386:                                   $imgsrc.
 2387:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2388:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2389:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2390:                 }
 2391:                 if ($switchserver) {
 2392:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2393:                 } else {
 2394:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2395:                 }
 2396:             }
 2397:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2398:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2399:                           $coursetitle.'</span></td></tr>'."\n";
 2400:             $itemcount ++;
 2401:         }
 2402:     }
 2403:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2404:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2405:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2406:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2407:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2408:     for (my $k=0; $k<$maxnum+1; $k++) {
 2409:         my $vpos = $k+1;
 2410:         my $selstr;
 2411:         if ($k == $maxnum) {
 2412:             $selstr = ' selected="selected" ';
 2413:         }
 2414:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2415:     }
 2416:     $datatable .= '</select>&nbsp;'."\n".
 2417:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2418:                   '<td colspan="2">'.
 2419:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2420:                   ('&nbsp;'x2).
 2421:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2422:                   ('&nbsp;'x2);
 2423:     if ($type eq 'textbooks') {
 2424:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2425:                       ('&nbsp;'x2).
 2426:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2427:                       ('&nbsp;'x2).
 2428:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2429:         if ($switchserver) {
 2430:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2431:         } else {
 2432:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2433:         }
 2434:         $datatable .= '</span>'."\n";
 2435:     }
 2436:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2437:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2438:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2439:                   &Apache::loncommon::selectcourse_link
 2440:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2441:                   '</span></td>'."\n".
 2442:                   '</tr>'."\n";
 2443:     $itemcount ++;
 2444:     return $datatable;
 2445: }
 2446: 
 2447: sub textbookcourses_javascript {
 2448:     my ($settings) = @_;
 2449:     return unless(ref($settings) eq 'HASH');
 2450:     my (%ordered,%total,%jstext);
 2451:     foreach my $type ('textbooks','templates') {
 2452:         $total{$type} = 0;
 2453:         if (ref($settings->{$type}) eq 'HASH') {
 2454:             foreach my $item (keys(%{$settings->{$type}})) {
 2455:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2456:                     my $num = $settings->{$type}->{$item}{'order'};
 2457:                     $ordered{$type}{$num} = $item;
 2458:                 }
 2459:             }
 2460:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2461:         }
 2462:         my @jsarray = ();
 2463:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2464:             push(@jsarray,$ordered{$type}{$item});
 2465:         }
 2466:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2467:     }
 2468:     return <<"ENDSCRIPT";
 2469: <script type="text/javascript">
 2470: // <![CDATA[
 2471: function reorderBooks(form,item,caller) {
 2472:     var changedVal;
 2473: $jstext{'textbooks'};
 2474: $jstext{'templates'};
 2475:     var newpos;
 2476:     var maxh;
 2477:     if (caller == 'textbooks') {  
 2478:         newpos = 'textbooks_addbook_pos';
 2479:         maxh = 1 + $total{'textbooks'};
 2480:     } else {
 2481:         newpos = 'templates_addbook_pos';
 2482:         maxh = 1 + $total{'templates'};
 2483:     }
 2484:     var current = new Array;
 2485:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2486:     if (item == newpos) {
 2487:         changedVal = newitemVal;
 2488:     } else {
 2489:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2490:         current[newitemVal] = newpos;
 2491:     }
 2492:     if (caller == 'textbooks') {
 2493:         for (var i=0; i<textbooks.length; i++) {
 2494:             var elementName = 'textbooks_'+textbooks[i];
 2495:             if (elementName != item) {
 2496:                 if (form.elements[elementName]) {
 2497:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2498:                     current[currVal] = elementName;
 2499:                 }
 2500:             }
 2501:         }
 2502:     }
 2503:     if (caller == 'templates') {
 2504:         for (var i=0; i<templates.length; i++) {
 2505:             var elementName = 'templates_'+templates[i];
 2506:             if (elementName != item) {
 2507:                 if (form.elements[elementName]) {
 2508:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2509:                     current[currVal] = elementName;
 2510:                 }
 2511:             }
 2512:         }
 2513:     }
 2514:     var oldVal;
 2515:     for (var j=0; j<maxh; j++) {
 2516:         if (current[j] == undefined) {
 2517:             oldVal = j;
 2518:         }
 2519:     }
 2520:     if (oldVal < changedVal) {
 2521:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2522:            var elementName = current[k];
 2523:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2524:         }
 2525:     } else {
 2526:         for (var k=changedVal; k<oldVal; k++) {
 2527:             var elementName = current[k];
 2528:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2529:         }
 2530:     }
 2531:     return;
 2532: }
 2533: 
 2534: // ]]>
 2535: </script>
 2536: 
 2537: ENDSCRIPT
 2538: }
 2539: 
 2540: sub ltitools_javascript {
 2541:     my ($settings) = @_;
 2542:     my $togglejs = &ltitools_toggle_js();
 2543:     unless (ref($settings) eq 'HASH') {
 2544:         return $togglejs;
 2545:     }
 2546:     my (%ordered,$total,%jstext);
 2547:     $total = 0;
 2548:     foreach my $item (keys(%{$settings})) {
 2549:         if (ref($settings->{$item}) eq 'HASH') {
 2550:             my $num = $settings->{$item}{'order'};
 2551:             $ordered{$num} = $item;
 2552:         }
 2553:     }
 2554:     $total = scalar(keys(%{$settings}));
 2555:     my @jsarray = ();
 2556:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2557:         push(@jsarray,$ordered{$item});
 2558:     }
 2559:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2560:     return <<"ENDSCRIPT";
 2561: <script type="text/javascript">
 2562: // <![CDATA[
 2563: function reorderLTITools(form,item) {
 2564:     var changedVal;
 2565: $jstext
 2566:     var newpos = 'ltitools_add_pos';
 2567:     var maxh = 1 + $total;
 2568:     var current = new Array;
 2569:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2570:     if (item == newpos) {
 2571:         changedVal = newitemVal;
 2572:     } else {
 2573:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2574:         current[newitemVal] = newpos;
 2575:     }
 2576:     for (var i=0; i<ltitools.length; i++) {
 2577:         var elementName = 'ltitools_'+ltitools[i];
 2578:         if (elementName != item) {
 2579:             if (form.elements[elementName]) {
 2580:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2581:                 current[currVal] = elementName;
 2582:             }
 2583:         }
 2584:     }
 2585:     var oldVal;
 2586:     for (var j=0; j<maxh; j++) {
 2587:         if (current[j] == undefined) {
 2588:             oldVal = j;
 2589:         }
 2590:     }
 2591:     if (oldVal < changedVal) {
 2592:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2593:            var elementName = current[k];
 2594:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2595:         }
 2596:     } else {
 2597:         for (var k=changedVal; k<oldVal; k++) {
 2598:             var elementName = current[k];
 2599:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2600:         }
 2601:     }
 2602:     return;
 2603: }
 2604: 
 2605: // ]]>
 2606: </script>
 2607: 
 2608: $togglejs
 2609: 
 2610: ENDSCRIPT
 2611: }
 2612: 
 2613: sub ltitools_toggle_js {
 2614:     return <<"ENDSCRIPT";
 2615: <script type="text/javascript">
 2616: // <![CDATA[
 2617: 
 2618: function toggleLTITools(form,setting,item) {
 2619:     var radioname = '';
 2620:     var divid = '';
 2621:     if (setting == 'user') {
 2622:         divid = 'ltitools_'+setting+'_div_'+item;
 2623:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2624:         if (document.getElementById(divid)) {
 2625:             if (document.getElementById(checkid)) {
 2626:                 if (document.getElementById(checkid).checked) {
 2627:                     document.getElementById(divid).style.display = 'inline-block';
 2628:                 } else {
 2629:                     document.getElementById(divid).style.display = 'none';
 2630:                 }
 2631:             }
 2632:         }
 2633:     }
 2634:     return;
 2635: }
 2636: // ]]>
 2637: </script>
 2638: 
 2639: ENDSCRIPT
 2640: }
 2641: 
 2642: sub print_autoenroll {
 2643:     my ($dom,$settings,$rowtotal) = @_;
 2644:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2645:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2646:     if (ref($settings) eq 'HASH') {
 2647:         if (exists($settings->{'run'})) {
 2648:             if ($settings->{'run'} eq '0') {
 2649:                 $runoff = ' checked="checked" ';
 2650:                 $runon = ' ';
 2651:             } else {
 2652:                 $runon = ' checked="checked" ';
 2653:                 $runoff = ' ';
 2654:             }
 2655:         } else {
 2656:             if ($autorun) {
 2657:                 $runon = ' checked="checked" ';
 2658:                 $runoff = ' ';
 2659:             } else {
 2660:                 $runoff = ' checked="checked" ';
 2661:                 $runon = ' ';
 2662:             }
 2663:         }
 2664:         if (exists($settings->{'co-owners'})) {
 2665:             if ($settings->{'co-owners'} eq '0') {
 2666:                 $coownersoff = ' checked="checked" ';
 2667:                 $coownerson = ' ';
 2668:             } else {
 2669:                 $coownerson = ' checked="checked" ';
 2670:                 $coownersoff = ' ';
 2671:             }
 2672:         } else {
 2673:             $coownersoff = ' checked="checked" ';
 2674:             $coownerson = ' ';
 2675:         }
 2676:         if (exists($settings->{'sender_domain'})) {
 2677:             $defdom = $settings->{'sender_domain'};
 2678:         }
 2679:         if (exists($settings->{'autofailsafe'})) {
 2680:             $failsafe = $settings->{'autofailsafe'};
 2681:         }
 2682:     } else {
 2683:         if ($autorun) {
 2684:             $runon = ' checked="checked" ';
 2685:             $runoff = ' ';
 2686:         } else {
 2687:             $runoff = ' checked="checked" ';
 2688:             $runon = ' ';
 2689:         }
 2690:     }
 2691:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2692:     my $notif_sender;
 2693:     if (ref($settings) eq 'HASH') {
 2694:         $notif_sender = $settings->{'sender_uname'};
 2695:     }
 2696:     my $datatable='<tr class="LC_odd_row">'.
 2697:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2698:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2699:                   '<input type="radio" name="autoenroll_run"'.
 2700:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2701:                   '<label><input type="radio" name="autoenroll_run"'.
 2702:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2703:                   '</tr><tr>'.
 2704:                   '<td>'.&mt('Notification messages - sender').
 2705:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2706:                   &mt('username').':&nbsp;'.
 2707:                   '<input type="text" name="sender_uname" value="'.
 2708:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2709:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2710:                   '<tr class="LC_odd_row">'.
 2711:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2712:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2713:                   '<input type="radio" name="autoassign_coowners"'.
 2714:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2715:                   '<label><input type="radio" name="autoassign_coowners"'.
 2716:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2717:                   '</tr><tr>'.
 2718:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2719:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2720:                   '<input type="text" name="autoenroll_failsafe"'.
 2721:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 2722:     $$rowtotal += 4;
 2723:     return $datatable;
 2724: }
 2725: 
 2726: sub print_autoupdate {
 2727:     my ($position,$dom,$settings,$rowtotal) = @_;
 2728:     my $datatable;
 2729:     if ($position eq 'top') {
 2730:         my $updateon = ' ';
 2731:         my $updateoff = ' checked="checked" ';
 2732:         my $classlistson = ' ';
 2733:         my $classlistsoff = ' checked="checked" ';
 2734:         if (ref($settings) eq 'HASH') {
 2735:             if ($settings->{'run'} eq '1') {
 2736:                 $updateon = $updateoff;
 2737:                 $updateoff = ' ';
 2738:             }
 2739:             if ($settings->{'classlists'} eq '1') {
 2740:                 $classlistson = $classlistsoff;
 2741:                 $classlistsoff = ' ';
 2742:             }
 2743:         }
 2744:         my %title = (
 2745:                    run => 'Auto-update active?',
 2746:                    classlists => 'Update information in classlists?',
 2747:                     );
 2748:         $datatable = '<tr class="LC_odd_row">'. 
 2749:                   '<td>'.&mt($title{'run'}).'</td>'.
 2750:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2751:                   '<input type="radio" name="autoupdate_run"'.
 2752:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2753:                   '<label><input type="radio" name="autoupdate_run"'.
 2754:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2755:                   '</tr><tr>'.
 2756:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2757:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2758:                   '<label><input type="radio" name="classlists"'.
 2759:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2760:                   '<label><input type="radio" name="classlists"'.
 2761:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2762:                   '</tr>';
 2763:         $$rowtotal += 2;
 2764:     } elsif ($position eq 'middle') {
 2765:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2766:         my $numinrow = 3;
 2767:         my $locknamesettings;
 2768:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2769:                                      $dom,$numinrow,$othertitle,
 2770:                                     'lockablenames',$rowtotal);
 2771:         $$rowtotal ++;
 2772:     } else {
 2773:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2774:         my @fields = ('lastname','firstname','middlename','generation',
 2775:                       'permanentemail','id');
 2776:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2777:         my $numrows = 0;
 2778:         if (ref($types) eq 'ARRAY') {
 2779:             if (@{$types} > 0) {
 2780:                 $datatable = 
 2781:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2782:                                          \@fields,$types,\$numrows);
 2783:                     $$rowtotal += @{$types}; 
 2784:             }
 2785:         }
 2786:         $datatable .= 
 2787:             &usertype_update_row($settings,{'default' => $othertitle},
 2788:                                  \%fieldtitles,\@fields,['default'],
 2789:                                  \$numrows);
 2790:         $$rowtotal ++;     
 2791:     }
 2792:     return $datatable;
 2793: }
 2794: 
 2795: sub print_autocreate {
 2796:     my ($dom,$settings,$rowtotal) = @_;
 2797:     my (%createon,%createoff,%currhash);
 2798:     my @types = ('xml','req');
 2799:     if (ref($settings) eq 'HASH') {
 2800:         foreach my $item (@types) {
 2801:             $createoff{$item} = ' checked="checked" ';
 2802:             $createon{$item} = ' ';
 2803:             if (exists($settings->{$item})) {
 2804:                 if ($settings->{$item}) {
 2805:                     $createon{$item} = ' checked="checked" ';
 2806:                     $createoff{$item} = ' ';
 2807:                 }
 2808:             }
 2809:         }
 2810:         if ($settings->{'xmldc'} ne '') {
 2811:             $currhash{$settings->{'xmldc'}} = 1;
 2812:         }
 2813:     } else {
 2814:         foreach my $item (@types) {
 2815:             $createoff{$item} = ' checked="checked" ';
 2816:             $createon{$item} = ' ';
 2817:         }
 2818:     }
 2819:     $$rowtotal += 2;
 2820:     my $numinrow = 2;
 2821:     my $datatable='<tr class="LC_odd_row">'.
 2822:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2823:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2824:                   '<input type="radio" name="autocreate_xml"'.
 2825:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2826:                   '<label><input type="radio" name="autocreate_xml"'.
 2827:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2828:                   '</td></tr><tr>'.
 2829:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2830:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2831:                   '<input type="radio" name="autocreate_req"'.
 2832:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2833:                   '<label><input type="radio" name="autocreate_req"'.
 2834:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2835:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2836:                                                    'autocreate_xmldc',%currhash);
 2837:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2838:     if ($numdc > 1) {
 2839:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2840:                       '</td><td class="LC_left_item">';
 2841:     } else {
 2842:         $datatable .= &mt('Course creation processed as:').
 2843:                       '</td><td class="LC_right_item">';
 2844:     }
 2845:     $datatable .= $dctable.'</td></tr>';
 2846:     $$rowtotal += $rows;
 2847:     return $datatable;
 2848: }
 2849: 
 2850: sub print_directorysrch {
 2851:     my ($position,$dom,$settings,$rowtotal) = @_;
 2852:     my $datatable;
 2853:     if ($position eq 'top') {
 2854:         my $instsrchon = ' ';
 2855:         my $instsrchoff = ' checked="checked" ';
 2856:         my ($exacton,$containson,$beginson);
 2857:         my $instlocalon = ' ';
 2858:         my $instlocaloff = ' checked="checked" ';
 2859:         if (ref($settings) eq 'HASH') {
 2860:             if ($settings->{'available'} eq '1') {
 2861:                 $instsrchon = $instsrchoff;
 2862:                 $instsrchoff = ' ';
 2863:             }
 2864:             if ($settings->{'localonly'} eq '1') {
 2865:                 $instlocalon = $instlocaloff;
 2866:                 $instlocaloff = ' ';
 2867:             }
 2868:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2869:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2870:                     if ($type eq 'exact') {
 2871:                         $exacton = ' checked="checked" ';
 2872:                     } elsif ($type eq 'contains') {
 2873:                         $containson = ' checked="checked" ';
 2874:                     } elsif ($type eq 'begins') {
 2875:                         $beginson = ' checked="checked" ';
 2876:                     }
 2877:                 }
 2878:             } else {
 2879:                 if ($settings->{'searchtypes'} eq 'exact') {
 2880:                     $exacton = ' checked="checked" ';
 2881:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2882:                     $containson = ' checked="checked" ';
 2883:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2884:                     $exacton = ' checked="checked" ';
 2885:                     $containson = ' checked="checked" ';
 2886:                 }
 2887:             }
 2888:         }
 2889:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2890:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2891: 
 2892:         my $numinrow = 4;
 2893:         my $cansrchrow = 0;
 2894:         $datatable='<tr class="LC_odd_row">'.
 2895:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2896:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2897:                    '<input type="radio" name="dirsrch_available"'.
 2898:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2899:                    '<label><input type="radio" name="dirsrch_available"'.
 2900:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2901:                    '</tr><tr>'.
 2902:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2903:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2904:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2905:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2906:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2907:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2908:                    '</tr>';
 2909:         $$rowtotal += 2;
 2910:         if (ref($usertypes) eq 'HASH') {
 2911:             if (keys(%{$usertypes}) > 0) {
 2912:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2913:                                              $numinrow,$othertitle,'cansearch',
 2914:                                              $rowtotal);
 2915:                 $cansrchrow = 1;
 2916:             }
 2917:         }
 2918:         if ($cansrchrow) {
 2919:             $$rowtotal ++;
 2920:             $datatable .= '<tr>';
 2921:         } else {
 2922:             $datatable .= '<tr class="LC_odd_row">';
 2923:         }
 2924:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2925:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2926:         foreach my $title (@{$titleorder}) {
 2927:             if (defined($searchtitles->{$title})) {
 2928:                 my $check = ' ';
 2929:                 if (ref($settings) eq 'HASH') {
 2930:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2931:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2932:                             $check = ' checked="checked" ';
 2933:                         }
 2934:                     }
 2935:                 }
 2936:                 $datatable .= '<td class="LC_left_item">'.
 2937:                               '<span class="LC_nobreak"><label>'.
 2938:                               '<input type="checkbox" name="searchby" '.
 2939:                               'value="'.$title.'"'.$check.'/>'.
 2940:                               $searchtitles->{$title}.'</label></span></td>';
 2941:             }
 2942:         }
 2943:         $datatable .= '</tr></table></td></tr>';
 2944:         $$rowtotal ++;
 2945:         if ($cansrchrow) {
 2946:             $datatable .= '<tr class="LC_odd_row">';
 2947:         } else {
 2948:             $datatable .= '<tr>';
 2949:         }
 2950:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2951:                       '<td class="LC_left_item" colspan="2">'.
 2952:                       '<span class="LC_nobreak"><label>'.
 2953:                       '<input type="checkbox" name="searchtypes" '.
 2954:                       $exacton.' value="exact" />'.&mt('Exact match').
 2955:                       '</label>&nbsp;'.
 2956:                       '<label><input type="checkbox" name="searchtypes" '.
 2957:                       $beginson.' value="begins" />'.&mt('Begins with').
 2958:                       '</label>&nbsp;'.
 2959:                       '<label><input type="checkbox" name="searchtypes" '.
 2960:                       $containson.' value="contains" />'.&mt('Contains').
 2961:                       '</label></span></td></tr>';
 2962:         $$rowtotal ++;
 2963:     } else {
 2964:         my $domsrchon = ' checked="checked" ';
 2965:         my $domsrchoff = ' ';
 2966:         my $domlocalon = ' ';
 2967:         my $domlocaloff = ' checked="checked" ';
 2968:         if (ref($settings) eq 'HASH') {
 2969:             if ($settings->{'lclocalonly'} eq '1') {
 2970:                 $domlocalon = $domlocaloff;
 2971:                 $domlocaloff = ' ';
 2972:             }
 2973:             if ($settings->{'lcavailable'} eq '0') {
 2974:                 $domsrchoff = $domsrchon;
 2975:                 $domsrchon = ' ';
 2976:             }
 2977:         }
 2978:         $datatable='<tr class="LC_odd_row">'.
 2979:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2980:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2981:                       '<input type="radio" name="dirsrch_domavailable"'.
 2982:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2983:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2984:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2985:                       '</tr><tr>'.
 2986:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2987:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2988:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2989:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2990:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2991:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2992:                       '</tr>';
 2993:         $$rowtotal += 2;
 2994:     }
 2995:     return $datatable;
 2996: }
 2997: 
 2998: sub print_contacts {
 2999:     my ($position,$dom,$settings,$rowtotal) = @_;
 3000:     my $datatable;
 3001:     my @contacts = ('adminemail','supportemail');
 3002:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3003:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3004:     if ($position eq 'top') {
 3005:         if (ref($settings) eq 'HASH') {
 3006:             foreach my $item (@contacts) {
 3007:                 if (exists($settings->{$item})) {
 3008:                     $to{$item} = $settings->{$item};
 3009:                 }
 3010:             }
 3011:         }
 3012:     } elsif ($position eq 'middle') {
 3013:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3014:                      'updatesmail','idconflictsmail','hostipmail');
 3015:         foreach my $type (@mailings) {
 3016:             $otheremails{$type} = '';
 3017:         }
 3018:     } elsif ($position eq 'lower') {
 3019:         if (ref($settings) eq 'HASH') {
 3020:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3021:                 %lonstatus = %{$settings->{'lonstatus'}};
 3022:             }
 3023:         }
 3024:     } else {
 3025:         @mailings = ('helpdeskmail','otherdomsmail');
 3026:         foreach my $type (@mailings) {
 3027:             $otheremails{$type} = '';
 3028:         }
 3029:         $bccemails{'helpdeskmail'} = '';
 3030:         $bccemails{'otherdomsmail'} = '';
 3031:         $includestr{'helpdeskmail'} = '';
 3032:         $includestr{'otherdomsmail'} = '';
 3033:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3034:     }
 3035:     if (ref($settings) eq 'HASH') {
 3036:         unless (($position eq 'top') || ($position eq 'lower')) {
 3037:             foreach my $type (@mailings) {
 3038:                 if (exists($settings->{$type})) {
 3039:                     if (ref($settings->{$type}) eq 'HASH') {
 3040:                         foreach my $item (@contacts) {
 3041:                             if ($settings->{$type}{$item}) {
 3042:                                 $checked{$type}{$item} = ' checked="checked" ';
 3043:                             }
 3044:                         }
 3045:                         $otheremails{$type} = $settings->{$type}{'others'};
 3046:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3047:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3048:                             if ($settings->{$type}{'include'} ne '') {
 3049:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3050:                                 $includestr{$type} = &unescape($includestr{$type});
 3051:                             }
 3052:                         }
 3053:                     }
 3054:                 } elsif ($type eq 'lonstatusmail') {
 3055:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3056:                 }
 3057:             }
 3058:         }
 3059:         if ($position eq 'bottom') {
 3060:             foreach my $type (@mailings) {
 3061:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3062:                 if ($settings->{$type}{'include'} ne '') {
 3063:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3064:                     $includestr{$type} = &unescape($includestr{$type});
 3065:                 }
 3066:             }
 3067:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3068:                 if (ref($fields) eq 'ARRAY') {
 3069:                     foreach my $field (@{$fields}) {
 3070:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3071:                     }
 3072:                 }
 3073:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3074:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3075:                 } else {
 3076:                     $maxsize = '1.0';
 3077:                 }
 3078:             } else {
 3079:                 if (ref($fields) eq 'ARRAY') {
 3080:                     foreach my $field (@{$fields}) {
 3081:                         $currfield{$field} = 'yes';
 3082:                     }
 3083:                 }
 3084:                 $maxsize = '1.0';
 3085:             }
 3086:         }
 3087:     } else {
 3088:         if ($position eq 'top') {
 3089:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3090:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3091:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3092:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3093:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3094:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3095:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3096:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3097:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3098:         } elsif ($position eq 'bottom') {
 3099:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3100:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3101:             if (ref($fields) eq 'ARRAY') {
 3102:                 foreach my $field (@{$fields}) {
 3103:                     $currfield{$field} = 'yes';
 3104:                 }
 3105:             }
 3106:             $maxsize = '1.0';
 3107:         }
 3108:     }
 3109:     my ($titles,$short_titles) = &contact_titles();
 3110:     my $rownum = 0;
 3111:     my $css_class;
 3112:     if ($position eq 'top') {
 3113:         foreach my $item (@contacts) {
 3114:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3115:             $datatable .= '<tr'.$css_class.'>'. 
 3116:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3117:                           '</span></td><td class="LC_right_item">'.
 3118:                           '<input type="text" name="'.$item.'" value="'.
 3119:                           $to{$item}.'" /></td></tr>';
 3120:             $rownum ++;
 3121:         }
 3122:     } elsif ($position eq 'bottom') {
 3123:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3124:         $datatable .= '<tr'.$css_class.'>'.
 3125:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3126:                       &mt('(e-mail, subject, and description always shown)').
 3127:                       '</td><td class="LC_left_item">';
 3128:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3129:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3130:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3131:             foreach my $field (@{$fields}) {
 3132:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3133:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3134:                     $datatable .= ' '.&mt('(logged-in users)');
 3135:                 }
 3136:                 $datatable .='</td><td>';
 3137:                 my $clickaction;
 3138:                 if ($field eq 'screenshot') {
 3139:                     $clickaction = ' onclick="screenshotSize(this);"';
 3140:                 }
 3141:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3142:                     foreach my $option (@{$possoptions->{$field}}) {
 3143:                         my $checked;
 3144:                         if ($currfield{$field} eq $option) {
 3145:                             $checked = ' checked="checked"';
 3146:                         }
 3147:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3148:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3149:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3150:                                       '</label></span>'.('&nbsp;'x2);
 3151:                     }
 3152:                 }
 3153:                 if ($field eq 'screenshot') {
 3154:                     my $display;
 3155:                     if ($currfield{$field} eq 'no') {
 3156:                         $display = ' style="display:none"';
 3157:                     }
 3158:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3159:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3160:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3161:                 }
 3162:                 $datatable .= '</td></tr>';
 3163:             }
 3164:             $datatable .= '</table>';
 3165:         }
 3166:         $datatable .= '</td></tr>'."\n";
 3167:         $rownum ++;
 3168:     }
 3169:     unless (($position eq 'top') || ($position eq 'lower')) {
 3170:         foreach my $type (@mailings) {
 3171:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3172:             $datatable .= '<tr'.$css_class.'>'.
 3173:                           '<td><span class="LC_nobreak">'.
 3174:                           $titles->{$type}.': </span></td>'.
 3175:                           '<td class="LC_left_item">';
 3176:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3177:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3178:             }
 3179:             $datatable .= '<span class="LC_nobreak">';
 3180:             foreach my $item (@contacts) {
 3181:                 $datatable .= '<label>'.
 3182:                               '<input type="checkbox" name="'.$type.'"'.
 3183:                               $checked{$type}{$item}.
 3184:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3185:                               '</label>&nbsp;';
 3186:             }
 3187:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3188:                           '<input type="text" name="'.$type.'_others" '.
 3189:                           'value="'.$otheremails{$type}.'"  />';
 3190:             my %locchecked;
 3191:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3192:                 foreach my $loc ('s','b') {
 3193:                     if ($includeloc{$type} eq $loc) {
 3194:                         $locchecked{$loc} = ' checked="checked"';
 3195:                         last;
 3196:                     }
 3197:                 }
 3198:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3199:                               '<input type="text" name="'.$type.'_bcc" '.
 3200:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3201:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3202:                               &mt('Text automatically added to e-mail:').' '.
 3203:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3204:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3205:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3206:                               ('&nbsp;'x2).
 3207:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3208:                               '</span></fieldset>';
 3209:             }
 3210:             $datatable .= '</td></tr>'."\n";
 3211:             $rownum ++;
 3212:         }
 3213:     }
 3214:     if ($position eq 'middle') {
 3215:         my %choices;
 3216:         my $corelink = &core_link_msu();
 3217:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3218:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3219:                                         $corelink);
 3220:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3221:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3222:         my %defaultchecked = ('reporterrors'  => 'on',
 3223:                               'reportupdates' => 'on',
 3224:                               'reportstatus'  => 'on');
 3225:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3226:                                                    \%choices,$rownum);
 3227:         $datatable .= $reports;
 3228:     } elsif ($position eq 'lower') {
 3229:         my (%current,%excluded,%weights);
 3230:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3231:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3232:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 3233:         } else {
 3234:             $current{'errorthreshold'} = $defaults->{'threshold'};
 3235:         }
 3236:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3237:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 3238:         } else {
 3239:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 3240:         }
 3241:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3242:             foreach my $type ('E','W','N','U') {
 3243:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3244:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3245:                 } else {
 3246:                     $weights{$type} = $defaults->{$type};
 3247:                 }
 3248:             }
 3249:         } else {
 3250:             foreach my $type ('E','W','N','U') {
 3251:                 $weights{$type} = $defaults->{$type};
 3252:             }
 3253:         }
 3254:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3255:             if (@{$lonstatus{'excluded'}} > 0) {
 3256:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3257:             }
 3258:         }
 3259:         foreach my $item ('errorthreshold','errorsysmail') {
 3260:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3261:             $datatable .= '<tr'.$css_class.'>'.
 3262:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 3263:                           $titles->{$item}.
 3264:                           '</span></td><td class="LC_left_item">'.
 3265:                           '<input type="text" name="'.$item.'" value="'.
 3266:                           $current{$item}.'" size="5" /></td></tr>';
 3267:             $rownum ++;
 3268:         }
 3269:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3270:         $datatable .= '<tr'.$css_class.'>'.
 3271:                       '<td class="LC_left_item">'.
 3272:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 3273:                       '</span></td><td class="LC_left_item"><table><tr>';
 3274:         foreach my $type ('E','W','N','U') {
 3275:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 3276:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 3277:                           $weights{$type}.'" size="5" /></td>';
 3278:         }
 3279:         $datatable .= '</tr></table></tr>';
 3280:         $rownum ++;
 3281:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3282:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 3283:                       $titles->{'errorexcluded'}.'</td>'.
 3284:                       '<td class="LC_left_item"><table>';
 3285:         my $numinrow = 4;
 3286:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 3287:         for (my $i=0; $i<@ids; $i++) {
 3288:             my $rem = $i%($numinrow);
 3289:             if ($rem == 0) {
 3290:                 if ($i > 0) {
 3291:                     $datatable .= '</tr>';
 3292:                 }
 3293:                 $datatable .= '<tr>';
 3294:             }
 3295:             my $check;
 3296:             if ($excluded{$ids[$i]}) {
 3297:                 $check = ' checked="checked" ';
 3298:             }
 3299:             $datatable .= '<td class="LC_left_item">'.
 3300:                           '<span class="LC_nobreak"><label>'.
 3301:                           '<input type="checkbox" name="errorexcluded" '.
 3302:                           'value="'.$ids[$i].'"'.$check.' />'.
 3303:                           $ids[$i].'</label></span></td>';
 3304:         }
 3305:         my $colsleft = $numinrow - @ids%($numinrow);
 3306:         if ($colsleft > 1 ) {
 3307:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3308:                           '&nbsp;</td>';
 3309:         } elsif ($colsleft == 1) {
 3310:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3311:         }
 3312:         $datatable .= '</tr></table></td></tr>';
 3313:         $rownum ++;
 3314:     } elsif ($position eq 'bottom') {
 3315:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3316:         my (@posstypes,%usertypeshash);
 3317:         if (ref($types) eq 'ARRAY') {
 3318:             @posstypes = @{$types};
 3319:         }
 3320:         if (@posstypes) {
 3321:             if (ref($usertypes) eq 'HASH') {
 3322:                 %usertypeshash = %{$usertypes};
 3323:             }
 3324:             my @overridden;
 3325:             my $numinrow = 4;
 3326:             if (ref($settings) eq 'HASH') {
 3327:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3328:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3329:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3330:                             push(@overridden,$key);
 3331:                             foreach my $item (@contacts) {
 3332:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3333:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3334:                                 }
 3335:                             }
 3336:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3337:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3338:                             $includeloc{'override_'.$key} = '';
 3339:                             $includestr{'override_'.$key} = '';
 3340:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3341:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 3342:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3343:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3344:                             }
 3345:                         }
 3346:                     }
 3347:                 }
 3348:             }
 3349:             my $customclass = 'LC_helpdesk_override';
 3350:             my $optionsprefix = 'LC_options_helpdesk_';
 3351: 
 3352:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3353: 
 3354:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3355:                                          $numinrow,$othertitle,'overrides',
 3356:                                          \$rownum,$onclicktypes,$customclass);
 3357:             $rownum ++;
 3358:             $usertypeshash{'default'} = $othertitle;
 3359:             foreach my $status (@posstypes) {
 3360:                 my $css_class;
 3361:                 if ($rownum%2) {
 3362:                     $css_class = 'LC_odd_row ';
 3363:                 }
 3364:                 $css_class .= $customclass;
 3365:                 my $rowid = $optionsprefix.$status;
 3366:                 my $hidden = 1;
 3367:                 my $currstyle = 'display:none';
 3368:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3369:                     $currstyle = 'display:table-row';
 3370:                     $hidden = 0;
 3371:                 }
 3372:                 my $key = 'override_'.$status;
 3373:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3374:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3375:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3376:                                                   \@contacts,$short_titles);
 3377:                 unless ($hidden) {
 3378:                     $rownum ++;
 3379:                 }
 3380:             }
 3381:         }
 3382:     }
 3383:     $$rowtotal += $rownum;
 3384:     return $datatable;
 3385: }
 3386: 
 3387: sub core_link_msu {
 3388:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3389:                                           &mt('LON-CAPA core group - MSU'),600,500);
 3390: }
 3391: 
 3392: sub overridden_helpdesk {
 3393:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3394:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3395:     my $class = 'LC_left_item';
 3396:     if ($css_class) {
 3397:         $css_class = ' class="'.$css_class.'"';
 3398:     }
 3399:     if ($rowid) {
 3400:         $rowid = ' id="'.$rowid.'"';
 3401:     }
 3402:     if ($rowstyle) {
 3403:         $rowstyle = ' style="'.$rowstyle.'"';
 3404:     }
 3405:     my ($output,$description);
 3406:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3407:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3408:               "<td>$description</td>\n".
 3409:               '<td class="'.$class.'" colspan="2">'.
 3410:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3411:               '<span class="LC_nobreak">';
 3412:     if (ref($contacts) eq 'ARRAY') {
 3413:         foreach my $item (@{$contacts}) {
 3414:             my $check;
 3415:             if (ref($checked) eq 'HASH') {
 3416:                $check = $checked->{$item};
 3417:             }
 3418:             my $title;
 3419:             if (ref($short_titles) eq 'HASH') {
 3420:                 $title = $short_titles->{$item};
 3421:             }
 3422:             $output .= '<label>'.
 3423:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3424:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3425:         }
 3426:     }
 3427:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3428:                '<input type="text" name="override_'.$type.'_others" '.
 3429:                'value="'.$otheremails.'"  />';
 3430:     my %locchecked;
 3431:     foreach my $loc ('s','b') {
 3432:         if ($includeloc eq $loc) {
 3433:             $locchecked{$loc} = ' checked="checked"';
 3434:             last;
 3435:         }
 3436:     }
 3437:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3438:                '<input type="text" name="override_'.$type.'_bcc" '.
 3439:                'value="'.$bccemails.'"  /></fieldset>'.
 3440:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3441:                &mt('Text automatically added to e-mail:').' '.
 3442:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3443:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3444:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3445:                ('&nbsp;'x2).
 3446:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3447:                '</span></fieldset>'.
 3448:                '</td></tr>'."\n";
 3449:     return $output;
 3450: }
 3451: 
 3452: sub contacts_javascript {
 3453:     return <<"ENDSCRIPT";
 3454: 
 3455: <script type="text/javascript">
 3456: // <![CDATA[
 3457: 
 3458: function screenshotSize(field) {
 3459:     if (document.getElementById('help_screenshotsize')) {
 3460:         if (field.value == 'no') {
 3461:             document.getElementById('help_screenshotsize').style.display="none";
 3462:         } else {
 3463:             document.getElementById('help_screenshotsize').style.display="";
 3464:         }
 3465:     }
 3466:     return;
 3467: }
 3468: 
 3469: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3470:     if (form.elements[checkbox].length != undefined) {
 3471:         var count = 0;
 3472:         if (docount) {
 3473:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3474:                 if (form.elements[checkbox][i].checked) {
 3475:                     count ++;
 3476:                 }
 3477:             }
 3478:         }
 3479:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3480:             var type = form.elements[checkbox][i].value;
 3481:             if (document.getElementById(prefix+type)) {
 3482:                 if (form.elements[checkbox][i].checked) {
 3483:                     document.getElementById(prefix+type).style.display = 'table-row';
 3484:                     if (count % 2 == 1) {
 3485:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3486:                     } else {
 3487:                         document.getElementById(prefix+type).className = target;
 3488:                     }
 3489:                     count ++;
 3490:                 } else {
 3491:                     document.getElementById(prefix+type).style.display = 'none';
 3492:                 }
 3493:             }
 3494:         }
 3495:     }
 3496:     return;
 3497: }
 3498: 
 3499: // ]]>
 3500: </script>
 3501: 
 3502: ENDSCRIPT
 3503: }
 3504: 
 3505: sub print_helpsettings {
 3506:     my ($position,$dom,$settings,$rowtotal) = @_;
 3507:     my $confname = $dom.'-domainconfig';
 3508:     my $formname = 'display';
 3509:     my ($datatable,$itemcount);
 3510:     if ($position eq 'top') {
 3511:         $itemcount = 1;
 3512:         my (%choices,%defaultchecked,@toggles);
 3513:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3514:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3515:                                      &mt('LON-CAPA bug tracker'),600,500));
 3516:         %defaultchecked = ('submitbugs' => 'on');
 3517:         @toggles = ('submitbugs');
 3518:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3519:                                                      \%choices,$itemcount);
 3520:         $$rowtotal ++;
 3521:     } else {
 3522:         my $css_class;
 3523:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3524:         my (%customroles,%ordered,%current);
 3525:         if (ref($settings) eq 'HASH') {
 3526:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3527:                 %current = %{$settings->{'adhoc'}};
 3528:             }
 3529:         }
 3530:         my $count = 0;
 3531:         foreach my $key (sort(keys(%existing))) {
 3532:             if ($key=~/^rolesdef\_(\w+)$/) {
 3533:                 my $rolename = $1;
 3534:                 my (%privs,$order);
 3535:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3536:                 $customroles{$rolename} = \%privs;
 3537:                 if (ref($current{$rolename}) eq 'HASH') {
 3538:                     $order = $current{$rolename}{'order'};
 3539:                 }
 3540:                 if ($order eq '') {
 3541:                     $order = $count;
 3542:                 }
 3543:                 $ordered{$order} = $rolename;
 3544:                 $count++;
 3545:             }
 3546:         }
 3547:         my $maxnum = scalar(keys(%ordered));
 3548:         my @roles_by_num = ();
 3549:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3550:             push(@roles_by_num,$item);
 3551:         }
 3552:         my $context = 'domprefs';
 3553:         my $crstype = 'Course';
 3554:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3555:         my @accesstypes = ('all','dh','da','none');
 3556:         my ($numstatustypes,@jsarray);
 3557:         if (ref($types) eq 'ARRAY') {
 3558:             if (@{$types} > 0) {
 3559:                 $numstatustypes = scalar(@{$types});
 3560:                 push(@accesstypes,'status');
 3561:                 @jsarray = ('bystatus');
 3562:             }
 3563:         }
 3564:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3565:         if (keys(%domhelpdesk)) {
 3566:             push(@accesstypes,('inc','exc'));
 3567:             push(@jsarray,('notinc','notexc'));
 3568:         }
 3569:         my $hiddenstr = join("','",@jsarray);
 3570:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3571:         my $context = 'domprefs';
 3572:         my $crstype = 'Course';
 3573:         my $prefix = 'helproles_';
 3574:         my $add_class = 'LC_hidden';
 3575:         foreach my $num (@roles_by_num) {
 3576:             my $role = $ordered{$num};
 3577:             my ($desc,$access,@statuses);
 3578:             if (ref($current{$role}) eq 'HASH') {
 3579:                 $desc = $current{$role}{'desc'};
 3580:                 $access = $current{$role}{'access'};
 3581:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3582:                     @statuses = @{$current{$role}{'insttypes'}};
 3583:                 }
 3584:             }
 3585:             if ($desc eq '') {
 3586:                 $desc = $role;
 3587:             }
 3588:             my $identifier = 'custhelp'.$num;
 3589:             my %full=();
 3590:             my %levels= (
 3591:                          course => {},
 3592:                          domain => {},
 3593:                          system => {},
 3594:                         );
 3595:             my %levelscurrent=(
 3596:                                course => {},
 3597:                                domain => {},
 3598:                                system => {},
 3599:                               );
 3600:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3601:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3602:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3603:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3604:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3605:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3606:             for (my $k=0; $k<=$maxnum; $k++) {
 3607:                 my $vpos = $k+1;
 3608:                 my $selstr;
 3609:                 if ($k == $num) {
 3610:                     $selstr = ' selected="selected" ';
 3611:                 }
 3612:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3613:             }
 3614:             $datatable .= '</select>'.('&nbsp;'x2).
 3615:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3616:                           '</td>'.
 3617:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3618:                           &mt('Name shown to users:').
 3619:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3620:                           '</fieldset>'.
 3621:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3622:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3623:                           '<fieldset>'.
 3624:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3625:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3626:                                                                    \%levelscurrent,$identifier,
 3627:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3628:                           '</fieldset></td>';
 3629:             $itemcount ++;
 3630:         }
 3631:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3632:         my $newcust = 'custhelp'.$count;
 3633:         my (%privs,%levelscurrent);
 3634:         my %full=();
 3635:         my %levels= (
 3636:                      course => {},
 3637:                      domain => {},
 3638:                      system => {},
 3639:                     );
 3640:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3641:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3642:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3643:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3644:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3645:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3646:         for (my $k=0; $k<$maxnum+1; $k++) {
 3647:             my $vpos = $k+1;
 3648:             my $selstr;
 3649:             if ($k == $maxnum) {
 3650:                 $selstr = ' selected="selected" ';
 3651:             }
 3652:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3653:         }
 3654:         $datatable .= '</select>&nbsp;'."\n".
 3655:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3656:                       '</label></span></td>'.
 3657:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3658:                       '<span class="LC_nobreak">'.
 3659:                       &mt('Internal name:').
 3660:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3661:                       '</span>'.('&nbsp;'x4).
 3662:                       '<span class="LC_nobreak">'.
 3663:                       &mt('Name shown to users:').
 3664:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3665:                       '</span></fieldset>'.
 3666:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3667:                                              $usertypes,$types,\%domhelpdesk).
 3668:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3669:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3670:                                                                 \@templateroles,$newcust).
 3671:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3672:                                                                \%levelscurrent,$newcust).
 3673:                       '</fieldset>'.
 3674:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 3675:                       '</td></tr>';
 3676:         $count ++;
 3677:         $$rowtotal += $count;
 3678:     }
 3679:     return $datatable;
 3680: }
 3681: 
 3682: sub adhocbutton {
 3683:     my ($prefix,$num,$field,$visibility) = @_;
 3684:     my %lt = &Apache::lonlocal::texthash(
 3685:                                           show => 'Show details',
 3686:                                           hide => 'Hide details',
 3687:                                         );
 3688:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3689:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3690:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3691:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3692: }
 3693: 
 3694: sub helpsettings_javascript {
 3695:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3696:     return unless(ref($roles_by_num) eq 'ARRAY');
 3697:     my %html_js_lt = &Apache::lonlocal::texthash(
 3698:                                           show => 'Show details',
 3699:                                           hide => 'Hide details',
 3700:                                         );
 3701:     &html_escape(\%html_js_lt);
 3702:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3703:     return <<"ENDSCRIPT";
 3704: <script type="text/javascript">
 3705: // <![CDATA[
 3706: 
 3707: function reorderHelpRoles(form,item) {
 3708:     var changedVal;
 3709: $jstext
 3710:     var newpos = 'helproles_${total}_pos';
 3711:     var maxh = 1 + $total;
 3712:     var current = new Array();
 3713:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3714:     if (item == newpos) {
 3715:         changedVal = newitemVal;
 3716:     } else {
 3717:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3718:         current[newitemVal] = newpos;
 3719:     }
 3720:     for (var i=0; i<helproles.length; i++) {
 3721:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3722:         if (elementName != item) {
 3723:             if (form.elements[elementName]) {
 3724:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3725:                 current[currVal] = elementName;
 3726:             }
 3727:         }
 3728:     }
 3729:     var oldVal;
 3730:     for (var j=0; j<maxh; j++) {
 3731:         if (current[j] == undefined) {
 3732:             oldVal = j;
 3733:         }
 3734:     }
 3735:     if (oldVal < changedVal) {
 3736:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3737:            var elementName = current[k];
 3738:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3739:         }
 3740:     } else {
 3741:         for (var k=changedVal; k<oldVal; k++) {
 3742:             var elementName = current[k];
 3743:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3744:         }
 3745:     }
 3746:     return;
 3747: }
 3748: 
 3749: function helpdeskAccess(num) {
 3750:     var curraccess = null;
 3751:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3752:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3753:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3754:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3755:             }
 3756:         }
 3757:     }
 3758:     var shown = Array();
 3759:     var hidden = Array();
 3760:     if (curraccess == 'none') {
 3761:         hidden = Array('$hiddenstr');
 3762:     } else {
 3763:         if (curraccess == 'status') {
 3764:             shown = Array('bystatus');
 3765:             hidden = Array('notinc','notexc');
 3766:         } else {
 3767:             if (curraccess == 'exc') {
 3768:                 shown = Array('notexc');
 3769:                 hidden = Array('notinc','bystatus');
 3770:             }
 3771:             if (curraccess == 'inc') {
 3772:                 shown = Array('notinc');
 3773:                 hidden = Array('notexc','bystatus');
 3774:             }
 3775:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3776:                 hidden = Array('notinc','notexc','bystatus');
 3777:             }
 3778:         }
 3779:     }
 3780:     if (hidden.length > 0) {
 3781:         for (var i=0; i<hidden.length; i++) {
 3782:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3783:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3784:             }
 3785:         }
 3786:     }
 3787:     if (shown.length > 0) {
 3788:         for (var i=0; i<shown.length; i++) {
 3789:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3790:                 if (shown[i] == 'privs') {
 3791:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3792:                 } else {
 3793:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3794:                 }
 3795:             }
 3796:         }
 3797:     }
 3798:     return;
 3799: }
 3800: 
 3801: function toggleHelpdeskItem(num,field) {
 3802:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3803:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3804:             document.getElementById('helproles_'+num+'_'+field).className =
 3805:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3806:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3807:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3808:             }
 3809:         } else {
 3810:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3811:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3812:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3813:             }
 3814:         }
 3815:     }
 3816:     return;
 3817: }
 3818: 
 3819: // ]]>
 3820: </script>
 3821: 
 3822: ENDSCRIPT
 3823: }
 3824: 
 3825: sub helpdeskroles_access {
 3826:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3827:         $usertypes,$types,$domhelpdesk) = @_;
 3828:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3829:     my %lt = &Apache::lonlocal::texthash(
 3830:                     'rou'    => 'Role usage',
 3831:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3832:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3833:                     'dh'     => 'All with domain helpdesk role',
 3834:                     'da'     => 'All with domain helpdesk assistant role',
 3835:                     'none'   => 'None',
 3836:                     'status' => 'Determined based on institutional status',
 3837:                     'inc'    => 'Include all, but exclude specific personnel',
 3838:                     'exc'    => 'Exclude all, but include specific personnel',
 3839:                   );
 3840:     my %usecheck = (
 3841:                      all => ' checked="checked"',
 3842:                    );
 3843:     my %displaydiv = (
 3844:                       status => 'none',
 3845:                       inc    => 'none',
 3846:                       exc    => 'none',
 3847:                       priv   => 'block',
 3848:                      );
 3849:     my $output;
 3850:     if (ref($current) eq 'HASH') {
 3851:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3852:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3853:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3854:                 delete($usecheck{'all'});
 3855:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3856:                     my $access = $1;
 3857:                     $displaydiv{$access} = 'inline';
 3858:                 } elsif ($current->{access} eq 'none') {
 3859:                     $displaydiv{'priv'} = 'none';
 3860:                 }
 3861:             }
 3862:         }
 3863:     }
 3864:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3865:               '<p>'.$lt{'whi'}.'</p>';
 3866:     foreach my $access (@{$accesstypes}) {
 3867:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3868:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3869:                    $lt{$access}.'</label>';
 3870:         if ($access eq 'status') {
 3871:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3872:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3873:                                                                  $othertitle,$usertypes,$types).
 3874:                        '</div>';
 3875:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3876:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3877:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3878:                        '</div>';
 3879:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3880:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3881:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3882:                        '</div>';
 3883:         }
 3884:         $output .= '</p>';
 3885:     }
 3886:     $output .= '</fieldset>';
 3887:     return $output;
 3888: }
 3889: 
 3890: sub radiobutton_prefs {
 3891:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3892:         $additional,$align) = @_;
 3893:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3894:                    (ref($choices) eq 'HASH'));
 3895: 
 3896:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3897: 
 3898:     foreach my $item (@{$toggles}) {
 3899:         if ($defaultchecked->{$item} eq 'on') {
 3900:             $checkedon{$item} = ' checked="checked" ';
 3901:             $checkedoff{$item} = ' ';
 3902:         } elsif ($defaultchecked->{$item} eq 'off') {
 3903:             $checkedoff{$item} = ' checked="checked" ';
 3904:             $checkedon{$item} = ' ';
 3905:         }
 3906:     }
 3907:     if (ref($settings) eq 'HASH') {
 3908:         foreach my $item (@{$toggles}) {
 3909:             if ($settings->{$item} eq '1') {
 3910:                 $checkedon{$item} =  ' checked="checked" ';
 3911:                 $checkedoff{$item} = ' ';
 3912:             } elsif ($settings->{$item} eq '0') {
 3913:                 $checkedoff{$item} =  ' checked="checked" ';
 3914:                 $checkedon{$item} = ' ';
 3915:             }
 3916:         }
 3917:     }
 3918:     if ($onclick) {
 3919:         $onclick = ' onclick="'.$onclick.'"';
 3920:     }
 3921:     foreach my $item (@{$toggles}) {
 3922:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3923:         $datatable .=
 3924:             '<tr'.$css_class.'><td valign="top">'.
 3925:             '<span class="LC_nobreak">'.$choices->{$item}.
 3926:             '</span></td>';
 3927:         if ($align eq 'left') {
 3928:             $datatable .= '<td class="LC_left_item">';
 3929:         } else {
 3930:             $datatable .= '<td class="LC_right_item">';
 3931:         }
 3932:         $datatable .=
 3933:             '<span class="LC_nobreak">'.
 3934:             '<label><input type="radio" name="'.
 3935:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3936:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3937:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3938:             '</span>'.$additional.
 3939:             '</td>'.
 3940:             '</tr>';
 3941:         $itemcount ++;
 3942:     }
 3943:     return ($datatable,$itemcount);
 3944: }
 3945: 
 3946: sub print_ltitools {
 3947:     my ($dom,$settings,$rowtotal) = @_;
 3948:     my $rownum = 0;
 3949:     my $css_class;
 3950:     my $itemcount = 1;
 3951:     my $maxnum = 0;
 3952:     my %ordered;
 3953:     if (ref($settings) eq 'HASH') {
 3954:         foreach my $item (keys(%{$settings})) {
 3955:             if (ref($settings->{$item}) eq 'HASH') {
 3956:                 my $num = $settings->{$item}{'order'};
 3957:                 $ordered{$num} = $item;
 3958:             }
 3959:         }
 3960:     }
 3961:     my $confname = $dom.'-domainconfig';
 3962:     my $switchserver = &check_switchserver($dom,$confname);
 3963:     my $maxnum = scalar(keys(%ordered));
 3964:     my $datatable;
 3965:     my %lt = &ltitools_names();
 3966:     my @courseroles = ('cc','in','ta','ep','st');
 3967:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3968:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 3969:     if (keys(%ordered)) {
 3970:         my @items = sort { $a <=> $b } keys(%ordered);
 3971:         for (my $i=0; $i<@items; $i++) {
 3972:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3973:             my $item = $ordered{$items[$i]};
 3974:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 3975:             if (ref($settings->{$item}) eq 'HASH') {
 3976:                 $title = $settings->{$item}->{'title'};
 3977:                 $url = $settings->{$item}->{'url'};
 3978:                 $key = $settings->{$item}->{'key'};
 3979:                 $secret = $settings->{$item}->{'secret'};
 3980:                 $lifetime = $settings->{$item}->{'lifetime'};
 3981:                 my $image = $settings->{$item}->{'image'};
 3982:                 if ($image ne '') {
 3983:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3984:                 }
 3985:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 3986:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 3987:                 } else {
 3988:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 3989:                 }
 3990:             }
 3991:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 3992:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3993:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3994:             for (my $k=0; $k<=$maxnum; $k++) {
 3995:                 my $vpos = $k+1;
 3996:                 my $selstr;
 3997:                 if ($k == $i) {
 3998:                     $selstr = ' selected="selected" ';
 3999:                 }
 4000:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4001:             }
 4002:             $datatable .= '</select>'.('&nbsp;'x2).
 4003:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4004:                 &mt('Delete?').'</label></span></td>'.
 4005:                 '<td colspan="2">'.
 4006:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4007:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4008:                 ('&nbsp;'x2).
 4009:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4010:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4011:                 ('&nbsp;'x2).
 4012:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4013:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4014:                 ('&nbsp;'x2).
 4015:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4016:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4017:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4018:                 '<br /><br />'.
 4019:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4020:                 ' value="'.$url.'" /></span>'.
 4021:                 ('&nbsp;'x2).
 4022:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4023:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4024:                 ('&nbsp;'x2).
 4025:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4026:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4027:                 ('&nbsp;'x2).
 4028:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4029:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4030:                 '<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>'.
 4031:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4032:                 '</fieldset>'.
 4033:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4034:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4035:             my %currdisp;
 4036:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4037:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4038:                     $currdisp{'window'} = ' checked="checked"';
 4039:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4040:                     $currdisp{'tab'} = ' checked="checked"';
 4041:                 } else {
 4042:                     $currdisp{'iframe'} = ' checked="checked"';
 4043:                 }
 4044:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4045:                     $currdisp{'width'} = $1;
 4046:                 }
 4047:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4048:                      $currdisp{'height'} = $1;
 4049:                 }
 4050:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4051:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4052:             } else {
 4053:                 $currdisp{'iframe'} = ' checked="checked"';
 4054:             }
 4055:             foreach my $disp ('iframe','tab','window') {
 4056:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4057:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4058:             }
 4059:             $datatable .= ('&nbsp;'x4);
 4060:             foreach my $dimen ('width','height') {
 4061:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4062:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4063:                               ('&nbsp;'x2);
 4064:             }
 4065:             $datatable .= '</span><br />'.
 4066:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4067:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4068:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4069:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4070:                           '</textarea></div><div style=""></div>'.
 4071:                           '<div style="padding:0;clear:both;margin:0;border:0"></div>';
 4072:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4073:             if ($imgsrc) {
 4074:                 $datatable .= $imgsrc.
 4075:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4076:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4077:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4078:             } else {
 4079:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4080:             }
 4081:             if ($switchserver) {
 4082:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4083:             } else {
 4084:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4085:             }
 4086:             $datatable .= '</span></fieldset>';
 4087:             my (%checkedfields,%rolemaps,$userincdom);
 4088:             if (ref($settings->{$item}) eq 'HASH') {
 4089:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4090:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4091:                 }
 4092:                 $userincdom = $settings->{$item}->{'incdom'};
 4093:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4094:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4095:                     $checkedfields{'roles'} = 1;
 4096:                 }
 4097:             }
 4098:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4099:                           '<span class="LC_nobreak">';
 4100:             my $userfieldstyle = 'display:none;';
 4101:             my $seluserdom = '';
 4102:             my $unseluserdom = ' selected="selected"';
 4103:             foreach my $field (@fields) {
 4104:                 my ($checked,$onclick,$id,$spacer);
 4105:                 if ($checkedfields{$field}) {
 4106:                     $checked = ' checked="checked"';
 4107:                 }
 4108:                 if ($field eq 'user') {
 4109:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4110:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4111:                     if ($checked) {
 4112:                         $userfieldstyle = 'display:inline-block';
 4113:                         if ($userincdom) {
 4114:                             $seluserdom = $unseluserdom;
 4115:                             $unseluserdom = '';
 4116:                         }
 4117:                     }
 4118:                 } else {
 4119:                     $spacer = ('&nbsp;' x2);
 4120:                 }
 4121:                 $datatable .= '<label>'.
 4122:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4123:                               $lt{$field}.'</label>'.$spacer;
 4124:             }
 4125:             $datatable .= '</span>';
 4126:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4127:                           '<span class="LC_nobreak"> : '.
 4128:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4129:                           '<option value="">'.&mt('Select').'</option>'.
 4130:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4131:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4132:                           '</select></span></div>';
 4133:             $datatable .= '</fieldset>'.
 4134:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4135:             foreach my $role (@courseroles) {
 4136:                 my ($selected,$selectnone);
 4137:                 if (!$rolemaps{$role}) {
 4138:                     $selectnone = ' selected="selected"';
 4139:                 }
 4140:                 $datatable .= '<td align="center">'.
 4141:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4142:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4143:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4144:                 foreach my $ltirole (@ltiroles) {
 4145:                     unless ($selectnone) {
 4146:                         if ($rolemaps{$role} eq $ltirole) {
 4147:                             $selected = ' selected="selected"';
 4148:                         } else {
 4149:                             $selected = '';
 4150:                         }
 4151:                     }
 4152:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4153:                 }
 4154:                 $datatable .= '</select></td>';
 4155:             }
 4156:             $datatable .= '</tr></table></fieldset>';
 4157:             my %courseconfig;
 4158:             if (ref($settings->{$item}) eq 'HASH') {
 4159:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4160:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4161:                 }
 4162:             }
 4163:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4164:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4165:                 my $checked;
 4166:                 if ($courseconfig{$item}) {
 4167:                     $checked = ' checked="checked"';
 4168:                 }
 4169:                 $datatable .= '<label>'.
 4170:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4171:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4172:             }
 4173:             $datatable .= '</span></fieldset>'.
 4174:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4175:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4176:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4177:                 my %custom = %{$settings->{$item}->{'custom'}};
 4178:                 if (keys(%custom) > 0) {
 4179:                     foreach my $key (sort(keys(%custom))) {
 4180:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4181:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4182:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4183:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4184:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4185:                     }
 4186:                 }
 4187:             }
 4188:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4189:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4190:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4191:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4192:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4193:             $itemcount ++;
 4194:         }
 4195:     }
 4196:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4197:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4198:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4199:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4200:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4201:     for (my $k=0; $k<$maxnum+1; $k++) {
 4202:         my $vpos = $k+1;
 4203:         my $selstr;
 4204:         if ($k == $maxnum) {
 4205:             $selstr = ' selected="selected" ';
 4206:         }
 4207:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4208:     }
 4209:     $datatable .= '</select>&nbsp;'."\n".
 4210:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4211:                   '<td colspan="2">'.
 4212:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4213:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4214:                   ('&nbsp;'x2).
 4215:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4216:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4217:                   ('&nbsp;'x2).
 4218:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4219:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4220:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4221:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4222:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4223:                   '<br />'.
 4224:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4225:                   ('&nbsp;'x2).
 4226:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4227:                   ('&nbsp;'x2).
 4228:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4229:                   ('&nbsp;'x2).
 4230:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4231:                   '<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".
 4232:                   '</fieldset>'.
 4233:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4234:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4235:     my %defaultdisp;
 4236:     $defaultdisp{'iframe'} = ' checked="checked"';
 4237:     foreach my $disp ('iframe','tab','window') {
 4238:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4239:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4240:     }
 4241:     $datatable .= ('&nbsp;'x4);
 4242:     foreach my $dimen ('width','height') {
 4243:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4244:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4245:                       ('&nbsp;'x2);
 4246:     }
 4247:     $datatable .= '</span><br />'.
 4248:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4249:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 4250:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4251:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4252:                   '</div><div style=""></div>'.
 4253:                   '<div style="padding:0;clear:both;margin:0;border:0"></div>';
 4254:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4255:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4256:     if ($switchserver) {
 4257:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4258:     } else {
 4259:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4260:     }
 4261:     $datatable .= '</span></fieldset>'.
 4262:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4263:                   '<span class="LC_nobreak">';
 4264:     foreach my $field (@fields) {
 4265:         my ($id,$onclick,$spacer);
 4266:         if ($field eq 'user') {
 4267:             $id = ' id="ltitools_user_field_add"';
 4268:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 4269:         } else {
 4270:             $spacer = ('&nbsp;' x2);
 4271:         }
 4272:         $datatable .= '<label>'.
 4273:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 4274:                       $lt{$field}.'</label>'.$spacer;
 4275:     }
 4276:     $datatable .= '</span>'.
 4277:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 4278:                   '<span class="LC_nobreak"> : '.
 4279:                   '<select name="ltitools_userincdom_add">'.
 4280:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 4281:                   '<option value="0">'.&mt('username').'</option>'.
 4282:                   '<option value="1">'.&mt('username:domain').'</option>'.
 4283:                   '</select></span></div></fieldset>';
 4284:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4285:     foreach my $role (@courseroles) {
 4286:         my ($checked,$checkednone);
 4287:         $datatable .= '<td align="center">'.
 4288:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4289:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4290:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4291:         foreach my $ltirole (@ltiroles) {
 4292:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4293:         }
 4294:         $datatable .= '</select></td>';
 4295:     }
 4296:     $datatable .= '</tr></table></fieldset>'.
 4297:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4298:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4299:         $datatable .= '<label>'.
 4300:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4301:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4302:     }
 4303:     $datatable .= '</span></fieldset>'.
 4304:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4305:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4306:                   '<tr><td><span class="LC_nobreak">'.
 4307:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4308:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4309:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4310:                   '</table></fieldset>'."\n".
 4311:                   '</td>'."\n".
 4312:                   '</tr>'."\n";
 4313:     $itemcount ++;
 4314:     return $datatable;
 4315: }
 4316: 
 4317: sub ltitools_names {
 4318:     my %lt = &Apache::lonlocal::texthash(
 4319:                                           'title'          => 'Title',
 4320:                                           'version'        => 'Version',
 4321:                                           'msgtype'        => 'Message Type',
 4322:                                           'sigmethod'      => 'Signature Method',
 4323:                                           'url'            => 'URL',
 4324:                                           'key'            => 'Key',
 4325:                                           'lifetime'       => 'Nonce lifetime (s)',
 4326:                                           'secret'         => 'Secret',
 4327:                                           'icon'           => 'Icon',
 4328:                                           'user'           => 'User',
 4329:                                           'fullname'       => 'Full Name',
 4330:                                           'firstname'      => 'First Name',
 4331:                                           'lastname'       => 'Last Name',
 4332:                                           'email'          => 'E-mail',
 4333:                                           'roles'          => 'Role',
 4334:                                           'window'         => 'Window',
 4335:                                           'tab'            => 'Tab',
 4336:                                           'iframe'         => 'iFrame',
 4337:                                           'height'         => 'Height',
 4338:                                           'width'          => 'Width',
 4339:                                           'linktext'       => 'Default Link Text',
 4340:                                           'explanation'    => 'Default Explanation',
 4341:                                           'crstarget'      => 'Display target',
 4342:                                           'crslabel'       => 'Course label',
 4343:                                           'crstitle'       => 'Course title',
 4344:                                           'crslinktext'    => 'Link Text',
 4345:                                           'crsexplanation' => 'Explanation',
 4346:                                           'crsappend'      => 'Provider URL',
 4347:                                         );
 4348: 
 4349:     return %lt;
 4350: }
 4351: 
 4352: sub print_coursedefaults {
 4353:     my ($position,$dom,$settings,$rowtotal) = @_;
 4354:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4355:     my $itemcount = 1;
 4356:     my %choices =  &Apache::lonlocal::texthash (
 4357:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4358:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4359:         coursecredits        => 'Credits can be specified for courses',
 4360:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4361:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4362:         texengine            => 'Default method to display mathematics',
 4363:         postsubmit           => 'Disable submit button/keypress following student submission',
 4364:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4365:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4366:     );
 4367:     my %staticdefaults = (
 4368:                            anonsurvey_threshold => 10,
 4369:                            uploadquota          => 500,
 4370:                            postsubmit           => 60,
 4371:                            mysqltables          => 172800,
 4372:                          );
 4373:     if ($position eq 'top') {
 4374:         %defaultchecked = (
 4375:                             'uselcmath'       => 'on',
 4376:                             'usejsme'         => 'on',
 4377:                             'canclone'        => 'none',
 4378:                           );
 4379:         @toggles = ('uselcmath','usejsme');
 4380:         my $deftex = $Apache::lonnet::deftex;
 4381:         if (ref($settings) eq 'HASH') {
 4382:             if ($settings->{'texengine'}) {
 4383:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 4384:                     $deftex = $settings->{'texengine'};
 4385:                 }
 4386:             }
 4387:         }
 4388:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4389:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 4390:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 4391:                        '</span></td><td class="LC_right_item">'.
 4392:                        '<select name="texengine">'."\n";
 4393:         my %texoptions = (
 4394:                             MathJax  => 'MathJax',
 4395:                             mimetex  => &mt('Convert to Images'),
 4396:                             tth      => &mt('TeX to HTML'),
 4397:                          );
 4398:         foreach my $renderer ('MathJax','mimetex','tth') {
 4399:             my $selected = '';
 4400:             if ($renderer eq $deftex) {
 4401:                 $selected = ' selected="selected"';
 4402:             }
 4403:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 4404:         }
 4405:         $mathdisp .= '</select></td></tr>'."\n";
 4406:         $itemcount ++;
 4407:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4408:                                                      \%choices,$itemcount);
 4409:         $datatable = $mathdisp.$datatable;
 4410:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4411:         $datatable .=
 4412:             '<tr'.$css_class.'><td valign="top">'.
 4413:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4414:             '</span></td><td class="LC_left_item">';
 4415:         my $currcanclone = 'none';
 4416:         my $onclick;
 4417:         my @cloneoptions = ('none','domain');
 4418:         my %clonetitles = &Apache::lonlocal::texthash (
 4419:                              none     => 'No additional course requesters',
 4420:                              domain   => "Any course requester in course's domain",
 4421:                              instcode => 'Course requests for official courses ...',
 4422:                           );
 4423:         my (%codedefaults,@code_order,@posscodes);
 4424:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4425:                                                     \@code_order) eq 'ok') {
 4426:             if (@code_order > 0) {
 4427:                 push(@cloneoptions,'instcode');
 4428:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4429:             }
 4430:         }
 4431:         if (ref($settings) eq 'HASH') {
 4432:             if ($settings->{'canclone'}) {
 4433:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4434:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4435:                         if (@code_order > 0) {
 4436:                             $currcanclone = 'instcode';
 4437:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4438:                         }
 4439:                     }
 4440:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4441:                     $currcanclone = $settings->{'canclone'};
 4442:                 }
 4443:             }
 4444:         }
 4445:         foreach my $option (@cloneoptions) {
 4446:             my ($checked,$additional);
 4447:             if ($currcanclone eq $option) {
 4448:                 $checked = ' checked="checked"';
 4449:             }
 4450:             if ($option eq 'instcode') {
 4451:                 if (@code_order) {
 4452:                     my $show = 'none';
 4453:                     if ($checked) {
 4454:                         $show = 'block';
 4455:                     }
 4456:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4457:                                   &mt('Institutional codes for new and cloned course have identical:').
 4458:                                   '<br />';
 4459:                     foreach my $item (@code_order) {
 4460:                         my $codechk;
 4461:                         if ($checked) {
 4462:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4463:                                 $codechk = ' checked="checked"';
 4464:                             }
 4465:                         }
 4466:                         $additional .= '<label>'.
 4467:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4468:                                        $item.'</label>';
 4469:                     }
 4470:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4471:                 }
 4472:             }
 4473:             $datatable .=
 4474:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4475:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4476:                 '</label>&nbsp;'.$additional.'</span><br />';
 4477:         }
 4478:         $datatable .= '</td>'.
 4479:                       '</tr>';
 4480:         $itemcount ++;
 4481:     } else {
 4482:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4483:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4484:         my $currusecredits = 0;
 4485:         my $postsubmitclient = 1;
 4486:         my @types = ('official','unofficial','community','textbook');
 4487:         if (ref($settings) eq 'HASH') {
 4488:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4489:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4490:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4491:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4492:                 }
 4493:             }
 4494:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4495:                 foreach my $type (@types) {
 4496:                     next if ($type eq 'community');
 4497:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4498:                     if ($defcredits{$type} ne '') {
 4499:                         $currusecredits = 1;
 4500:                     }
 4501:                 }
 4502:             }
 4503:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4504:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4505:                     $postsubmitclient = 0;
 4506:                     foreach my $type (@types) {
 4507:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4508:                     }
 4509:                 } else {
 4510:                     foreach my $type (@types) {
 4511:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4512:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4513:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4514:                             } else {
 4515:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4516:                             }
 4517:                         } else {
 4518:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4519:                         }
 4520:                     }
 4521:                 }
 4522:             } else {
 4523:                 foreach my $type (@types) {
 4524:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4525:                 }
 4526:             }
 4527:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4528:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4529:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4530:                 }
 4531:             } else {
 4532:                 foreach my $type (@types) {
 4533:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4534:                 }
 4535:             }
 4536:         } else {
 4537:             foreach my $type (@types) {
 4538:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4539:             }
 4540:         }
 4541:         if (!$currdefresponder) {
 4542:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4543:         } elsif ($currdefresponder < 1) {
 4544:             $currdefresponder = 1;
 4545:         }
 4546:         foreach my $type (@types) {
 4547:             if ($curruploadquota{$type} eq '') {
 4548:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4549:             }
 4550:         }
 4551:         $datatable .=
 4552:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4553:                 $choices{'anonsurvey_threshold'}.
 4554:                 '</span></td>'.
 4555:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4556:                 '<input type="text" name="anonsurvey_threshold"'.
 4557:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4558:                 '</td></tr>'."\n";
 4559:         $itemcount ++;
 4560:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4561:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4562:                       $choices{'uploadquota'}.
 4563:                       '</span></td>'.
 4564:                       '<td align="right" class="LC_right_item">'.
 4565:                       '<table><tr>';
 4566:         foreach my $type (@types) {
 4567:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4568:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4569:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4570:         }
 4571:         $datatable .= '</tr></table></td></tr>'."\n";
 4572:         $itemcount ++;
 4573:         my $onclick = "toggleDisplay(this.form,'credits');";
 4574:         my $display = 'none';
 4575:         if ($currusecredits) {
 4576:             $display = 'block';
 4577:         }
 4578:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4579:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4580:         foreach my $type (@types) {
 4581:             next if ($type eq 'community');
 4582:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4583:                            '<input type="text" name="'.$type.'_credits"'.
 4584:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4585:         }
 4586:         $additional .= '</tr></table></div>'."\n";
 4587:         %defaultchecked = ('coursecredits' => 'off');
 4588:         @toggles = ('coursecredits');
 4589:         my $current = {
 4590:                         'coursecredits' => $currusecredits,
 4591:                       };
 4592:         (my $table,$itemcount) =
 4593:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4594:                                \%choices,$itemcount,$onclick,$additional,'left');
 4595:         $datatable .= $table;
 4596:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4597:         my $display = 'none';
 4598:         if ($postsubmitclient) {
 4599:             $display = 'block';
 4600:         }
 4601:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4602:                       &mt('Number of seconds submit is disabled').'<br />'.
 4603:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4604:                       '<table><tr>';
 4605:         foreach my $type (@types) {
 4606:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4607:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4608:                            $deftimeout{$type}.'" size="5" /></td>';
 4609:         }
 4610:         $additional .= '</tr></table></div>'."\n";
 4611:         %defaultchecked = ('postsubmit' => 'on');
 4612:         @toggles = ('postsubmit');
 4613:         $current = {
 4614:                        'postsubmit' => $postsubmitclient,
 4615:                    };
 4616:         ($table,$itemcount) =
 4617:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4618:                                \%choices,$itemcount,$onclick,$additional,'left');
 4619:         $datatable .= $table;
 4620:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4621:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4622:                       $choices{'mysqltables'}.
 4623:                       '</span></td>'.
 4624:                       '<td align="right" class="LC_right_item">'.
 4625:                       '<table><tr>';
 4626:         foreach my $type (@types) {
 4627:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4628:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4629:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4630:         }
 4631:         $datatable .= '</tr></table></td></tr>'."\n";
 4632:         $itemcount ++;
 4633: 
 4634:     }
 4635:     $$rowtotal += $itemcount;
 4636:     return $datatable;
 4637: }
 4638: 
 4639: sub print_selfenrollment {
 4640:     my ($position,$dom,$settings,$rowtotal) = @_;
 4641:     my ($css_class,$datatable);
 4642:     my $itemcount = 1;
 4643:     my @types = ('official','unofficial','community','textbook');
 4644:     if (($position eq 'top') || ($position eq 'middle')) {
 4645:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4646:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4647:         my @rows;
 4648:         my $key;
 4649:         if ($position eq 'top') {
 4650:             $key = 'admin'; 
 4651:             if (ref($rowsref) eq 'ARRAY') {
 4652:                 @rows = @{$rowsref};
 4653:             }
 4654:         } elsif ($position eq 'middle') {
 4655:             $key = 'default';
 4656:             @rows = ('types','registered','approval','limit');
 4657:         }
 4658:         foreach my $row (@rows) {
 4659:             if (defined($titlesref->{$row})) {
 4660:                 $itemcount ++;
 4661:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4662:                 $datatable .= '<tr'.$css_class.'>'.
 4663:                               '<td>'.$titlesref->{$row}.'</td>'.
 4664:                               '<td class="LC_left_item">'.
 4665:                               '<table><tr>';
 4666:                 my (%current,%currentcap);
 4667:                 if (ref($settings) eq 'HASH') {
 4668:                     if (ref($settings->{$key}) eq 'HASH') {
 4669:                         foreach my $type (@types) {
 4670:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4671:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4672:                             }
 4673:                             if (($row eq 'limit') && ($key eq 'default')) {
 4674:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4675:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4676:                                 }
 4677:                             }
 4678:                         }
 4679:                     }
 4680:                 }
 4681:                 my %roles = (
 4682:                              '0' => &Apache::lonnet::plaintext('dc'),
 4683:                             ); 
 4684:             
 4685:                 foreach my $type (@types) {
 4686:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4687:                         $datatable .= '<th>'.&mt($type).'</th>';
 4688:                     }
 4689:                 }
 4690:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4691:                     $datatable .= '</tr><tr>';
 4692:                 }
 4693:                 foreach my $type (@types) {
 4694:                     if ($type eq 'community') {
 4695:                         $roles{'1'} = &mt('Community personnel');
 4696:                     } else {
 4697:                         $roles{'1'} = &mt('Course personnel');
 4698:                     }
 4699:                     $datatable .= '<td style="vertical-align: top">';
 4700:                     if ($position eq 'top') {
 4701:                         my %checked;
 4702:                         if ($current{$type} eq '0') {
 4703:                             $checked{'0'} = ' checked="checked"';
 4704:                         } else {
 4705:                             $checked{'1'} = ' checked="checked"';
 4706:                         }
 4707:                         foreach my $role ('1','0') {
 4708:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4709:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4710:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4711:                                           $roles{$role}.'</label></span> ';
 4712:                         }
 4713:                     } else {
 4714:                         if ($row eq 'types') {
 4715:                             my %checked;
 4716:                             if ($current{$type} =~ /^(all|dom)$/) {
 4717:                                 $checked{$1} = ' checked="checked"';
 4718:                             } else {
 4719:                                 $checked{''} = ' checked="checked"';
 4720:                             }
 4721:                             foreach my $val ('','dom','all') {
 4722:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4723:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4724:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4725:                             }
 4726:                         } elsif ($row eq 'registered') {
 4727:                             my %checked;
 4728:                             if ($current{$type} eq '1') {
 4729:                                 $checked{'1'} = ' checked="checked"';
 4730:                             } else {
 4731:                                 $checked{'0'} = ' checked="checked"';
 4732:                             }
 4733:                             foreach my $val ('0','1') {
 4734:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4735:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4736:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4737:                             }
 4738:                         } elsif ($row eq 'approval') {
 4739:                             my %checked;
 4740:                             if ($current{$type} =~ /^([12])$/) {
 4741:                                 $checked{$1} = ' checked="checked"';
 4742:                             } else {
 4743:                                 $checked{'0'} = ' checked="checked"';
 4744:                             }
 4745:                             for my $val (0..2) {
 4746:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4747:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4748:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4749:                             }
 4750:                         } elsif ($row eq 'limit') {
 4751:                             my %checked;
 4752:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4753:                                 $checked{$1} = ' checked="checked"';
 4754:                             } else {
 4755:                                 $checked{'none'} = ' checked="checked"';
 4756:                             }
 4757:                             my $cap;
 4758:                             if ($currentcap{$type} =~ /^\d+$/) {
 4759:                                 $cap = $currentcap{$type};
 4760:                             }
 4761:                             foreach my $val ('none','allstudents','selfenrolled') {
 4762:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4763:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4764:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4765:                             }
 4766:                             $datatable .= '<br />'.
 4767:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4768:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4769:                                           '</span>'; 
 4770:                         }
 4771:                     }
 4772:                     $datatable .= '</td>';
 4773:                 }
 4774:                 $datatable .= '</tr>';
 4775:             }
 4776:             $datatable .= '</table></td></tr>';
 4777:         }
 4778:     } elsif ($position eq 'bottom') {
 4779:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4780:     }
 4781:     $$rowtotal += $itemcount;
 4782:     return $datatable;
 4783: }
 4784: 
 4785: sub print_validation_rows {
 4786:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4787:     my ($itemsref,$namesref,$fieldsref);
 4788:     if ($caller eq 'selfenroll') { 
 4789:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4790:     } elsif ($caller eq 'requestcourses') {
 4791:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4792:     }
 4793:     my %currvalidation;
 4794:     if (ref($settings) eq 'HASH') {
 4795:         if (ref($settings->{'validation'}) eq 'HASH') {
 4796:             %currvalidation = %{$settings->{'validation'}};
 4797:         }
 4798:     }
 4799:     my $datatable;
 4800:     my $itemcount = 0;
 4801:     foreach my $item (@{$itemsref}) {
 4802:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4803:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4804:                       $namesref->{$item}.
 4805:                       '</span></td>'.
 4806:                       '<td class="LC_left_item">';
 4807:         if (($item eq 'url') || ($item eq 'button')) {
 4808:             $datatable .= '<span class="LC_nobreak">'.
 4809:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4810:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4811:         } elsif ($item eq 'fields') {
 4812:             my @currfields;
 4813:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4814:                 @currfields = @{$currvalidation{$item}};
 4815:             }
 4816:             foreach my $field (@{$fieldsref}) {
 4817:                 my $check = '';
 4818:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4819:                     $check = ' checked="checked"';
 4820:                 }
 4821:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4822:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4823:                               ' value="'.$field.'"'.$check.' />'.$field.
 4824:                               '</label></span> ';
 4825:             }
 4826:         } elsif ($item eq 'markup') {
 4827:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 4828:                            $currvalidation{$item}.
 4829:                               '</textarea>';
 4830:         }
 4831:         $datatable .= '</td></tr>'."\n";
 4832:         if (ref($rowtotal)) {
 4833:             $itemcount ++;
 4834:         }
 4835:     }
 4836:     if ($caller eq 'requestcourses') {
 4837:         my %currhash;
 4838:         if (ref($settings) eq 'HASH') {
 4839:             if (ref($settings->{'validation'}) eq 'HASH') {
 4840:                 if ($settings->{'validation'}{'dc'} ne '') {
 4841:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4842:                 }
 4843:             }
 4844:         }
 4845:         my $numinrow = 2;
 4846:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4847:                                                        'validationdc',%currhash);
 4848:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4849:         $datatable .= '<tr'.$css_class.'><td>';
 4850:         if ($numdc > 1) {
 4851:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4852:         } else {
 4853:             $datatable .=  &mt('Course creation processed as: ');
 4854:         }
 4855:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4856:         $itemcount ++;
 4857:     }
 4858:     if (ref($rowtotal)) {
 4859:         $$rowtotal += $itemcount;
 4860:     }
 4861:     return $datatable;
 4862: }
 4863: 
 4864: sub print_passwords {
 4865:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 4866:     my ($datatable,$css_class);
 4867:     my $itemcount = 0;
 4868:     my %titles = &Apache::lonlocal::texthash (
 4869:         captcha        => '"Forgot Password" CAPTCHA validation',
 4870:         link           => 'Reset link expiration (hours)',
 4871:         case           => 'Case-sensitive usernames/e-mail',
 4872:         prelink        => 'Information required (form 1)',
 4873:         postlink       => 'Information required (form 2)',
 4874:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 4875:         customtext     => 'Domain specific text (HTML)',
 4876:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 4877:         intauth_check  => 'Check bcrypt cost if authenticated',
 4878:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 4879:         permanent      => 'Permanent e-mail address',
 4880:         critical       => 'Critical notification address',
 4881:         notify         => 'Notification address',
 4882:         min            => 'Minimum password length',
 4883:         max            => 'Maximum password length',
 4884:         chars          => 'Required characters',
 4885:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 4886:     );
 4887:     if ($position eq 'top') {
 4888:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4889:         my $shownlinklife = 2;
 4890:         my $prelink = 'both';
 4891:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 4892:         if (ref($settings) eq 'HASH') {
 4893:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 4894:                 $shownlinklife = $settings->{resetlink};
 4895:             }
 4896:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 4897:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 4898:             }
 4899:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 4900:                 $prelink = $settings->{resetprelink};
 4901:             }
 4902:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 4903:                 %postlink = %{$settings->{resetpostlink}};
 4904:             }
 4905:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 4906:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 4907:             }
 4908:             if ($settings->{resetremove}) {
 4909:                 $nostdtext = 1;
 4910:             }
 4911:             if ($settings->{resetcustom}) {
 4912:                 $customurl = $settings->{resetcustom};
 4913:             }
 4914:         } else {
 4915:             if (ref($types) eq 'ARRAY') {
 4916:                 foreach my $item (@{$types}) {
 4917:                     $casesens{$item} = 1;
 4918:                     $postlink{$item} = ['username','email'];
 4919:                 }
 4920:             }
 4921:             $casesens{'default'} = 1;
 4922:             $postlink{'default'} = ['username','email'];
 4923:             $prelink = 'both';
 4924:             %emailsrc = (
 4925:                           permanent => 1,
 4926:                           critical  => 1,
 4927:                           notify    => 1,
 4928:             );
 4929:         }
 4930:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 4931:         $itemcount ++;
 4932:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4933:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 4934:                       '<td class="LC_left_item">'.
 4935:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 4936:                       'name="passwords_link" size="3" /></td></tr>';
 4937:         $itemcount ++;
 4938:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4939:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 4940:                       '<td class="LC_left_item">';
 4941:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4942:             foreach my $item (@{$types}) {
 4943:                 my $checkedcase;
 4944:                 if ($casesens{$item}) {
 4945:                     $checkedcase = ' checked="checked"';
 4946:                 }
 4947:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4948:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 4949:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 4950:                               '</span>&nbsp;&nbsp; ';
 4951:             }
 4952:         }
 4953:         my $checkedcase;
 4954:         if ($casesens{'default'}) {
 4955:             $checkedcase = ' checked="checked"';
 4956:         }
 4957:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 4958:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 4959:                       $othertitle.'</label></span></td>';
 4960:         $itemcount ++;
 4961:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4962:         my %checkedpre = (
 4963:                              both => ' checked="checked"',
 4964:                              either => '',
 4965:                          );
 4966:         if ($prelink eq 'either') {
 4967:             $checkedpre{either} = ' checked="checked"';
 4968:             $checkedpre{both} = '';
 4969:         }
 4970:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 4971:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 4972:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 4973:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 4974:                       '<span class="LC_nobreak"><label>'.
 4975:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 4976:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 4977:         $itemcount ++;
 4978:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4979:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 4980:                       '<td class="LC_left_item">';
 4981:         my %postlinked;
 4982:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 4983:             foreach my $item (@{$types}) {
 4984:                 undef(%postlinked);
 4985:                 $datatable .= '<fieldset style="display: inline-block;">'.
 4986:                               '<legend>'.$usertypes->{$item}.'</legend>';
 4987:                 if (ref($postlink{$item}) eq 'ARRAY') {
 4988:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 4989:                 }
 4990:                 foreach my $field ('email','username') {
 4991:                     my $checked;
 4992:                     if ($postlinked{$field}) {
 4993:                         $checked = ' checked="checked"';
 4994:                     }
 4995:                     $datatable .= '<span class="LC_nobreak"><label>'.
 4996:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 4997:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 4998:                                   '<span>&nbsp;&nbsp; ';
 4999:                 }
 5000:                 $datatable .= '</fieldset>';
 5001:             }
 5002:         }
 5003:         if (ref($postlink{'default'}) eq 'ARRAY') {
 5004:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 5005:         }
 5006:         $datatable .= '<fieldset style="display: inline-block;">'.
 5007:                       '<legend>'.$othertitle.'</legend>';
 5008:         foreach my $field ('email','username') {
 5009:             my $checked;
 5010:             if ($postlinked{$field}) {
 5011:                 $checked = ' checked="checked"';
 5012:             }
 5013:             $datatable .= '<span class="LC_nobreak"><label>'.
 5014:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 5015:                           $field.'"'.$checked.' />'.$field.'</label>'.
 5016:                           '<span>&nbsp;&nbsp; ';
 5017:         }
 5018:         $datatable .= '</fieldset></td></tr>';
 5019:         $itemcount ++;
 5020:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5021:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 5022:                       '<td class="LC_left_item">';
 5023:         foreach my $type ('permanent','critical','notify') {
 5024:             my $checkedemail;
 5025:             if ($emailsrc{$type}) {
 5026:                 $checkedemail = ' checked="checked"';
 5027:             }
 5028:             $datatable .= '<span class="LC_nobreak"><label>'.
 5029:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 5030:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 5031:                           '<span>&nbsp;&nbsp; ';
 5032:         }
 5033:         $datatable .= '</td></tr>';
 5034:         $itemcount ++;
 5035:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5036:         my $switchserver = &check_switchserver($dom,$confname);
 5037:         my ($showstd,$noshowstd);
 5038:         if ($nostdtext) {
 5039:             $noshowstd = ' checked="checked"';
 5040:         } else {
 5041:             $showstd = ' checked="checked"';
 5042:         }
 5043:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 5044:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5045:                       &mt('Retain standard text:').
 5046:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 5047:                       &mt('Yes').'</label>'.'&nbsp;'.
 5048:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 5049:                       &mt('No').'</label></span><br />'.
 5050:                       '<span class="LC_fontsize_small">'.
 5051:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 5052:                       &mt('Include custom text:');
 5053:         if ($customurl) {
 5054:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 5055:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 5056:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 5057:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 5058:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 5059:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 5060:         }
 5061:         if ($switchserver) {
 5062:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 5063:         } else {
 5064:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 5065:                          '<input type="file" name="passwords_customfile" /></span>';
 5066:         }
 5067:         $datatable .= '</td></tr>';
 5068:     } elsif ($position eq 'middle') {
 5069:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 5070:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 5071:         my %defaults;
 5072:         if (ref($domconf{'defaults'}) eq 'HASH') {
 5073:             %defaults = %{$domconf{'defaults'}};
 5074:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 5075:                 $defaults{'intauth_cost'} = 10;
 5076:             }
 5077:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 5078:                 $defaults{'intauth_check'} = 0;
 5079:             }
 5080:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 5081:                 $defaults{'intauth_switch'} = 0;
 5082:             }
 5083:         } else {
 5084:             %defaults = (
 5085:                           'intauth_cost'   => 10,
 5086:                           'intauth_check'  => 0,
 5087:                           'intauth_switch' => 0,
 5088:                         );
 5089:         }
 5090:         foreach my $item (@items) {
 5091:             if ($itemcount%2) {
 5092:                 $css_class = '';
 5093:             } else {
 5094:                 $css_class = ' class="LC_odd_row" ';
 5095:             }
 5096:             $datatable .= '<tr'.$css_class.'>'.
 5097:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 5098:                           '</span></td><td class="LC_left_item" colspan="3">';
 5099:             if ($item eq 'intauth_switch') {
 5100:                 my @options = (0,1,2);
 5101:                 my %optiondesc = &Apache::lonlocal::texthash (
 5102:                                    0 => 'No',
 5103:                                    1 => 'Yes',
 5104:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 5105:                                  );
 5106:                 $datatable .= '<table width="100%">';
 5107:                 foreach my $option (@options) {
 5108:                     my $checked = ' ';
 5109:                     if ($defaults{$item} eq $option) {
 5110:                         $checked = ' checked="checked"';
 5111:                     }
 5112:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 5113:                                   '<label><input type="radio" name="'.$item.
 5114:                                   '" value="'.$option.'"'.$checked.' />'.
 5115:                                   $optiondesc{$option}.'</label></span></td></tr>';
 5116:                 }
 5117:                 $datatable .= '</table>';
 5118:             } elsif ($item eq 'intauth_check') {
 5119:                 my @options = (0,1,2);
 5120:                 my %optiondesc = &Apache::lonlocal::texthash (
 5121:                                    0 => 'No',
 5122:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 5123:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 5124:                                  );
 5125:                 $datatable .= '<table width="100%">';
 5126:                 foreach my $option (@options) {
 5127:                     my $checked = ' ';
 5128:                     my $onclick;
 5129:                     if ($defaults{$item} eq $option) {
 5130:                         $checked = ' checked="checked"';
 5131:                     }
 5132:                     if ($option == 2) {
 5133:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 5134:                     }
 5135:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 5136:                                   '<label><input type="radio" name="'.$item.
 5137:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 5138:                                   $optiondesc{$option}.'</label></span></td></tr>';
 5139:                 }
 5140:                 $datatable .= '</table>';
 5141:             } else {
 5142:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 5143:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 5144:             }
 5145:             $datatable .= '</td></tr>';
 5146:             $itemcount ++;
 5147:         }
 5148:     } elsif ($position eq 'lower') {
 5149:         my ($min,$max,%chars,$numsaved);
 5150:         $min = $Apache::lonnet::passwdmin;
 5151:         if (ref($settings) eq 'HASH') {
 5152:             if ($settings->{min}) {
 5153:                 $min = $settings->{min};
 5154:             }
 5155:             if ($settings->{max}) {
 5156:                 $max = $settings->{max};
 5157:             }
 5158:             if (ref($settings->{chars}) eq 'ARRAY') {
 5159:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 5160:             }
 5161:             if ($settings->{numsaved}) {
 5162:                 $numsaved = $settings->{numsaved};
 5163:             }
 5164:         }
 5165:         my %rulenames = &Apache::lonlocal::texthash(
 5166:                                                      uc => 'At least one upper case letter',
 5167:                                                      lc => 'At least one lower case letter',
 5168:                                                      num => 'At least one number',
 5169:                                                      spec => 'At least one non-alphanumeric',
 5170:                                                    );
 5171:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5172:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 5173:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5174:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 5175:                       'onblur="javascript:warnIntPass(this);" />'.
 5176:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 5177:                       '</span></td></tr>';
 5178:         $itemcount ++;
 5179:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5180:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 5181:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5182:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 5183:                       'onblur="javascript:warnIntPass(this);" />'.
 5184:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 5185:                       '</span></td></tr>';
 5186:         $itemcount ++;
 5187:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5188:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 5189:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 5190:                       '</span></td>';
 5191:         my $numinrow = 2;
 5192:         my @possrules = ('uc','lc','num','spec');
 5193:         $datatable .= '<td class="LC_left_item"><table>';
 5194:         for (my $i=0; $i<@possrules; $i++) {
 5195:             my ($rem,$checked);
 5196:             if ($chars{$possrules[$i]}) {
 5197:                 $checked = ' checked="checked"';
 5198:             }
 5199:             $rem = $i%($numinrow);
 5200:             if ($rem == 0) {
 5201:                 if ($i > 0) {
 5202:                     $datatable .= '</tr>';
 5203:                 }
 5204:                 $datatable .= '<tr>';
 5205:             }
 5206:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 5207:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 5208:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 5209:         }
 5210:         my $rem = @possrules%($numinrow);
 5211:         my $colsleft = $numinrow - $rem;
 5212:         if ($colsleft > 1 ) {
 5213:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5214:                           '&nbsp;</td>';
 5215:         } elsif ($colsleft == 1) {
 5216:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5217:         }
 5218:         $datatable .='</table></td></tr>';
 5219:         $itemcount ++;
 5220:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5221:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 5222:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 5223:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 5224:                       'onblur="javascript:warnIntPass(this);" />'.
 5225:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 5226:                       '</span></td></tr>';
 5227:     } else {
 5228:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5229:         my %ownerchg = (
 5230:                           by  => {},
 5231:                           for => {},
 5232:                        );
 5233:         my %ownertitles = &Apache::lonlocal::texthash (
 5234:                             by  => 'Course owner status(es) allowed',
 5235:                             for => 'Student status(es) allowed',
 5236:                           );
 5237:         if (ref($settings) eq 'HASH') {
 5238:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 5239:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 5240:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 5241:                 }
 5242:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 5243:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 5244:                 }
 5245:             }
 5246:         }
 5247:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5248:         $datatable .= '<tr '.$css_class.'>'.
 5249:                       '<td>'.
 5250:                       &mt('Requirements').'<ul>'.
 5251:                       '<li>'.&mt("Course 'type' is not a Community").'</li>'.
 5252:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 5253:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 5254:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 5255:                       '</ul>'.
 5256:                       '</td>'.
 5257:                       '<td class="LC_left_item">';
 5258:         foreach my $item ('by','for') {
 5259:             $datatable .= '<fieldset style="display: inline-block;">'.
 5260:                           '<legend>'.$ownertitles{$item}.'</legend>';
 5261:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5262:                 foreach my $type (@{$types}) {
 5263:                     my $checked;
 5264:                     if ($ownerchg{$item}{$type}) {
 5265:                         $checked = ' checked="checked"';
 5266:                     }
 5267:                     $datatable .= '<span class="LC_nobreak"><label>'.
 5268:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 5269:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 5270:                                   '</span>&nbsp;&nbsp; ';
 5271:                 }
 5272:             }
 5273:             my $checked;
 5274:             if ($ownerchg{$item}{'default'}) {
 5275:                 $checked = ' checked="checked"';
 5276:             }
 5277:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 5278:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 5279:                           $othertitle.'</label></span></fieldset>';
 5280:         }
 5281:         $datatable .= '</td></tr>';
 5282:     }
 5283:     return $datatable;
 5284: }
 5285: 
 5286: sub print_usersessions {
 5287:     my ($position,$dom,$settings,$rowtotal) = @_;
 5288:     my ($css_class,$datatable,%checked,%choices);
 5289:     my (%by_ip,%by_location,@intdoms);
 5290:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 5291: 
 5292:     my @alldoms = &Apache::lonnet::all_domains();
 5293:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 5294:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5295:     my %altids = &id_for_thisdom(%servers);
 5296:     my $itemcount = 1;
 5297:     if ($position eq 'top') {
 5298:         if (keys(%serverhomes) > 1) {
 5299:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 5300:             my ($curroffloadnow,$curroffloadoth);
 5301:             if (ref($settings) eq 'HASH') {
 5302:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 5303:                     $curroffloadnow = $settings->{'offloadnow'};
 5304:                 }
 5305:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 5306:                     $curroffloadoth = $settings->{'offloadoth'};
 5307:                 }
 5308:             }
 5309:             my $other_insts = scalar(keys(%by_location));
 5310:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 5311:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 5312:         } else {
 5313:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5314:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 5315:         }
 5316:     } else {
 5317:         if (keys(%by_location) == 0) {
 5318:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 5319:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 5320:         } else {
 5321:             my %lt = &usersession_titles();
 5322:             my $numinrow = 5;
 5323:             my $prefix;
 5324:             my @types;
 5325:             if ($position eq 'bottom') {
 5326:                 $prefix = 'remote';
 5327:                 @types = ('version','excludedomain','includedomain');
 5328:             } else {
 5329:                 $prefix = 'hosted';
 5330:                 @types = ('excludedomain','includedomain');
 5331:             }
 5332:             my (%current,%checkedon,%checkedoff);
 5333:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 5334:             my @locations = sort(keys(%by_location));
 5335:             foreach my $type (@types) {
 5336:                 $checkedon{$type} = '';
 5337:                 $checkedoff{$type} = ' checked="checked"';
 5338:             }
 5339:             if (ref($settings) eq 'HASH') {
 5340:                 if (ref($settings->{$prefix}) eq 'HASH') {
 5341:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 5342:                         $current{$key} = $settings->{$prefix}{$key};
 5343:                         if ($key eq 'version') {
 5344:                             if ($current{$key} ne '') {
 5345:                                 $checkedon{$key} = ' checked="checked"';
 5346:                                 $checkedoff{$key} = '';
 5347:                             }
 5348:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 5349:                             $checkedon{$key} = ' checked="checked"';
 5350:                             $checkedoff{$key} = '';
 5351:                         }
 5352:                     }
 5353:                 }
 5354:             }
 5355:             foreach my $type (@types) {
 5356:                 next if ($type ne 'version' && !@locations);
 5357:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5358:                 $datatable .= '<tr'.$css_class.'>
 5359:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 5360:                                <span class="LC_nobreak">&nbsp;
 5361:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 5362:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 5363:                 if ($type eq 'version') {
 5364:                     my $selector = '<select name="'.$prefix.'_version">';
 5365:                     foreach my $version (@lcversions) {
 5366:                         my $selected = '';
 5367:                         if ($current{'version'} eq $version) {
 5368:                             $selected = ' selected="selected"';
 5369:                         }
 5370:                         $selector .= ' <option value="'.$version.'"'.
 5371:                                      $selected.'>'.$version.'</option>';
 5372:                     }
 5373:                     $selector .= '</select> ';
 5374:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 5375:                 } else {
 5376:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 5377:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 5378:                                  ' />'.('&nbsp;'x2).
 5379:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 5380:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 5381:                                  "\n".
 5382:                                  '</div><div><table>';
 5383:                     my $rem;
 5384:                     for (my $i=0; $i<@locations; $i++) {
 5385:                         my ($showloc,$value,$checkedtype);
 5386:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 5387:                             my $ip = $by_location{$locations[$i]}->[0];
 5388:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 5389:                                  $value = join(':',@{$by_ip{$ip}});
 5390:                                 $showloc = join(', ',@{$by_ip{$ip}});
 5391:                                 if (ref($current{$type}) eq 'ARRAY') {
 5392:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 5393:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 5394:                                             $checkedtype = ' checked="checked"';
 5395:                                             last;
 5396:                                         }
 5397:                                     }
 5398:                                 }
 5399:                             }
 5400:                         }
 5401:                         $rem = $i%($numinrow);
 5402:                         if ($rem == 0) {
 5403:                             if ($i > 0) {
 5404:                                 $datatable .= '</tr>';
 5405:                             }
 5406:                             $datatable .= '<tr>';
 5407:                         }
 5408:                         $datatable .= '<td class="LC_left_item">'.
 5409:                                       '<span class="LC_nobreak"><label>'.
 5410:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 5411:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 5412:                                       '</label></span></td>';
 5413:                     }
 5414:                     $rem = @locations%($numinrow);
 5415:                     my $colsleft = $numinrow - $rem;
 5416:                     if ($colsleft > 1 ) {
 5417:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5418:                                       '&nbsp;</td>';
 5419:                     } elsif ($colsleft == 1) {
 5420:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5421:                     }
 5422:                     $datatable .= '</tr></table>';
 5423:                 }
 5424:                 $datatable .= '</td></tr>';
 5425:                 $itemcount ++;
 5426:             }
 5427:         }
 5428:     }
 5429:     $$rowtotal += $itemcount;
 5430:     return $datatable;
 5431: }
 5432: 
 5433: sub build_location_hashes {
 5434:     my ($intdoms,$by_ip,$by_location) = @_;
 5435:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 5436:                   (ref($by_location) eq 'HASH')); 
 5437:     my %iphost = &Apache::lonnet::get_iphost();
 5438:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 5439:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 5440:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 5441:         foreach my $id (@{$iphost{$primary_ip}}) {
 5442:             my $intdom = &Apache::lonnet::internet_dom($id);
 5443:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 5444:                 push(@{$intdoms},$intdom);
 5445:             }
 5446:         }
 5447:     }
 5448:     foreach my $ip (keys(%iphost)) {
 5449:         if (ref($iphost{$ip}) eq 'ARRAY') {
 5450:             foreach my $id (@{$iphost{$ip}}) {
 5451:                 my $location = &Apache::lonnet::internet_dom($id);
 5452:                 if ($location) {
 5453:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 5454:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5455:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 5456:                             push(@{$by_ip->{$ip}},$location);
 5457:                         }
 5458:                     } else {
 5459:                         $by_ip->{$ip} = [$location];
 5460:                     }
 5461:                 }
 5462:             }
 5463:         }
 5464:     }
 5465:     foreach my $ip (sort(keys(%{$by_ip}))) {
 5466:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5467:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 5468:             my $first = $by_ip->{$ip}->[0];
 5469:             if (ref($by_location->{$first}) eq 'ARRAY') {
 5470:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 5471:                     push(@{$by_location->{$first}},$ip);
 5472:                 }
 5473:             } else {
 5474:                 $by_location->{$first} = [$ip];
 5475:             }
 5476:         }
 5477:     }
 5478:     return;
 5479: }
 5480: 
 5481: sub current_offloads_to {
 5482:     my ($dom,$settings,$servers) = @_;
 5483:     my (%spareid,%otherdomconfigs);
 5484:     if (ref($servers) eq 'HASH') {
 5485:         foreach my $lonhost (sort(keys(%{$servers}))) {
 5486:             my $gotspares;
 5487:             if (ref($settings) eq 'HASH') {
 5488:                 if (ref($settings->{'spares'}) eq 'HASH') {
 5489:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 5490:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 5491:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 5492:                         $gotspares = 1;
 5493:                     }
 5494:                 }
 5495:             }
 5496:             unless ($gotspares) {
 5497:                 my $gotspares;
 5498:                 my $serverhomeID =
 5499:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 5500:                 my $serverhomedom =
 5501:                     &Apache::lonnet::host_domain($serverhomeID);
 5502:                 if ($serverhomedom ne $dom) {
 5503:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 5504:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5505:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5506:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5507:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5508:                                 $gotspares = 1;
 5509:                             }
 5510:                         }
 5511:                     } else {
 5512:                         $otherdomconfigs{$serverhomedom} =
 5513:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 5514:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 5515:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5516:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5517:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 5518:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5519:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5520:                                         $gotspares = 1;
 5521:                                     }
 5522:                                 }
 5523:                             }
 5524:                         }
 5525:                     }
 5526:                 }
 5527:             }
 5528:             unless ($gotspares) {
 5529:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5530:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5531:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5532:                } else {
 5533:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 5534:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 5535:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5536:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5537:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5538:                     } else {
 5539:                         my %what = (
 5540:                              spareid => 1,
 5541:                         );
 5542:                         my ($result,$returnhash) = 
 5543:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 5544:                         if ($result eq 'ok') { 
 5545:                             if (ref($returnhash) eq 'HASH') {
 5546:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 5547:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 5548:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 5549:                                 }
 5550:                             }
 5551:                         }
 5552:                     }
 5553:                 }
 5554:             }
 5555:         }
 5556:     }
 5557:     return %spareid;
 5558: }
 5559: 
 5560: sub spares_row {
 5561:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 5562:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 5563:     my $css_class;
 5564:     my $numinrow = 4;
 5565:     my $itemcount = 1;
 5566:     my $datatable;
 5567:     my %typetitles = &sparestype_titles();
 5568:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5569:         foreach my $server (sort(keys(%{$servers}))) {
 5570:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5571:             my ($othercontrol,$serverdom);
 5572:             if ($serverhome ne $server) {
 5573:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5574:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5575:             } else {
 5576:                 $serverdom = &Apache::lonnet::host_domain($server);
 5577:                 if ($serverdom ne $dom) {
 5578:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5579:                 }
 5580:             }
 5581:             next unless (ref($spareid->{$server}) eq 'HASH');
 5582:             my ($checkednow,$checkedoth);
 5583:             if (ref($curroffloadnow) eq 'HASH') {
 5584:                 if ($curroffloadnow->{$server}) {
 5585:                     $checkednow = ' checked="checked"';
 5586:                 }
 5587:             }
 5588:             if (ref($curroffloadoth) eq 'HASH') {
 5589:                 if ($curroffloadoth->{$server}) {
 5590:                     $checkedoth = ' checked="checked"';
 5591:                 }
 5592:             }
 5593:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5594:             $datatable .= '<tr'.$css_class.'>
 5595:                            <td rowspan="2">
 5596:                             <span class="LC_nobreak">'.
 5597:                           &mt('[_1] when busy, offloads to:'
 5598:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5599:                           '<span class="LC_nobreak">'."\n".
 5600:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5601:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 5602:                           "\n";
 5603:             if ($other_insts) {
 5604:                 $datatable .= '<br />'.
 5605:                               '<span class="LC_nobreak">'."\n".
 5606:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 5607:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 5608:                           "\n";
 5609:             }
 5610:             my (%current,%canselect);
 5611:             my @choices = 
 5612:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5613:             foreach my $type ('primary','default') {
 5614:                 if (ref($spareid->{$server}) eq 'HASH') {
 5615:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5616:                         my @spares = @{$spareid->{$server}{$type}};
 5617:                         if (@spares > 0) {
 5618:                             if ($othercontrol) {
 5619:                                 $current{$type} = join(', ',@spares);
 5620:                             } else {
 5621:                                 $current{$type} .= '<table>';
 5622:                                 my $numspares = scalar(@spares);
 5623:                                 for (my $i=0;  $i<@spares; $i++) {
 5624:                                     my $rem = $i%($numinrow);
 5625:                                     if ($rem == 0) {
 5626:                                         if ($i > 0) {
 5627:                                             $current{$type} .= '</tr>';
 5628:                                         }
 5629:                                         $current{$type} .= '<tr>';
 5630:                                     }
 5631:                                     $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;'.
 5632:                                                        $spareid->{$server}{$type}[$i].
 5633:                                                        '</label></td>'."\n";
 5634:                                 }
 5635:                                 my $rem = @spares%($numinrow);
 5636:                                 my $colsleft = $numinrow - $rem;
 5637:                                 if ($colsleft > 1 ) {
 5638:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5639:                                                        '" class="LC_left_item">'.
 5640:                                                        '&nbsp;</td>';
 5641:                                 } elsif ($colsleft == 1) {
 5642:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5643:                                 }
 5644:                                 $current{$type} .= '</tr></table>';
 5645:                             }
 5646:                         }
 5647:                     }
 5648:                     if ($current{$type} eq '') {
 5649:                         $current{$type} = &mt('None specified');
 5650:                     }
 5651:                     if ($othercontrol) {
 5652:                         if ($type eq 'primary') {
 5653:                             $canselect{$type} = $othercontrol;
 5654:                         }
 5655:                     } else {
 5656:                         $canselect{$type} = 
 5657:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5658:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5659:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5660:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5661:                         if (@choices > 0) {
 5662:                             foreach my $lonhost (@choices) {
 5663:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5664:                             }
 5665:                         }
 5666:                         $canselect{$type} .= '</select>'."\n";
 5667:                     }
 5668:                 } else {
 5669:                     $current{$type} = &mt('Could not be determined');
 5670:                     if ($type eq 'primary') {
 5671:                         $canselect{$type} =  $othercontrol;
 5672:                     }
 5673:                 }
 5674:                 if ($type eq 'default') {
 5675:                     $datatable .= '<tr'.$css_class.'>';
 5676:                 }
 5677:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5678:                               '<td>'.$current{$type}.'</td>'."\n".
 5679:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5680:             }
 5681:             $itemcount ++;
 5682:         }
 5683:     }
 5684:     $$rowtotal += $itemcount;
 5685:     return $datatable;
 5686: }
 5687: 
 5688: sub possible_newspares {
 5689:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5690:     my $serverhostname = &Apache::lonnet::hostname($server);
 5691:     my %excluded;
 5692:     if ($serverhostname ne '') {
 5693:         %excluded = (
 5694:                        $serverhostname => 1,
 5695:                     );
 5696:     }
 5697:     if (ref($currspares) eq 'HASH') {
 5698:         foreach my $type (keys(%{$currspares})) {
 5699:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5700:                 if (@{$currspares->{$type}} > 0) {
 5701:                     foreach my $curr (@{$currspares->{$type}}) {
 5702:                         my $hostname = &Apache::lonnet::hostname($curr);
 5703:                         $excluded{$hostname} = 1;
 5704:                     }
 5705:                 }
 5706:             }
 5707:         }
 5708:     }
 5709:     my @choices;
 5710:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5711:         if (keys(%{$serverhomes}) > 1) {
 5712:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5713:                 unless ($excluded{$name}) {
 5714:                     if (exists($altids->{$serverhomes->{$name}})) {
 5715:                         push(@choices,$altids->{$serverhomes->{$name}});
 5716:                     } else {
 5717:                         push(@choices,$serverhomes->{$name});
 5718:                     }
 5719:                 }
 5720:             }
 5721:         }
 5722:     }
 5723:     return sort(@choices);
 5724: }
 5725: 
 5726: sub print_loadbalancing {
 5727:     my ($dom,$settings,$rowtotal) = @_;
 5728:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5729:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5730:     my $numinrow = 1;
 5731:     my $datatable;
 5732:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5733:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 5734:     if (ref($settings) eq 'HASH') {
 5735:         %existing = %{$settings};
 5736:     }
 5737:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5738:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5739:                                   \%currtargets,\%currrules,\%currcookies);
 5740:     } else {
 5741:         return;
 5742:     }
 5743:     my ($othertitle,$usertypes,$types) =
 5744:         &Apache::loncommon::sorted_inst_types($dom);
 5745:     my $rownum = 8;
 5746:     if (ref($types) eq 'ARRAY') {
 5747:         $rownum += scalar(@{$types});
 5748:     }
 5749:     my @css_class = ('LC_odd_row','LC_even_row');
 5750:     my $balnum = 0;
 5751:     my $islast;
 5752:     my (@toshow,$disabledtext);
 5753:     if (keys(%currbalancer) > 0) {
 5754:         @toshow = sort(keys(%currbalancer));
 5755:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5756:             push(@toshow,'');
 5757:         }
 5758:     } else {
 5759:         @toshow = ('');
 5760:         $disabledtext = &mt('No existing load balancer');
 5761:     }
 5762:     foreach my $lonhost (@toshow) {
 5763:         if ($balnum == scalar(@toshow)-1) {
 5764:             $islast = 1;
 5765:         } else {
 5766:             $islast = 0;
 5767:         }
 5768:         my $cssidx = $balnum%2;
 5769:         my $targets_div_style = 'display: none';
 5770:         my $disabled_div_style = 'display: block';
 5771:         my $homedom_div_style = 'display: none';
 5772:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5773:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5774:                       '<p>';
 5775:         if ($lonhost eq '') {
 5776:             $datatable .= '<span class="LC_nobreak">';
 5777:             if (keys(%currbalancer) > 0) {
 5778:                 $datatable .= &mt('Add balancer:');
 5779:             } else {
 5780:                 $datatable .= &mt('Enable balancer:');
 5781:             }
 5782:             $datatable .= '&nbsp;'.
 5783:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5784:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5785:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5786:                           '<option value="" selected="selected">'.&mt('None').
 5787:                           '</option>'."\n";
 5788:             foreach my $server (sort(keys(%servers))) {
 5789:                 next if ($currbalancer{$server});
 5790:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5791:             }
 5792:             $datatable .=
 5793:                 '</select>'."\n".
 5794:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5795:         } else {
 5796:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5797:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5798:                            &mt('Stop balancing').'</label>'.
 5799:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5800:             $targets_div_style = 'display: block';
 5801:             $disabled_div_style = 'display: none';
 5802:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5803:                 $homedom_div_style = 'display: block';
 5804:             }
 5805:         }
 5806:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5807:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5808:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5809:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5810:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5811:         my @sparestypes = ('primary','default');
 5812:         my %typetitles = &sparestype_titles();
 5813:         my %hostherechecked = (
 5814:                                   no => ' checked="checked"',
 5815:                               );
 5816:         my %balcookiechecked = (
 5817:                                   no => ' checked="checked"',
 5818:                                );
 5819:         foreach my $sparetype (@sparestypes) {
 5820:             my $targettable;
 5821:             for (my $i=0; $i<$numspares; $i++) {
 5822:                 my $checked;
 5823:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5824:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5825:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5826:                             $checked = ' checked="checked"';
 5827:                         }
 5828:                     }
 5829:                 }
 5830:                 my ($chkboxval,$disabled);
 5831:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5832:                     $chkboxval = $spares[$i];
 5833:                 }
 5834:                 if (exists($currbalancer{$spares[$i]})) {
 5835:                     $disabled = ' disabled="disabled"';
 5836:                 }
 5837:                 $targettable .=
 5838:                     '<td><span class="LC_nobreak"><label>'.
 5839:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5840:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5841:                     '</span></label></span></td>';
 5842:                 my $rem = $i%($numinrow);
 5843:                 if ($rem == 0) {
 5844:                     if (($i > 0) && ($i < $numspares-1)) {
 5845:                         $targettable .= '</tr>';
 5846:                     }
 5847:                     if ($i < $numspares-1) {
 5848:                         $targettable .= '<tr>';
 5849:                     }
 5850:                 }
 5851:             }
 5852:             if ($targettable ne '') {
 5853:                 my $rem = $numspares%($numinrow);
 5854:                 my $colsleft = $numinrow - $rem;
 5855:                 if ($colsleft > 1 ) {
 5856:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5857:                                     '&nbsp;</td>';
 5858:                 } elsif ($colsleft == 1) {
 5859:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5860:                 }
 5861:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5862:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5863:             }
 5864:             $hostherechecked{$sparetype} = '';
 5865:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5866:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5867:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5868:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5869:                         $hostherechecked{'no'} = '';
 5870:                     }
 5871:                 }
 5872:             }
 5873:         }
 5874:         if ($currcookies{$lonhost}) {
 5875:             %balcookiechecked = (
 5876:                                     yes => ' checked="checked"',
 5877:                                 );
 5878:         }
 5879:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5880:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5881:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5882:         foreach my $sparetype (@sparestypes) {
 5883:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5884:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5885:                           '</i></label><br />';
 5886:         }
 5887:         $datatable .= &mt('Use balancer cookie').'<br />'.
 5888:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 5889:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 5890:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 5891:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 5892:                       '</div></td></tr>'.
 5893:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5894:                                            $othertitle,$usertypes,$types,\%servers,
 5895:                                            \%currbalancer,$lonhost,
 5896:                                            $targets_div_style,$homedom_div_style,
 5897:                                            $css_class[$cssidx],$balnum,$islast);
 5898:         $$rowtotal += $rownum;
 5899:         $balnum ++;
 5900:     }
 5901:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5902:     return $datatable;
 5903: }
 5904: 
 5905: sub get_loadbalancers_config {
 5906:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 5907:     return unless ((ref($servers) eq 'HASH') &&
 5908:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5909:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 5910:                    (ref($currcookies) eq 'HASH'));
 5911:     if (keys(%{$existing}) > 0) {
 5912:         my $oldlonhost;
 5913:         foreach my $key (sort(keys(%{$existing}))) {
 5914:             if ($key eq 'lonhost') {
 5915:                 $oldlonhost = $existing->{'lonhost'};
 5916:                 $currbalancer->{$oldlonhost} = 1;
 5917:             } elsif ($key eq 'targets') {
 5918:                 if ($oldlonhost) {
 5919:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5920:                 }
 5921:             } elsif ($key eq 'rules') {
 5922:                 if ($oldlonhost) {
 5923:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5924:                 }
 5925:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5926:                 $currbalancer->{$key} = 1;
 5927:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5928:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5929:                 if ($existing->{$key}{'cookie'}) {
 5930:                     $currcookies->{$key} = 1;
 5931:                 }
 5932:             }
 5933:         }
 5934:     } else {
 5935:         my ($balancerref,$targetsref) =
 5936:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5937:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5938:             foreach my $server (sort(keys(%{$balancerref}))) {
 5939:                 $currbalancer->{$server} = 1;
 5940:                 $currtargets->{$server} = $targetsref->{$server};
 5941:             }
 5942:         }
 5943:     }
 5944:     return;
 5945: }
 5946: 
 5947: sub loadbalancing_rules {
 5948:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5949:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5950:         $css_class,$balnum,$islast) = @_;
 5951:     my $output;
 5952:     my $num = 0;
 5953:     my ($alltypes,$othertypes,$titles) =
 5954:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5955:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5956:         foreach my $type (@{$alltypes}) {
 5957:             $num ++;
 5958:             my $current;
 5959:             if (ref($currrules) eq 'HASH') {
 5960:                 $current = $currrules->{$type};
 5961:             }
 5962:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5963:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5964:                     $current = '';
 5965:                 }
 5966:             }
 5967:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5968:                                              $servers,$currbalancer,$lonhost,$dom,
 5969:                                              $targets_div_style,$homedom_div_style,
 5970:                                              $css_class,$balnum,$num,$islast);
 5971:         }
 5972:     }
 5973:     return $output;
 5974: }
 5975: 
 5976: sub loadbalancing_titles {
 5977:     my ($dom,$intdom,$usertypes,$types) = @_;
 5978:     my %othertypes = (
 5979:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5980:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5981:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5982:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5983:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5984:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5985:                      );
 5986:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5987:     my @available;
 5988:     if (ref($types) eq 'ARRAY') {
 5989:         @available = @{$types};
 5990:     }
 5991:     unless (grep(/^default$/,@available)) {
 5992:         push(@available,'default');
 5993:     }
 5994:     unshift(@alltypes,@available);
 5995:     my %titles;
 5996:     foreach my $type (@alltypes) {
 5997:         if ($type =~ /^_LC_/) {
 5998:             $titles{$type} = $othertypes{$type};
 5999:         } elsif ($type eq 'default') {
 6000:             $titles{$type} = &mt('All users from [_1]',$dom);
 6001:             if (ref($types) eq 'ARRAY') {
 6002:                 if (@{$types} > 0) {
 6003:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 6004:                 }
 6005:             }
 6006:         } elsif (ref($usertypes) eq 'HASH') {
 6007:             $titles{$type} = $usertypes->{$type};
 6008:         }
 6009:     }
 6010:     return (\@alltypes,\%othertypes,\%titles);
 6011: }
 6012: 
 6013: sub loadbalance_rule_row {
 6014:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 6015:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 6016:     my @rulenames;
 6017:     my %ruletitles = &offloadtype_text();
 6018:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 6019:         @rulenames = ('balancer','offloadedto','specific');
 6020:     } else {
 6021:         @rulenames = ('default','homeserver');
 6022:         if ($type eq '_LC_external') {
 6023:             push(@rulenames,'externalbalancer');
 6024:         } else {
 6025:             push(@rulenames,'specific');
 6026:         }
 6027:         push(@rulenames,'none');
 6028:     }
 6029:     my $style = $targets_div_style;
 6030:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 6031:         $style = $homedom_div_style;
 6032:     }
 6033:     my $space;
 6034:     if ($islast && $num == 1) {
 6035:         $space = '<div display="inline-block">&nbsp;</div>';
 6036:     }
 6037:     my $output =
 6038:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 6039:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 6040:         '<td valaign="top">'.$space.
 6041:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 6042:     for (my $i=0; $i<@rulenames; $i++) {
 6043:         my $rule = $rulenames[$i];
 6044:         my ($checked,$extra);
 6045:         if ($rulenames[$i] eq 'default') {
 6046:             $rule = '';
 6047:         }
 6048:         if ($rulenames[$i] eq 'specific') {
 6049:             if (ref($servers) eq 'HASH') {
 6050:                 my $default;
 6051:                 if (($current ne '') && (exists($servers->{$current}))) {
 6052:                     $checked = ' checked="checked"';
 6053:                 }
 6054:                 unless ($checked) {
 6055:                     $default = ' selected="selected"';
 6056:                 }
 6057:                 $extra =
 6058:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6059:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 6060:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 6061:                     '<option value=""'.$default.'></option>'."\n";
 6062:                 foreach my $server (sort(keys(%{$servers}))) {
 6063:                     if (ref($currbalancer) eq 'HASH') {
 6064:                         next if (exists($currbalancer->{$server}));
 6065:                     }
 6066:                     my $selected;
 6067:                     if ($server eq $current) {
 6068:                         $selected = ' selected="selected"';
 6069:                     }
 6070:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 6071:                 }
 6072:                 $extra .= '</select>';
 6073:             }
 6074:         } elsif ($rule eq $current) {
 6075:             $checked = ' checked="checked"';
 6076:         }
 6077:         $output .= '<span class="LC_nobreak"><label>'.
 6078:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 6079:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 6080:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 6081:                    ')"'.$checked.' />&nbsp;';
 6082:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 6083:             $output .= $ruletitles{'particular'};
 6084:         } else {
 6085:             $output .= $ruletitles{$rulenames[$i]};
 6086:         }
 6087:         $output .= '</label>'.$extra.'</span><br />'."\n";
 6088:     }
 6089:     $output .= '</div></td></tr>'."\n";
 6090:     return $output;
 6091: }
 6092: 
 6093: sub offloadtype_text {
 6094:     my %ruletitles = &Apache::lonlocal::texthash (
 6095:            'default'          => 'Offloads to default destinations',
 6096:            'homeserver'       => "Offloads to user's home server",
 6097:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 6098:            'specific'         => 'Offloads to specific server',
 6099:            'none'             => 'No offload',
 6100:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 6101:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 6102:            'particular'       => 'Session hosted (after re-auth) on server:',
 6103:     );
 6104:     return %ruletitles;
 6105: }
 6106: 
 6107: sub sparestype_titles {
 6108:     my %typestitles = &Apache::lonlocal::texthash (
 6109:                           'primary' => 'primary',
 6110:                           'default' => 'default',
 6111:                       );
 6112:     return %typestitles;
 6113: }
 6114: 
 6115: sub contact_titles {
 6116:     my %titles = &Apache::lonlocal::texthash (
 6117:                    'supportemail'    => 'Support E-mail address',
 6118:                    'adminemail'      => 'Default Server Admin E-mail address',
 6119:                    'errormail'       => 'Error reports to be e-mailed to',
 6120:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 6121:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 6122:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 6123:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 6124:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 6125:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 6126:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 6127:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 6128:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 6129:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 6130:                    'errorweights'    => 'Weights used to compute error count',
 6131:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 6132:                  );
 6133:     my %short_titles = &Apache::lonlocal::texthash (
 6134:                            adminemail   => 'Admin E-mail address',
 6135:                            supportemail => 'Support E-mail',
 6136:                        );   
 6137:     return (\%titles,\%short_titles);
 6138: }
 6139: 
 6140: sub helpform_fields {
 6141:     my %titles =  &Apache::lonlocal::texthash (
 6142:                        'username'   => 'Name',
 6143:                        'user'       => 'Username/domain',
 6144:                        'phone'      => 'Phone',
 6145:                        'cc'         => 'Cc e-mail',
 6146:                        'course'     => 'Course Details',
 6147:                        'section'    => 'Sections',
 6148:                        'screenshot' => 'File upload',
 6149:     );
 6150:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 6151:     my %possoptions = (
 6152:                         username     => ['yes','no','req'],
 6153:                         phone        => ['yes','no','req'],
 6154:                         user         => ['yes','no'],
 6155:                         cc           => ['yes','no'],
 6156:                         course       => ['yes','no'],
 6157:                         section      => ['yes','no'],
 6158:                         screenshot   => ['yes','no'],
 6159:                       );
 6160:     my %fieldoptions = &Apache::lonlocal::texthash (
 6161:                          'yes'  => 'Optional',
 6162:                          'req'  => 'Required',
 6163:                          'no'   => "Not shown",
 6164:     );
 6165:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 6166: }
 6167: 
 6168: sub tool_titles {
 6169:     my %titles = &Apache::lonlocal::texthash (
 6170:                      aboutme    => 'Personal web page',
 6171:                      blog       => 'Blog',
 6172:                      webdav     => 'WebDAV',
 6173:                      portfolio  => 'Portfolio',
 6174:                      official   => 'Official courses (with institutional codes)',
 6175:                      unofficial => 'Unofficial courses',
 6176:                      community  => 'Communities',
 6177:                      textbook   => 'Textbook courses',
 6178:                  );
 6179:     return %titles;
 6180: }
 6181: 
 6182: sub courserequest_titles {
 6183:     my %titles = &Apache::lonlocal::texthash (
 6184:                                    official   => 'Official',
 6185:                                    unofficial => 'Unofficial',
 6186:                                    community  => 'Communities',
 6187:                                    textbook   => 'Textbook',
 6188:                                    norequest  => 'Not allowed',
 6189:                                    approval   => 'Approval by Dom. Coord.',
 6190:                                    validate   => 'With validation',
 6191:                                    autolimit  => 'Numerical limit',
 6192:                                    unlimited  => '(blank for unlimited)',
 6193:                  );
 6194:     return %titles;
 6195: }
 6196: 
 6197: sub authorrequest_titles {
 6198:     my %titles = &Apache::lonlocal::texthash (
 6199:                                    norequest  => 'Not allowed',
 6200:                                    approval   => 'Approval by Dom. Coord.',
 6201:                                    automatic  => 'Automatic approval',
 6202:                  );
 6203:     return %titles;
 6204: }
 6205: 
 6206: sub courserequest_conditions {
 6207:     my %conditions = &Apache::lonlocal::texthash (
 6208:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 6209:        validate   => '(Processing of request subject to institutional validation).',
 6210:                  );
 6211:     return %conditions;
 6212: }
 6213: 
 6214: 
 6215: sub print_usercreation {
 6216:     my ($position,$dom,$settings,$rowtotal) = @_;
 6217:     my $numinrow = 4;
 6218:     my $datatable;
 6219:     if ($position eq 'top') {
 6220:         $$rowtotal ++;
 6221:         my $rowcount = 0;
 6222:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 6223:         if (ref($rules) eq 'HASH') {
 6224:             if (keys(%{$rules}) > 0) {
 6225:                 $datatable .= &user_formats_row('username',$settings,$rules,
 6226:                                                 $ruleorder,$numinrow,$rowcount);
 6227:                 $$rowtotal ++;
 6228:                 $rowcount ++;
 6229:             }
 6230:         }
 6231:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 6232:         if (ref($idrules) eq 'HASH') {
 6233:             if (keys(%{$idrules}) > 0) {
 6234:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 6235:                                                 $idruleorder,$numinrow,$rowcount);
 6236:                 $$rowtotal ++;
 6237:                 $rowcount ++;
 6238:             }
 6239:         }
 6240:         if ($rowcount == 0) {
 6241:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 6242:             $$rowtotal ++;
 6243:             $rowcount ++;
 6244:         }
 6245:     } elsif ($position eq 'middle') {
 6246:         my @creators = ('author','course','requestcrs');
 6247:         my ($rules,$ruleorder) =
 6248:             &Apache::lonnet::inst_userrules($dom,'username');
 6249:         my %lt = &usercreation_types();
 6250:         my %checked;
 6251:         if (ref($settings) eq 'HASH') {
 6252:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 6253:                 foreach my $item (@creators) {
 6254:                     $checked{$item} = $settings->{'cancreate'}{$item};
 6255:                 }
 6256:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 6257:                 foreach my $item (@creators) {
 6258:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 6259:                         $checked{$item} = 'none';
 6260:                     }
 6261:                 }
 6262:             }
 6263:         }
 6264:         my $rownum = 0;
 6265:         foreach my $item (@creators) {
 6266:             $rownum ++;
 6267:             if ($checked{$item} eq '') {
 6268:                 $checked{$item} = 'any';
 6269:             }
 6270:             my $css_class;
 6271:             if ($rownum%2) {
 6272:                 $css_class = '';
 6273:             } else {
 6274:                 $css_class = ' class="LC_odd_row" ';
 6275:             }
 6276:             $datatable .= '<tr'.$css_class.'>'.
 6277:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 6278:                          '</span></td><td align="right">';
 6279:             my @options = ('any');
 6280:             if (ref($rules) eq 'HASH') {
 6281:                 if (keys(%{$rules}) > 0) {
 6282:                     push(@options,('official','unofficial'));
 6283:                 }
 6284:             }
 6285:             push(@options,'none');
 6286:             foreach my $option (@options) {
 6287:                 my $type = 'radio';
 6288:                 my $check = ' ';
 6289:                 if ($checked{$item} eq $option) {
 6290:                     $check = ' checked="checked" ';
 6291:                 } 
 6292:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6293:                               '<input type="'.$type.'" name="can_createuser_'.
 6294:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 6295:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 6296:             }
 6297:             $datatable .= '</td></tr>';
 6298:         }
 6299:     } else {
 6300:         my @contexts = ('author','course','domain');
 6301:         my @authtypes = ('int','krb4','krb5','loc');
 6302:         my %checked;
 6303:         if (ref($settings) eq 'HASH') {
 6304:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 6305:                 foreach my $item (@contexts) {
 6306:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 6307:                         foreach my $auth (@authtypes) {
 6308:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 6309:                                 $checked{$item}{$auth} = ' checked="checked" ';
 6310:                             }
 6311:                         }
 6312:                     }
 6313:                 }
 6314:             }
 6315:         } else {
 6316:             foreach my $item (@contexts) {
 6317:                 foreach my $auth (@authtypes) {
 6318:                     $checked{$item}{$auth} = ' checked="checked" ';
 6319:                 }
 6320:             }
 6321:         }
 6322:         my %title = &context_names();
 6323:         my %authname = &authtype_names();
 6324:         my $rownum = 0;
 6325:         my $css_class; 
 6326:         foreach my $item (@contexts) {
 6327:             if ($rownum%2) {
 6328:                 $css_class = '';
 6329:             } else {
 6330:                 $css_class = ' class="LC_odd_row" ';
 6331:             }
 6332:             $datatable .=   '<tr'.$css_class.'>'.
 6333:                             '<td>'.$title{$item}.
 6334:                             '</td><td class="LC_left_item">'.
 6335:                             '<span class="LC_nobreak">';
 6336:             foreach my $auth (@authtypes) {
 6337:                 $datatable .= '<label>'. 
 6338:                               '<input type="checkbox" name="'.$item.'_auth" '.
 6339:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 6340:                               $authname{$auth}.'</label>&nbsp;';
 6341:             }
 6342:             $datatable .= '</span></td></tr>';
 6343:             $rownum ++;
 6344:         }
 6345:         $$rowtotal += $rownum;
 6346:     }
 6347:     return $datatable;
 6348: }
 6349: 
 6350: sub print_selfcreation {
 6351:     my ($position,$dom,$settings,$rowtotal) = @_;
 6352:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 6353:         $emaildomain,$datatable);
 6354:     if (ref($settings) eq 'HASH') {
 6355:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 6356:             $createsettings = $settings->{'cancreate'};
 6357:             if (ref($createsettings) eq 'HASH') {
 6358:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 6359:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 6360:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 6361:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 6362:                         @selfcreate = ('email','login','sso');
 6363:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 6364:                         @selfcreate = ($createsettings->{'selfcreate'});
 6365:                     }
 6366:                 }
 6367:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 6368:                     $processing = $createsettings->{'selfcreateprocessing'};
 6369:                 }
 6370:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 6371:                     $emailoptions = $createsettings->{'emailoptions'};
 6372:                 }
 6373:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 6374:                     $emailverified = $createsettings->{'emailverified'};
 6375:                 }
 6376:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 6377:                     $emaildomain = $createsettings->{'emaildomain'};
 6378:                 }
 6379:             }
 6380:         }
 6381:     }
 6382:     my %radiohash;
 6383:     my $numinrow = 4;
 6384:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 6385:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6386:     if ($position eq 'top') {
 6387:         my %choices = &Apache::lonlocal::texthash (
 6388:                                                       cancreate_login      => 'Institutional Login',
 6389:                                                       cancreate_sso        => 'Institutional Single Sign On',
 6390:                                                   );
 6391:         my @toggles = sort(keys(%choices));
 6392:         my %defaultchecked = (
 6393:                                'cancreate_login' => 'off',
 6394:                                'cancreate_sso'   => 'off',
 6395:                              );
 6396:         my ($onclick,$itemcount);
 6397:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6398:                                                      \%choices,$itemcount,$onclick);
 6399:         $$rowtotal += $itemcount;
 6400: 
 6401:         if (ref($usertypes) eq 'HASH') {
 6402:             if (keys(%{$usertypes}) > 0) {
 6403:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 6404:                                              $dom,$numinrow,$othertitle,
 6405:                                              'statustocreate',$rowtotal);
 6406:                 $$rowtotal ++;
 6407:             }
 6408:         }
 6409:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 6410:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6411:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 6412:         my $rem;
 6413:         my $numperrow = 2;
 6414:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 6415:         $datatable .= '<tr'.$css_class.'>'.
 6416:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 6417:                      '<td class="LC_left_item">'."\n".
 6418:                      '<table>'."\n";
 6419:         for (my $i=0; $i<@fields; $i++) {
 6420:             $rem = $i%($numperrow);
 6421:             if ($rem == 0) {
 6422:                 if ($i > 0) {
 6423:                     $datatable .= '</tr>';
 6424:                 }
 6425:                 $datatable .= '<tr>';
 6426:             }
 6427:             my $currval;
 6428:             if (ref($createsettings) eq 'HASH') {
 6429:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 6430:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 6431:                 }
 6432:             }
 6433:             $datatable .= '<td class="LC_left_item">'.
 6434:                           '<span class="LC_nobreak">'.
 6435:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 6436:                           'value="'.$currval.'" size="10" />&nbsp;'.
 6437:                           $fieldtitles{$fields[$i]}.'</span></td>';
 6438:         }
 6439:         my $colsleft = $numperrow - $rem;
 6440:         if ($colsleft > 1 ) {
 6441:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6442:                          '&nbsp;</td>';
 6443:         } elsif ($colsleft == 1) {
 6444:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6445:         }
 6446:         $datatable .= '</tr></table></td></tr>';
 6447:         $$rowtotal ++;
 6448:     } elsif ($position eq 'middle') {
 6449:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 6450:         my @posstypes;
 6451:         if (ref($types) eq 'ARRAY') {
 6452:             @posstypes = @{$types};
 6453:         }
 6454:         unless (grep(/^default$/,@posstypes)) {
 6455:             push(@posstypes,'default');
 6456:         }
 6457:         my %usertypeshash;
 6458:         if (ref($usertypes) eq 'HASH') {
 6459:             %usertypeshash = %{$usertypes};
 6460:         }
 6461:         $usertypeshash{'default'} = $othertitle;
 6462:         foreach my $status (@posstypes) {
 6463:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 6464:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 6465:             $$rowtotal ++;
 6466:         }
 6467:     } else {
 6468:         my %choices = &Apache::lonlocal::texthash (
 6469:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 6470:                                                   );
 6471:         my @toggles = sort(keys(%choices));
 6472:         my %defaultchecked = (
 6473:                                'cancreate_email' => 'off',
 6474:                              );
 6475:         my $customclass = 'LC_selfcreate_email';
 6476:         my $classprefix = 'LC_canmodify_emailusername_';
 6477:         my $optionsprefix = 'LC_options_emailusername_';
 6478:         my $display = 'none';
 6479:         my $rowstyle = 'display:none';
 6480:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 6481:             $display = 'block';
 6482:             $rowstyle = 'display:table-row';
 6483:         }
 6484:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 6485:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6486:                                                      \%choices,$$rowtotal,$onclick);
 6487:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 6488:                                          $rowstyle);
 6489:         $$rowtotal ++;
 6490:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 6491:                                       $rowstyle);
 6492:         $$rowtotal ++;
 6493:         my (@ordered,@posstypes,%usertypeshash);
 6494:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6495:         my ($emailrules,$emailruleorder) =
 6496:             &Apache::lonnet::inst_userrules($dom,'email');
 6497:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6498:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6499:         if (ref($types) eq 'ARRAY') {
 6500:             @posstypes = @{$types};
 6501:         }
 6502:         if (@posstypes) {
 6503:             unless (grep(/^default$/,@posstypes)) {
 6504:                 push(@posstypes,'default');
 6505:             }
 6506:             if (ref($usertypes) eq 'HASH') {
 6507:                 %usertypeshash = %{$usertypes};
 6508:             }
 6509:             my $currassign;
 6510:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 6511:                 $currassign = {
 6512:                                   selfassign => $domdefaults{'inststatusguest'},
 6513:                               };
 6514:                 @ordered = @{$domdefaults{'inststatusguest'}};
 6515:             } else {
 6516:                 $currassign = { selfassign => [] };
 6517:             }
 6518:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 6519:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 6520:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 6521:                                          $numinrow,$othertitle,'selfassign',
 6522:                                          $rowtotal,$onclicktypes,$customclass,
 6523:                                          $rowstyle);
 6524:             $$rowtotal ++;
 6525:             $usertypeshash{'default'} = $othertitle;
 6526:             foreach my $status (@posstypes) {
 6527:                 my $css_class;
 6528:                 if ($$rowtotal%2) {
 6529:                     $css_class = 'LC_odd_row ';
 6530:                 }
 6531:                 $css_class .= $customclass;
 6532:                 my $rowid = $optionsprefix.$status;
 6533:                 my $hidden = 1;
 6534:                 my $currstyle = 'display:none';
 6535:                 if (grep(/^\Q$status\E$/,@ordered)) {
 6536:                     $currstyle = $rowstyle;
 6537:                     $hidden = 0;
 6538:                 }
 6539:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6540:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 6541:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 6542:                 unless ($hidden) {
 6543:                     $$rowtotal ++;
 6544:                 }
 6545:             }
 6546:         } else {
 6547:             my $css_class;
 6548:             if ($$rowtotal%2) {
 6549:                 $css_class = 'LC_odd_row ';
 6550:             }
 6551:             $css_class .= $customclass;
 6552:             $usertypeshash{'default'} = $othertitle;
 6553:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6554:                                          $emailrules,$emailruleorder,$settings,'default','',
 6555:                                          $othertitle,$css_class,$rowstyle,$intdom);
 6556:             $$rowtotal ++;
 6557:         }
 6558:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 6559:         $numinrow = 1;
 6560:         if (@posstypes) {
 6561:             foreach my $status (@posstypes) {
 6562:                 my $rowid = $classprefix.$status;
 6563:                 my $datarowstyle = 'display:none';
 6564:                 if (grep(/^\Q$status\E$/,@ordered)) {
 6565:                     $datarowstyle = $rowstyle;
 6566:                 }
 6567:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 6568:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6569:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 6570:                 unless ($datarowstyle eq 'display:none') {
 6571:                     $$rowtotal ++;
 6572:                 }
 6573:             }
 6574:         } else {
 6575:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 6576:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6577:                                                    $infotitles,'',$customclass,$rowstyle);
 6578:         }
 6579:     }
 6580:     return $datatable;
 6581: }
 6582: 
 6583: sub selfcreate_javascript {
 6584:     return <<"ENDSCRIPT";
 6585: 
 6586: <script type="text/javascript">
 6587: // <![CDATA[
 6588: 
 6589: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 6590:     var x = document.getElementsByClassName(target);
 6591:     var insttypes = 0;
 6592:     var insttypeRegExp = new RegExp(prefix);
 6593:     if ((x.length != undefined) && (x.length > 0)) {
 6594:         if (form.elements[radio].length != undefined) {
 6595:             for (var i=0; i<form.elements[radio].length; i++) {
 6596:                 if (form.elements[radio][i].checked) {
 6597:                     if (form.elements[radio][i].value == 1) {
 6598:                         for (var j=0; j<x.length; j++) {
 6599:                             if (x[j].id == 'undefined') {
 6600:                                 x[j].style.display = 'table-row';
 6601:                             } else if (insttypeRegExp.test(x[j].id)) {
 6602:                                 insttypes ++;
 6603:                             } else {
 6604:                                 x[j].style.display = 'table-row';
 6605:                             }
 6606:                         }
 6607:                     } else {
 6608:                         for (var j=0; j<x.length; j++) {
 6609:                             x[j].style.display = 'none';
 6610:                         }
 6611:                     }
 6612:                     break;
 6613:                 }
 6614:             }
 6615:             if (insttypes > 0) {
 6616:                 toggleDataRow(form,checkbox,target,altprefix);
 6617:                 toggleDataRow(form,checkbox,target,prefix,1);
 6618:             }
 6619:         }
 6620:     }
 6621:     return;
 6622: }
 6623: 
 6624: function toggleDataRow(form,checkbox,target,prefix,docount) {
 6625:     if (form.elements[checkbox].length != undefined) {
 6626:         var count = 0;
 6627:         if (docount) {
 6628:             for (var i=0; i<form.elements[checkbox].length; i++) {
 6629:                 if (form.elements[checkbox][i].checked) {
 6630:                     count ++;
 6631:                 }
 6632:             }
 6633:         }
 6634:         for (var i=0; i<form.elements[checkbox].length; i++) {
 6635:             var type = form.elements[checkbox][i].value;
 6636:             if (document.getElementById(prefix+type)) {
 6637:                 if (form.elements[checkbox][i].checked) {
 6638:                     document.getElementById(prefix+type).style.display = 'table-row';
 6639:                     if (count % 2 == 1) {
 6640:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 6641:                     } else {
 6642:                         document.getElementById(prefix+type).className = target;
 6643:                     }
 6644:                     count ++;
 6645:                 } else {
 6646:                     document.getElementById(prefix+type).style.display = 'none';
 6647:                 }
 6648:             }
 6649:         }
 6650:     }
 6651:     return;
 6652: }
 6653: 
 6654: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 6655:     var caller = radio+'_'+status;
 6656:     if (form.elements[caller].length != undefined) {
 6657:         for (var i=0; i<form.elements[caller].length; i++) {
 6658:             if (form.elements[caller][i].checked) {
 6659:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 6660:                     var curr = form.elements[caller][i].value;
 6661:                     if (prefix) {
 6662:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 6663:                     }
 6664:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 6665:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 6666:                     if (curr == 'custom') {
 6667:                         if (prefix) {
 6668:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 6669:                         }
 6670:                     } else if (curr == 'inst') {
 6671:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 6672:                     } else if (curr == 'noninst') {
 6673:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 6674:                     }
 6675:                     break;
 6676:                 }
 6677:             }
 6678:         }
 6679:     }
 6680: }
 6681: 
 6682: // ]]>
 6683: </script>
 6684: 
 6685: ENDSCRIPT
 6686: }
 6687: 
 6688: sub noninst_users {
 6689:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 6690:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
 6691:     my $class = 'LC_left_item';
 6692:     if ($css_class) {
 6693:         $css_class = ' class="'.$css_class.'"';
 6694:     }
 6695:     if ($rowid) {
 6696:         $rowid = ' id="'.$rowid.'"';
 6697:     }
 6698:     if ($rowstyle) {
 6699:         $rowstyle = ' style="'.$rowstyle.'"';
 6700:     }
 6701:     my ($output,$description);
 6702:     if ($type eq 'default') {
 6703:         $description = &mt('Requests for: [_1]',$typetitle);
 6704:     } else {
 6705:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 6706:     }
 6707:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 6708:               "<td>$description</td>\n".
 6709:               '<td class="'.$class.'" colspan="2">'.
 6710:               '<table><tr>';
 6711:     my %headers = &Apache::lonlocal::texthash(
 6712:               approve  => 'Processing',
 6713:               email    => 'E-mail',
 6714:               username => 'Username',
 6715:     );
 6716:     foreach my $item ('approve','email','username') {
 6717:         $output .= '<th>'.$headers{$item}.'</th>';
 6718:     }
 6719:     $output .= '</tr><tr>';
 6720:     foreach my $item ('approve','email','username') {
 6721:         $output .= '<td valign="top">';
 6722:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 6723:         if ($item eq 'approve') {
 6724:             %choices = &Apache::lonlocal::texthash (
 6725:                                                      automatic => 'Automatically approved',
 6726:                                                      approval  => 'Queued for approval',
 6727:                                                    );
 6728:             @options = ('automatic','approval');
 6729:             $hashref = $processing;
 6730:             $defoption = 'automatic';
 6731:             $name = 'cancreate_emailprocess_'.$type;
 6732:         } elsif ($item eq 'email') {
 6733:             %choices = &Apache::lonlocal::texthash (
 6734:                                                      any     => 'Any e-mail',
 6735:                                                      inst    => 'Institutional only',
 6736:                                                      noninst => 'Non-institutional only',
 6737:                                                      custom  => 'Custom restrictions',
 6738:                                                    );
 6739:             @options = ('any','inst','noninst');
 6740:             my $showcustom;
 6741:             if (ref($emailrules) eq 'HASH') {
 6742:                 if (keys(%{$emailrules}) > 0) {
 6743:                     push(@options,'custom');
 6744:                     $showcustom = 'cancreate_emailrule';
 6745:                     if (ref($settings) eq 'HASH') {
 6746:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 6747:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 6748:                                 if (exists($emailrules->{$rule})) {
 6749:                                     $hascustom ++;
 6750:                                 }
 6751:                             }
 6752:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 6753:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 6754:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 6755:                                     if (exists($emailrules->{$rule})) {
 6756:                                         $hascustom ++;
 6757:                                     }
 6758:                                 }
 6759:                             }
 6760:                         }
 6761:                     }
 6762:                 }
 6763:             }
 6764:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 6765:                                                      "'cancreate_emaildomain','$type'".');"';
 6766:             $hashref = $emailoptions;
 6767:             $defoption = 'any';
 6768:             $name = 'cancreate_emailoptions_'.$type;
 6769:         } elsif ($item eq 'username') {
 6770:             %choices = &Apache::lonlocal::texthash (
 6771:                                                      all    => 'Same as e-mail',
 6772:                                                      first  => 'Omit @domain',
 6773:                                                      free   => 'Free to choose',
 6774:                                                    );
 6775:             @options = ('all','first','free');
 6776:             $hashref = $emailverified;
 6777:             $defoption = 'all';
 6778:             $name = 'cancreate_usernameoptions_'.$type;
 6779:         }
 6780:         foreach my $option (@options) {
 6781:             my $checked;
 6782:             if (ref($hashref) eq 'HASH') {
 6783:                 if ($type eq '') {
 6784:                     if (!exists($hashref->{'default'})) {
 6785:                         if ($option eq $defoption) {
 6786:                             $checked = ' checked="checked"';
 6787:                         }
 6788:                     } else {
 6789:                         if ($hashref->{'default'} eq $option) {
 6790:                             $checked = ' checked="checked"';
 6791:                         }
 6792:                     }
 6793:                 } else {
 6794:                     if (!exists($hashref->{$type})) {
 6795:                         if ($option eq $defoption) {
 6796:                             $checked = ' checked="checked"';
 6797:                         }
 6798:                     } else {
 6799:                         if ($hashref->{$type} eq $option) {
 6800:                             $checked = ' checked="checked"';
 6801:                         }
 6802:                     }
 6803:                 }
 6804:             } elsif (($item eq 'email') && ($hascustom)) {
 6805:                 if ($option eq 'custom') {
 6806:                     $checked = ' checked="checked"';
 6807:                 }
 6808:             } elsif ($option eq $defoption) {
 6809:                 $checked = ' checked="checked"';
 6810:             }
 6811:             $output .= '<span class="LC_nobreak"><label>'.
 6812:                        '<input type="radio" name="'.$name.'"'.
 6813:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 6814:                        $choices{$option}.'</label></span><br />';
 6815:             if ($item eq 'email') {
 6816:                 if ($option eq 'custom') {
 6817:                     my $id = 'cancreate_emailrule_'.$type;
 6818:                     my $display = 'none';
 6819:                     if ($checked) {
 6820:                         $display = 'inline';
 6821:                     }
 6822:                     my $numinrow = 2;
 6823:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 6824:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 6825:                                &user_formats_row('email',$settings,$emailrules,
 6826:                                                  $emailruleorder,$numinrow,'',$type);
 6827:                               '</table></fieldset>';
 6828:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 6829:                     my %text = &Apache::lonlocal::texthash (
 6830:                                                              inst    => 'must end:',
 6831:                                                              noninst => 'cannot end:',
 6832:                                                            );
 6833:                     my $value;
 6834:                     if (ref($emaildomain) eq 'HASH') {
 6835:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 6836:                             $value = $emaildomain->{$type}->{$option};
 6837:                         }
 6838:                     }
 6839:                     if ($value eq '') {
 6840:                         $value = '@'.$intdom;
 6841:                     }
 6842:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 6843:                     my $display = 'none';
 6844:                     if ($checked) {
 6845:                         $display = 'inline';
 6846:                     }
 6847:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 6848:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 6849:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 6850:                                '</div>';
 6851:                 }
 6852:             }
 6853:         }
 6854:         $output .= '</td>'."\n";
 6855:     }
 6856:     $output .= "</tr></table></td></tr>\n";
 6857:     return $output;
 6858: }
 6859: 
 6860: sub captcha_choice {
 6861:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 6862:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 6863:         $vertext,$currver); 
 6864:     my %lt = &captcha_phrases();
 6865:     $keyentry = 'hidden';
 6866:     my $colspan=2;
 6867:     if ($context eq 'cancreate') {
 6868:         $rowname = &mt('CAPTCHA validation');
 6869:     } elsif ($context eq 'login') {
 6870:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 6871:     } elsif ($context eq 'passwords') {
 6872:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 6873:         $colspan=1;
 6874:     }
 6875:     if (ref($settings) eq 'HASH') {
 6876:         if ($settings->{'captcha'}) {
 6877:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6878:         } else {
 6879:             $checked{'original'} = ' checked="checked"';
 6880:         }
 6881:         if ($settings->{'captcha'} eq 'recaptcha') {
 6882:             $pubtext = $lt{'pub'};
 6883:             $privtext = $lt{'priv'};
 6884:             $keyentry = 'text';
 6885:             $vertext = $lt{'ver'};
 6886:             $currver = $settings->{'recaptchaversion'};
 6887:             if ($currver ne '2') {
 6888:                 $currver = 1;
 6889:             }
 6890:         }
 6891:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6892:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6893:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6894:         }
 6895:     } else {
 6896:         $checked{'original'} = ' checked="checked"';
 6897:     }
 6898:     my $css_class;
 6899:     if ($itemcount%2) {
 6900:         $css_class = 'LC_odd_row';
 6901:     }
 6902:     if ($customcss) {
 6903:         $css_class .= " $customcss";
 6904:     }
 6905:     $css_class =~ s/^\s+//;
 6906:     if ($css_class) {
 6907:         $css_class = ' class="'.$css_class.'"';
 6908:     }
 6909:     if ($rowstyle) {
 6910:         $css_class .= ' style="'.$rowstyle.'"';
 6911:     }
 6912:     my $output = '<tr'.$css_class.'>'.
 6913:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 6914:                  '<table><tr><td>'."\n";
 6915:     foreach my $option ('original','recaptcha','notused') {
 6916:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6917:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6918:                    $lt{$option}.'</label></span>';
 6919:         unless ($option eq 'notused') {
 6920:             $output .= ('&nbsp;'x2)."\n";
 6921:         }
 6922:     }
 6923: #
 6924: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6925: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6926: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6927: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6928: #
 6929:     $output .= '</td></tr>'."\n".
 6930:                '<tr><td class="LC_zero_height">'."\n".
 6931:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6932:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6933:                $currpub.'" size="40" /></span><br />'."\n".
 6934:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6935:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6936:                $currpriv.'" size="40" /></span><br />'.
 6937:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6938:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6939:                $currver.'" size="3" /></span><br />'.
 6940:                '</td></tr></table>'."\n".
 6941:                '</td></tr>';
 6942:     return $output;
 6943: }
 6944: 
 6945: sub user_formats_row {
 6946:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 6947:     my $output;
 6948:     my %text = (
 6949:                    'username' => 'new usernames',
 6950:                    'id'       => 'IDs',
 6951:                );
 6952:     unless ($type eq 'email') {
 6953:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6954:         $output = '<tr '.$css_class.'>'.
 6955:                   '<td><span class="LC_nobreak">'.
 6956:                   &mt("Format rules to check for $text{$type}: ").
 6957:                   '</td><td class="LC_left_item" colspan="2"><table>';
 6958:     }
 6959:     my $rem;
 6960:     if (ref($ruleorder) eq 'ARRAY') {
 6961:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6962:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6963:                 my $rem = $i%($numinrow);
 6964:                 if ($rem == 0) {
 6965:                     if ($i > 0) {
 6966:                         $output .= '</tr>';
 6967:                     }
 6968:                     $output .= '<tr>';
 6969:                 }
 6970:                 my $check = ' ';
 6971:                 if (ref($settings) eq 'HASH') {
 6972:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6973:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6974:                             $check = ' checked="checked" ';
 6975:                         }
 6976:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 6977:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 6978:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 6979:                                 $check = ' checked="checked" ';
 6980:                             }
 6981:                         }
 6982:                     }
 6983:                 }
 6984:                 my $name = $type.'_rule';
 6985:                 if ($type eq 'email') {
 6986:                     $name .= '_'.$status;
 6987:                 }
 6988:                 $output .= '<td class="LC_left_item">'.
 6989:                            '<span class="LC_nobreak"><label>'.
 6990:                            '<input type="checkbox" name="'.$name.'" '.
 6991:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6992:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6993:             }
 6994:         }
 6995:         $rem = @{$ruleorder}%($numinrow);
 6996:     }
 6997:     my $colsleft;
 6998:     if ($rem) {
 6999:         $colsleft = $numinrow - $rem;
 7000:     }
 7001:     if ($colsleft > 1 ) {
 7002:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7003:                    '&nbsp;</td>';
 7004:     } elsif ($colsleft == 1) {
 7005:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7006:     }
 7007:     $output .= '</tr></table>';
 7008:     unless ($type eq 'email') {
 7009:         $output .= '</td></tr>';
 7010:     }
 7011:     return $output;
 7012: }
 7013: 
 7014: sub usercreation_types {
 7015:     my %lt = &Apache::lonlocal::texthash (
 7016:                     author     => 'When adding a co-author',
 7017:                     course     => 'When adding a user to a course',
 7018:                     requestcrs => 'When requesting a course',
 7019:                     any        => 'Any',
 7020:                     official   => 'Institutional only ',
 7021:                     unofficial => 'Non-institutional only',
 7022:                     none       => 'None',
 7023:     );
 7024:     return %lt;
 7025: }
 7026: 
 7027: sub selfcreation_types {
 7028:     my %lt = &Apache::lonlocal::texthash (
 7029:                     selfcreate => 'User creates own account',
 7030:                     any        => 'Any',
 7031:                     official   => 'Institutional only ',
 7032:                     unofficial => 'Non-institutional only',
 7033:                     email      => 'E-mail address',
 7034:                     login      => 'Institutional Login',
 7035:                     sso        => 'SSO',
 7036:              );
 7037: }
 7038: 
 7039: sub authtype_names {
 7040:     my %lt = &Apache::lonlocal::texthash(
 7041:                       int    => 'Internal',
 7042:                       krb4   => 'Kerberos 4',
 7043:                       krb5   => 'Kerberos 5',
 7044:                       loc    => 'Local',
 7045:                   );
 7046:     return %lt;
 7047: }
 7048: 
 7049: sub context_names {
 7050:     my %context_title = &Apache::lonlocal::texthash(
 7051:        author => 'Creating users when an Author',
 7052:        course => 'Creating users when in a course',
 7053:        domain => 'Creating users when a Domain Coordinator',
 7054:     );
 7055:     return %context_title;
 7056: }
 7057: 
 7058: sub print_usermodification {
 7059:     my ($position,$dom,$settings,$rowtotal) = @_;
 7060:     my $numinrow = 4;
 7061:     my ($context,$datatable,$rowcount);
 7062:     if ($position eq 'top') {
 7063:         $rowcount = 0;
 7064:         $context = 'author'; 
 7065:         foreach my $role ('ca','aa') {
 7066:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7067:                                                    $numinrow,$rowcount);
 7068:             $$rowtotal ++;
 7069:             $rowcount ++;
 7070:         }
 7071:     } elsif ($position eq 'bottom') {
 7072:         $context = 'course';
 7073:         $rowcount = 0;
 7074:         foreach my $role ('st','ep','ta','in','cr') {
 7075:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 7076:                                                    $numinrow,$rowcount);
 7077:             $$rowtotal ++;
 7078:             $rowcount ++;
 7079:         }
 7080:     }
 7081:     return $datatable;
 7082: }
 7083: 
 7084: sub print_defaults {
 7085:     my ($position,$dom,$settings,$rowtotal) = @_;
 7086:     my $rownum = 0;
 7087:     my ($datatable,$css_class,$titles);
 7088:     unless ($position eq 'bottom') {
 7089:         $titles = &defaults_titles($dom);
 7090:     }
 7091:     if ($position eq 'top') {
 7092:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 7093:                      'datelocale_def','portal_def');
 7094:         my %defaults;
 7095:         if (ref($settings) eq 'HASH') {
 7096:             %defaults = %{$settings};
 7097:         } else {
 7098:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7099:             foreach my $item (@items) {
 7100:                 $defaults{$item} = $domdefaults{$item};
 7101:             }
 7102:         }
 7103:         foreach my $item (@items) {
 7104:             if ($rownum%2) {
 7105:                 $css_class = '';
 7106:             } else {
 7107:                 $css_class = ' class="LC_odd_row" ';
 7108:             }
 7109:             $datatable .= '<tr'.$css_class.'>'.
 7110:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 7111:                           '</span></td><td class="LC_right_item" colspan="3">';
 7112:             if ($item eq 'auth_def') {
 7113:                 my @authtypes = ('internal','krb4','krb5','localauth');
 7114:                 my %shortauth = (
 7115:                                  internal => 'int',
 7116:                                  krb4 => 'krb4',
 7117:                                  krb5 => 'krb5',
 7118:                                  localauth  => 'loc'
 7119:                                 );
 7120:                 my %authnames = &authtype_names();
 7121:                 foreach my $auth (@authtypes) {
 7122:                     my $checked = ' ';
 7123:                     if ($defaults{$item} eq $auth) {
 7124:                         $checked = ' checked="checked" ';
 7125:                     }
 7126:                     $datatable .= '<label><input type="radio" name="'.$item.
 7127:                                   '" value="'.$auth.'"'.$checked.'/>'.
 7128:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 7129:                 }
 7130:             } elsif ($item eq 'timezone_def') {
 7131:                 my $includeempty = 1;
 7132:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 7133:             } elsif ($item eq 'datelocale_def') {
 7134:                 my $includeempty = 1;
 7135:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 7136:             } elsif ($item eq 'lang_def') {
 7137:                 my $includeempty = 1;
 7138:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 7139:             } else {
 7140:                 my $size;
 7141:                 if ($item eq 'portal_def') {
 7142:                     $size = ' size="25"';
 7143:                 }
 7144:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7145:                               $defaults{$item}.'"'.$size.' />';
 7146:             }
 7147:             $datatable .= '</td></tr>';
 7148:             $rownum ++;
 7149:         }
 7150:     } else {
 7151:         my %defaults;
 7152:         if (ref($settings) eq 'HASH') {
 7153:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7154:                 my $maxnum = @{$settings->{'inststatusorder'}};
 7155:                 for (my $i=0; $i<$maxnum; $i++) {
 7156:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 7157:                     my $item = $settings->{'inststatusorder'}->[$i];
 7158:                     my $title = $settings->{'inststatustypes'}->{$item};
 7159:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 7160:                     $datatable .= '<tr'.$css_class.'>'.
 7161:                                   '<td><span class="LC_nobreak">'.
 7162:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 7163:                     for (my $k=0; $k<=$maxnum; $k++) {
 7164:                         my $vpos = $k+1;
 7165:                         my $selstr;
 7166:                         if ($k == $i) {
 7167:                             $selstr = ' selected="selected" ';
 7168:                         }
 7169:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7170:                     }
 7171:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 7172:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 7173:                                   &mt('delete').'</span></td>'.
 7174:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed').':'.
 7175:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 7176:                                   '</span></td></tr>';
 7177:                 }
 7178:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 7179:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 7180:                 $datatable .= '<tr '.$css_class.'>'.
 7181:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 7182:                 for (my $k=0; $k<=$maxnum; $k++) {
 7183:                     my $vpos = $k+1;
 7184:                     my $selstr;
 7185:                     if ($k == $maxnum) {
 7186:                         $selstr = ' selected="selected" ';
 7187:                     }
 7188:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7189:                 }
 7190:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 7191:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 7192:                               '&nbsp;'.&mt('(new)').
 7193:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 7194:                               &mt('Name displayed').':'.
 7195:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 7196:                               '</tr>'."\n";
 7197:                 $rownum ++;
 7198:             }
 7199:         }
 7200:     }
 7201:     $$rowtotal += $rownum;
 7202:     return $datatable;
 7203: }
 7204: 
 7205: sub get_languages_hash {
 7206:     my %langchoices;
 7207:     foreach my $id (&Apache::loncommon::languageids()) {
 7208:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 7209:         if ($code ne '') {
 7210:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 7211:         }
 7212:     }
 7213:     return %langchoices;
 7214: }
 7215: 
 7216: sub defaults_titles {
 7217:     my ($dom) = @_;
 7218:     my %titles = &Apache::lonlocal::texthash (
 7219:                    'auth_def'      => 'Default authentication type',
 7220:                    'auth_arg_def'  => 'Default authentication argument',
 7221:                    'lang_def'      => 'Default language',
 7222:                    'timezone_def'  => 'Default timezone',
 7223:                    'datelocale_def' => 'Default locale for dates',
 7224:                    'portal_def'     => 'Portal/Default URL',
 7225:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 7226:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 7227:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 7228:                  );
 7229:     if ($dom) {
 7230:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 7231:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 7232:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 7233:         $protocol = 'http' if ($protocol ne 'https');
 7234:         if ($uint_dom) {
 7235:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 7236:                                          $uint_dom);
 7237:         }
 7238:     }
 7239:     return (\%titles);
 7240: }
 7241: 
 7242: sub print_scantron {
 7243:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 7244:     if ($position eq 'top') {
 7245:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 7246:     } else {
 7247:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 7248:     }
 7249: }
 7250: 
 7251: sub scantron_javascript {
 7252:     return <<"ENDSCRIPT";
 7253: 
 7254: <script type="text/javascript">
 7255: // <![CDATA[
 7256: 
 7257: function toggleScantron(form) {
 7258:     var csvfieldset = new Array();
 7259:     if (document.getElementById('scantroncsv_cols')) {
 7260:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 7261:     }
 7262:     if (document.getElementById('scantroncsv_options')) {
 7263:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 7264:     }
 7265:     if (csvfieldset.length) {
 7266:         if (document.getElementById('scantronconfcsv')) {
 7267:             var scantroncsv = document.getElementById('scantronconfcsv');
 7268:             if (scantroncsv.checked) {
 7269:                 for (var i=0; i<csvfieldset.length; i++) {
 7270:                     csvfieldset[i].style.display = 'block';
 7271:                 }
 7272:             } else {
 7273:                 for (var i=0; i<csvfieldset.length; i++) {
 7274:                     csvfieldset[i].style.display = 'none';
 7275:                 }
 7276:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 7277:                 if (csvselects.length) {
 7278:                     for (var j=0; j<csvselects.length; j++) {
 7279:                         csvselects[j].selectedIndex = 0;
 7280:                     }
 7281:                 }
 7282:             }
 7283:         }
 7284:     }
 7285:     return;
 7286: }
 7287: // ]]>
 7288: </script>
 7289: 
 7290: ENDSCRIPT
 7291: 
 7292: }
 7293: 
 7294: sub print_scantronformat {
 7295:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 7296:     my $itemcount = 1;
 7297:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 7298:         %confhash);
 7299:     my $switchserver = &check_switchserver($dom,$confname);
 7300:     my %lt = &Apache::lonlocal::texthash (
 7301:                 default => 'Default bubblesheet format file error',
 7302:                 custom  => 'Custom bubblesheet format file error',
 7303:              );
 7304:     my %scantronfiles = (
 7305:         default => 'default.tab',
 7306:         custom => 'custom.tab',
 7307:     );
 7308:     foreach my $key (keys(%scantronfiles)) {
 7309:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 7310:                               .$scantronfiles{$key};
 7311:     }
 7312:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 7313:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 7314:         if (!$switchserver) {
 7315:             my $servadm = $r->dir_config('lonAdmEMail');
 7316:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 7317:             if ($configuserok eq 'ok') {
 7318:                 if ($author_ok eq 'ok') {
 7319:                     my %legacyfile = (
 7320:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 7321:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 7322:                     );
 7323:                     my %md5chk;
 7324:                     foreach my $type (keys(%legacyfile)) {
 7325:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 7326:                         chomp($md5chk{$type});
 7327:                     }
 7328:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 7329:                         foreach my $type (keys(%legacyfile)) {
 7330:                             ($scantronurls{$type},my $error) =
 7331:                                 &legacy_scantronformat($r,$dom,$confname,
 7332:                                                  $type,$legacyfile{$type},
 7333:                                                  $scantronurls{$type},
 7334:                                                  $scantronfiles{$type});
 7335:                             if ($error ne '') {
 7336:                                 $error{$type} = $error;
 7337:                             }
 7338:                         }
 7339:                         if (keys(%error) == 0) {
 7340:                             $is_custom = 1;
 7341:                             $confhash{'scantron'}{'scantronformat'} =
 7342:                                 $scantronurls{'custom'};
 7343:                             my $putresult =
 7344:                                 &Apache::lonnet::put_dom('configuration',
 7345:                                                          \%confhash,$dom);
 7346:                             if ($putresult ne 'ok') {
 7347:                                 $error{'custom'} =
 7348:                                     '<span class="LC_error">'.
 7349:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7350:                             }
 7351:                         }
 7352:                     } else {
 7353:                         ($scantronurls{'default'},my $error) =
 7354:                             &legacy_scantronformat($r,$dom,$confname,
 7355:                                           'default',$legacyfile{'default'},
 7356:                                           $scantronurls{'default'},
 7357:                                           $scantronfiles{'default'});
 7358:                         if ($error eq '') {
 7359:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 7360:                             my $putresult =
 7361:                                 &Apache::lonnet::put_dom('configuration',
 7362:                                                          \%confhash,$dom);
 7363:                             if ($putresult ne 'ok') {
 7364:                                 $error{'default'} =
 7365:                                     '<span class="LC_error">'.
 7366:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7367:                             }
 7368:                         } else {
 7369:                             $error{'default'} = $error;
 7370:                         }
 7371:                     }
 7372:                 }
 7373:             }
 7374:         } else {
 7375:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 7376:         }
 7377:     }
 7378:     if (ref($settings) eq 'HASH') {
 7379:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 7380:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 7381:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 7382:                 $scantronurl = '';
 7383:             } else {
 7384:                 $scantronurl = $settings->{'scantronformat'};
 7385:             }
 7386:             $is_custom = 1;
 7387:         } else {
 7388:             $scantronurl = $scantronurls{'default'};
 7389:         }
 7390:     } else {
 7391:         if ($is_custom) {
 7392:             $scantronurl = $scantronurls{'custom'};
 7393:         } else {
 7394:             $scantronurl = $scantronurls{'default'};
 7395:         }
 7396:     }
 7397:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7398:     $datatable .= '<tr'.$css_class.'>';
 7399:     if (!$is_custom) {
 7400:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 7401:                       '<span class="LC_nobreak">';
 7402:         if ($scantronurl) {
 7403:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 7404:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 7405:         } else {
 7406:             $datatable = &mt('File unavailable for display');
 7407:         }
 7408:         $datatable .= '</span></td>';
 7409:         if (keys(%error) == 0) { 
 7410:             $datatable .= '<td valign="bottom">';
 7411:             if (!$switchserver) {
 7412:                 $datatable .= &mt('Upload:').'<br />';
 7413:             }
 7414:         } else {
 7415:             my $errorstr;
 7416:             foreach my $key (sort(keys(%error))) {
 7417:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7418:             }
 7419:             $datatable .= '<td>'.$errorstr;
 7420:         }
 7421:     } else {
 7422:         if (keys(%error) > 0) {
 7423:             my $errorstr;
 7424:             foreach my $key (sort(keys(%error))) {
 7425:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7426:             } 
 7427:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 7428:         } elsif ($scantronurl) {
 7429:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 7430:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7431:             $datatable .= '<td><span class="LC_nobreak">'.
 7432:                           $link.
 7433:                           '<label><input type="checkbox" name="scantronformat_del"'.
 7434:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 7435:                           '<td><span class="LC_nobreak">&nbsp;'.
 7436:                           &mt('Replace:').'</span><br />';
 7437:         }
 7438:     }
 7439:     if (keys(%error) == 0) {
 7440:         if ($switchserver) {
 7441:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 7442:         } else {
 7443:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7444:                          '<input type="file" name="scantronformat" /></span>';
 7445:         }
 7446:     }
 7447:     $datatable .= '</td></tr>';
 7448:     $$rowtotal ++;
 7449:     return $datatable;
 7450: }
 7451: 
 7452: sub legacy_scantronformat {
 7453:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 7454:     my ($url,$error);
 7455:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 7456:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 7457:         (my $result,$url) =
 7458:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 7459:                          '','',$newfile);
 7460:         if ($result ne 'ok') {
 7461:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 7462:         }
 7463:     }
 7464:     return ($url,$error);
 7465: }
 7466: 
 7467: sub print_scantronconfig {
 7468:     my ($dom,$settings,$rowtotal) = @_;
 7469:     my $itemcount = 2;
 7470:     my $is_checked = ' checked="checked"';
 7471:     my %optionson = (
 7472:                      hdr => ' checked="checked"',
 7473:                      pad => ' checked="checked"',
 7474:                      rem => ' checked="checked"',
 7475:                     );
 7476:     my %optionsoff = (
 7477:                       hdr => '',
 7478:                       pad => '',
 7479:                       rem => '',
 7480:                      );
 7481:     my $currcsvsty = 'none';
 7482:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 7483:     my @fields = &scantroncsv_fields();
 7484:     my %titles = &scantronconfig_titles();
 7485:     if (ref($settings) eq 'HASH') {
 7486:         if (ref($settings->{config}) eq 'HASH') {
 7487:             if ($settings->{config}->{dat}) {
 7488:                 $checked{'dat'} = $is_checked;
 7489:             }
 7490:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 7491:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 7492:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 7493:                     if (keys(%csvfields) > 0) {
 7494:                         $checked{'csv'} = $is_checked;
 7495:                         $currcsvsty = 'block';
 7496:                     }
 7497:                 }
 7498:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 7499:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 7500:                     foreach my $option (keys(%optionson)) {
 7501:                         unless ($csvoptions{$option}) {
 7502:                             $optionsoff{$option} = $optionson{$option};
 7503:                             $optionson{$option} = '';
 7504:                         }
 7505:                     }
 7506:                 }
 7507:             }
 7508:         } else {
 7509:             $checked{'dat'} = $is_checked;
 7510:         }
 7511:     } else {
 7512:         $checked{'dat'} = $is_checked;
 7513:     }
 7514:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 7515:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 7516:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 7517:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 7518:     foreach my $item ('dat','csv') {
 7519:         my $id;
 7520:         if ($item eq 'csv') {
 7521:             $id = 'id="scantronconfcsv" ';
 7522:         }
 7523:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 7524:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 7525:         if ($item eq 'csv') {
 7526:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 7527:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 7528:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 7529:             foreach my $col (@fields) {
 7530:                 my $selnone;
 7531:                 if ($csvfields{$col} eq '') {
 7532:                     $selnone = ' selected="selected"';
 7533:                 }
 7534:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 7535:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 7536:                               '<option value=""'.$selnone.'></option>';
 7537:                 for (my $i=0; $i<20; $i++) {
 7538:                     my $shown = $i+1;
 7539:                     my $sel;
 7540:                     unless ($selnone) {
 7541:                         if (exists($csvfields{$col})) {
 7542:                             if ($csvfields{$col} == $i) {
 7543:                                 $sel = ' selected="selected"';
 7544:                             }
 7545:                         }
 7546:                     }
 7547:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 7548:                 }
 7549:                 $datatable .= '</select></td></tr>';
 7550:            }
 7551:            $datatable .= '</table></fieldset>'.
 7552:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 7553:                          '<legend>'.&mt('CSV Options').'</legend>';
 7554:            foreach my $option ('hdr','pad','rem') {
 7555:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 7556:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 7557:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 7558:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 7559:            }
 7560:            $datatable .= '</fieldset>';
 7561:            $itemcount ++;
 7562:         }
 7563:     }
 7564:     $datatable .= '</td></tr>';
 7565:     $$rowtotal ++;
 7566:     return $datatable;
 7567: }
 7568: 
 7569: sub scantronconfig_titles {
 7570:     return &Apache::lonlocal::texthash(
 7571:                                           dat => 'Standard format (.dat)',
 7572:                                           csv => 'Comma separated values (.csv)',
 7573:                                           hdr => 'Remove first line in file (contains column titles)',
 7574:                                           pad => 'Prepend 0s to PaperID',
 7575:                                           rem => 'Remove leading spaces (except Question Response columns)',
 7576:                                           CODE => 'CODE',
 7577:                                           ID   => 'Student ID',
 7578:                                           PaperID => 'Paper ID',
 7579:                                           FirstName => 'First Name',
 7580:                                           LastName => 'Last Name',
 7581:                                           FirstQuestion => 'First Question Response',
 7582:                                           Section => 'Section',
 7583:     );
 7584: }
 7585: 
 7586: sub scantroncsv_fields {
 7587:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 7588: }
 7589: 
 7590: sub print_coursecategories {
 7591:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 7592:     my $datatable;
 7593:     if ($position eq 'top') {
 7594:         my (%checked);
 7595:         my @catitems = ('unauth','auth');
 7596:         my @cattypes = ('std','domonly','codesrch','none');
 7597:         $checked{'unauth'} = 'std';
 7598:         $checked{'auth'} = 'std';
 7599:         if (ref($settings) eq 'HASH') {
 7600:             foreach my $type (@cattypes) {
 7601:                 if ($type eq $settings->{'unauth'}) {
 7602:                     $checked{'unauth'} = $type;
 7603:                 }
 7604:                 if ($type eq $settings->{'auth'}) {
 7605:                     $checked{'auth'} = $type;
 7606:                 }
 7607:             }
 7608:         }
 7609:         my %lt = &Apache::lonlocal::texthash (
 7610:                                                unauth   => 'Catalog type for unauthenticated users',
 7611:                                                auth     => 'Catalog type for authenticated users',
 7612:                                                none     => 'No catalog',
 7613:                                                std      => 'Standard catalog',
 7614:                                                domonly  => 'Domain-only catalog',
 7615:                                                codesrch => "Code search form",
 7616:                                              );
 7617:        my $itemcount = 0;
 7618:        foreach my $item (@catitems) {
 7619:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 7620:            $datatable .= '<tr '.$css_class.'>'.
 7621:                          '<td>'.$lt{$item}.'</td>'.
 7622:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 7623:            foreach my $type (@cattypes) {
 7624:                my $ischecked;
 7625:                if ($checked{$item} eq $type) {
 7626:                    $ischecked=' checked="checked"';
 7627:                }
 7628:                $datatable .= '<label>'.
 7629:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 7630:                              ' />'.$lt{$type}.'</label>&nbsp;';
 7631:            }
 7632:            $datatable .= '</span></td></tr>';
 7633:            $itemcount ++;
 7634:         }
 7635:         $$rowtotal += $itemcount;
 7636:     } elsif ($position eq 'middle') {
 7637:         my $toggle_cats_crs = ' ';
 7638:         my $toggle_cats_dom = ' checked="checked" ';
 7639:         my $can_cat_crs = ' ';
 7640:         my $can_cat_dom = ' checked="checked" ';
 7641:         my $toggle_catscomm_comm = ' ';
 7642:         my $toggle_catscomm_dom = ' checked="checked" ';
 7643:         my $can_catcomm_comm = ' ';
 7644:         my $can_catcomm_dom = ' checked="checked" ';
 7645: 
 7646:         if (ref($settings) eq 'HASH') {
 7647:             if ($settings->{'togglecats'} eq 'crs') {
 7648:                 $toggle_cats_crs = $toggle_cats_dom;
 7649:                 $toggle_cats_dom = ' ';
 7650:             }
 7651:             if ($settings->{'categorize'} eq 'crs') {
 7652:                 $can_cat_crs = $can_cat_dom;
 7653:                 $can_cat_dom = ' ';
 7654:             }
 7655:             if ($settings->{'togglecatscomm'} eq 'comm') {
 7656:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 7657:                 $toggle_catscomm_dom = ' ';
 7658:             }
 7659:             if ($settings->{'categorizecomm'} eq 'comm') {
 7660:                 $can_catcomm_comm = $can_catcomm_dom;
 7661:                 $can_catcomm_dom = ' ';
 7662:             }
 7663:         }
 7664:         my %title = &Apache::lonlocal::texthash (
 7665:                      togglecats     => 'Show/Hide a course in catalog',
 7666:                      togglecatscomm => 'Show/Hide a community in catalog',
 7667:                      categorize     => 'Assign a category to a course',
 7668:                      categorizecomm => 'Assign a category to a community',
 7669:                     );
 7670:         my %level = &Apache::lonlocal::texthash (
 7671:                      dom  => 'Set in Domain',
 7672:                      crs  => 'Set in Course',
 7673:                      comm => 'Set in Community',
 7674:                     );
 7675:         $datatable = '<tr class="LC_odd_row">'.
 7676:                   '<td>'.$title{'togglecats'}.'</td>'.
 7677:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7678:                   '<input type="radio" name="togglecats"'.
 7679:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7680:                   '<label><input type="radio" name="togglecats"'.
 7681:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7682:                   '</tr><tr>'.
 7683:                   '<td>'.$title{'categorize'}.'</td>'.
 7684:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7685:                   '<label><input type="radio" name="categorize"'.
 7686:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7687:                   '<label><input type="radio" name="categorize"'.
 7688:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7689:                   '</tr><tr class="LC_odd_row">'.
 7690:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 7691:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7692:                   '<input type="radio" name="togglecatscomm"'.
 7693:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7694:                   '<label><input type="radio" name="togglecatscomm"'.
 7695:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7696:                   '</tr><tr>'.
 7697:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 7698:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7699:                   '<label><input type="radio" name="categorizecomm"'.
 7700:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7701:                   '<label><input type="radio" name="categorizecomm"'.
 7702:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7703:                   '</tr>';
 7704:         $$rowtotal += 4;
 7705:     } else {
 7706:         my $css_class;
 7707:         my $itemcount = 1;
 7708:         my $cathash; 
 7709:         if (ref($settings) eq 'HASH') {
 7710:             $cathash = $settings->{'cats'};
 7711:         }
 7712:         if (ref($cathash) eq 'HASH') {
 7713:             my (@cats,@trails,%allitems,%idx,@jsarray);
 7714:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 7715:                                                    \%allitems,\%idx,\@jsarray);
 7716:             my $maxdepth = scalar(@cats);
 7717:             my $colattrib = '';
 7718:             if ($maxdepth > 2) {
 7719:                 $colattrib = ' colspan="2" ';
 7720:             }
 7721:             my @path;
 7722:             if (@cats > 0) {
 7723:                 if (ref($cats[0]) eq 'ARRAY') {
 7724:                     my $numtop = @{$cats[0]};
 7725:                     my $maxnum = $numtop;
 7726:                     my %default_names = (
 7727:                           instcode    => &mt('Official courses'),
 7728:                           communities => &mt('Communities'),
 7729:                     );
 7730: 
 7731:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 7732:                         ($cathash->{'instcode::0'} eq '') ||
 7733:                         (!grep(/^communities$/,@{$cats[0]})) || 
 7734:                         ($cathash->{'communities::0'} eq '')) {
 7735:                         $maxnum ++;
 7736:                     }
 7737:                     my $lastidx;
 7738:                     for (my $i=0; $i<$numtop; $i++) {
 7739:                         my $parent = $cats[0][$i];
 7740:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7741:                         my $item = &escape($parent).'::0';
 7742:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 7743:                         $lastidx = $idx{$item};
 7744:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7745:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 7746:                         for (my $k=0; $k<=$maxnum; $k++) {
 7747:                             my $vpos = $k+1;
 7748:                             my $selstr;
 7749:                             if ($k == $i) {
 7750:                                 $selstr = ' selected="selected" ';
 7751:                             }
 7752:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7753:                         }
 7754:                         $datatable .= '</select></span></td><td>';
 7755:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 7756:                             $datatable .=  '<span class="LC_nobreak">'
 7757:                                            .$default_names{$parent}.'</span>';
 7758:                             if ($parent eq 'instcode') {
 7759:                                 $datatable .= '<br /><span class="LC_nobreak">('
 7760:                                               .&mt('with institutional codes')
 7761:                                               .')</span></td><td'.$colattrib.'>';
 7762:                             } else {
 7763:                                 $datatable .= '<table><tr><td>';
 7764:                             }
 7765:                             $datatable .= '<span class="LC_nobreak">'
 7766:                                           .'<label><input type="radio" name="'
 7767:                                           .$parent.'" value="1" checked="checked" />'
 7768:                                           .&mt('Display').'</label>';
 7769:                             if ($parent eq 'instcode') {
 7770:                                 $datatable .= '&nbsp;';
 7771:                             } else {
 7772:                                 $datatable .= '</span></td></tr><tr><td>'
 7773:                                               .'<span class="LC_nobreak">';
 7774:                             }
 7775:                             $datatable .= '<label><input type="radio" name="'
 7776:                                           .$parent.'" value="0" />'
 7777:                                           .&mt('Do not display').'</label></span>';
 7778:                             if ($parent eq 'communities') {
 7779:                                 $datatable .= '</td></tr></table>';
 7780:                             }
 7781:                             $datatable .= '</td>';
 7782:                         } else {
 7783:                             $datatable .= $parent
 7784:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 7785:                                           .'<input type="checkbox" name="deletecategory" '
 7786:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 7787:                         }
 7788:                         my $depth = 1;
 7789:                         push(@path,$parent);
 7790:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 7791:                         pop(@path);
 7792:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 7793:                         $itemcount ++;
 7794:                     }
 7795:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7796:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 7797:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 7798:                     for (my $k=0; $k<=$maxnum; $k++) {
 7799:                         my $vpos = $k+1;
 7800:                         my $selstr;
 7801:                         if ($k == $numtop) {
 7802:                             $selstr = ' selected="selected" ';
 7803:                         }
 7804:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7805:                     }
 7806:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 7807:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 7808:                                   .'</tr>'."\n";
 7809:                     $itemcount ++;
 7810:                     foreach my $default ('instcode','communities') {
 7811:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 7812:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7813:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 7814:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 7815:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 7816:                             for (my $k=0; $k<=$maxnum; $k++) {
 7817:                                 my $vpos = $k+1;
 7818:                                 my $selstr;
 7819:                                 if ($k == $maxnum) {
 7820:                                     $selstr = ' selected="selected" ';
 7821:                                 }
 7822:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7823:                             }
 7824:                             $datatable .= '</select></span></td>'.
 7825:                                           '<td><span class="LC_nobreak">'.
 7826:                                           $default_names{$default}.'</span>';
 7827:                             if ($default eq 'instcode') {
 7828:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 7829:                                               .&mt('with institutional codes').')</span>';
 7830:                             }
 7831:                             $datatable .= '</td>'
 7832:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 7833:                                           .&mt('Display').'</label>&nbsp;'
 7834:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 7835:                                           .&mt('Do not display').'</label></span></td></tr>';
 7836:                         }
 7837:                     }
 7838:                 }
 7839:             } else {
 7840:                 $datatable .= &initialize_categories($itemcount);
 7841:             }
 7842:         } else {
 7843:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 7844:                           .&initialize_categories($itemcount);
 7845:         }
 7846:         $$rowtotal += $itemcount;
 7847:     }
 7848:     return $datatable;
 7849: }
 7850: 
 7851: sub print_serverstatuses {
 7852:     my ($dom,$settings,$rowtotal) = @_;
 7853:     my $datatable;
 7854:     my @pages = &serverstatus_pages();
 7855:     my (%namedaccess,%machineaccess);
 7856:     foreach my $type (@pages) {
 7857:         $namedaccess{$type} = '';
 7858:         $machineaccess{$type}= '';
 7859:     }
 7860:     if (ref($settings) eq 'HASH') {
 7861:         foreach my $type (@pages) {
 7862:             if (exists($settings->{$type})) {
 7863:                 if (ref($settings->{$type}) eq 'HASH') {
 7864:                     foreach my $key (keys(%{$settings->{$type}})) {
 7865:                         if ($key eq 'namedusers') {
 7866:                             $namedaccess{$type} = $settings->{$type}->{$key};
 7867:                         } elsif ($key eq 'machines') {
 7868:                             $machineaccess{$type} = $settings->{$type}->{$key};
 7869:                         }
 7870:                     }
 7871:                 }
 7872:             }
 7873:         }
 7874:     }
 7875:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7876:     my $rownum = 0;
 7877:     my $css_class;
 7878:     foreach my $type (@pages) {
 7879:         $rownum ++;
 7880:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 7881:         $datatable .= '<tr'.$css_class.'>'.
 7882:                       '<td><span class="LC_nobreak">'.
 7883:                       $titles->{$type}.'</span></td>'.
 7884:                       '<td class="LC_left_item">'.
 7885:                       '<input type="text" name="'.$type.'_namedusers" '.
 7886:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 7887:                       '<td class="LC_right_item">'.
 7888:                       '<span class="LC_nobreak">'.
 7889:                       '<input type="text" name="'.$type.'_machines" '.
 7890:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 7891:                       '</span></td></tr>'."\n";
 7892:     }
 7893:     $$rowtotal += $rownum;
 7894:     return $datatable;
 7895: }
 7896: 
 7897: sub serverstatus_pages {
 7898:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 7899:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 7900:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 7901:             'uniquecodes','diskusage','coursecatalog');
 7902: }
 7903: 
 7904: sub defaults_javascript {
 7905:     my ($settings) = @_;
 7906:     return unless (ref($settings) eq 'HASH');
 7907:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7908:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 7909:         if ($maxnum eq '') {
 7910:             $maxnum = 0;
 7911:         }
 7912:         $maxnum ++;
 7913:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 7914:         return <<"ENDSCRIPT";
 7915: <script type="text/javascript">
 7916: // <![CDATA[
 7917: function reorderTypes(form,caller) {
 7918:     var changedVal;
 7919: $jstext 
 7920:     var newpos = 'addinststatus_pos';
 7921:     var current = new Array;
 7922:     var maxh = $maxnum;
 7923:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7924:     var oldVal;
 7925:     if (caller == newpos) {
 7926:         changedVal = newitemVal;
 7927:     } else {
 7928:         var curritem = 'inststatus_pos_'+caller;
 7929:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7930:         current[newitemVal] = newpos;
 7931:     }
 7932:     for (var i=0; i<inststatuses.length; i++) {
 7933:         if (inststatuses[i] != caller) {
 7934:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7935:             if (form.elements[elementName]) {
 7936:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7937:                 current[currVal] = elementName;
 7938:             }
 7939:         }
 7940:     }
 7941:     for (var j=0; j<maxh; j++) {
 7942:         if (current[j] == undefined) {
 7943:             oldVal = j;
 7944:         }
 7945:     }
 7946:     if (oldVal < changedVal) {
 7947:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7948:            var elementName = current[k];
 7949:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7950:         }
 7951:     } else {
 7952:         for (var k=changedVal; k<oldVal; k++) {
 7953:             var elementName = current[k];
 7954:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7955:         }
 7956:     }
 7957:     return;
 7958: }
 7959: 
 7960: // ]]>
 7961: </script>
 7962: 
 7963: ENDSCRIPT
 7964:     }
 7965: }
 7966: 
 7967: sub passwords_javascript {
 7968:     my %intalert = &Apache::lonlocal::texthash (
 7969:         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.',
 7970:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
 7971:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
 7972:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
 7973:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
 7974:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
 7975:     );
 7976:     &js_escape(\%intalert);
 7977:     my $defmin = $Apache::lonnet::passwdmin;
 7978:     my $intauthjs = <<"ENDSCRIPT";
 7979: 
 7980: function warnIntAuth(field) {
 7981:     if (field.name == 'intauth_check') {
 7982:         if (field.value == '2') {
 7983:             alert('$intalert{authcheck}');
 7984:         }
 7985:     }
 7986:     if (field.name == 'intauth_cost') {
 7987:         field.value.replace(/\s/g,'');
 7988:         if (field.value != '') {
 7989:             var regexdigit=/^\\d+\$/;
 7990:             if (!regexdigit.test(field.value)) {
 7991:                 alert('$intalert{authcost}');
 7992:             }
 7993:         }
 7994:     }
 7995:     return;
 7996: }
 7997: 
 7998: function warnIntPass(field) {
 7999:     field.value.replace(/^\s+/,'');
 8000:     field.value.replace(/\s+\$/,'');
 8001:     var regexdigit=/^\\d+\$/;
 8002:     if (field.name == 'passwords_min') {
 8003:         if (field.value == '') {
 8004:             alert('$intalert{passmin}');
 8005:             field.value = '$defmin';
 8006:         } else {
 8007:             if (!regexdigit.test(field.value)) {
 8008:                 alert('$intalert{passmin}');
 8009:                 field.value = '$defmin';
 8010:             }
 8011:             var minval = parseInt(field.value,10);
 8012:             if (minval < $defmin) {
 8013:                 alert('$intalert{passmin}');
 8014:                 field.value = '$defmin';
 8015:             }
 8016:         }
 8017:     } else {
 8018:         if (field.value == '0') {
 8019:             field.value = '';
 8020:         }
 8021:         if (field.value != '') {
 8022:             if (field.name == 'passwords_expire') {
 8023:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/;
 8024:                 if (!regexpposnum.test(field.value)) {
 8025:                     alert('$intalert{passexp}');
 8026:                     field.value = '';
 8027:                 } else {
 8028:                     var expval = parseFloat(field.value);
 8029:                     if (expval == 0) {
 8030:                         alert('$intalert{passexp}');
 8031:                         field.value = '';
 8032:                     }
 8033:                 }
 8034:             } else {
 8035:                 if (!regexdigit.test(field.value)) {
 8036:                     if (field.name == 'passwords_max') {
 8037:                         alert('$intalert{passmax}');
 8038:                     } else {
 8039:                         if (field.name == 'passwords_numsaved') {
 8040:                             alert('$intalert{passnum}');
 8041:                         }
 8042:                     }
 8043:                     field.value = '';
 8044:                 }
 8045:             }
 8046:         }
 8047:     }
 8048:     return;
 8049: }
 8050: 
 8051: ENDSCRIPT
 8052:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
 8053: }
 8054: 
 8055: sub coursecategories_javascript {
 8056:     my ($settings) = @_;
 8057:     my ($output,$jstext,$cathash);
 8058:     if (ref($settings) eq 'HASH') {
 8059:         $cathash = $settings->{'cats'};
 8060:     }
 8061:     if (ref($cathash) eq 'HASH') {
 8062:         my (@cats,@jsarray,%idx);
 8063:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 8064:         if (@jsarray > 0) {
 8065:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 8066:             for (my $i=0; $i<@jsarray; $i++) {
 8067:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 8068:                     my $catstr = join('","',@{$jsarray[$i]});
 8069:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 8070:                 }
 8071:             }
 8072:         }
 8073:     } else {
 8074:         $jstext  = '    var categories = Array(1);'."\n".
 8075:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 8076:     }
 8077:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 8078:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 8079:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 8080:     &js_escape(\$instcode_reserved);
 8081:     &js_escape(\$communities_reserved);
 8082:     &js_escape(\$choose_again);
 8083:     $output = <<"ENDSCRIPT";
 8084: <script type="text/javascript">
 8085: // <![CDATA[
 8086: function reorderCats(form,parent,item,idx) {
 8087:     var changedVal;
 8088: $jstext
 8089:     var newpos = 'addcategory_pos';
 8090:     if (parent == '') {
 8091:         var has_instcode = 0;
 8092:         var maxtop = categories[idx].length;
 8093:         for (var j=0; j<maxtop; j++) {
 8094:             if (categories[idx][j] == 'instcode::0') {
 8095:                 has_instcode == 1;
 8096:             }
 8097:         }
 8098:         if (has_instcode == 0) {
 8099:             categories[idx][maxtop] = 'instcode_pos';
 8100:         }
 8101:     } else {
 8102:         newpos += '_'+parent;
 8103:     }
 8104:     var maxh = 1 + categories[idx].length;
 8105:     var current = new Array;
 8106:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 8107:     if (item == newpos) {
 8108:         changedVal = newitemVal;
 8109:     } else {
 8110:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 8111:         current[newitemVal] = newpos;
 8112:     }
 8113:     for (var i=0; i<categories[idx].length; i++) {
 8114:         var elementName = categories[idx][i];
 8115:         if (elementName != item) {
 8116:             if (form.elements[elementName]) {
 8117:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 8118:                 current[currVal] = elementName;
 8119:             }
 8120:         }
 8121:     }
 8122:     var oldVal;
 8123:     for (var j=0; j<maxh; j++) {
 8124:         if (current[j] == undefined) {
 8125:             oldVal = j;
 8126:         }
 8127:     }
 8128:     if (oldVal < changedVal) {
 8129:         for (var k=oldVal+1; k<=changedVal ; k++) {
 8130:            var elementName = current[k];
 8131:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 8132:         }
 8133:     } else {
 8134:         for (var k=changedVal; k<oldVal; k++) {
 8135:             var elementName = current[k];
 8136:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 8137:         }
 8138:     }
 8139:     return;
 8140: }
 8141: 
 8142: function categoryCheck(form) {
 8143:     if (form.elements['addcategory_name'].value == 'instcode') {
 8144:         alert('$instcode_reserved\\n$choose_again');
 8145:         return false;
 8146:     }
 8147:     if (form.elements['addcategory_name'].value == 'communities') {
 8148:         alert('$communities_reserved\\n$choose_again');
 8149:         return false;
 8150:     }
 8151:     return true;
 8152: }
 8153: 
 8154: // ]]>
 8155: </script>
 8156: 
 8157: ENDSCRIPT
 8158:     return $output;
 8159: }
 8160: 
 8161: sub initialize_categories {
 8162:     my ($itemcount) = @_;
 8163:     my ($datatable,$css_class,$chgstr);
 8164:     my %default_names = &Apache::lonlocal::texthash (
 8165:                       instcode    => 'Official courses (with institutional codes)',
 8166:                       communities => 'Communities',
 8167:                         );
 8168:     my $select0 = ' selected="selected"';
 8169:     my $select1 = '';
 8170:     foreach my $default ('instcode','communities') {
 8171:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8172:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 8173:         if ($default eq 'communities') {
 8174:             $select1 = $select0;
 8175:             $select0 = '';
 8176:         }
 8177:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8178:                      .'<select name="'.$default.'_pos">'
 8179:                      .'<option value="0"'.$select0.'>1</option>'
 8180:                      .'<option value="1"'.$select1.'>2</option>'
 8181:                      .'<option value="2">3</option></select>&nbsp;'
 8182:                      .$default_names{$default}
 8183:                      .'</span></td><td><span class="LC_nobreak">'
 8184:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 8185:                      .&mt('Display').'</label>&nbsp;<label>'
 8186:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 8187:                  .'</label></span></td></tr>';
 8188:         $itemcount ++;
 8189:     }
 8190:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8191:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 8192:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 8193:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 8194:                   .'<option value="0">1</option>'
 8195:                   .'<option value="1">2</option>'
 8196:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 8197:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 8198:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 8199:                   .'</td></tr>';
 8200:     return $datatable;
 8201: }
 8202: 
 8203: sub build_category_rows {
 8204:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 8205:     my ($text,$name,$item,$chgstr);
 8206:     if (ref($cats) eq 'ARRAY') {
 8207:         my $maxdepth = scalar(@{$cats});
 8208:         if (ref($cats->[$depth]) eq 'HASH') {
 8209:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 8210:                 my $numchildren = @{$cats->[$depth]{$parent}};
 8211:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8212:                 $text .= '<td><table class="LC_data_table">';
 8213:                 my ($idxnum,$parent_name,$parent_item);
 8214:                 my $higher = $depth - 1;
 8215:                 if ($higher == 0) {
 8216:                     $parent_name = &escape($parent).'::'.$higher;
 8217:                 } else {
 8218:                     if (ref($path) eq 'ARRAY') {
 8219:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8220:                     }
 8221:                 }
 8222:                 $parent_item = 'addcategory_pos_'.$parent_name;
 8223:                 for (my $j=0; $j<=$numchildren; $j++) {
 8224:                     if ($j < $numchildren) {
 8225:                         $name = $cats->[$depth]{$parent}[$j];
 8226:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 8227:                         $idxnum = $idx->{$item};
 8228:                     } else {
 8229:                         $name = $parent_name;
 8230:                         $item = $parent_item;
 8231:                     }
 8232:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 8233:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 8234:                     for (my $i=0; $i<=$numchildren; $i++) {
 8235:                         my $vpos = $i+1;
 8236:                         my $selstr;
 8237:                         if ($j == $i) {
 8238:                             $selstr = ' selected="selected" ';
 8239:                         }
 8240:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 8241:                     }
 8242:                     $text .= '</select>&nbsp;';
 8243:                     if ($j < $numchildren) {
 8244:                         my $deeper = $depth+1;
 8245:                         $text .= $name.'&nbsp;'
 8246:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 8247:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 8248:                         if(ref($path) eq 'ARRAY') {
 8249:                             push(@{$path},$name);
 8250:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 8251:                             pop(@{$path});
 8252:                         }
 8253:                     } else {
 8254:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 8255:                         if ($j == $numchildren) {
 8256:                             $text .= $name;
 8257:                         } else {
 8258:                             $text .= $item;
 8259:                         }
 8260:                         $text .= '" value="" />';
 8261:                     }
 8262:                     $text .= '</td></tr>';
 8263:                 }
 8264:                 $text .= '</table></td>';
 8265:             } else {
 8266:                 my $higher = $depth-1;
 8267:                 if ($higher == 0) {
 8268:                     $name = &escape($parent).'::'.$higher;
 8269:                 } else {
 8270:                     if (ref($path) eq 'ARRAY') {
 8271:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 8272:                     }
 8273:                 }
 8274:                 my $colspan;
 8275:                 if ($parent ne 'instcode') {
 8276:                     $colspan = $maxdepth - $depth - 1;
 8277:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 8278:                 }
 8279:             }
 8280:         }
 8281:     }
 8282:     return $text;
 8283: }
 8284: 
 8285: sub modifiable_userdata_row {
 8286:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 8287:         $rowid,$customcss,$rowstyle) = @_;
 8288:     my ($role,$rolename,$statustype);
 8289:     $role = $item;
 8290:     if ($context eq 'cancreate') {
 8291:         if ($item =~ /^(emailusername)_(.+)$/) {
 8292:             $role = $1;
 8293:             $statustype = $2;
 8294:             if (ref($usertypes) eq 'HASH') {
 8295:                 if ($usertypes->{$statustype}) {
 8296:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 8297:                 } else {
 8298:                     $rolename = &mt('Data provided by user');
 8299:                 }
 8300:             }
 8301:         }
 8302:     } elsif ($context eq 'selfcreate') {
 8303:         if (ref($usertypes) eq 'HASH') {
 8304:             $rolename = $usertypes->{$role};
 8305:         } else {
 8306:             $rolename = $role;
 8307:         }
 8308:     } else {
 8309:         if ($role eq 'cr') {
 8310:             $rolename = &mt('Custom role');
 8311:         } else {
 8312:             $rolename = &Apache::lonnet::plaintext($role);
 8313:         }
 8314:     }
 8315:     my (@fields,%fieldtitles);
 8316:     if (ref($fieldsref) eq 'ARRAY') {
 8317:         @fields = @{$fieldsref};
 8318:     } else {
 8319:         @fields = ('lastname','firstname','middlename','generation',
 8320:                    'permanentemail','id');
 8321:     }
 8322:     if ((ref($titlesref) eq 'HASH')) {
 8323:         %fieldtitles = %{$titlesref};
 8324:     } else {
 8325:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8326:     }
 8327:     my $output;
 8328:     my $css_class;
 8329:     if ($rowcount%2) {
 8330:         $css_class = 'LC_odd_row';
 8331:     }
 8332:     if ($customcss) {
 8333:         $css_class .= " $customcss";
 8334:     }
 8335:     $css_class =~ s/^\s+//;
 8336:     if ($css_class) {
 8337:         $css_class = ' class="'.$css_class.'"';
 8338:     }
 8339:     if ($rowstyle) {
 8340:         $css_class .= ' style="'.$rowstyle.'"';
 8341:     }
 8342:     if ($rowid) {
 8343:         $rowid = ' id="'.$rowid.'"';
 8344:     }
 8345: 
 8346:     $output = '<tr '.$css_class.$rowid.'>'.
 8347:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 8348:               '<td class="LC_left_item" colspan="2"><table>';
 8349:     my $rem;
 8350:     my %checks;
 8351:     if (ref($settings) eq 'HASH') {
 8352:         if (ref($settings->{$context}) eq 'HASH') {
 8353:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 8354:                 my $hashref = $settings->{$context}->{$role};
 8355:                 if ($role eq 'emailusername') {
 8356:                     if ($statustype) {
 8357:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 8358:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 8359:                             if (ref($hashref) eq 'HASH') { 
 8360:                                 foreach my $field (@fields) {
 8361:                                     if ($hashref->{$field}) {
 8362:                                         $checks{$field} = $hashref->{$field};
 8363:                                     }
 8364:                                 }
 8365:                             }
 8366:                         }
 8367:                     }
 8368:                 } else {
 8369:                     if (ref($hashref) eq 'HASH') {
 8370:                         foreach my $field (@fields) {
 8371:                             if ($hashref->{$field}) {
 8372:                                 $checks{$field} = ' checked="checked" ';
 8373:                             }
 8374:                         }
 8375:                     }
 8376:                 }
 8377:             }
 8378:         }
 8379:     }
 8380: 
 8381:     my $total = scalar(@fields);
 8382:     for (my $i=0; $i<$total; $i++) {
 8383:         $rem = $i%($numinrow);
 8384:         if ($rem == 0) {
 8385:             if ($i > 0) {
 8386:                 $output .= '</tr>';
 8387:             }
 8388:             $output .= '<tr>';
 8389:         }
 8390:         my $check = ' ';
 8391:         unless ($role eq 'emailusername') {
 8392:             if (exists($checks{$fields[$i]})) {
 8393:                 $check = $checks{$fields[$i]};
 8394:             } else {
 8395:                 if ($role eq 'st') {
 8396:                     if (ref($settings) ne 'HASH') {
 8397:                         $check = ' checked="checked" '; 
 8398:                     }
 8399:                 }
 8400:             }
 8401:         }
 8402:         $output .= '<td class="LC_left_item">'.
 8403:                    '<span class="LC_nobreak">';
 8404:         if ($role eq 'emailusername') {
 8405:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 8406:                 $checks{$fields[$i]} = 'omit';
 8407:             }
 8408:             foreach my $option ('required','optional','omit') {
 8409:                 my $checked='';
 8410:                 if ($checks{$fields[$i]} eq $option) {
 8411:                     $checked='checked="checked" ';
 8412:                 }
 8413:                 $output .= '<label>'.
 8414:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 8415:                            &mt($option).'</label>'.('&nbsp;' x2);
 8416:             }
 8417:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 8418:         } else {
 8419:             $output .= '<label>'.
 8420:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 8421:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 8422:                        '</label>';
 8423:         }
 8424:         $output .= '</span></td>';
 8425:     }
 8426:     $rem = $total%$numinrow;
 8427:     my $colsleft;
 8428:     if ($rem) {
 8429:         $colsleft = $numinrow - $rem;
 8430:     }
 8431:     if ($colsleft > 1) {
 8432:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8433:                    '&nbsp;</td>';
 8434:     } elsif ($colsleft == 1) {
 8435:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 8436:     }
 8437:     $output .= '</tr></table></td></tr>';
 8438:     return $output;
 8439: }
 8440: 
 8441: sub insttypes_row {
 8442:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 8443:         $customcss,$rowstyle) = @_;
 8444:     my %lt = &Apache::lonlocal::texthash (
 8445:                       cansearch => 'Users allowed to search',
 8446:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 8447:                       lockablenames => 'User preference to lock name',
 8448:                       selfassign    => 'Self-reportable affiliations',
 8449:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 8450:              );
 8451:     my $showdom;
 8452:     if ($context eq 'cansearch') {
 8453:         $showdom = ' ('.$dom.')';
 8454:     }
 8455:     my $class = 'LC_left_item';
 8456:     if ($context eq 'statustocreate') {
 8457:         $class = 'LC_right_item';
 8458:     }
 8459:     my $css_class;
 8460:     if ($$rowtotal%2) {
 8461:         $css_class = 'LC_odd_row';
 8462:     }
 8463:     if ($customcss) {
 8464:         $css_class .= ' '.$customcss;
 8465:     }
 8466:     $css_class =~ s/^\s+//;
 8467:     if ($css_class) {
 8468:         $css_class = ' class="'.$css_class.'"';
 8469:     }
 8470:     if ($rowstyle) {
 8471:         $css_class .= ' style="'.$rowstyle.'"';
 8472:     }
 8473:     if ($onclick) {
 8474:         $onclick = 'onclick="'.$onclick.'" ';
 8475:     }
 8476:     my $output = '<tr'.$css_class.'>'.
 8477:                  '<td>'.$lt{$context}.$showdom.
 8478:                  '</td><td class="'.$class.'" colspan="2"><table>';
 8479:     my $rem;
 8480:     if (ref($types) eq 'ARRAY') {
 8481:         for (my $i=0; $i<@{$types}; $i++) {
 8482:             if (defined($usertypes->{$types->[$i]})) {
 8483:                 my $rem = $i%($numinrow);
 8484:                 if ($rem == 0) {
 8485:                     if ($i > 0) {
 8486:                         $output .= '</tr>';
 8487:                     }
 8488:                     $output .= '<tr>';
 8489:                 }
 8490:                 my $check = ' ';
 8491:                 if (ref($settings) eq 'HASH') {
 8492:                     if (ref($settings->{$context}) eq 'ARRAY') {
 8493:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 8494:                             $check = ' checked="checked" ';
 8495:                         }
 8496:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 8497:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 8498:                             $check = ' checked="checked" ';
 8499:                         }
 8500:                     } elsif ($context eq 'statustocreate') {
 8501:                         $check = ' checked="checked" ';
 8502:                     }
 8503:                 }
 8504:                 $output .= '<td class="LC_left_item">'.
 8505:                            '<span class="LC_nobreak"><label>'.
 8506:                            '<input type="checkbox" name="'.$context.'" '.
 8507:                            'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
 8508:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 8509:             }
 8510:         }
 8511:         $rem = @{$types}%($numinrow);
 8512:     }
 8513:     my $colsleft = $numinrow - $rem;
 8514:     if ($context eq 'overrides') {
 8515:         if ($colsleft > 1) {
 8516:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8517:         } else {
 8518:             $output .= '<td class="LC_left_item">';
 8519:         }
 8520:         $output .= '&nbsp;';
 8521:     } else {
 8522:         if ($rem == 0) {
 8523:             $output .= '<tr>';
 8524:         }
 8525:         if ($colsleft > 1) {
 8526:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8527:         } else {
 8528:             $output .= '<td class="LC_left_item">';
 8529:         }
 8530:         my $defcheck = ' ';
 8531:         if (ref($settings) eq 'HASH') {  
 8532:             if (ref($settings->{$context}) eq 'ARRAY') {
 8533:                 if (grep(/^default$/,@{$settings->{$context}})) {
 8534:                     $defcheck = ' checked="checked" ';
 8535:                 }
 8536:             } elsif ($context eq 'statustocreate') {
 8537:                 $defcheck = ' checked="checked" ';
 8538:             }
 8539:         }
 8540:         $output .= '<span class="LC_nobreak"><label>'.
 8541:                    '<input type="checkbox" name="'.$context.'" '.
 8542:                    'value="default"'.$defcheck.$onclick.' />'.
 8543:                    $othertitle.'</label></span>';
 8544:     }
 8545:     $output .= '</td></tr></table></td></tr>';
 8546:     return $output;
 8547: }
 8548: 
 8549: sub sorted_searchtitles {
 8550:     my %searchtitles = &Apache::lonlocal::texthash(
 8551:                          'uname' => 'username',
 8552:                          'lastname' => 'last name',
 8553:                          'lastfirst' => 'last name, first name',
 8554:                      );
 8555:     my @titleorder = ('uname','lastname','lastfirst');
 8556:     return (\%searchtitles,\@titleorder);
 8557: }
 8558: 
 8559: sub sorted_searchtypes {
 8560:     my %srchtypes_desc = (
 8561:                            exact    => 'is exact match',
 8562:                            contains => 'contains ..',
 8563:                            begins   => 'begins with ..',
 8564:                          );
 8565:     my @srchtypeorder = ('exact','begins','contains');
 8566:     return (\%srchtypes_desc,\@srchtypeorder);
 8567: }
 8568: 
 8569: sub usertype_update_row {
 8570:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 8571:     my $datatable;
 8572:     my $numinrow = 4;
 8573:     foreach my $type (@{$types}) {
 8574:         if (defined($usertypes->{$type})) {
 8575:             $$rownums ++;
 8576:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 8577:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 8578:                           '</td><td class="LC_left_item"><table>';
 8579:             for (my $i=0; $i<@{$fields}; $i++) {
 8580:                 my $rem = $i%($numinrow);
 8581:                 if ($rem == 0) {
 8582:                     if ($i > 0) {
 8583:                         $datatable .= '</tr>';
 8584:                     }
 8585:                     $datatable .= '<tr>';
 8586:                 }
 8587:                 my $check = ' ';
 8588:                 if (ref($settings) eq 'HASH') {
 8589:                     if (ref($settings->{'fields'}) eq 'HASH') {
 8590:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 8591:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 8592:                                 $check = ' checked="checked" ';
 8593:                             }
 8594:                         }
 8595:                     }
 8596:                 }
 8597: 
 8598:                 if ($i == @{$fields}-1) {
 8599:                     my $colsleft = $numinrow - $rem;
 8600:                     if ($colsleft > 1) {
 8601:                         $datatable .= '<td colspan="'.$colsleft.'">';
 8602:                     } else {
 8603:                         $datatable .= '<td>';
 8604:                     }
 8605:                 } else {
 8606:                     $datatable .= '<td>';
 8607:                 }
 8608:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8609:                               '<input type="checkbox" name="updateable_'.$type.
 8610:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 8611:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 8612:             }
 8613:             $datatable .= '</tr></table></td></tr>';
 8614:         }
 8615:     }
 8616:     return $datatable;
 8617: }
 8618: 
 8619: sub modify_login {
 8620:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8621:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 8622:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 8623:     %title = ( coursecatalog => 'Display course catalog',
 8624:                adminmail => 'Display administrator E-mail address',
 8625:                helpdesk  => 'Display "Contact Helpdesk" link',
 8626:                newuser => 'Link for visitors to create a user account',
 8627:                loginheader => 'Log-in box header');
 8628:     @offon = ('off','on');
 8629:     if (ref($domconfig{login}) eq 'HASH') {
 8630:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 8631:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 8632:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 8633:             }
 8634:         }
 8635:     }
 8636:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 8637:                                            \%domconfig,\%loginhash);
 8638:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8639:     foreach my $item (@toggles) {
 8640:         $loginhash{login}{$item} = $env{'form.'.$item};
 8641:     }
 8642:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 8643:     if (ref($colchanges{'login'}) eq 'HASH') {  
 8644:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 8645:                                          \%loginhash);
 8646:     }
 8647: 
 8648:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8649:     my %domservers = &Apache::lonnet::get_servers($dom);
 8650:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 8651:     if (keys(%servers) > 1) {
 8652:         foreach my $lonhost (keys(%servers)) {
 8653:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 8654:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 8655:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 8656:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 8657:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 8658:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8659:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8660:                         $changes{'loginvia'}{$lonhost} = 1;
 8661:                     } else {
 8662:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 8663:                         $changes{'loginvia'}{$lonhost} = 1;
 8664:                     }
 8665:                 } else {
 8666:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8667:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8668:                         $changes{'loginvia'}{$lonhost} = 1;
 8669:                     }
 8670:                 }
 8671:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 8672:                     foreach my $item (@loginvia_attribs) {
 8673:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 8674:                     }
 8675:                 } else {
 8676:                     foreach my $item (@loginvia_attribs) {
 8677:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8678:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8679:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 8680:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8681:                                 $new = '/';
 8682:                             }
 8683:                         }
 8684:                         if (($item eq 'custompath') && 
 8685:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8686:                             $new = '';
 8687:                         }
 8688:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 8689:                             $changes{'loginvia'}{$lonhost} = 1;
 8690:                         }
 8691:                         if ($item eq 'exempt') {
 8692:                             $new = &check_exempt_addresses($new);
 8693:                         }
 8694:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8695:                     }
 8696:                 }
 8697:             } else {
 8698:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8699:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8700:                     $changes{'loginvia'}{$lonhost} = 1;
 8701:                     foreach my $item (@loginvia_attribs) {
 8702:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8703:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8704:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8705:                                 $new = '/';
 8706:                             }
 8707:                         }
 8708:                         if (($item eq 'custompath') && 
 8709:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8710:                             $new = '';
 8711:                         }
 8712:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8713:                     }
 8714:                 }
 8715:             }
 8716:         }
 8717:     }
 8718: 
 8719:     my $servadm = $r->dir_config('lonAdmEMail');
 8720:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 8721:     if (ref($domconfig{'login'}) eq 'HASH') {
 8722:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 8723:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 8724:                 if ($lang eq 'nolang') {
 8725:                     push(@currlangs,$lang);
 8726:                 } elsif (defined($langchoices{$lang})) {
 8727:                     push(@currlangs,$lang);
 8728:                 } else {
 8729:                     next;
 8730:                 }
 8731:             }
 8732:         }
 8733:     }
 8734:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 8735:     if (@currlangs > 0) {
 8736:         foreach my $lang (@currlangs) {
 8737:             if (grep(/^\Q$lang\E$/,@delurls)) {
 8738:                 $changes{'helpurl'}{$lang} = 1;
 8739:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 8740:                 $changes{'helpurl'}{$lang} = 1;
 8741:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 8742:                 push(@newlangs,$lang);
 8743:             } else {
 8744:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8745:             }
 8746:         }
 8747:     }
 8748:     unless (grep(/^nolang$/,@currlangs)) {
 8749:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 8750:             $changes{'helpurl'}{'nolang'} = 1;
 8751:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 8752:             push(@newlangs,'nolang');
 8753:         }
 8754:     }
 8755:     if ($env{'form.loginhelpurl_add_lang'}) {
 8756:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 8757:             ($env{'form.loginhelpurl_add_file.filename'})) {
 8758:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 8759:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 8760:         }
 8761:     }
 8762:     if ((@newlangs > 0) || ($addedfile)) {
 8763:         my $error;
 8764:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8765:         if ($configuserok eq 'ok') {
 8766:             if ($switchserver) {
 8767:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 8768:             } elsif ($author_ok eq 'ok') {
 8769:                 my @allnew = @newlangs;
 8770:                 if ($addedfile ne '') {
 8771:                     push(@allnew,$addedfile);
 8772:                 }
 8773:                 foreach my $lang (@allnew) {
 8774:                     my $formelem = 'loginhelpurl_'.$lang;
 8775:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 8776:                         $formelem = 'loginhelpurl_add_file';
 8777:                     }
 8778:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8779:                                                                "help/$lang",'','',$newfile{$lang});
 8780:                     if ($result eq 'ok') {
 8781:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 8782:                         $changes{'helpurl'}{$lang} = 1;
 8783:                     } else {
 8784:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 8785:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8786:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 8787:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 8788:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8789:                         }
 8790:                     }
 8791:                 }
 8792:             } else {
 8793:                 $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);
 8794:             }
 8795:         } else {
 8796:             $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);
 8797:         }
 8798:         if ($error) {
 8799:             &Apache::lonnet::logthis($error);
 8800:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8801:         }
 8802:     }
 8803: 
 8804:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 8805:     if (ref($domconfig{'login'}) eq 'HASH') {
 8806:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 8807:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 8808:                 if ($domservers{$lonhost}) {
 8809:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8810:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 8811:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 8812:                     }
 8813:                 }
 8814:             }
 8815:         }
 8816:     }
 8817:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 8818:     foreach my $lonhost (sort(keys(%domservers))) {
 8819:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8820:             $changes{'headtag'}{$lonhost} = 1;
 8821:         } else {
 8822:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 8823:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 8824:             }
 8825:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 8826:                 push(@newhosts,$lonhost);
 8827:             } elsif ($currheadtagurls{$lonhost}) {
 8828:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 8829:                 if ($currexempt{$lonhost}) {
 8830:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 8831:                         $changes{'headtag'}{$lonhost} = 1;
 8832:                     }
 8833:                 } elsif ($possexempt{$lonhost}) {
 8834:                     $changes{'headtag'}{$lonhost} = 1;
 8835:                 }
 8836:                 if ($possexempt{$lonhost}) {
 8837:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8838:                 }
 8839:             }
 8840:         }
 8841:     }
 8842:     if (@newhosts) {
 8843:         my $error;
 8844:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8845:         if ($configuserok eq 'ok') {
 8846:             if ($switchserver) {
 8847:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8848:             } elsif ($author_ok eq 'ok') {
 8849:                 foreach my $lonhost (@newhosts) {
 8850:                     my $formelem = 'loginheadtag_'.$lonhost;
 8851:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8852:                                                                           "login/headtag/$lonhost",'','',
 8853:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8854:                     if ($result eq 'ok') {
 8855:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8856:                         $changes{'headtag'}{$lonhost} = 1;
 8857:                         if ($possexempt{$lonhost}) {
 8858:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8859:                         }
 8860:                     } else {
 8861:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 8862:                                            $newheadtagurls{$lonhost},$result);
 8863:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8864:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 8865:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 8866:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 8867:                         }
 8868:                     }
 8869:                 }
 8870:             } else {
 8871:                 $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);
 8872:             }
 8873:         } else {
 8874:             $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);
 8875:         }
 8876:         if ($error) {
 8877:             &Apache::lonnet::logthis($error);
 8878:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8879:         }
 8880:     }
 8881:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 8882: 
 8883:     my $defaulthelpfile = '/adm/loginproblems.html';
 8884:     my $defaulttext = &mt('Default in use');
 8885: 
 8886:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 8887:                                              $dom);
 8888:     if ($putresult eq 'ok') {
 8889:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8890:         my %defaultchecked = (
 8891:                     'coursecatalog' => 'on',
 8892:                     'helpdesk'      => 'on',
 8893:                     'adminmail'     => 'off',
 8894:                     'newuser'       => 'off',
 8895:         );
 8896:         if (ref($domconfig{'login'}) eq 'HASH') {
 8897:             foreach my $item (@toggles) {
 8898:                 if ($defaultchecked{$item} eq 'on') { 
 8899:                     if (($domconfig{'login'}{$item} eq '0') &&
 8900:                         ($env{'form.'.$item} eq '1')) {
 8901:                         $changes{$item} = 1;
 8902:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8903:                               $domconfig{'login'}{$item} eq '1') &&
 8904:                              ($env{'form.'.$item} eq '0')) {
 8905:                         $changes{$item} = 1;
 8906:                     }
 8907:                 } elsif ($defaultchecked{$item} eq 'off') {
 8908:                     if (($domconfig{'login'}{$item} eq '1') &&
 8909:                         ($env{'form.'.$item} eq '0')) {
 8910:                         $changes{$item} = 1;
 8911:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8912:                               $domconfig{'login'}{$item} eq '0') &&
 8913:                              ($env{'form.'.$item} eq '1')) {
 8914:                         $changes{$item} = 1;
 8915:                     }
 8916:                 }
 8917:             }
 8918:         }
 8919:         if (keys(%changes) > 0 || $colchgtext) {
 8920:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8921:             if (ref($lastactref) eq 'HASH') {
 8922:                 $lastactref->{'domainconfig'} = 1;
 8923:             }
 8924:             $resulttext = &mt('Changes made:').'<ul>';
 8925:             foreach my $item (sort(keys(%changes))) {
 8926:                 if ($item eq 'loginvia') {
 8927:                     if (ref($changes{$item}) eq 'HASH') {
 8928:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 8929:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8930:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 8931:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 8932:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 8933:                                     $protocol = 'http' if ($protocol ne 'https');
 8934:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 8935: 
 8936:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 8937:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 8938:                                     } else {
 8939:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 8940:                                     }
 8941:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 8942:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 8943:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 8944:                                     }
 8945:                                     $resulttext .= '</li>';
 8946:                                 } else {
 8947:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 8948:                                 }
 8949:                             } else {
 8950:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 8951:                             }
 8952:                         }
 8953:                         $resulttext .= '</ul></li>';
 8954:                     }
 8955:                 } elsif ($item eq 'helpurl') {
 8956:                     if (ref($changes{$item}) eq 'HASH') {
 8957:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 8958:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 8959:                                 my ($chg,$link);
 8960:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 8961:                                 if ($lang eq 'nolang') {
 8962:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 8963:                                 } else {
 8964:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 8965:                                 }
 8966:                                 $resulttext .= '<li>'.$chg.'</li>';
 8967:                             } else {
 8968:                                 my $chg;
 8969:                                 if ($lang eq 'nolang') {
 8970:                                     $chg = &mt('custom log-in help file for no preferred language');
 8971:                                 } else {
 8972:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 8973:                                 }
 8974:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 8975:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 8976:                                                       '?inhibitmenu=yes',$chg,600,500).
 8977:                                                '</li>';
 8978:                             }
 8979:                         }
 8980:                     }
 8981:                 } elsif ($item eq 'headtag') {
 8982:                     if (ref($changes{$item}) eq 'HASH') {
 8983:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8984:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8985:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 8986:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8987:                                 $resulttext .= '<li><a href="'.
 8988:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 8989:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 8990:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 8991:                                 if ($possexempt{$lonhost}) {
 8992:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 8993:                                 } else {
 8994:                                     $resulttext .= &mt('included for any client IP');
 8995:                                 }
 8996:                                 $resulttext .= '</li>';
 8997:                             }
 8998:                         }
 8999:                     }
 9000:                 } elsif ($item eq 'captcha') {
 9001:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9002:                         my $chgtxt;
 9003:                         if ($loginhash{'login'}{$item} eq 'notused') {
 9004:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 9005:                         } else {
 9006:                             my %captchas = &captcha_phrases();
 9007:                             if ($captchas{$loginhash{'login'}{$item}}) {
 9008:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 9009:                             } else {
 9010:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 9011:                             }
 9012:                         }
 9013:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9014:                     }
 9015:                 } elsif ($item eq 'recaptchakeys') {
 9016:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9017:                         my ($privkey,$pubkey);
 9018:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 9019:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 9020:                             $privkey = $loginhash{'login'}{$item}{'private'};
 9021:                         }
 9022:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 9023:                         if (!$pubkey) {
 9024:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 9025:                         } else {
 9026:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9027:                         }
 9028:                         if (!$privkey) {
 9029:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 9030:                         } else {
 9031:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 9032:                         }
 9033:                         $chgtxt .= '</ul>';
 9034:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 9035:                     }
 9036:                 } elsif ($item eq 'recaptchaversion') {
 9037:                     if (ref($loginhash{'login'}) eq 'HASH') {
 9038:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 9039:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 9040:                                            '</li>';
 9041:                         }
 9042:                     }
 9043:                 } else {
 9044:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 9045:                 }
 9046:             }
 9047:             $resulttext .= $colchgtext.'</ul>';
 9048:         } else {
 9049:             $resulttext = &mt('No changes made to log-in page settings');
 9050:         }
 9051:     } else {
 9052:         $resulttext = '<span class="LC_error">'.
 9053: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9054:     }
 9055:     if ($errors) {
 9056:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 9057:                        $errors.'</ul>';
 9058:     }
 9059:     return $resulttext;
 9060: }
 9061: 
 9062: sub check_exempt_addresses {
 9063:     my ($iplist) = @_;
 9064:     $iplist =~ s/^\s+//;
 9065:     $iplist =~ s/\s+$//;
 9066:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 9067:     my (@okips,$new);
 9068:     foreach my $ip (@poss_ips) {
 9069:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 9070:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 9071:                 push(@okips,$ip);
 9072:             }
 9073:         }
 9074:     }
 9075:     if (@okips > 0) {
 9076:         $new = join(',',@okips);
 9077:     } else {
 9078:         $new = '';
 9079:     }
 9080:     return $new;
 9081: }
 9082: 
 9083: sub color_font_choices {
 9084:     my %choices =
 9085:         &Apache::lonlocal::texthash (
 9086:             img => "Header",
 9087:             bgs => "Background colors",
 9088:             links => "Link colors",
 9089:             images => "Images",
 9090:             font => "Font color",
 9091:             fontmenu => "Font menu",
 9092:             pgbg => "Page",
 9093:             tabbg => "Header",
 9094:             sidebg => "Border",
 9095:             link => "Link",
 9096:             alink => "Active link",
 9097:             vlink => "Visited link",
 9098:         );
 9099:     return %choices;
 9100: }
 9101: 
 9102: sub modify_rolecolors {
 9103:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 9104:     my ($resulttext,%rolehash);
 9105:     $rolehash{'rolecolors'} = {};
 9106:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 9107:         if ($domconfig{'rolecolors'} eq '') {
 9108:             $domconfig{'rolecolors'} = {};
 9109:         }
 9110:     }
 9111:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 9112:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 9113:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 9114:                                              $dom);
 9115:     if ($putresult eq 'ok') {
 9116:         if (keys(%changes) > 0) {
 9117:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 9118:             if (ref($lastactref) eq 'HASH') {
 9119:                 $lastactref->{'domainconfig'} = 1;
 9120:             }
 9121:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 9122:                                              $rolehash{'rolecolors'});
 9123:         } else {
 9124:             $resulttext = &mt('No changes made to default color schemes');
 9125:         }
 9126:     } else {
 9127:         $resulttext = '<span class="LC_error">'.
 9128: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9129:     }
 9130:     if ($errors) {
 9131:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9132:                        $errors.'</ul>';
 9133:     }
 9134:     return $resulttext;
 9135: }
 9136: 
 9137: sub modify_colors {
 9138:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 9139:     my (%changes,%choices);
 9140:     my @bgs;
 9141:     my @links = ('link','alink','vlink');
 9142:     my @logintext;
 9143:     my @images;
 9144:     my $servadm = $r->dir_config('lonAdmEMail');
 9145:     my $errors;
 9146:     my %defaults;
 9147:     foreach my $role (@{$roles}) {
 9148:         if ($role eq 'login') {
 9149:             %choices = &login_choices();
 9150:             @logintext = ('textcol','bgcol');
 9151:         } else {
 9152:             %choices = &color_font_choices();
 9153:         }
 9154:         if ($role eq 'login') {
 9155:             @images = ('img','logo','domlogo','login');
 9156:             @bgs = ('pgbg','mainbg','sidebg');
 9157:         } else {
 9158:             @images = ('img');
 9159:             @bgs = ('pgbg','tabbg','sidebg');
 9160:         }
 9161:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 9162:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 9163:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 9164:         }
 9165:         if ($role eq 'login') {
 9166:             foreach my $item (@logintext) {
 9167:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9168:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9169:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9170:                 }
 9171:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 9172:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9173:                 }
 9174:             }
 9175:         } else {
 9176:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 9177:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 9178:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 9179:             }
 9180:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 9181:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 9182:             }
 9183:         }
 9184:         foreach my $item (@bgs) {
 9185:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9186:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9187:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9188:             }
 9189:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 9190:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9191:             }
 9192:         }
 9193:         foreach my $item (@links) {
 9194:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 9195:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 9196:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 9197:             }
 9198:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 9199:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 9200:             }
 9201:         }
 9202:         my ($configuserok,$author_ok,$switchserver) = 
 9203:             &config_check($dom,$confname,$servadm);
 9204:         my ($width,$height) = &thumb_dimensions();
 9205:         if (ref($domconfig->{$role}) ne 'HASH') {
 9206:             $domconfig->{$role} = {};
 9207:         }
 9208:         foreach my $img (@images) {
 9209:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 9210:                 if (defined($env{'form.login_showlogo_'.$img})) {
 9211:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 9212:                 } else { 
 9213:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 9214:                 }
 9215:             } 
 9216: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 9217: 		 && !defined($domconfig->{$role}{$img})
 9218: 		 && !$env{'form.'.$role.'_del_'.$img}
 9219: 		 && $env{'form.'.$role.'_import_'.$img}) {
 9220: 		# import the old configured image from the .tab setting
 9221: 		# if they haven't provided a new one 
 9222: 		$domconfig->{$role}{$img} = 
 9223: 		    $env{'form.'.$role.'_import_'.$img};
 9224: 	    }
 9225:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 9226:                 my $error;
 9227:                 if ($configuserok eq 'ok') {
 9228:                     if ($switchserver) {
 9229:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 9230:                     } else {
 9231:                         if ($author_ok eq 'ok') {
 9232:                             my ($result,$logourl) = 
 9233:                                 &publishlogo($r,'upload',$role.'_'.$img,
 9234:                                            $dom,$confname,$img,$width,$height);
 9235:                             if ($result eq 'ok') {
 9236:                                 $confhash->{$role}{$img} = $logourl;
 9237:                                 $changes{$role}{'images'}{$img} = 1;
 9238:                             } else {
 9239:                                 $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);
 9240:                             }
 9241:                         } else {
 9242:                             $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);
 9243:                         }
 9244:                     }
 9245:                 } else {
 9246:                     $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);
 9247:                 }
 9248:                 if ($error) {
 9249:                     &Apache::lonnet::logthis($error);
 9250:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9251:                 }
 9252:             } elsif ($domconfig->{$role}{$img} ne '') {
 9253:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 9254:                     my $error;
 9255:                     if ($configuserok eq 'ok') {
 9256: # is confname an author?
 9257:                         if ($switchserver eq '') {
 9258:                             if ($author_ok eq 'ok') {
 9259:                                 my ($result,$logourl) = 
 9260:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 9261:                                             $dom,$confname,$img,$width,$height);
 9262:                                 if ($result eq 'ok') {
 9263:                                     $confhash->{$role}{$img} = $logourl;
 9264: 				    $changes{$role}{'images'}{$img} = 1;
 9265:                                 }
 9266:                             }
 9267:                         }
 9268:                     }
 9269:                 }
 9270:             }
 9271:         }
 9272:         if (ref($domconfig) eq 'HASH') {
 9273:             if (ref($domconfig->{$role}) eq 'HASH') {
 9274:                 foreach my $img (@images) {
 9275:                     if ($domconfig->{$role}{$img} ne '') {
 9276:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9277:                             $confhash->{$role}{$img} = '';
 9278:                             $changes{$role}{'images'}{$img} = 1;
 9279:                         } else {
 9280:                             if ($confhash->{$role}{$img} eq '') {
 9281:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 9282:                             }
 9283:                         }
 9284:                     } else {
 9285:                         if ($env{'form.'.$role.'_del_'.$img}) {
 9286:                             $confhash->{$role}{$img} = '';
 9287:                             $changes{$role}{'images'}{$img} = 1;
 9288:                         } 
 9289:                     }
 9290:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 9291:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 9292:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 9293:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 9294:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 9295:                             }
 9296:                         } else {
 9297:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9298:                                 $changes{$role}{'showlogo'}{$img} = 1;
 9299:                             }
 9300:                         }
 9301:                     }
 9302:                 }
 9303:                 if ($domconfig->{$role}{'font'} ne '') {
 9304:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 9305:                         $changes{$role}{'font'} = 1;
 9306:                     }
 9307:                 } else {
 9308:                     if ($confhash->{$role}{'font'}) {
 9309:                         $changes{$role}{'font'} = 1;
 9310:                     }
 9311:                 }
 9312:                 if ($role ne 'login') {
 9313:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 9314:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 9315:                             $changes{$role}{'fontmenu'} = 1;
 9316:                         }
 9317:                     } else {
 9318:                         if ($confhash->{$role}{'fontmenu'}) {
 9319:                             $changes{$role}{'fontmenu'} = 1;
 9320:                         }
 9321:                     }
 9322:                 }
 9323:                 foreach my $item (@bgs) {
 9324:                     if ($domconfig->{$role}{$item} ne '') {
 9325:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9326:                             $changes{$role}{'bgs'}{$item} = 1;
 9327:                         } 
 9328:                     } else {
 9329:                         if ($confhash->{$role}{$item}) {
 9330:                             $changes{$role}{'bgs'}{$item} = 1;
 9331:                         }
 9332:                     }
 9333:                 }
 9334:                 foreach my $item (@links) {
 9335:                     if ($domconfig->{$role}{$item} ne '') {
 9336:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9337:                             $changes{$role}{'links'}{$item} = 1;
 9338:                         }
 9339:                     } else {
 9340:                         if ($confhash->{$role}{$item}) {
 9341:                             $changes{$role}{'links'}{$item} = 1;
 9342:                         }
 9343:                     }
 9344:                 }
 9345:                 foreach my $item (@logintext) {
 9346:                     if ($domconfig->{$role}{$item} ne '') {
 9347:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 9348:                             $changes{$role}{'logintext'}{$item} = 1;
 9349:                         }
 9350:                     } else {
 9351:                         if ($confhash->{$role}{$item}) {
 9352:                             $changes{$role}{'logintext'}{$item} = 1;
 9353:                         }
 9354:                     }
 9355:                 }
 9356:             } else {
 9357:                 &default_change_checker($role,\@images,\@links,\@bgs,
 9358:                                         \@logintext,$confhash,\%changes); 
 9359:             }
 9360:         } else {
 9361:             &default_change_checker($role,\@images,\@links,\@bgs,
 9362:                                     \@logintext,$confhash,\%changes); 
 9363:         }
 9364:     }
 9365:     return ($errors,%changes);
 9366: }
 9367: 
 9368: sub config_check {
 9369:     my ($dom,$confname,$servadm) = @_;
 9370:     my ($configuserok,$author_ok,$switchserver,%currroles);
 9371:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 9372:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 9373:                                                    $confname,$servadm);
 9374:     if ($configuserok eq 'ok') {
 9375:         $switchserver = &check_switchserver($dom,$confname);
 9376:         if ($switchserver eq '') {
 9377:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 9378:         }
 9379:     }
 9380:     return ($configuserok,$author_ok,$switchserver);
 9381: }
 9382: 
 9383: sub default_change_checker {
 9384:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 9385:     foreach my $item (@{$links}) {
 9386:         if ($confhash->{$role}{$item}) {
 9387:             $changes->{$role}{'links'}{$item} = 1;
 9388:         }
 9389:     }
 9390:     foreach my $item (@{$bgs}) {
 9391:         if ($confhash->{$role}{$item}) {
 9392:             $changes->{$role}{'bgs'}{$item} = 1;
 9393:         }
 9394:     }
 9395:     foreach my $item (@{$logintext}) {
 9396:         if ($confhash->{$role}{$item}) {
 9397:             $changes->{$role}{'logintext'}{$item} = 1;
 9398:         }
 9399:     }
 9400:     foreach my $img (@{$images}) {
 9401:         if ($env{'form.'.$role.'_del_'.$img}) {
 9402:             $confhash->{$role}{$img} = '';
 9403:             $changes->{$role}{'images'}{$img} = 1;
 9404:         }
 9405:         if ($role eq 'login') {
 9406:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 9407:                 $changes->{$role}{'showlogo'}{$img} = 1;
 9408:             }
 9409:         }
 9410:     }
 9411:     if ($confhash->{$role}{'font'}) {
 9412:         $changes->{$role}{'font'} = 1;
 9413:     }
 9414: }
 9415: 
 9416: sub display_colorchgs {
 9417:     my ($dom,$changes,$roles,$confhash) = @_;
 9418:     my (%choices,$resulttext);
 9419:     if (!grep(/^login$/,@{$roles})) {
 9420:         $resulttext = &mt('Changes made:').'<br />';
 9421:     }
 9422:     foreach my $role (@{$roles}) {
 9423:         if ($role eq 'login') {
 9424:             %choices = &login_choices();
 9425:         } else {
 9426:             %choices = &color_font_choices();
 9427:         }
 9428:         if (ref($changes->{$role}) eq 'HASH') {
 9429:             if ($role ne 'login') {
 9430:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 9431:             }
 9432:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 9433:                 if ($role ne 'login') {
 9434:                     $resulttext .= '<ul>';
 9435:                 }
 9436:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 9437:                     if ($role ne 'login') {
 9438:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 9439:                     }
 9440:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 9441:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 9442:                             if ($confhash->{$role}{$key}{$item}) {
 9443:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 9444:                             } else {
 9445:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 9446:                             }
 9447:                         } elsif ($confhash->{$role}{$item} eq '') {
 9448:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 9449:                         } else {
 9450:                             my $newitem = $confhash->{$role}{$item};
 9451:                             if ($key eq 'images') {
 9452:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 9453:                             }
 9454:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 9455:                         }
 9456:                     }
 9457:                     if ($role ne 'login') {
 9458:                         $resulttext .= '</ul></li>';
 9459:                     }
 9460:                 } else {
 9461:                     if ($confhash->{$role}{$key} eq '') {
 9462:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 9463:                     } else {
 9464:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 9465:                     }
 9466:                 }
 9467:                 if ($role ne 'login') {
 9468:                     $resulttext .= '</ul>';
 9469:                 }
 9470:             }
 9471:         }
 9472:     }
 9473:     return $resulttext;
 9474: }
 9475: 
 9476: sub thumb_dimensions {
 9477:     return ('200','50');
 9478: }
 9479: 
 9480: sub check_dimensions {
 9481:     my ($inputfile) = @_;
 9482:     my ($fullwidth,$fullheight);
 9483:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 9484:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 9485:             my $imageinfo = <PIPE>;
 9486:             if (!close(PIPE)) {
 9487:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 9488:             }
 9489:             chomp($imageinfo);
 9490:             my ($fullsize) = 
 9491:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 9492:             if ($fullsize) {
 9493:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 9494:             }
 9495:         }
 9496:     }
 9497:     return ($fullwidth,$fullheight);
 9498: }
 9499: 
 9500: sub check_configuser {
 9501:     my ($uhome,$dom,$confname,$servadm) = @_;
 9502:     my ($configuserok,%currroles);
 9503:     if ($uhome eq 'no_host') {
 9504:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 9505:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
 9506:         $configuserok = 
 9507:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 9508:                              $configpass,'','','','','',undef,$servadm);
 9509:     } else {
 9510:         $configuserok = 'ok';
 9511:         %currroles = 
 9512:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 9513:     }
 9514:     return ($configuserok,%currroles);
 9515: }
 9516: 
 9517: sub check_authorstatus {
 9518:     my ($dom,$confname,%currroles) = @_;
 9519:     my $author_ok;
 9520:     if (!$currroles{':'.$dom.':au'}) {
 9521:         my $start = time;
 9522:         my $end = 0;
 9523:         $author_ok = 
 9524:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 9525:                                         'au',$end,$start,'','','domconfig');
 9526:     } else {
 9527:         $author_ok = 'ok';
 9528:     }
 9529:     return $author_ok;
 9530: }
 9531: 
 9532: sub publishlogo {
 9533:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 9534:     my ($output,$fname,$logourl);
 9535:     if ($action eq 'upload') {
 9536:         $fname=$env{'form.'.$formname.'.filename'};
 9537:         chop($env{'form.'.$formname});
 9538:     } else {
 9539:         ($fname) = ($formname =~ /([^\/]+)$/);
 9540:     }
 9541:     if ($savefileas ne '') {
 9542:         $fname = $savefileas;
 9543:     }
 9544:     $fname=&Apache::lonnet::clean_filename($fname);
 9545: # See if there is anything left
 9546:     unless ($fname) { return ('error: no uploaded file'); }
 9547:     $fname="$subdir/$fname";
 9548:     my $docroot=$r->dir_config('lonDocRoot');
 9549:     my $filepath="$docroot/priv";
 9550:     my $relpath = "$dom/$confname";
 9551:     my ($fnamepath,$file,$fetchthumb);
 9552:     $file=$fname;
 9553:     if ($fname=~m|/|) {
 9554:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 9555:     }
 9556:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 9557:     my $count;
 9558:     for ($count=5;$count<=$#parts;$count++) {
 9559:         $filepath.="/$parts[$count]";
 9560:         if ((-e $filepath)!=1) {
 9561:             mkdir($filepath,02770);
 9562:         }
 9563:     }
 9564:     # Check for bad extension and disallow upload
 9565:     if ($file=~/\.(\w+)$/ &&
 9566:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 9567:         $output = 
 9568:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 9569:     } elsif ($file=~/\.(\w+)$/ &&
 9570:         !defined(&Apache::loncommon::fileembstyle($1))) {
 9571:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 9572:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 9573:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 9574:     } elsif (-d "$filepath/$file") {
 9575:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 9576:     } else {
 9577:         my $source = $filepath.'/'.$file;
 9578:         my $logfile;
 9579:         if (!open($logfile,">>",$source.'.log')) {
 9580:             return (&mt('No write permission to Authoring Space'));
 9581:         }
 9582:         print $logfile
 9583: "\n================= Publish ".localtime()." ================\n".
 9584: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 9585: # Save the file
 9586:         if (!open(FH,">",$source)) {
 9587:             &Apache::lonnet::logthis('Failed to create '.$source);
 9588:             return (&mt('Failed to create file'));
 9589:         }
 9590:         if ($action eq 'upload') {
 9591:             if (!print FH ($env{'form.'.$formname})) {
 9592:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 9593:                 return (&mt('Failed to write file'));
 9594:             }
 9595:         } else {
 9596:             my $original = &Apache::lonnet::filelocation('',$formname);
 9597:             if(!copy($original,$source)) {
 9598:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 9599:                 return (&mt('Failed to write file'));
 9600:             }
 9601:         }
 9602:         close(FH);
 9603:         chmod(0660, $source); # Permissions to rw-rw---.
 9604: 
 9605:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 9606:         my $copyfile=$targetdir.'/'.$file;
 9607: 
 9608:         my @parts=split(/\//,$targetdir);
 9609:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 9610:         for (my $count=5;$count<=$#parts;$count++) {
 9611:             $path.="/$parts[$count]";
 9612:             if (!-e $path) {
 9613:                 print $logfile "\nCreating directory ".$path;
 9614:                 mkdir($path,02770);
 9615:             }
 9616:         }
 9617:         my $versionresult;
 9618:         if (-e $copyfile) {
 9619:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 9620:         } else {
 9621:             $versionresult = 'ok';
 9622:         }
 9623:         if ($versionresult eq 'ok') {
 9624:             if (copy($source,$copyfile)) {
 9625:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 9626:                 $output = 'ok';
 9627:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 9628:                 push(@{$modified_urls},[$copyfile,$source]);
 9629:                 my $metaoutput = 
 9630:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 9631:                 unless ($registered_cleanup) {
 9632:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9633:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9634:                     $registered_cleanup=1;
 9635:                 }
 9636:             } else {
 9637:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 9638:                 $output = &mt('Failed to copy file to RES space').", $!";
 9639:             }
 9640:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 9641:                 my $inputfile = $filepath.'/'.$file;
 9642:                 my $outfile = $filepath.'/'.'tn-'.$file;
 9643:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 9644:                 if ($fullwidth ne '' && $fullheight ne '') { 
 9645:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 9646:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 9647:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 9648:                         system({$args[0]} @args);
 9649:                         chmod(0660, $filepath.'/tn-'.$file);
 9650:                         if (-e $outfile) {
 9651:                             my $copyfile=$targetdir.'/tn-'.$file;
 9652:                             if (copy($outfile,$copyfile)) {
 9653:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 9654:                                 my $thumb_metaoutput = 
 9655:                                     &write_metadata($dom,$confname,$formname,
 9656:                                                     $targetdir,'tn-'.$file,$logfile);
 9657:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 9658:                                 unless ($registered_cleanup) {
 9659:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9660:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9661:                                     $registered_cleanup=1;
 9662:                                 }
 9663:                             } else {
 9664:                                 print $logfile "\nUnable to write ".$copyfile.
 9665:                                                ':'.$!."\n";
 9666:                             }
 9667:                         }
 9668:                     }
 9669:                 }
 9670:             }
 9671:         } else {
 9672:             $output = $versionresult;
 9673:         }
 9674:     }
 9675:     return ($output,$logourl);
 9676: }
 9677: 
 9678: sub logo_versioning {
 9679:     my ($targetdir,$file,$logfile) = @_;
 9680:     my $target = $targetdir.'/'.$file;
 9681:     my ($maxversion,$fn,$extn,$output);
 9682:     $maxversion = 0;
 9683:     if ($file =~ /^(.+)\.(\w+)$/) {
 9684:         $fn=$1;
 9685:         $extn=$2;
 9686:     }
 9687:     opendir(DIR,$targetdir);
 9688:     while (my $filename=readdir(DIR)) {
 9689:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 9690:             $maxversion=($1>$maxversion)?$1:$maxversion;
 9691:         }
 9692:     }
 9693:     $maxversion++;
 9694:     print $logfile "\nCreating old version ".$maxversion."\n";
 9695:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 9696:     if (copy($target,$copyfile)) {
 9697:         print $logfile "Copied old target to ".$copyfile."\n";
 9698:         $copyfile=$copyfile.'.meta';
 9699:         if (copy($target.'.meta',$copyfile)) {
 9700:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 9701:             $output = 'ok';
 9702:         } else {
 9703:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 9704:             $output = &mt('Failed to copy old meta').", $!, ";
 9705:         }
 9706:     } else {
 9707:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 9708:         $output = &mt('Failed to copy old target').", $!, ";
 9709:     }
 9710:     return $output;
 9711: }
 9712: 
 9713: sub write_metadata {
 9714:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 9715:     my (%metadatafields,%metadatakeys,$output);
 9716:     $metadatafields{'title'}=$formname;
 9717:     $metadatafields{'creationdate'}=time;
 9718:     $metadatafields{'lastrevisiondate'}=time;
 9719:     $metadatafields{'copyright'}='public';
 9720:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 9721:                                          $env{'user.domain'};
 9722:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 9723:     $metadatafields{'domain'}=$dom;
 9724:     {
 9725:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 9726:         my $mfh;
 9727:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 9728:             foreach (sort(keys(%metadatafields))) {
 9729:                 unless ($_=~/\./) {
 9730:                     my $unikey=$_;
 9731:                     $unikey=~/^([A-Za-z]+)/;
 9732:                     my $tag=$1;
 9733:                     $tag=~tr/A-Z/a-z/;
 9734:                     print $mfh "\n\<$tag";
 9735:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 9736:                         my $value=$metadatafields{$unikey.'.'.$_};
 9737:                         $value=~s/\"/\'\'/g;
 9738:                         print $mfh ' '.$_.'="'.$value.'"';
 9739:                     }
 9740:                     print $mfh '>'.
 9741:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 9742:                             .'</'.$tag.'>';
 9743:                 }
 9744:             }
 9745:             $output = 'ok';
 9746:             print $logfile "\nWrote metadata";
 9747:             close($mfh);
 9748:         } else {
 9749:             print $logfile "\nFailed to open metadata file";
 9750:             $output = &mt('Could not write metadata');
 9751:         }
 9752:     }
 9753:     return $output;
 9754: }
 9755: 
 9756: sub notifysubscribed {
 9757:     foreach my $targetsource (@{$modified_urls}){
 9758:         next unless (ref($targetsource) eq 'ARRAY');
 9759:         my ($target,$source)=@{$targetsource};
 9760:         if ($source ne '') {
 9761:             if (open(my $logfh,">>",$source.'.log')) {
 9762:                 print $logfh "\nCleanup phase: Notifications\n";
 9763:                 my @subscribed=&subscribed_hosts($target);
 9764:                 foreach my $subhost (@subscribed) {
 9765:                     print $logfh "\nNotifying host ".$subhost.':';
 9766:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 9767:                     print $logfh $reply;
 9768:                 }
 9769:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 9770:                 foreach my $subhost (@subscribedmeta) {
 9771:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 9772:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 9773:                                                         $subhost);
 9774:                     print $logfh $reply;
 9775:                 }
 9776:                 print $logfh "\n============ Done ============\n";
 9777:                 close($logfh);
 9778:             }
 9779:         }
 9780:     }
 9781:     return OK;
 9782: }
 9783: 
 9784: sub subscribed_hosts {
 9785:     my ($target) = @_;
 9786:     my @subscribed;
 9787:     if (open(my $fh,"<","$target.subscription")) {
 9788:         while (my $subline=<$fh>) {
 9789:             if ($subline =~ /^($match_lonid):/) {
 9790:                 my $host = $1;
 9791:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 9792:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 9793:                         push(@subscribed,$host);
 9794:                     }
 9795:                 }
 9796:             }
 9797:         }
 9798:     }
 9799:     return @subscribed;
 9800: }
 9801: 
 9802: sub check_switchserver {
 9803:     my ($dom,$confname) = @_;
 9804:     my ($allowed,$switchserver);
 9805:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 9806:     if ($home eq 'no_host') {
 9807:         $home = &Apache::lonnet::domain($dom,'primary');
 9808:     }
 9809:     my @ids=&Apache::lonnet::current_machine_ids();
 9810:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 9811:     if (!$allowed) {
 9812: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 9813:     }
 9814:     return $switchserver;
 9815: }
 9816: 
 9817: sub modify_quotas {
 9818:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9819:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 9820:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 9821:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 9822:         $validationfieldsref);
 9823:     if ($action eq 'quotas') {
 9824:         $context = 'tools'; 
 9825:     } else {
 9826:         $context = $action;
 9827:     }
 9828:     if ($context eq 'requestcourses') {
 9829:         @usertools = ('official','unofficial','community','textbook');
 9830:         @options =('norequest','approval','validate','autolimit');
 9831:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 9832:         %titles = &courserequest_titles();
 9833:         $toolregexp = join('|',@usertools);
 9834:         %conditions = &courserequest_conditions();
 9835:         $confname = $dom.'-domainconfig';
 9836:         my $servadm = $r->dir_config('lonAdmEMail');
 9837:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9838:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 9839:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 9840:     } elsif ($context eq 'requestauthor') {
 9841:         @usertools = ('author');
 9842:         %titles = &authorrequest_titles();
 9843:     } else {
 9844:         @usertools = ('aboutme','blog','webdav','portfolio');
 9845:         %titles = &tool_titles();
 9846:     }
 9847:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9848:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9849:     foreach my $key (keys(%env)) {
 9850:         if ($context eq 'requestcourses') {
 9851:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9852:                 my $item = $1;
 9853:                 my $type = $2;
 9854:                 if ($type =~ /^limit_(.+)/) {
 9855:                     $limithash{$item}{$1} = $env{$key};
 9856:                 } else {
 9857:                     $confhash{$item}{$type} = $env{$key};
 9858:                 }
 9859:             }
 9860:         } elsif ($context eq 'requestauthor') {
 9861:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 9862:                 $confhash{$1} = $env{$key};
 9863:             }
 9864:         } else {
 9865:             if ($key =~ /^form\.quota_(.+)$/) {
 9866:                 $confhash{'defaultquota'}{$1} = $env{$key};
 9867:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 9868:                 $confhash{'authorquota'}{$1} = $env{$key};
 9869:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 9870:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 9871:             }
 9872:         }
 9873:     }
 9874:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9875:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 9876:         @approvalnotify = sort(@approvalnotify);
 9877:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 9878:         my @crstypes = ('official','unofficial','community','textbook');
 9879:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 9880:         foreach my $type (@hasuniquecode) {
 9881:             if (grep(/^\Q$type\E$/,@crstypes)) {
 9882:                 $confhash{'uniquecode'}{$type} = 1;
 9883:             }
 9884:         }
 9885:         my (%newbook,%allpos);
 9886:         if ($context eq 'requestcourses') {
 9887:             foreach my $type ('textbooks','templates') {
 9888:                 @{$allpos{$type}} = (); 
 9889:                 my $invalid;
 9890:                 if ($type eq 'textbooks') {
 9891:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 9892:                 } else {
 9893:                     $invalid = &mt('Invalid LON-CAPA course for template');
 9894:                 }
 9895:                 if ($env{'form.'.$type.'_addbook'}) {
 9896:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 9897:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 9898:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 9899:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 9900:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9901:                         } else {
 9902:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 9903:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 9904:                             $position =~ s/\D+//g;
 9905:                             if ($position ne '') {
 9906:                                 $allpos{$type}[$position] = $newbook{$type};
 9907:                             }
 9908:                         }
 9909:                     } else {
 9910:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9911:                     }
 9912:                 }
 9913:             } 
 9914:         }
 9915:         if (ref($domconfig{$action}) eq 'HASH') {
 9916:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 9917:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 9918:                     $changes{'notify'}{'approval'} = 1;
 9919:                 }
 9920:             } else {
 9921:                 if ($confhash{'notify'}{'approval'}) {
 9922:                     $changes{'notify'}{'approval'} = 1;
 9923:                 }
 9924:             }
 9925:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 9926:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9927:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 9928:                         unless ($confhash{'uniquecode'}{$crstype}) {
 9929:                             $changes{'uniquecode'} = 1;
 9930:                         }
 9931:                     }
 9932:                     unless ($changes{'uniquecode'}) {
 9933:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 9934:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 9935:                                 $changes{'uniquecode'} = 1;
 9936:                             }
 9937:                         }
 9938:                     }
 9939:                } else {
 9940:                    $changes{'uniquecode'} = 1;
 9941:                }
 9942:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 9943:                 $changes{'uniquecode'} = 1;
 9944:             }
 9945:             if ($context eq 'requestcourses') {
 9946:                 foreach my $type ('textbooks','templates') {
 9947:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9948:                         my %deletions;
 9949:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 9950:                         if (@todelete) {
 9951:                             map { $deletions{$_} = 1; } @todelete;
 9952:                         }
 9953:                         my %imgdeletions;
 9954:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 9955:                         if (@todeleteimages) {
 9956:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9957:                         }
 9958:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 9959:                         for (my $i=0; $i<=$maxnum; $i++) {
 9960:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 9961:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 9962:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 9963:                                 if ($deletions{$key}) {
 9964:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 9965:                                         #FIXME need to obsolete item in RES space
 9966:                                     }
 9967:                                     next;
 9968:                                 } else {
 9969:                                     my $newpos = $env{'form.'.$itemid};
 9970:                                     $newpos =~ s/\D+//g;
 9971:                                     foreach my $item ('subject','title','publisher','author') {
 9972:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9973:                                                  ($type eq 'templates'));
 9974:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 9975:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 9976:                                             $changes{$type}{$key} = 1;
 9977:                                         }
 9978:                                     }
 9979:                                     $allpos{$type}[$newpos] = $key;
 9980:                                 }
 9981:                                 if ($imgdeletions{$key}) {
 9982:                                     $changes{$type}{$key} = 1;
 9983:                                     #FIXME need to obsolete item in RES space
 9984:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 9985:                                     my ($cdom,$cnum) = split(/_/,$key);
 9986:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9987:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9988:                                     } else {
 9989:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 9990:                                                                                       $cdom,$cnum,$type,$configuserok,
 9991:                                                                                       $switchserver,$author_ok);
 9992:                                         if ($imgurl) {
 9993:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 9994:                                             $changes{$type}{$key} = 1; 
 9995:                                         }
 9996:                                         if ($error) {
 9997:                                             &Apache::lonnet::logthis($error);
 9998:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9999:                                         }
10000:                                     } 
10001:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
10002:                                     $confhash{$type}{$key}{'image'} = 
10003:                                         $domconfig{$action}{$type}{$key}{'image'};
10004:                                 }
10005:                             }
10006:                         }
10007:                     }
10008:                 }
10009:             }
10010:         } else {
10011:             if ($confhash{'notify'}{'approval'}) {
10012:                 $changes{'notify'}{'approval'} = 1;
10013:             }
10014:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
10015:                 $changes{'uniquecode'} = 1;
10016:             }
10017:         }
10018:         if ($context eq 'requestcourses') {
10019:             foreach my $type ('textbooks','templates') {
10020:                 if ($newbook{$type}) {
10021:                     $changes{$type}{$newbook{$type}} = 1;
10022:                     foreach my $item ('subject','title','publisher','author') {
10023:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
10024:                                  ($type eq 'template'));
10025:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
10026:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
10027:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
10028:                         }
10029:                     }
10030:                     if ($type eq 'textbooks') {
10031:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
10032:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
10033:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
10034:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
10035:                             } else {
10036:                                 my ($imageurl,$error) =
10037:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
10038:                                                             $configuserok,$switchserver,$author_ok);
10039:                                 if ($imageurl) {
10040:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
10041:                                 }
10042:                                 if ($error) {
10043:                                     &Apache::lonnet::logthis($error);
10044:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10045:                                 }
10046:                             }
10047:                         }
10048:                     }
10049:                 }
10050:                 if (@{$allpos{$type}} > 0) {
10051:                     my $idx = 0;
10052:                     foreach my $item (@{$allpos{$type}}) {
10053:                         if ($item ne '') {
10054:                             $confhash{$type}{$item}{'order'} = $idx;
10055:                             if (ref($domconfig{$action}) eq 'HASH') {
10056:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
10057:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
10058:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
10059:                                             $changes{$type}{$item} = 1;
10060:                                         }
10061:                                     }
10062:                                 }
10063:                             }
10064:                             $idx ++;
10065:                         }
10066:                     }
10067:                 }
10068:             }
10069:             if (ref($validationitemsref) eq 'ARRAY') {
10070:                 foreach my $item (@{$validationitemsref}) {
10071:                     if ($item eq 'fields') {
10072:                         my @changed;
10073:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
10074:                         if (@{$confhash{'validation'}{$item}} > 0) {
10075:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
10076:                         }
10077:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10078:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10079:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
10080:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
10081:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
10082:                                 } else {
10083:                                     @changed = @{$confhash{'validation'}{$item}};
10084:                                 }
10085:                             } else {
10086:                                 @changed = @{$confhash{'validation'}{$item}};
10087:                             }
10088:                         } else {
10089:                             @changed = @{$confhash{'validation'}{$item}};
10090:                         }
10091:                         if (@changed) {
10092:                             if ($confhash{'validation'}{$item}) {
10093:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
10094:                             } else {
10095:                                 $changes{'validation'}{$item} = &mt('None');
10096:                             }
10097:                         }
10098:                     } else {
10099:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
10100:                         if ($item eq 'markup') {
10101:                             if ($env{'form.requestcourses_validation_'.$item}) {
10102:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10103:                             }
10104:                         }
10105:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10106:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10107:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
10108:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10109:                                 }
10110:                             } else {
10111:                                 if ($confhash{'validation'}{$item} ne '') {
10112:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10113:                                 }
10114:                             }
10115:                         } else {
10116:                             if ($confhash{'validation'}{$item} ne '') {
10117:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
10118:                             }
10119:                         }
10120:                     }
10121:                 }
10122:             }
10123:             if ($env{'form.validationdc'}) {
10124:                 my $newval = $env{'form.validationdc'};
10125:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10126:                 if (exists($domcoords{$newval})) {
10127:                     $confhash{'validation'}{'dc'} = $newval;
10128:                 }
10129:             }
10130:             if (ref($confhash{'validation'}) eq 'HASH') {
10131:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10132:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10133:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10134:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
10135:                                 if ($confhash{'validation'}{'dc'} eq '') {
10136:                                     $changes{'validation'}{'dc'} = &mt('None');
10137:                                 } else {
10138:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10139:                                 }
10140:                             }
10141:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
10142:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10143:                         }
10144:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
10145:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10146:                     }
10147:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
10148:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
10149:                 }
10150:             } else {
10151:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
10152:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
10153:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
10154:                             $changes{'validation'}{'dc'} = &mt('None');
10155:                         }
10156:                     }
10157:                 }
10158:             }
10159:         }
10160:     } else {
10161:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
10162:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
10163:     }
10164:     foreach my $item (@usertools) {
10165:         foreach my $type (@{$types},'default','_LC_adv') {
10166:             my $unset; 
10167:             if ($context eq 'requestcourses') {
10168:                 $unset = '0';
10169:                 if ($type eq '_LC_adv') {
10170:                     $unset = '';
10171:                 }
10172:                 if ($confhash{$item}{$type} eq 'autolimit') {
10173:                     $confhash{$item}{$type} .= '=';
10174:                     unless ($limithash{$item}{$type} =~ /\D/) {
10175:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
10176:                     }
10177:                 }
10178:             } elsif ($context eq 'requestauthor') {
10179:                 $unset = '0';
10180:                 if ($type eq '_LC_adv') {
10181:                     $unset = '';
10182:                 }
10183:             } else {
10184:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
10185:                     $confhash{$item}{$type} = 1;
10186:                 } else {
10187:                     $confhash{$item}{$type} = 0;
10188:                 }
10189:             }
10190:             if (ref($domconfig{$action}) eq 'HASH') {
10191:                 if ($action eq 'requestauthor') {
10192:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
10193:                         $changes{$type} = 1;
10194:                     }
10195:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
10196:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
10197:                         $changes{$item}{$type} = 1;
10198:                     }
10199:                 } else {
10200:                     if ($context eq 'requestcourses') {
10201:                         if ($confhash{$item}{$type} ne $unset) {
10202:                             $changes{$item}{$type} = 1;
10203:                         }
10204:                     } else {
10205:                         if (!$confhash{$item}{$type}) {
10206:                             $changes{$item}{$type} = 1;
10207:                         }
10208:                     }
10209:                 }
10210:             } else {
10211:                 if ($context eq 'requestcourses') {
10212:                     if ($confhash{$item}{$type} ne $unset) {
10213:                         $changes{$item}{$type} = 1;
10214:                     }
10215:                 } elsif ($context eq 'requestauthor') {
10216:                     if ($confhash{$type} ne $unset) {
10217:                         $changes{$type} = 1;
10218:                     }
10219:                 } else {
10220:                     if (!$confhash{$item}{$type}) {
10221:                         $changes{$item}{$type} = 1;
10222:                     }
10223:                 }
10224:             }
10225:         }
10226:     }
10227:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
10228:         if (ref($domconfig{'quotas'}) eq 'HASH') {
10229:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10230:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
10231:                     if (exists($confhash{'defaultquota'}{$key})) {
10232:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
10233:                             $changes{'defaultquota'}{$key} = 1;
10234:                         }
10235:                     } else {
10236:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
10237:                     }
10238:                 }
10239:             } else {
10240:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
10241:                     if (exists($confhash{'defaultquota'}{$key})) {
10242:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
10243:                             $changes{'defaultquota'}{$key} = 1;
10244:                         }
10245:                     } else {
10246:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
10247:                     }
10248:                 }
10249:             }
10250:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10251:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
10252:                     if (exists($confhash{'authorquota'}{$key})) {
10253:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
10254:                             $changes{'authorquota'}{$key} = 1;
10255:                         }
10256:                     } else {
10257:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
10258:                     }
10259:                 }
10260:             }
10261:         }
10262:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
10263:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
10264:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10265:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
10266:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
10267:                             $changes{'defaultquota'}{$key} = 1;
10268:                         }
10269:                     } else {
10270:                         if (!exists($domconfig{'quotas'}{$key})) {
10271:                             $changes{'defaultquota'}{$key} = 1;
10272:                         }
10273:                     }
10274:                 } else {
10275:                     $changes{'defaultquota'}{$key} = 1;
10276:                 }
10277:             }
10278:         }
10279:         if (ref($confhash{'authorquota'}) eq 'HASH') {
10280:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
10281:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
10282:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
10283:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
10284:                             $changes{'authorquota'}{$key} = 1;
10285:                         }
10286:                     } else {
10287:                         $changes{'authorquota'}{$key} = 1;
10288:                     }
10289:                 } else {
10290:                     $changes{'authorquota'}{$key} = 1;
10291:                 }
10292:             }
10293:         }
10294:     }
10295: 
10296:     if ($context eq 'requestauthor') {
10297:         $domdefaults{'requestauthor'} = \%confhash;
10298:     } else {
10299:         foreach my $key (keys(%confhash)) {
10300:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
10301:                 $domdefaults{$key} = $confhash{$key};
10302:             }
10303:         }
10304:     }
10305: 
10306:     my %quotahash = (
10307:                       $action => { %confhash }
10308:                     );
10309:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
10310:                                              $dom);
10311:     if ($putresult eq 'ok') {
10312:         if (keys(%changes) > 0) {
10313:             my $cachetime = 24*60*60;
10314:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10315:             if (ref($lastactref) eq 'HASH') {
10316:                 $lastactref->{'domdefaults'} = 1;
10317:             }
10318:             $resulttext = &mt('Changes made:').'<ul>';
10319:             unless (($context eq 'requestcourses') ||
10320:                     ($context eq 'requestauthor')) {
10321:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
10322:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
10323:                     foreach my $type (@{$types},'default') {
10324:                         if (defined($changes{'defaultquota'}{$type})) {
10325:                             my $typetitle = $usertypes->{$type};
10326:                             if ($type eq 'default') {
10327:                                 $typetitle = $othertitle;
10328:                             }
10329:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
10330:                         }
10331:                     }
10332:                     $resulttext .= '</ul></li>';
10333:                 }
10334:                 if (ref($changes{'authorquota'}) eq 'HASH') {
10335:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
10336:                     foreach my $type (@{$types},'default') {
10337:                         if (defined($changes{'authorquota'}{$type})) {
10338:                             my $typetitle = $usertypes->{$type};
10339:                             if ($type eq 'default') {
10340:                                 $typetitle = $othertitle;
10341:                             }
10342:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
10343:                         }
10344:                     }
10345:                     $resulttext .= '</ul></li>';
10346:                 }
10347:             }
10348:             my %newenv;
10349:             foreach my $item (@usertools) {
10350:                 my (%haschgs,%inconf);
10351:                 if ($context eq 'requestauthor') {
10352:                     %haschgs = %changes;
10353:                     %inconf = %confhash;
10354:                 } else {
10355:                     if (ref($changes{$item}) eq 'HASH') {
10356:                         %haschgs = %{$changes{$item}};
10357:                     }
10358:                     if (ref($confhash{$item}) eq 'HASH') {
10359:                         %inconf = %{$confhash{$item}};
10360:                     }
10361:                 }
10362:                 if (keys(%haschgs) > 0) {
10363:                     my $newacc = 
10364:                         &Apache::lonnet::usertools_access($env{'user.name'},
10365:                                                           $env{'user.domain'},
10366:                                                           $item,'reload',$context);
10367:                     if (($context eq 'requestcourses') ||
10368:                         ($context eq 'requestauthor')) {
10369:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
10370:                             $newenv{'environment.canrequest.'.$item} = $newacc;
10371:                         }
10372:                     } else {
10373:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
10374:                             $newenv{'environment.availabletools.'.$item} = $newacc;
10375:                         }
10376:                     }
10377:                     unless ($context eq 'requestauthor') {
10378:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
10379:                     }
10380:                     foreach my $type (@{$types},'default','_LC_adv') {
10381:                         if ($haschgs{$type}) {
10382:                             my $typetitle = $usertypes->{$type};
10383:                             if ($type eq 'default') {
10384:                                 $typetitle = $othertitle;
10385:                             } elsif ($type eq '_LC_adv') {
10386:                                 $typetitle = 'LON-CAPA Advanced Users'; 
10387:                             }
10388:                             if ($inconf{$type}) {
10389:                                 if ($context eq 'requestcourses') {
10390:                                     my $cond;
10391:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
10392:                                         if ($1 eq '') {
10393:                                             $cond = &mt('(Automatic processing of any request).');
10394:                                         } else {
10395:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
10396:                                         }
10397:                                     } else { 
10398:                                         $cond = $conditions{$inconf{$type}};
10399:                                     }
10400:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
10401:                                 } elsif ($context eq 'requestauthor') {
10402:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
10403:                                                              $titles{$inconf{$type}},$typetitle);
10404: 
10405:                                 } else {
10406:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
10407:                                 }
10408:                             } else {
10409:                                 if ($type eq '_LC_adv') {
10410:                                     if ($inconf{$type} eq '0') {
10411:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10412:                                     } else { 
10413:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
10414:                                     }
10415:                                 } else {
10416:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
10417:                                 }
10418:                             }
10419:                         }
10420:                     }
10421:                     unless ($context eq 'requestauthor') {
10422:                         $resulttext .= '</ul></li>';
10423:                     }
10424:                 }
10425:             }
10426:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
10427:                 if (ref($changes{'notify'}) eq 'HASH') {
10428:                     if ($changes{'notify'}{'approval'}) {
10429:                         if (ref($confhash{'notify'}) eq 'HASH') {
10430:                             if ($confhash{'notify'}{'approval'}) {
10431:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
10432:                             } else {
10433:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
10434:                             }
10435:                         }
10436:                     }
10437:                 }
10438:             }
10439:             if ($action eq 'requestcourses') {
10440:                 my @offon = ('off','on');
10441:                 if ($changes{'uniquecode'}) {
10442:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
10443:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
10444:                         $resulttext .= '<li>'.
10445:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
10446:                                        '</li>';
10447:                     } else {
10448:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
10449:                                        '</li>';
10450:                     }
10451:                 }
10452:                 foreach my $type ('textbooks','templates') {
10453:                     if (ref($changes{$type}) eq 'HASH') {
10454:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
10455:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
10456:                             my %coursehash = &Apache::lonnet::coursedescription($key);
10457:                             my $coursetitle = $coursehash{'description'};
10458:                             my $position = $confhash{$type}{$key}{'order'} + 1;
10459:                             $resulttext .= '<li>';
10460:                             foreach my $item ('subject','title','publisher','author') {
10461:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
10462:                                          ($type eq 'templates'));
10463:                                 my $name = $item.':';
10464:                                 $name =~ s/^(\w)/\U$1/;
10465:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
10466:                             }
10467:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
10468:                             if ($type eq 'textbooks') {
10469:                                 if ($confhash{$type}{$key}{'image'}) {
10470:                                     $resulttext .= ' '.&mt('Image: [_1]',
10471:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
10472:                                                    ' alt="Textbook cover" />').'<br />';
10473:                                 }
10474:                             }
10475:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
10476:                         }
10477:                         $resulttext .= '</ul></li>';
10478:                     }
10479:                 }
10480:                 if (ref($changes{'validation'}) eq 'HASH') {
10481:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
10482:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
10483:                         foreach my $item (@{$validationitemsref}) {
10484:                             if (exists($changes{'validation'}{$item})) {
10485:                                 if ($item eq 'markup') {
10486:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
10487:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
10488:                                 } else {
10489:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
10490:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
10491:                                 }
10492:                             }
10493:                         }
10494:                         if (exists($changes{'validation'}{'dc'})) {
10495:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
10496:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
10497:                         }
10498:                     }
10499:                 }
10500:             }
10501:             $resulttext .= '</ul>';
10502:             if (keys(%newenv)) {
10503:                 &Apache::lonnet::appenv(\%newenv);
10504:             }
10505:         } else {
10506:             if ($context eq 'requestcourses') {
10507:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
10508:             } elsif ($context eq 'requestauthor') {
10509:                 $resulttext = &mt('No changes made to rights to request author space.');
10510:             } else {
10511:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
10512:             }
10513:         }
10514:     } else {
10515:         $resulttext = '<span class="LC_error">'.
10516: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10517:     }
10518:     if ($errors) {
10519:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
10520:                        '<ul>'.$errors.'</ul></p>';
10521:     }
10522:     return $resulttext;
10523: }
10524: 
10525: sub process_textbook_image {
10526:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
10527:     my $filename = $env{'form.'.$caller.'.filename'};
10528:     my ($error,$url);
10529:     my ($width,$height) = (50,50);
10530:     if ($configuserok eq 'ok') {
10531:         if ($switchserver) {
10532:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
10533:                          $switchserver);
10534:         } elsif ($author_ok eq 'ok') {
10535:             my ($result,$imageurl) =
10536:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10537:                              "$type/$cdom/$cnum/cover",$width,$height);
10538:             if ($result eq 'ok') {
10539:                 $url = $imageurl;
10540:             } else {
10541:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10542:             }
10543:         } else {
10544:             $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);
10545:         }
10546:     } else {
10547:         $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);
10548:     }
10549:     return ($url,$error);
10550: }
10551: 
10552: sub modify_ltitools {
10553:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10554:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10555:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
10556:     my $confname = $dom.'-domainconfig';
10557:     my $servadm = $r->dir_config('lonAdmEMail');
10558:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10559:     my (%posslti,%possfield);
10560:     my @courseroles = ('cc','in','ta','ep','st');
10561:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
10562:     map { $posslti{$_} = 1; } @ltiroles;
10563:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
10564:     map { $possfield{$_} = 1; } @allfields;
10565:     my %lt = &ltitools_names();
10566:     if ($env{'form.ltitools_add'}) {
10567:         my $title = $env{'form.ltitools_add_title'};
10568:         $title =~ s/(`)/'/g;
10569:         ($newid,my $error) = &get_ltitools_id($dom,$title);
10570:         if ($newid) {
10571:             my $position = $env{'form.ltitools_add_pos'};
10572:             $position =~ s/\D+//g;
10573:             if ($position ne '') {
10574:                 $allpos[$position] = $newid;
10575:             }
10576:             $changes{$newid} = 1;
10577:             foreach my $item ('title','url','key','secret','lifetime') {
10578:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
10579:                 if ($item eq 'lifetime') {
10580:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
10581:                 }
10582:                 if ($env{'form.ltitools_add_'.$item}) {
10583:                     if (($item eq 'key') || ($item eq 'secret')) {
10584:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10585:                     } else {
10586:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10587:                     }
10588:                 }
10589:             }
10590:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
10591:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
10592:             }
10593:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
10594:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
10595:             }
10596:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
10597:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
10598:             } else {
10599:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
10600:             }
10601:             foreach my $item ('width','height','linktext','explanation') {
10602:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
10603:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
10604:                 if (($item eq 'width') || ($item eq 'height')) {
10605:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
10606:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
10607:                     }
10608:                 } else {
10609:                     if ($env{'form.ltitools_add_'.$item} ne '') {
10610:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
10611:                     }
10612:                 }
10613:             }
10614:             if ($env{'form.ltitools_add_target'} eq 'window') {
10615:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10616:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
10617:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10618:             } else {
10619:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
10620:             }
10621:             if ($env{'form.ltitools_add_image.filename'} ne '') {
10622:                 my ($imageurl,$error) =
10623:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
10624:                                             $configuserok,$switchserver,$author_ok);
10625:                 if ($imageurl) {
10626:                     $confhash{$newid}{'image'} = $imageurl;
10627:                 }
10628:                 if ($error) {
10629:                     &Apache::lonnet::logthis($error);
10630:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10631:                 }
10632:             }
10633:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
10634:             foreach my $field (@fields) {
10635:                 if ($possfield{$field}) {
10636:                     if ($field eq 'roles') {
10637:                         foreach my $role (@courseroles) {
10638:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
10639:                             if (($choice ne '') && ($posslti{$choice})) {
10640:                                 $confhash{$newid}{'roles'}{$role} = $choice;
10641:                                 if ($role eq 'cc') {
10642:                                     $confhash{$newid}{'roles'}{'co'} = $choice;
10643:                                 }
10644:                             }
10645:                         }
10646:                     } else {
10647:                         $confhash{$newid}{'fields'}{$field} = 1;
10648:                     }
10649:                 }
10650:             }
10651:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
10652:                 if ($confhash{$newid}{'fields'}{'user'}) {
10653:                     if ($env{'form.ltitools_userincdom_add'}) {
10654:                         $confhash{$newid}{'incdom'} = 1;
10655:                     }
10656:                 }
10657:             }
10658:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
10659:             foreach my $item (@courseconfig) {
10660:                 $confhash{$newid}{'crsconf'}{$item} = 1;
10661:             }
10662:             if ($env{'form.ltitools_add_custom'}) {
10663:                 my $name = $env{'form.ltitools_add_custom_name'};
10664:                 my $value = $env{'form.ltitools_add_custom_value'};
10665:                 $value =~ s/(`)/'/g;
10666:                 $name =~ s/(`)/'/g;
10667:                 $confhash{$newid}{'custom'}{$name} = $value;
10668:             }
10669:         } else {
10670:             my $error = &mt('Failed to acquire unique ID for new external tool');
10671:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10672:         }
10673:     }
10674:     if (ref($domconfig{$action}) eq 'HASH') {
10675:         my %deletions;
10676:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
10677:         if (@todelete) {
10678:             map { $deletions{$_} = 1; } @todelete;
10679:         }
10680:         my %customadds;
10681:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
10682:         if (@newcustom) {
10683:             map { $customadds{$_} = 1; } @newcustom;
10684:         }
10685:         my %imgdeletions;
10686:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
10687:         if (@todeleteimages) {
10688:             map { $imgdeletions{$_} = 1; } @todeleteimages;
10689:         }
10690:         my $maxnum = $env{'form.ltitools_maxnum'};
10691:         for (my $i=0; $i<=$maxnum; $i++) {
10692:             my $itemid = $env{'form.ltitools_id_'.$i};
10693:             $itemid =~ s/\D+//g;
10694:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10695:                 if ($deletions{$itemid}) {
10696:                     if ($domconfig{$action}{$itemid}{'image'}) {
10697:                         #FIXME need to obsolete item in RES space
10698:                     }
10699:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
10700:                     next;
10701:                 } else {
10702:                     my $newpos = $env{'form.ltitools_'.$itemid};
10703:                     $newpos =~ s/\D+//g;
10704:                     foreach my $item ('title','url','lifetime') {
10705:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10706:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
10707:                             $changes{$itemid} = 1;
10708:                         }
10709:                     }
10710:                     foreach my $item ('key','secret') {
10711:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10712:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
10713:                             $changes{$itemid} = 1;
10714:                         }
10715:                     }
10716:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
10717:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
10718:                     }
10719:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
10720:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
10721:                     }
10722:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
10723:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
10724:                     } else {
10725:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1';
10726:                     }
10727:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
10728:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
10729:                             $changes{$itemid} = 1;
10730:                         }
10731:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
10732:                         $changes{$itemid} = 1;
10733:                     }
10734:                     foreach my $size ('width','height') {
10735:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
10736:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
10737:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
10738:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
10739:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10740:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
10741:                                     $changes{$itemid} = 1;
10742:                                 }
10743:                             } else {
10744:                                 $changes{$itemid} = 1;
10745:                             }
10746:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10747:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
10748:                                 $changes{$itemid} = 1;
10749:                             }
10750:                         }
10751:                     }
10752:                     foreach my $item ('linktext','explanation') {
10753:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
10754:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
10755:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
10756:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10757:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10758:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
10759:                                     $changes{$itemid} = 1;
10760:                                 }
10761:                             } else {
10762:                                 $changes{$itemid} = 1;
10763:                             }
10764:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10765:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
10766:                                 $changes{$itemid} = 1;
10767:                             }
10768:                         }
10769:                     }
10770:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
10771:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10772:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
10773:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10774:                     } else {
10775:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
10776:                     }
10777:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10778:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
10779:                             $changes{$itemid} = 1;
10780:                         }
10781:                     } else {
10782:                         $changes{$itemid} = 1;
10783:                     }
10784:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
10785:                     foreach my $item ('label','title','target','linktext','explanation','append') {
10786:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
10787:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
10788:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
10789:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
10790:                                     $changes{$itemid} = 1;
10791:                                 }
10792:                             } else {
10793:                                 $changes{$itemid} = 1;
10794:                             }
10795:                         }
10796:                     }
10797:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
10798:                     foreach my $field (@fields) {
10799:                         if ($possfield{$field}) {
10800:                             if ($field eq 'roles') {
10801:                                 foreach my $role (@courseroles) {
10802:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
10803:                                     if (($choice ne '') && ($posslti{$choice})) {
10804:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
10805:                                         if ($role eq 'cc') {
10806:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
10807:                                         }
10808:                                     }
10809:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
10810:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
10811:                                             $changes{$itemid} = 1;
10812:                                         }
10813:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
10814:                                         $changes{$itemid} = 1;
10815:                                     }
10816:                                 }
10817:                             } else {
10818:                                 $confhash{$itemid}{'fields'}{$field} = 1;
10819:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
10820:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
10821:                                         $changes{$itemid} = 1;
10822:                                     }
10823:                                 } else {
10824:                                     $changes{$itemid} = 1;
10825:                                 }
10826:                             }
10827:                         }
10828:                     }
10829:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
10830:                         if ($confhash{$itemid}{'fields'}{'user'}) {
10831:                             if ($env{'form.ltitools_userincdom_'.$i}) {
10832:                                 $confhash{$itemid}{'incdom'} = 1;
10833:                             }
10834:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
10835:                                 $changes{$itemid} = 1;
10836:                             }
10837:                         }
10838:                     }
10839:                     $allpos[$newpos] = $itemid;
10840:                 }
10841:                 if ($imgdeletions{$itemid}) {
10842:                     $changes{$itemid} = 1;
10843:                     #FIXME need to obsolete item in RES space
10844:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
10845:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
10846:                                                                  $itemid,$configuserok,$switchserver,
10847:                                                                  $author_ok);
10848:                     if ($imgurl) {
10849:                         $confhash{$itemid}{'image'} = $imgurl;
10850:                         $changes{$itemid} = 1;
10851:                     }
10852:                     if ($error) {
10853:                         &Apache::lonnet::logthis($error);
10854:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10855:                     }
10856:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
10857:                     $confhash{$itemid}{'image'} =
10858:                        $domconfig{$action}{$itemid}{'image'};
10859:                 }
10860:                 if ($customadds{$i}) {
10861:                     my $name = $env{'form.ltitools_custom_name_'.$i};
10862:                     $name =~ s/(`)/'/g;
10863:                     $name =~ s/^\s+//;
10864:                     $name =~ s/\s+$//;
10865:                     my $value = $env{'form.ltitools_custom_value_'.$i};
10866:                     $value =~ s/(`)/'/g;
10867:                     $value =~ s/^\s+//;
10868:                     $value =~ s/\s+$//;
10869:                     if ($name ne '') {
10870:                         $confhash{$itemid}{'custom'}{$name} = $value;
10871:                         $changes{$itemid} = 1;
10872:                     }
10873:                 }
10874:                 my %customdels;
10875:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i);
10876:                 if (@customdeletions) {
10877:                     $changes{$itemid} = 1;
10878:                 }
10879:                 map { $customdels{$_} = 1; } @customdeletions;
10880:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
10881:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
10882:                         unless ($customdels{$key}) {
10883:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
10884:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i};
10885:                             }
10886:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
10887:                                 $changes{$itemid} = 1;
10888:                             }
10889:                         }
10890:                     }
10891:                 }
10892:                 unless ($changes{$itemid}) {
10893:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
10894:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
10895:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
10896:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
10897:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
10898:                                         $changes{$itemid} = 1;
10899:                                         last;
10900:                                     }
10901:                                 }
10902:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
10903:                                 $changes{$itemid} = 1;
10904:                             }
10905:                         }
10906:                         last if ($changes{$itemid});
10907:                     }
10908:                 }
10909:             }
10910:         }
10911:     }
10912:     if (@allpos > 0) {
10913:         my $idx = 0;
10914:         foreach my $itemid (@allpos) {
10915:             if ($itemid ne '') {
10916:                 $confhash{$itemid}{'order'} = $idx;
10917:                 if (ref($domconfig{$action}) eq 'HASH') {
10918:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10919:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
10920:                             $changes{$itemid} = 1;
10921:                         }
10922:                     }
10923:                 }
10924:                 $idx ++;
10925:             }
10926:         }
10927:     }
10928:     my %ltitoolshash = (
10929:                           $action => { %confhash }
10930:                        );
10931:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
10932:                                              $dom);
10933:     if ($putresult eq 'ok') {
10934:         my %ltienchash = (
10935:                              $action => { %encconfig }
10936:                          );
10937:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
10938:         if (keys(%changes) > 0) {
10939:             my $cachetime = 24*60*60;
10940:             my %ltiall = %confhash;
10941:             foreach my $id (keys(%ltiall)) {
10942:                 if (ref($encconfig{$id}) eq 'HASH') {
10943:                     foreach my $item ('key','secret') {
10944:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
10945:                     }
10946:                 }
10947:             }
10948:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
10949:             if (ref($lastactref) eq 'HASH') {
10950:                 $lastactref->{'ltitools'} = 1;
10951:             }
10952:             $resulttext = &mt('Changes made:').'<ul>';
10953:             my %bynum;
10954:             foreach my $itemid (sort(keys(%changes))) {
10955:                 my $position = $confhash{$itemid}{'order'};
10956:                 $bynum{$position} = $itemid;
10957:             }
10958:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
10959:                 my $itemid = $bynum{$pos};
10960:                 if (ref($confhash{$itemid}) ne 'HASH') {
10961:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
10962:                 } else {
10963:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
10964:                     if ($confhash{$itemid}{'image'}) {
10965:                         $resulttext .= '&nbsp;'.
10966:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
10967:                                        ' alt="'.&mt('Tool Provider icon').'" />';
10968:                     }
10969:                     $resulttext .= '</li><ul>';
10970:                     my $position = $pos + 1;
10971:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
10972:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
10973:                         if ($confhash{$itemid}{$item} ne '') {
10974:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
10975:                         }
10976:                     }
10977:                     if ($encconfig{$itemid}{'key'} ne '') {
10978:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
10979:                     }
10980:                     if ($encconfig{$itemid}{'secret'} ne '') {
10981:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
10982:                         my $num = length($encconfig{$itemid}{'secret'});
10983:                         $resulttext .= ('*'x$num).'</li>';
10984:                     }
10985:                     $resulttext .= '<li>'.&mt('Configurable in course:');
10986:                     my @possconfig = ('label','title','target','linktext','explanation','append');
10987:                     my $numconfig = 0;
10988:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') {
10989:                         foreach my $item (@possconfig) {
10990:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
10991:                                 $numconfig ++;
10992:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
10993:                             }
10994:                         }
10995:                     }
10996:                     if (!$numconfig) {
10997:                         $resulttext .= &mt('None');
10998:                     }
10999:                     $resulttext .= '</li>';
11000:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
11001:                         my $displaylist;
11002:                         if ($confhash{$itemid}{'display'}{'target'}) {
11003:                             $displaylist = &mt('Display target').':&nbsp;'.
11004:                                            $confhash{$itemid}{'display'}{'target'}.',';
11005:                         }
11006:                         foreach my $size ('width','height') {
11007:                             if ($confhash{$itemid}{'display'}{$size}) {
11008:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
11009:                                                 $confhash{$itemid}{'display'}{$size}.',';
11010:                             }
11011:                         }
11012:                         if ($displaylist) {
11013:                             $displaylist =~ s/,$//;
11014:                             $resulttext .= '<li>'.$displaylist.'</li>';
11015:                         }
11016:                         foreach my $item ('linktext','explanation') {
11017:                             if ($confhash{$itemid}{'display'}{$item}) {
11018:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
11019:                             }
11020:                         }
11021:                     }
11022:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
11023:                         my $fieldlist;
11024:                         foreach my $field (@allfields) {
11025:                             if ($confhash{$itemid}{'fields'}{$field}) {
11026:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
11027:                             }
11028:                         }
11029:                         if ($fieldlist) {
11030:                             $fieldlist =~ s/,$//;
11031:                             if ($confhash{$itemid}{'fields'}{'user'}) {
11032:                                 if ($confhash{$itemid}{'incdom'}) {
11033:                                     $fieldlist .= ' ('.&mt('username:domain').')';
11034:                                 } else {
11035:                                     $fieldlist .= ' ('.&mt('username').')';
11036:                                 }
11037:                             }
11038:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
11039:                         }
11040:                     }
11041:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
11042:                         my $rolemaps;
11043:                         foreach my $role (@courseroles) {
11044:                             if ($confhash{$itemid}{'roles'}{$role}) {
11045:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
11046:                                              $confhash{$itemid}{'roles'}{$role}.',';
11047:                             }
11048:                         }
11049:                         if ($rolemaps) {
11050:                             $rolemaps =~ s/,$//;
11051:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
11052:                         }
11053:                     }
11054:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
11055:                         my $customlist;
11056:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
11057:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
11058:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
11059:                             }
11060:                         }
11061:                         if ($customlist) {
11062:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
11063:                         }
11064:                     }
11065:                     $resulttext .= '</ul></li>';
11066:                 }
11067:             }
11068:             $resulttext .= '</ul>';
11069:         } else {
11070:             $resulttext = &mt('No changes made.');
11071:         }
11072:     } else {
11073:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
11074:     }
11075:     if ($errors) {
11076:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11077:                        $errors.'</ul>';
11078:     }
11079:     return $resulttext;
11080: }
11081: 
11082: sub process_ltitools_image {
11083:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
11084:     my $filename = $env{'form.'.$caller.'.filename'};
11085:     my ($error,$url);
11086:     my ($width,$height) = (21,21);
11087:     if ($configuserok eq 'ok') {
11088:         if ($switchserver) {
11089:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
11090:                          $switchserver);
11091:         } elsif ($author_ok eq 'ok') {
11092:             my ($result,$imageurl,$madethumb) =
11093:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11094:                              "ltitools/$itemid/icon",$width,$height);
11095:             if ($result eq 'ok') {
11096:                 if ($madethumb) {
11097:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
11098:                     my $imagethumb = "$path/tn-".$imagefile;
11099:                     $url = $imagethumb;
11100:                 } else {
11101:                     $url = $imageurl;
11102:                 }
11103:             } else {
11104:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11105:             }
11106:         } else {
11107:             $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);
11108:         }
11109:     } else {
11110:         $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);
11111:     }
11112:     return ($url,$error);
11113: }
11114: 
11115: sub get_ltitools_id {
11116:     my ($cdom,$title) = @_;
11117:     # get lock on ltitools db
11118:     my $lockhash = {
11119:                       lock => $env{'user.name'}.
11120:                               ':'.$env{'user.domain'},
11121:                    };
11122:     my $tries = 0;
11123:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11124:     my ($id,$error);
11125: 
11126:     while (($gotlock ne 'ok') && ($tries<10)) {
11127:         $tries ++;
11128:         sleep (0.1);
11129:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
11130:     }
11131:     if ($gotlock eq 'ok') {
11132:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
11133:         if ($currids{'lock'}) {
11134:             delete($currids{'lock'});
11135:             if (keys(%currids)) {
11136:                 my @curr = sort { $a <=> $b } keys(%currids);
11137:                 if ($curr[-1] =~ /^\d+$/) {
11138:                     $id = 1 + $curr[-1];
11139:                 }
11140:             } else {
11141:                 $id = 1;
11142:             }
11143:             if ($id) {
11144:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
11145:                     $error = 'nostore';
11146:                 }
11147:             } else {
11148:                 $error = 'nonumber';
11149:             }
11150:         }
11151:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
11152:     } else {
11153:         $error = 'nolock';
11154:     }
11155:     return ($id,$error);
11156: }
11157: 
11158: sub modify_autoenroll {
11159:     my ($dom,$lastactref,%domconfig) = @_;
11160:     my ($resulttext,%changes);
11161:     my %currautoenroll;
11162:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
11163:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
11164:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
11165:         }
11166:     }
11167:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
11168:     my %title = ( run => 'Auto-enrollment active',
11169:                   sender => 'Sender for notification messages',
11170:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
11171:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
11172:     my @offon = ('off','on');
11173:     my $sender_uname = $env{'form.sender_uname'};
11174:     my $sender_domain = $env{'form.sender_domain'};
11175:     if ($sender_domain eq '') {
11176:         $sender_uname = '';
11177:     } elsif ($sender_uname eq '') {
11178:         $sender_domain = '';
11179:     }
11180:     my $coowners = $env{'form.autoassign_coowners'};
11181:     my $failsafe = $env{'form.autoenroll_failsafe'};
11182:     $failsafe =~ s{^\s+|\s+$}{}g;
11183:     if ($failsafe =~ /\D/) {
11184:         undef($failsafe);
11185:     }
11186:     my %autoenrollhash =  (
11187:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
11188:                                        'sender_uname' => $sender_uname,
11189:                                        'sender_domain' => $sender_domain,
11190:                                        'co-owners' => $coowners,
11191:                                        'autofailsafe' => $failsafe,
11192:                                 }
11193:                      );
11194:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
11195:                                              $dom);
11196:     if ($putresult eq 'ok') {
11197:         if (exists($currautoenroll{'run'})) {
11198:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
11199:                  $changes{'run'} = 1;
11200:              }
11201:         } elsif ($autorun) {
11202:             if ($env{'form.autoenroll_run'} ne '1') {
11203:                  $changes{'run'} = 1;
11204:             }
11205:         }
11206:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
11207:             $changes{'sender'} = 1;
11208:         }
11209:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
11210:             $changes{'sender'} = 1;
11211:         }
11212:         if ($currautoenroll{'co-owners'} ne '') {
11213:             if ($currautoenroll{'co-owners'} ne $coowners) {
11214:                 $changes{'coowners'} = 1;
11215:             }
11216:         } elsif ($coowners) {
11217:             $changes{'coowners'} = 1;
11218:         }
11219:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
11220:             $changes{'autofailsafe'} = 1;
11221:         }
11222:         if (keys(%changes) > 0) {
11223:             $resulttext = &mt('Changes made:').'<ul>';
11224:             if ($changes{'run'}) {
11225:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
11226:             }
11227:             if ($changes{'sender'}) {
11228:                 if ($sender_uname eq '' || $sender_domain eq '') {
11229:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
11230:                 } else {
11231:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
11232:                 }
11233:             }
11234:             if ($changes{'coowners'}) {
11235:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
11236:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
11237:                 if (ref($lastactref) eq 'HASH') {
11238:                     $lastactref->{'domainconfig'} = 1;
11239:                 }
11240:             }
11241:             if ($changes{'autofailsafe'}) {
11242:                 if ($failsafe ne '') {
11243:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
11244:                 } else {
11245:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
11246:                 }
11247:                 &Apache::lonnet::get_domain_defaults($dom,1);
11248:                 if (ref($lastactref) eq 'HASH') {
11249:                     $lastactref->{'domdefaults'} = 1;
11250:                 }
11251:             }
11252:             $resulttext .= '</ul>';
11253:         } else {
11254:             $resulttext = &mt('No changes made to auto-enrollment settings');
11255:         }
11256:     } else {
11257:         $resulttext = '<span class="LC_error">'.
11258: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11259:     }
11260:     return $resulttext;
11261: }
11262: 
11263: sub modify_autoupdate {
11264:     my ($dom,%domconfig) = @_;
11265:     my ($resulttext,%currautoupdate,%fields,%changes);
11266:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
11267:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
11268:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
11269:         }
11270:     }
11271:     my @offon = ('off','on');
11272:     my %title = &Apache::lonlocal::texthash (
11273:                    run => 'Auto-update:',
11274:                    classlists => 'Updates to user information in classlists?'
11275:                 );
11276:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11277:     my %fieldtitles = &Apache::lonlocal::texthash (
11278:                         id => 'Student/Employee ID',
11279:                         permanentemail => 'E-mail address',
11280:                         lastname => 'Last Name',
11281:                         firstname => 'First Name',
11282:                         middlename => 'Middle Name',
11283:                         generation => 'Generation',
11284:                       );
11285:     $othertitle = &mt('All users');
11286:     if (keys(%{$usertypes}) >  0) {
11287:         $othertitle = &mt('Other users');
11288:     }
11289:     foreach my $key (keys(%env)) {
11290:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
11291:             my ($usertype,$item) = ($1,$2);
11292:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
11293:                 if ($usertype eq 'default') {   
11294:                     push(@{$fields{$1}},$2);
11295:                 } elsif (ref($types) eq 'ARRAY') {
11296:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
11297:                         push(@{$fields{$1}},$2);
11298:                     }
11299:                 }
11300:             }
11301:         }
11302:     }
11303:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
11304:     @lockablenames = sort(@lockablenames);
11305:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
11306:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11307:         if (@changed) {
11308:             $changes{'lockablenames'} = 1;
11309:         }
11310:     } else {
11311:         if (@lockablenames) {
11312:             $changes{'lockablenames'} = 1;
11313:         }
11314:     }
11315:     my %updatehash = (
11316:                       autoupdate => { run => $env{'form.autoupdate_run'},
11317:                                       classlists => $env{'form.classlists'},
11318:                                       fields => {%fields},
11319:                                       lockablenames => \@lockablenames,
11320:                                     }
11321:                      );
11322:     foreach my $key (keys(%currautoupdate)) {
11323:         if (($key eq 'run') || ($key eq 'classlists')) {
11324:             if (exists($updatehash{autoupdate}{$key})) {
11325:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
11326:                     $changes{$key} = 1;
11327:                 }
11328:             }
11329:         } elsif ($key eq 'fields') {
11330:             if (ref($currautoupdate{$key}) eq 'HASH') {
11331:                 foreach my $item (@{$types},'default') {
11332:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
11333:                         my $change = 0;
11334:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
11335:                             if (!exists($fields{$item})) {
11336:                                 $change = 1;
11337:                                 last;
11338:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
11339:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
11340:                                     $change = 1;
11341:                                     last;
11342:                                 }
11343:                             }
11344:                         }
11345:                         if ($change) {
11346:                             push(@{$changes{$key}},$item);
11347:                         }
11348:                     } 
11349:                 }
11350:             }
11351:         } elsif ($key eq 'lockablenames') {
11352:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
11353:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
11354:                 if (@changed) {
11355:                     $changes{'lockablenames'} = 1;
11356:                 }
11357:             } else {
11358:                 if (@lockablenames) {
11359:                     $changes{'lockablenames'} = 1;
11360:                 }
11361:             }
11362:         }
11363:     }
11364:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
11365:         if (@lockablenames) {
11366:             $changes{'lockablenames'} = 1;
11367:         }
11368:     }
11369:     foreach my $item (@{$types},'default') {
11370:         if (defined($fields{$item})) {
11371:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
11372:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
11373:                     my $change = 0;
11374:                     if (ref($fields{$item}) eq 'ARRAY') {
11375:                         foreach my $type (@{$fields{$item}}) {
11376:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
11377:                                 $change = 1;
11378:                                 last;
11379:                             }
11380:                         }
11381:                     }
11382:                     if ($change) {
11383:                         push(@{$changes{'fields'}},$item);
11384:                     }
11385:                 } else {
11386:                     push(@{$changes{'fields'}},$item);
11387:                 }
11388:             } else {
11389:                 push(@{$changes{'fields'}},$item);
11390:             }
11391:         }
11392:     }
11393:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
11394:                                              $dom);
11395:     if ($putresult eq 'ok') {
11396:         if (keys(%changes) > 0) {
11397:             $resulttext = &mt('Changes made:').'<ul>';
11398:             foreach my $key (sort(keys(%changes))) {
11399:                 if ($key eq 'lockablenames') {
11400:                     $resulttext .= '<li>';
11401:                     if (@lockablenames) {
11402:                         $usertypes->{'default'} = $othertitle;
11403:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
11404:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
11405:                     } else {
11406:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
11407:                     }
11408:                     $resulttext .= '</li>';
11409:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
11410:                     foreach my $item (@{$changes{$key}}) {
11411:                         my @newvalues;
11412:                         foreach my $type (@{$fields{$item}}) {
11413:                             push(@newvalues,$fieldtitles{$type});
11414:                         }
11415:                         my $newvaluestr;
11416:                         if (@newvalues > 0) {
11417:                             $newvaluestr = join(', ',@newvalues);
11418:                         } else {
11419:                             $newvaluestr = &mt('none');
11420:                         }
11421:                         if ($item eq 'default') {
11422:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
11423:                         } else {
11424:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
11425:                         }
11426:                     }
11427:                 } else {
11428:                     my $newvalue;
11429:                     if ($key eq 'run') {
11430:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
11431:                     } else {
11432:                         $newvalue = $offon[$env{'form.'.$key}];
11433:                     }
11434:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
11435:                 }
11436:             }
11437:             $resulttext .= '</ul>';
11438:         } else {
11439:             $resulttext = &mt('No changes made to autoupdates');
11440:         }
11441:     } else {
11442:         $resulttext = '<span class="LC_error">'.
11443: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11444:     }
11445:     return $resulttext;
11446: }
11447: 
11448: sub modify_autocreate {
11449:     my ($dom,%domconfig) = @_;
11450:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
11451:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
11452:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
11453:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
11454:         }
11455:     }
11456:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
11457:                  req => 'Auto-creation of validated requests for official courses',
11458:                  xmldc => 'Identity of course creator of courses from XML files',
11459:                );
11460:     my @types = ('xml','req');
11461:     foreach my $item (@types) {
11462:         $newvals{$item} = $env{'form.autocreate_'.$item};
11463:         $newvals{$item} =~ s/\D//g;
11464:         $newvals{$item} = 0 if ($newvals{$item} eq '');
11465:     }
11466:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
11467:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
11468:     unless (exists($domcoords{$newvals{'xmldc'}})) {
11469:         $newvals{'xmldc'} = '';
11470:     } 
11471:     %autocreatehash =  (
11472:                         autocreate => { xml => $newvals{'xml'},
11473:                                         req => $newvals{'req'},
11474:                                       }
11475:                        );
11476:     if ($newvals{'xmldc'} ne '') {
11477:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
11478:     }
11479:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
11480:                                              $dom);
11481:     if ($putresult eq 'ok') {
11482:         my @items = @types;
11483:         if ($newvals{'xml'}) {
11484:             push(@items,'xmldc');
11485:         }
11486:         foreach my $item (@items) {
11487:             if (exists($currautocreate{$item})) {
11488:                 if ($currautocreate{$item} ne $newvals{$item}) {
11489:                     $changes{$item} = 1;
11490:                 }
11491:             } elsif ($newvals{$item}) {
11492:                 $changes{$item} = 1;
11493:             }
11494:         }
11495:         if (keys(%changes) > 0) {
11496:             my @offon = ('off','on'); 
11497:             $resulttext = &mt('Changes made:').'<ul>';
11498:             foreach my $item (@types) {
11499:                 if ($changes{$item}) {
11500:                     my $newtxt = $offon[$newvals{$item}];
11501:                     $resulttext .= '<li>'.
11502:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
11503:                                        '<b>','</b>').
11504:                                    '</li>';
11505:                 }
11506:             }
11507:             if ($changes{'xmldc'}) {
11508:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
11509:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
11510:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
11511:             }
11512:             $resulttext .= '</ul>';
11513:         } else {
11514:             $resulttext = &mt('No changes made to auto-creation settings');
11515:         }
11516:     } else {
11517:         $resulttext = '<span class="LC_error">'.
11518:             &mt('An error occurred: [_1]',$putresult).'</span>';
11519:     }
11520:     return $resulttext;
11521: }
11522: 
11523: sub modify_directorysrch {
11524:     my ($dom,$lastactref,%domconfig) = @_;
11525:     my ($resulttext,%changes);
11526:     my %currdirsrch;
11527:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
11528:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
11529:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
11530:         }
11531:     }
11532:     my %title = ( available => 'Institutional directory search available',
11533:                   localonly => 'Other domains can search institution',
11534:                   lcavailable => 'LON-CAPA directory search available',
11535:                   lclocalonly => 'Other domains can search LON-CAPA domain',
11536:                   searchby => 'Search types',
11537:                   searchtypes => 'Search latitude');
11538:     my @offon = ('off','on');
11539:     my @otherdoms = ('Yes','No');
11540: 
11541:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
11542:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
11543:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
11544: 
11545:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11546:     if (keys(%{$usertypes}) == 0) {
11547:         @cansearch = ('default');
11548:     } else {
11549:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
11550:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
11551:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
11552:                     push(@{$changes{'cansearch'}},$type);
11553:                 }
11554:             }
11555:             foreach my $type (@cansearch) {
11556:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
11557:                     push(@{$changes{'cansearch'}},$type);
11558:                 }
11559:             }
11560:         } else {
11561:             push(@{$changes{'cansearch'}},@cansearch);
11562:         }
11563:     }
11564: 
11565:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
11566:         foreach my $by (@{$currdirsrch{'searchby'}}) {
11567:             if (!grep(/^\Q$by\E$/,@searchby)) {
11568:                 push(@{$changes{'searchby'}},$by);
11569:             }
11570:         }
11571:         foreach my $by (@searchby) {
11572:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
11573:                 push(@{$changes{'searchby'}},$by);
11574:             }
11575:         }
11576:     } else {
11577:         push(@{$changes{'searchby'}},@searchby);
11578:     }
11579: 
11580:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
11581:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
11582:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
11583:                 push(@{$changes{'searchtypes'}},$type);
11584:             }
11585:         }
11586:         foreach my $type (@searchtypes) {
11587:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
11588:                 push(@{$changes{'searchtypes'}},$type);
11589:             }
11590:         }
11591:     } else {
11592:         if (exists($currdirsrch{'searchtypes'})) {
11593:             foreach my $type (@searchtypes) {  
11594:                 if ($type ne $currdirsrch{'searchtypes'}) { 
11595:                     push(@{$changes{'searchtypes'}},$type);
11596:                 }
11597:             }
11598:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
11599:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
11600:             }   
11601:         } else {
11602:             push(@{$changes{'searchtypes'}},@searchtypes); 
11603:         }
11604:     }
11605: 
11606:     my %dirsrch_hash =  (
11607:             directorysrch => { available => $env{'form.dirsrch_available'},
11608:                                cansearch => \@cansearch,
11609:                                localonly => $env{'form.dirsrch_instlocalonly'},
11610:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
11611:                                lcavailable => $env{'form.dirsrch_domavailable'},
11612:                                searchby => \@searchby,
11613:                                searchtypes => \@searchtypes,
11614:                              }
11615:             );
11616:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
11617:                                              $dom);
11618:     if ($putresult eq 'ok') {
11619:         if (exists($currdirsrch{'available'})) {
11620:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
11621:                  $changes{'available'} = 1;
11622:              }
11623:         } else {
11624:             if ($env{'form.dirsrch_available'} eq '1') {
11625:                 $changes{'available'} = 1;
11626:             }
11627:         }
11628:         if (exists($currdirsrch{'lcavailable'})) {
11629:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
11630:                 $changes{'lcavailable'} = 1;
11631:             }
11632:         } else {
11633:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
11634:                 $changes{'lcavailable'} = 1;
11635:             }
11636:         }
11637:         if (exists($currdirsrch{'localonly'})) {
11638:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
11639:                 $changes{'localonly'} = 1;
11640:             }
11641:         } else {
11642:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
11643:                 $changes{'localonly'} = 1;
11644:             }
11645:         }
11646:         if (exists($currdirsrch{'lclocalonly'})) {
11647:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
11648:                 $changes{'lclocalonly'} = 1;
11649:             }
11650:         } else {
11651:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
11652:                 $changes{'lclocalonly'} = 1;
11653:             }
11654:         }
11655:         if (keys(%changes) > 0) {
11656:             $resulttext = &mt('Changes made:').'<ul>';
11657:             if ($changes{'available'}) {
11658:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
11659:             }
11660:             if ($changes{'lcavailable'}) {
11661:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
11662:             }
11663:             if ($changes{'localonly'}) {
11664:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
11665:             }
11666:             if ($changes{'lclocalonly'}) {
11667:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
11668:             }
11669:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
11670:                 my $chgtext;
11671:                 if (ref($usertypes) eq 'HASH') {
11672:                     if (keys(%{$usertypes}) > 0) {
11673:                         foreach my $type (@{$types}) {
11674:                             if (grep(/^\Q$type\E$/,@cansearch)) {
11675:                                 $chgtext .= $usertypes->{$type}.'; ';
11676:                             }
11677:                         }
11678:                         if (grep(/^default$/,@cansearch)) {
11679:                             $chgtext .= $othertitle;
11680:                         } else {
11681:                             $chgtext =~ s/\; $//;
11682:                         }
11683:                         $resulttext .=
11684:                             '<li>'.
11685:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
11686:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
11687:                             '</li>';
11688:                     }
11689:                 }
11690:             }
11691:             if (ref($changes{'searchby'}) eq 'ARRAY') {
11692:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
11693:                 my $chgtext;
11694:                 foreach my $type (@{$titleorder}) {
11695:                     if (grep(/^\Q$type\E$/,@searchby)) {
11696:                         if (defined($searchtitles->{$type})) {
11697:                             $chgtext .= $searchtitles->{$type}.'; ';
11698:                         }
11699:                     }
11700:                 }
11701:                 $chgtext =~ s/\; $//;
11702:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
11703:             }
11704:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
11705:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
11706:                 my $chgtext;
11707:                 foreach my $type (@{$srchtypeorder}) {
11708:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
11709:                         if (defined($srchtypes_desc->{$type})) {
11710:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
11711:                         }
11712:                     }
11713:                 }
11714:                 $chgtext =~ s/\; $//;
11715:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
11716:             }
11717:             $resulttext .= '</ul>';
11718:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
11719:             if (ref($lastactref) eq 'HASH') {
11720:                 $lastactref->{'directorysrch'} = 1;
11721:             }
11722:         } else {
11723:             $resulttext = &mt('No changes made to directory search settings');
11724:         }
11725:     } else {
11726:         $resulttext = '<span class="LC_error">'.
11727:                       &mt('An error occurred: [_1]',$putresult).'</span>';
11728:     }
11729:     return $resulttext;
11730: }
11731: 
11732: sub modify_contacts {
11733:     my ($dom,$lastactref,%domconfig) = @_;
11734:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
11735:     if (ref($domconfig{'contacts'}) eq 'HASH') {
11736:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
11737:             $currsetting{$key} = $domconfig{'contacts'}{$key};
11738:         }
11739:     }
11740:     my (%others,%to,%bcc,%includestr,%includeloc);
11741:     my @contacts = ('supportemail','adminemail');
11742:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
11743:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
11744:     my @toggles = ('reporterrors','reportupdates','reportstatus');
11745:     my @lonstatus = ('threshold','sysmail','weights','excluded');
11746:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
11747:     foreach my $type (@mailings) {
11748:         @{$newsetting{$type}} = 
11749:             &Apache::loncommon::get_env_multiple('form.'.$type);
11750:         foreach my $item (@contacts) {
11751:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
11752:                 $contacts_hash{contacts}{$type}{$item} = 1;
11753:             } else {
11754:                 $contacts_hash{contacts}{$type}{$item} = 0;
11755:             }
11756:         }
11757:         $others{$type} = $env{'form.'.$type.'_others'};
11758:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
11759:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11760:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
11761:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
11762:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
11763:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
11764:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
11765:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11766:             }
11767:         }
11768:     }
11769:     foreach my $item (@contacts) {
11770:         $to{$item} = $env{'form.'.$item};
11771:         $contacts_hash{'contacts'}{$item} = $to{$item};
11772:     }
11773:     foreach my $item (@toggles) {
11774:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
11775:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
11776:         }
11777:     }
11778:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
11779:     foreach my $item (@lonstatus) {
11780:         if ($item eq 'excluded') {
11781:             my (%serverhomes,@excluded);
11782:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
11783:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
11784:             if (@possexcluded) {
11785:                 foreach my $id (sort(@possexcluded)) {
11786:                     if ($serverhomes{$id}) {
11787:                         push(@excluded,$id);
11788:                     }
11789:                 }
11790:             }
11791:             if (@excluded) {
11792:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
11793:             }
11794:         } elsif ($item eq 'weights') {
11795:             foreach my $type ('E','W','N','U') {
11796:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
11797:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
11798:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
11799:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
11800:                             $env{'form.error'.$item.'_'.$type};
11801:                     }
11802:                 }
11803:             }
11804:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
11805:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
11806:             if ($env{'form.error'.$item} =~ /^\d+$/) {
11807:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
11808:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
11809:                 }
11810:             }
11811:         }
11812:     }
11813:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
11814:         foreach my $field (@{$fields}) {
11815:             if (ref($possoptions->{$field}) eq 'ARRAY') {
11816:                 my $value = $env{'form.helpform_'.$field};
11817:                 $value =~ s/^\s+|\s+$//g;
11818:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
11819:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
11820:                     if ($field eq 'screenshot') {
11821:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
11822:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
11823:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
11824:                         }
11825:                     }
11826:                 }
11827:             }
11828:         }
11829:     }
11830:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11831:     my (@statuses,%usertypeshash,@overrides);
11832:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
11833:         @statuses = @{$types};
11834:         if (ref($usertypes) eq 'HASH') {
11835:             %usertypeshash = %{$usertypes};
11836:         }
11837:     }
11838:     if (@statuses) {
11839:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
11840:         foreach my $type (@possoverrides) {
11841:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
11842:                 push(@overrides,$type);
11843:             }
11844:         }
11845:         if (@overrides) {
11846:             foreach my $type (@overrides) {
11847:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
11848:                 foreach my $item (@contacts) {
11849:                     if (grep(/^\Q$item\E$/,@standard)) {
11850:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
11851:                         $newsetting{'override_'.$type}{$item} = 1;
11852:                     } else {
11853:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
11854:                         $newsetting{'override_'.$type}{$item} = 0;
11855:                     }
11856:                 }
11857:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
11858:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
11859:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
11860:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
11861:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
11862:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
11863:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
11864:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11865:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
11866:                 }
11867:             }    
11868:         }
11869:     }
11870:     if (keys(%currsetting) > 0) {
11871:         foreach my $item (@contacts) {
11872:             if ($to{$item} ne $currsetting{$item}) {
11873:                 $changes{$item} = 1;
11874:             }
11875:         }
11876:         foreach my $type (@mailings) {
11877:             foreach my $item (@contacts) {
11878:                 if (ref($currsetting{$type}) eq 'HASH') {
11879:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
11880:                         push(@{$changes{$type}},$item);
11881:                     }
11882:                 } else {
11883:                     push(@{$changes{$type}},@{$newsetting{$type}});
11884:                 }
11885:             }
11886:             if ($others{$type} ne $currsetting{$type}{'others'}) {
11887:                 push(@{$changes{$type}},'others');
11888:             }
11889:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11890:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
11891:                     push(@{$changes{$type}},'bcc'); 
11892:                 }
11893:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
11894:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
11895:                     push(@{$changes{$type}},'include');
11896:                 }
11897:             }
11898:         }
11899:         if (ref($fields) eq 'ARRAY') {
11900:             if (ref($currsetting{'helpform'}) eq 'HASH') {
11901:                 foreach my $field (@{$fields}) {
11902:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
11903:                         push(@{$changes{'helpform'}},$field);
11904:                     }
11905:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11906:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
11907:                             push(@{$changes{'helpform'}},'maxsize');
11908:                         }
11909:                     }
11910:                 }
11911:             } else {
11912:                 foreach my $field (@{$fields}) {
11913:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11914:                         push(@{$changes{'helpform'}},$field);
11915:                     }
11916:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11917:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11918:                             push(@{$changes{'helpform'}},'maxsize');
11919:                         }
11920:                     }
11921:                 }
11922:             }
11923:         }
11924:         if (@statuses) {
11925:             if (ref($currsetting{'overrides'}) eq 'HASH') {
11926:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
11927:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
11928:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
11929:                             foreach my $item (@contacts,'bcc','others','include') {
11930:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
11931:                                     push(@{$changes{'overrides'}},$key);
11932:                                     last;
11933:                                 }
11934:                             }
11935:                         } else {
11936:                             push(@{$changes{'overrides'}},$key);
11937:                         }
11938:                     }
11939:                 }
11940:                 foreach my $key (@overrides) {
11941:                     unless (exists($currsetting{'overrides'}{$key})) {
11942:                         push(@{$changes{'overrides'}},$key);
11943:                     }
11944:                 }
11945:             } else {
11946:                 foreach my $key (@overrides) {
11947:                     push(@{$changes{'overrides'}},$key);
11948:                 }
11949:             }
11950:         }
11951:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
11952:             foreach my $key ('excluded','weights','threshold','sysmail') {
11953:                 if ($key eq 'excluded') {
11954:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
11955:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
11956:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
11957:                             (@{$currsetting{'lonstatus'}{$key}})) {
11958:                             my @diffs =
11959:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
11960:                                                                    $currsetting{'lonstatus'}{$key});
11961:                             if (@diffs) {
11962:                                 push(@{$changes{'lonstatus'}},$key);
11963:                             }
11964:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
11965:                             push(@{$changes{'lonstatus'}},$key);
11966:                         }
11967:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
11968:                              (@{$currsetting{'lonstatus'}{$key}})) {
11969:                         push(@{$changes{'lonstatus'}},$key);
11970:                     }
11971:                 } elsif ($key eq 'weights') {
11972:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
11973:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
11974:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
11975:                             foreach my $type ('E','W','N','U') {
11976:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
11977:                                         $currsetting{'lonstatus'}{$key}{$type}) {
11978:                                     push(@{$changes{'lonstatus'}},$key);
11979:                                     last;
11980:                                 }
11981:                             }
11982:                         } else {
11983:                             foreach my $type ('E','W','N','U') {
11984:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
11985:                                     push(@{$changes{'lonstatus'}},$key);
11986:                                     last;
11987:                                 }
11988:                             }
11989:                         }
11990:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
11991:                         foreach my $type ('E','W','N','U') {
11992:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
11993:                                 push(@{$changes{'lonstatus'}},$key);
11994:                                 last;
11995:                             }
11996:                         }
11997:                     }
11998:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
11999:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
12000:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
12001:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
12002:                                 push(@{$changes{'lonstatus'}},$key);
12003:                             }
12004:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
12005:                             push(@{$changes{'lonstatus'}},$key);
12006:                         }
12007:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
12008:                         push(@{$changes{'lonstatus'}},$key);
12009:                     }
12010:                 }
12011:             }
12012:         } else {
12013:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
12014:                 foreach my $key ('excluded','weights','threshold','sysmail') {
12015:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
12016:                         push(@{$changes{'lonstatus'}},$key);
12017:                     }
12018:                 }
12019:             }
12020:         }
12021:     } else {
12022:         my %default;
12023:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
12024:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
12025:         $default{'errormail'} = 'adminemail';
12026:         $default{'packagesmail'} = 'adminemail';
12027:         $default{'helpdeskmail'} = 'supportemail';
12028:         $default{'otherdomsmail'} = 'supportemail';
12029:         $default{'lonstatusmail'} = 'adminemail';
12030:         $default{'requestsmail'} = 'adminemail';
12031:         $default{'updatesmail'} = 'adminemail';
12032:         $default{'hostipmail'} = 'adminemail';
12033:         foreach my $item (@contacts) {
12034:            if ($to{$item} ne $default{$item}) {
12035:                $changes{$item} = 1;
12036:            }
12037:         }
12038:         foreach my $type (@mailings) {
12039:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
12040:                 push(@{$changes{$type}},@{$newsetting{$type}});
12041:             }
12042:             if ($others{$type} ne '') {
12043:                 push(@{$changes{$type}},'others');
12044:             }
12045:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12046:                 if ($bcc{$type} ne '') {
12047:                     push(@{$changes{$type}},'bcc');
12048:                 }
12049:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
12050:                     push(@{$changes{$type}},'include');
12051:                 }
12052:             }
12053:         }
12054:         if (ref($fields) eq 'ARRAY') {
12055:             foreach my $field (@{$fields}) {
12056:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
12057:                     push(@{$changes{'helpform'}},$field);
12058:                 }
12059:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
12060:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
12061:                         push(@{$changes{'helpform'}},'maxsize');
12062:                     }
12063:                 }
12064:             }
12065:         }
12066:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
12067:             foreach my $key ('excluded','weights','threshold','sysmail') {
12068:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
12069:                     push(@{$changes{'lonstatus'}},$key);
12070:                 }
12071:             }
12072:         }
12073:     }
12074:     foreach my $item (@toggles) {
12075:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
12076:             $changes{$item} = 1;
12077:         } elsif ((!$env{'form.'.$item}) &&
12078:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
12079:             $changes{$item} = 1;
12080:         }
12081:     }
12082:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
12083:                                              $dom);
12084:     if ($putresult eq 'ok') {
12085:         if (keys(%changes) > 0) {
12086:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12087:             if (ref($lastactref) eq 'HASH') {
12088:                 $lastactref->{'domainconfig'} = 1;
12089:             }
12090:             my ($titles,$short_titles)  = &contact_titles();
12091:             $resulttext = &mt('Changes made:').'<ul>';
12092:             foreach my $item (@contacts) {
12093:                 if ($changes{$item}) {
12094:                     $resulttext .= '<li>'.$titles->{$item}.
12095:                                     &mt(' set to: ').
12096:                                     '<span class="LC_cusr_emph">'.
12097:                                     $to{$item}.'</span></li>';
12098:                 }
12099:             }
12100:             foreach my $type (@mailings) {
12101:                 if (ref($changes{$type}) eq 'ARRAY') {
12102:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12103:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
12104:                     } else {
12105:                         $resulttext .= '<li>'.$titles->{$type}.': ';
12106:                     }
12107:                     my @text;
12108:                     foreach my $item (@{$newsetting{$type}}) {
12109:                         push(@text,$short_titles->{$item});
12110:                     }
12111:                     if ($others{$type} ne '') {
12112:                         push(@text,$others{$type});
12113:                     }
12114:                     if (@text) {
12115:                         $resulttext .= '<span class="LC_cusr_emph">'.
12116:                                        join(', ',@text).'</span>';
12117:                     }
12118:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
12119:                         if ($bcc{$type} ne '') {
12120:                             my $bcctext;
12121:                             if (@text) {
12122:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
12123:                             } else {
12124:                                 $bcctext = '(Bcc)';
12125:                             }
12126:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
12127:                         } elsif (!@text) {
12128:                             $resulttext .= &mt('No one');
12129:                         }
12130:                         if ($includestr{$type} ne '') {
12131:                             if ($includeloc{$type} eq 'b') {
12132:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
12133:                             } elsif ($includeloc{$type} eq 's') {
12134:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
12135:                             }
12136:                         }
12137:                     } elsif (!@text) {
12138:                         $resulttext .= &mt('No recipients');
12139:                     }
12140:                     $resulttext .= '</li>';
12141:                 }
12142:             }
12143:             if (ref($changes{'overrides'}) eq 'ARRAY') {
12144:                 my @deletions;
12145:                 foreach my $type (@{$changes{'overrides'}}) {
12146:                     if ($usertypeshash{$type}) {
12147:                         if (grep(/^\Q$type\E/,@overrides)) {
12148:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
12149:                                                       $usertypeshash{$type}).'<ul><li>';
12150:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
12151:                                 my @text;
12152:                                 foreach my $item (@contacts) {
12153:                                     if ($newsetting{'override_'.$type}{$item}) {
12154:                                         push(@text,$short_titles->{$item});
12155:                                     }
12156:                                 }
12157:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
12158:                                     push(@text,$newsetting{'override_'.$type}{'others'});
12159:                                 }
12160: 
12161:                                 if (@text) {
12162:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
12163:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
12164:                                 }
12165:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
12166:                                     my $bcctext;
12167:                                     if (@text) {
12168:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
12169:                                     } else {
12170:                                         $bcctext = '(Bcc)';
12171:                                     }
12172:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
12173:                                 } elsif (!@text) {
12174:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
12175:                                 }
12176:                                 $resulttext .= '</li>';
12177:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
12178:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
12179:                                     if ($loc eq 'b') {
12180:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
12181:                                     } elsif ($loc eq 's') {
12182:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
12183:                                     }
12184:                                 }
12185:                             }
12186:                             $resulttext .= '</li></ul></li>';
12187:                         } else {
12188:                             push(@deletions,$usertypeshash{$type});
12189:                         }
12190:                     }
12191:                 }
12192:                 if (@deletions) {
12193:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
12194:                                               join(', ',@deletions)).'</li>';
12195:                 }
12196:             }
12197:             my @offon = ('off','on');
12198:             my $corelink = &core_link_msu();
12199:             if ($changes{'reporterrors'}) {
12200:                 $resulttext .= '<li>'.
12201:                                &mt('E-mail error reports to [_1] set to "'.
12202:                                    $offon[$env{'form.reporterrors'}].'".',
12203:                                    $corelink).
12204:                                '</li>';
12205:             }
12206:             if ($changes{'reportupdates'}) {
12207:                 $resulttext .= '<li>'.
12208:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
12209:                                     $offon[$env{'form.reportupdates'}].'".',
12210:                                     $corelink).
12211:                                 '</li>';
12212:             }
12213:             if ($changes{'reportstatus'}) {
12214:                 $resulttext .= '<li>'.
12215:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
12216:                                     $offon[$env{'form.reportstatus'}].'".',
12217:                                     $corelink).
12218:                                 '</li>';
12219:             }
12220:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
12221:                 $resulttext .= '<li>'.
12222:                                &mt('Nightly status check e-mail settings').':<ul>';
12223:                 my (%defval,%use_def,%shown);
12224:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
12225:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
12226:                 $defval{'weights'} =
12227:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
12228:                 $defval{'excluded'} = &mt('None');
12229:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
12230:                     foreach my $item ('threshold','sysmail','weights','excluded') {
12231:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
12232:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
12233:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
12234:                             } elsif ($item eq 'weights') {
12235:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
12236:                                     foreach my $type ('E','W','N','U') {
12237:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
12238:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
12239:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
12240:                                         } else {
12241:                                             $shown{$item} .= $lonstatus_defs->{$type};
12242:                                         }
12243:                                         $shown{$item} .= ', ';
12244:                                     }
12245:                                     $shown{$item} =~ s/, $//;
12246:                                 } else {
12247:                                     $shown{$item} = $defval{$item};
12248:                                 }
12249:                             } elsif ($item eq 'excluded') {
12250:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
12251:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
12252:                                 } else {
12253:                                     $shown{$item} = $defval{$item};
12254:                                 }
12255:                             }
12256:                         } else {
12257:                             $shown{$item} = $defval{$item};
12258:                         }
12259:                     }
12260:                 } else {
12261:                     foreach my $item ('threshold','weights','excluded','sysmail') {
12262:                         $shown{$item} = $defval{$item};
12263:                     }
12264:                 }
12265:                 foreach my $item ('threshold','weights','excluded','sysmail') {
12266:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
12267:                                           $shown{$item}).'</li>';
12268:                 }
12269:                 $resulttext .= '</ul></li>';
12270:             }
12271:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
12272:                 my (@optional,@required,@unused,$maxsizechg);
12273:                 foreach my $field (@{$changes{'helpform'}}) {
12274:                     if ($field eq 'maxsize') {
12275:                         $maxsizechg = 1;
12276:                         next;
12277:                     }
12278:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
12279:                         push(@optional,$field);
12280:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
12281:                         push(@unused,$field);
12282:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
12283:                         push(@required,$field);
12284:                     }
12285:                 }
12286:                 if (@optional) {
12287:                     $resulttext .= '<li>'.
12288:                                    &mt('Help form fields changed to "Optional": [_1].',
12289:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
12290:                                    '</li>';
12291:                 }
12292:                 if (@required) {
12293:                     $resulttext .= '<li>'.
12294:                                    &mt('Help form fields changed to "Required": [_1].',
12295:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
12296:                                    '</li>';
12297:                 }
12298:                 if (@unused) {
12299:                     $resulttext .= '<li>'.
12300:                                    &mt('Help form fields changed to "Not shown": [_1].',
12301:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
12302:                                    '</li>';
12303:                 }
12304:                 if ($maxsizechg) {
12305:                     $resulttext .= '<li>'.
12306:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
12307:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
12308:                                    '</li>';
12309:                 }
12310:             }
12311:             $resulttext .= '</ul>';
12312:         } else {
12313:             $resulttext = &mt('No changes made to contacts and form settings');
12314:         }
12315:     } else {
12316:         $resulttext = '<span class="LC_error">'.
12317:             &mt('An error occurred: [_1].',$putresult).'</span>';
12318:     }
12319:     return $resulttext;
12320: }
12321: 
12322: sub modify_passwords {
12323:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12324:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
12325:         $updatedefaults,$updateconf);
12326:     my $customfn = 'resetpw.html';
12327:     if (ref($domconfig{'passwords'}) eq 'HASH') {
12328:         %current = %{$domconfig{'passwords'}};
12329:     }
12330:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12331:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12332:     if (ref($types) eq 'ARRAY') {
12333:         @oktypes = @{$types};
12334:     }
12335:     push(@oktypes,'default');
12336: 
12337:     my %titles = &Apache::lonlocal::texthash (
12338:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
12339:         intauth_check  => 'Check bcrypt cost if authenticated',
12340:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
12341:         permanent      => 'Permanent e-mail address',
12342:         critical       => 'Critical notification address',
12343:         notify         => 'Notification address',
12344:         min            => 'Minimum password length',
12345:         max            => 'Maximum password length',
12346:         chars          => 'Required characters',
12347:         numsaved       => 'Number of previous passwords to save',
12348:         reset          => 'Resetting Forgotten Password',
12349:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
12350:         rules          => 'Rules for LON-CAPA Passwords',
12351:         crsownerchg    => 'Course Owner Changing Student Passwords',
12352:         username       => 'Username',
12353:         email          => 'E-mail address',
12354:     );
12355: 
12356: #
12357: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
12358: #
12359:     my (%curr_defaults,%save_defaults);
12360:     if (ref($domconfig{'defaults'}) eq 'HASH') {
12361:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
12362:             if ($key =~ /^intauth_(cost|check|switch)$/) {
12363:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
12364:             } else {
12365:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
12366:             }
12367:         }
12368:     }
12369:     my %staticdefaults = (
12370:         'resetlink'      => 2,
12371:         'resetcase'      => \@oktypes,
12372:         'resetprelink'   => 'both',
12373:         'resetemail'     => ['critical','notify','permanent'],
12374:         'intauth_cost'   => 10,
12375:         'intauth_check'  => 0,
12376:         'intauth_switch' => 0,
12377:     );
12378:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
12379:     foreach my $type (@oktypes) {
12380:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
12381:     }
12382:     my $linklife = $env{'form.passwords_link'};
12383:     $linklife =~ s/^\s+|\s+$//g;
12384:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
12385:         $newvalues{'resetlink'} = $linklife;
12386:         if ($current{'resetlink'}) {
12387:             if ($current{'resetlink'} ne $linklife) {
12388:                 $changes{'reset'} = 1;
12389:             }
12390:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12391:             if ($staticdefaults{'resetlink'} ne $linklife) {
12392:                 $changes{'reset'} = 1;
12393:             }
12394:         }
12395:     } elsif ($current{'resetlink'}) {
12396:         $changes{'reset'} = 1;
12397:     }
12398:     my @casesens;
12399:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
12400:     foreach my $case (sort(@posscase)) {
12401:         if (grep(/^\Q$case\E$/,@oktypes)) {
12402:             push(@casesens,$case);
12403:         }
12404:     }
12405:     $newvalues{'resetcase'} = \@casesens;
12406:     if (ref($current{'resetcase'}) eq 'ARRAY') {
12407:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
12408:         if (@diffs > 0) {
12409:             $changes{'reset'} = 1;
12410:         }
12411:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12412:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
12413:         if (@diffs > 0) {
12414:             $changes{'reset'} = 1;
12415:         }
12416:     }
12417:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
12418:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
12419:         if (exists($current{'resetprelink'})) {
12420:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
12421:                 $changes{'reset'} = 1;
12422:             }
12423:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12424:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
12425:                 $changes{'reset'} = 1;
12426:             }
12427:         }
12428:     } elsif ($current{'resetprelink'}) {
12429:         $changes{'reset'} = 1;
12430:     }
12431:     foreach my $type (@oktypes) {
12432:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
12433:         my @postlink;
12434:         foreach my $item (sort(@possplink)) {
12435:             if ($item =~ /^(email|username)$/) {
12436:                 push(@postlink,$item);
12437:             }
12438:         }
12439:         $newvalues{'resetpostlink'}{$type} = \@postlink;
12440:         unless ($changes{'reset'}) {
12441:             if (ref($current{'resetpostlink'}) eq 'HASH') {
12442:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
12443:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
12444:                     if (@diffs > 0) {
12445:                         $changes{'reset'} = 1;
12446:                     }
12447:                 } else {
12448:                     $changes{'reset'} = 1;
12449:                 }
12450:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12451:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
12452:                 if (@diffs > 0) {
12453:                     $changes{'reset'} = 1;
12454:                 }
12455:             }
12456:         }
12457:     }
12458:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
12459:     my @resetemail;
12460:     foreach my $item (sort(@possemailsrc)) {
12461:         if ($item =~ /^(permanent|critical|notify)$/) {
12462:             push(@resetemail,$item);
12463:         }
12464:     }
12465:     $newvalues{'resetemail'} = \@resetemail;
12466:     unless ($changes{'reset'}) {
12467:         if (ref($current{'resetemail'}) eq 'ARRAY') {
12468:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
12469:             if (@diffs > 0) {
12470:                 $changes{'reset'} = 1;
12471:             }
12472:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
12473:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
12474:             if (@diffs > 0) {
12475:                 $changes{'reset'} = 1;
12476:             }
12477:         }
12478:     }
12479:     if ($env{'form.passwords_stdtext'} == 0) {
12480:         $newvalues{'resetremove'} = 1;
12481:         unless ($current{'resetremove'}) {
12482:             $changes{'reset'} = 1;
12483:         }
12484:     } elsif ($current{'resetremove'}) {
12485:         $changes{'reset'} = 1;
12486:     }
12487:     if ($env{'form.passwords_customfile.filename'} ne '') {
12488:         my $servadm = $r->dir_config('lonAdmEMail');
12489:         my $servadm = $r->dir_config('lonAdmEMail');
12490:         my ($configuserok,$author_ok,$switchserver) =
12491:             &config_check($dom,$confname,$servadm);
12492:         my $error;
12493:         if ($configuserok eq 'ok') {
12494:             if ($switchserver) {
12495:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
12496:             } else {
12497:                 if ($author_ok eq 'ok') {
12498:                     my ($result,$customurl) =
12499:                         &publishlogo($r,'upload','passwords_customfile',$dom,
12500:                                      $confname,'customtext/resetpw','','',$customfn);
12501:                     if ($result eq 'ok') {
12502:                         $newvalues{'resetcustom'} = $customurl;
12503:                         $changes{'reset'} = 1;
12504:                     } else {
12505:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
12506:                     }
12507:                 } else {
12508:                     $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);
12509:                 }
12510:             }
12511:         } else {
12512:             $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);
12513:         }
12514:         if ($error) {
12515:             &Apache::lonnet::logthis($error);
12516:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12517:         }
12518:     } elsif ($current{'resetcustom'}) {
12519:         if ($env{'form.passwords_custom_del'}) {
12520:             $changes{'reset'} = 1;
12521:         } else {
12522:             $newvalues{'resetcustom'} = $current{'resetcustom'};
12523:         }
12524:     }
12525:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
12526:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
12527:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
12528:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
12529:             $changes{'intauth'} = 1;
12530:         }
12531:     } else {
12532:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
12533:     }
12534:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
12535:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
12536:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
12537:             $changes{'intauth'} = 1;
12538:         }
12539:     } else {
12540:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
12541:     }
12542:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
12543:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
12544:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
12545:             $changes{'intauth'} = 1;
12546:         }
12547:     } else {
12548:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
12549:     }
12550:     foreach my $item ('cost','check','switch') {
12551:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
12552:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
12553:             $updatedefaults = 1;
12554:         }
12555:     }
12556:     foreach my $rule ('min','max','numsaved') {
12557:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
12558:         my $ruleok;
12559:         if ($rule eq 'min') {
12560:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
12561:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
12562:                     $ruleok = 1;
12563:                 }
12564:             }
12565:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
12566:                  ($env{'form.passwords_'.$rule} ne '0')) {
12567:             $ruleok = 1;
12568:         }
12569:         if ($ruleok) {
12570:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
12571:             if (exists($current{$rule})) {
12572:                 if ($newvalues{$rule} ne $current{$rule}) {
12573:                     $changes{'rules'} = 1;
12574:                 }
12575:             } elsif ($rule eq 'min') {
12576:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
12577:                     $changes{'rules'} = 1;
12578:                 }
12579:             } else {
12580:                 $changes{'rules'} = 1;
12581:             }
12582:         } elsif (exists($current{$rule})) {
12583:             $changes{'rules'} = 1;
12584:         }
12585:     }
12586:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
12587:     my @chars;
12588:     foreach my $item (sort(@posschars)) {
12589:         if ($item =~ /^(uc|lc|num|spec)$/) {
12590:             push(@chars,$item);
12591:         }
12592:     }
12593:     $newvalues{'chars'} = \@chars;
12594:     unless ($changes{'rules'}) {
12595:         if (ref($current{'chars'}) eq 'ARRAY') {
12596:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
12597:             if (@diffs > 0) {
12598:                 $changes{'rules'} = 1;
12599:             }
12600:         } else {
12601:             if (@chars > 0) {
12602:                 $changes{'rules'} = 1;
12603:             }
12604:         }
12605:     }
12606:     my %crsownerchg = (
12607:                         by => [],
12608:                         for => [],
12609:                       );
12610:     foreach my $item ('by','for') {
12611:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
12612:         foreach my $type (sort(@posstypes)) {
12613:             if (grep(/^\Q$type\E$/,@oktypes)) {
12614:                 push(@{$crsownerchg{$item}},$type);
12615:             }
12616:         }
12617:     }
12618:     $newvalues{'crsownerchg'} = \%crsownerchg;
12619:     if (ref($current{'crsownerchg'}) eq 'HASH') {
12620:         foreach my $item ('by','for') {
12621:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
12622:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
12623:                 if (@diffs > 0) {
12624:                     $changes{'crsownerchg'} = 1;
12625:                     last;
12626:                 }
12627:             }
12628:         }
12629:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
12630:         foreach my $item ('by','for') {
12631:             if (@{$crsownerchg{$item}} > 0) {
12632:                 $changes{'crsownerchg'} = 1;
12633:                 last;
12634:             }
12635:         }
12636:     }
12637: 
12638:     my %confighash = (
12639:                         defaults  => \%save_defaults,
12640:                         passwords => \%newvalues,
12641:                      );
12642:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
12643: 
12644:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
12645:     if ($putresult eq 'ok') {
12646:         if (keys(%changes) > 0) {
12647:             $resulttext = &mt('Changes made: ').'<ul>';
12648:             foreach my $key ('reset','intauth','rules','crsownerchg') {
12649:                 if ($changes{$key}) {
12650:                     unless ($key eq 'intauth') {
12651:                         $updateconf = 1;
12652:                     }
12653:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
12654:                     if ($key eq 'reset') {
12655:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
12656:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
12657:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
12658:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
12659:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
12660:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
12661:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
12662:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
12663:                             }
12664:                         } else {
12665:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
12666:                         }
12667:                         if ($confighash{'passwords'}{'resetlink'}) {
12668:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
12669:                         } else {
12670:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
12671:                                                   &mt('Will default to 2 hours').'</li>';
12672:                         }
12673:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
12674:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
12675:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
12676:                             } else {
12677:                                 my $casesens;
12678:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
12679:                                     if ($type eq 'default') {
12680:                                         $casesens .= $othertitle.', ';
12681:                                     } elsif ($usertypes->{$type} ne '') {
12682:                                         $casesens .= $usertypes->{$type}.', ';
12683:                                     }
12684:                                 }
12685:                                 $casesens =~ s/\Q, \E$//;
12686:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
12687:                             }
12688:                         } else {
12689:                             $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>';
12690:                         }
12691:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
12692:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
12693:                         } else {
12694:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
12695:                         }
12696:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
12697:                             my $output;
12698:                             if (ref($types) eq 'ARRAY') {
12699:                                 foreach my $type (@{$types}) {
12700:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
12701:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
12702:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
12703:                                         } else {
12704:                                             $output .= $usertypes->{$type}.' -- '.
12705:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
12706:                                         }
12707:                                     }
12708:                                 }
12709:                             }
12710:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
12711:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
12712:                                     $output .= $othertitle.' -- '.&mt('none');
12713:                                 } else {
12714:                                     $output .= $othertitle.' -- '.
12715:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
12716:                                 }
12717:                             }
12718:                             if ($output) {
12719:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
12720:                             } else {
12721:                                 $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>';
12722:                             }
12723:                         } else {
12724:                             $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>';
12725:                         }
12726:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
12727:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
12728:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
12729:                             } else {
12730:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
12731:                             }
12732:                         } else {
12733:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
12734:                         }
12735:                         if ($confighash{'passwords'}{'resetremove'}) {
12736:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
12737:                         } else {
12738:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
12739:                         }
12740:                         if ($confighash{'passwords'}{'resetcustom'}) {
12741:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
12742:                                                                             &mt('custom text'),600,500,undef,undef,
12743:                                                                             undef,undef,'background-color:#ffffff');
12744:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
12745:                         } else {
12746:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
12747:                         }
12748:                     } elsif ($key eq 'intauth') {
12749:                         foreach my $item ('cost','switch','check') {
12750:                             my $value = $save_defaults{$key.'_'.$item};
12751:                             if ($item eq 'switch') {
12752:                                 my %optiondesc = &Apache::lonlocal::texthash (
12753:                                                      0 => 'No',
12754:                                                      1 => 'Yes',
12755:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
12756:                                                  );
12757:                                 if ($value =~ /^(0|1|2)$/) {
12758:                                     $value = $optiondesc{$value};
12759:                                 } else {
12760:                                     $value = &mt('none -- defaults to No');
12761:                                 }
12762:                             } elsif ($item eq 'check') {
12763:                                 my %optiondesc = &Apache::lonlocal::texthash (
12764:                                                      0 => 'No',
12765:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12766:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
12767:                                                  );
12768:                                 if ($value =~ /^(0|1|2)$/) {
12769:                                     $value = $optiondesc{$value};
12770:                                 } else {
12771:                                     $value = &mt('none -- defaults to No');
12772:                                 }
12773:                             }
12774:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
12775:                         }
12776:                     } elsif ($key eq 'rules') {
12777:                         foreach my $rule ('min','max','numsaved') {
12778:                             if ($confighash{'passwords'}{$rule} eq '') {
12779:                                 if ($rule eq 'min') {
12780:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
12781:                                                    ' '.&mt('Default of [_1] will be used',
12782:                                                            $Apache::lonnet::passwdmin).'</li>';
12783:                                 } else {
12784:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
12785:                                 }
12786:                             } else {
12787:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
12788:                             }
12789:                         }
12790:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
12791:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
12792:                                 my %rulenames = &Apache::lonlocal::texthash(
12793:                                                      uc => 'At least one upper case letter',
12794:                                                      lc => 'At least one lower case letter',
12795:                                                      num => 'At least one number',
12796:                                                      spec => 'At least one non-alphanumeric',
12797:                                                    );
12798:                                 my $needed = '<ul><li>'.
12799:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
12800:                                              '</li></ul>';
12801:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
12802:                             } else {
12803:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
12804:                             }
12805:                         } else {
12806:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
12807:                         }
12808:                     } elsif ($key eq 'crsownerchg') {
12809:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
12810:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
12811:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
12812:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
12813:                             } else {
12814:                                 my %crsownerstr;
12815:                                 foreach my $item ('by','for') {
12816:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
12817:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
12818:                                             if ($type eq 'default') {
12819:                                                 $crsownerstr{$item} .= $othertitle.', ';
12820:                                             } elsif ($usertypes->{$type} ne '') {
12821:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
12822:                                             }
12823:                                         }
12824:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
12825:                                     }
12826:                                 }
12827:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
12828:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
12829:                             }
12830:                         } else {
12831:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
12832:                         }
12833:                     }
12834:                     $resulttext .= '</ul></li>';
12835:                 }
12836:             }
12837:             $resulttext .= '</ul>';
12838:         } else {
12839:             $resulttext = &mt('No changes made to password settings');
12840:         }
12841:         my $cachetime = 24*60*60;
12842:         if ($updatedefaults) {
12843:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12844:             if (ref($lastactref) eq 'HASH') {
12845:                 $lastactref->{'domdefaults'} = 1;
12846:             }
12847:         }
12848:         if ($updateconf) {
12849:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
12850:             if (ref($lastactref) eq 'HASH') {
12851:                 $lastactref->{'passwdconf'} = 1;
12852:             }
12853:         }
12854:     } else {
12855:         $resulttext = '<span class="LC_error">'.
12856:             &mt('An error occurred: [_1]',$putresult).'</span>';
12857:     }
12858:     if ($errors) {
12859:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
12860:                        $errors.'</ul></p>';
12861:     }
12862:     return $resulttext;
12863: }
12864: 
12865: sub modify_usercreation {
12866:     my ($dom,%domconfig) = @_;
12867:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
12868:     my $warningmsg;
12869:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
12870:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
12871:             if ($key eq 'cancreate') {
12872:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
12873:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
12874:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
12875:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12876:                         } else {
12877:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
12878:                         }
12879:                     }
12880:                 }
12881:             } elsif ($key eq 'email_rule') {
12882:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
12883:             } else {
12884:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
12885:             }
12886:         }
12887:     }
12888:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
12889:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
12890:     my @contexts = ('author','course','requestcrs');
12891:     foreach my $item(@contexts) {
12892:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
12893:     }
12894:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
12895:         foreach my $item (@contexts) {
12896:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
12897:                 push(@{$changes{'cancreate'}},$item);
12898:             }
12899:         }
12900:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
12901:         foreach my $item (@contexts) {
12902:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
12903:                 if ($cancreate{$item} ne 'any') {
12904:                     push(@{$changes{'cancreate'}},$item);
12905:                 }
12906:             } else {
12907:                 if ($cancreate{$item} ne 'none') {
12908:                     push(@{$changes{'cancreate'}},$item);
12909:                 }
12910:             }
12911:         }
12912:     } else {
12913:         foreach my $item (@contexts)  {
12914:             push(@{$changes{'cancreate'}},$item);
12915:         }
12916:     }
12917: 
12918:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
12919:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
12920:             if (!grep(/^\Q$type\E$/,@username_rule)) {
12921:                 push(@{$changes{'username_rule'}},$type);
12922:             }
12923:         }
12924:         foreach my $type (@username_rule) {
12925:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
12926:                 push(@{$changes{'username_rule'}},$type);
12927:             }
12928:         }
12929:     } else {
12930:         push(@{$changes{'username_rule'}},@username_rule);
12931:     }
12932: 
12933:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
12934:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
12935:             if (!grep(/^\Q$type\E$/,@id_rule)) {
12936:                 push(@{$changes{'id_rule'}},$type);
12937:             }
12938:         }
12939:         foreach my $type (@id_rule) {
12940:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
12941:                 push(@{$changes{'id_rule'}},$type);
12942:             }
12943:         }
12944:     } else {
12945:         push(@{$changes{'id_rule'}},@id_rule);
12946:     }
12947: 
12948:     my @authen_contexts = ('author','course','domain');
12949:     my @authtypes = ('int','krb4','krb5','loc');
12950:     my %authhash;
12951:     foreach my $item (@authen_contexts) {
12952:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
12953:         foreach my $auth (@authtypes) {
12954:             if (grep(/^\Q$auth\E$/,@authallowed)) {
12955:                 $authhash{$item}{$auth} = 1;
12956:             } else {
12957:                 $authhash{$item}{$auth} = 0;
12958:             }
12959:         }
12960:     }
12961:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
12962:         foreach my $item (@authen_contexts) {
12963:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
12964:                 foreach my $auth (@authtypes) {
12965:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
12966:                         push(@{$changes{'authtypes'}},$item);
12967:                         last;
12968:                     }
12969:                 }
12970:             }
12971:         }
12972:     } else {
12973:         foreach my $item (@authen_contexts) {
12974:             push(@{$changes{'authtypes'}},$item);
12975:         }
12976:     }
12977: 
12978:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
12979:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
12980:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
12981:     $save_usercreate{'id_rule'} = \@id_rule;
12982:     $save_usercreate{'username_rule'} = \@username_rule,
12983:     $save_usercreate{'authtypes'} = \%authhash;
12984: 
12985:     my %usercreation_hash =  (
12986:         usercreation     => \%save_usercreate,
12987:     );
12988: 
12989:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
12990:                                              $dom);
12991: 
12992:     if ($putresult eq 'ok') {
12993:         if (keys(%changes) > 0) {
12994:             $resulttext = &mt('Changes made:').'<ul>';
12995:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
12996:                 my %lt = &usercreation_types();
12997:                 foreach my $type (@{$changes{'cancreate'}}) {
12998:                     my $chgtext = $lt{$type}.', ';
12999:                     if ($cancreate{$type} eq 'none') {
13000:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
13001:                     } elsif ($cancreate{$type} eq 'any') {
13002:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
13003:                     } elsif ($cancreate{$type} eq 'official') {
13004:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
13005:                     } elsif ($cancreate{$type} eq 'unofficial') {
13006:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
13007:                     }
13008:                     $resulttext .= '<li>'.$chgtext.'</li>';
13009:                 }
13010:             }
13011:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
13012:                 my ($rules,$ruleorder) = 
13013:                     &Apache::lonnet::inst_userrules($dom,'username');
13014:                 my $chgtext = '<ul>';
13015:                 foreach my $type (@username_rule) {
13016:                     if (ref($rules->{$type}) eq 'HASH') {
13017:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
13018:                     }
13019:                 }
13020:                 $chgtext .= '</ul>';
13021:                 if (@username_rule > 0) {
13022:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
13023:                 } else {
13024:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
13025:                 }
13026:             }
13027:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
13028:                 my ($idrules,$idruleorder) = 
13029:                     &Apache::lonnet::inst_userrules($dom,'id');
13030:                 my $chgtext = '<ul>';
13031:                 foreach my $type (@id_rule) {
13032:                     if (ref($idrules->{$type}) eq 'HASH') {
13033:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
13034:                     }
13035:                 }
13036:                 $chgtext .= '</ul>';
13037:                 if (@id_rule > 0) {
13038:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
13039:                 } else {
13040:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
13041:                 }
13042:             }
13043:             my %authname = &authtype_names();
13044:             my %context_title = &context_names();
13045:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
13046:                 my $chgtext = '<ul>';
13047:                 foreach my $type (@{$changes{'authtypes'}}) {
13048:                     my @allowed;
13049:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
13050:                     foreach my $auth (@authtypes) {
13051:                         if ($authhash{$type}{$auth}) {
13052:                             push(@allowed,$authname{$auth});
13053:                         }
13054:                     }
13055:                     if (@allowed > 0) {
13056:                         $chgtext .= join(', ',@allowed).'</li>';
13057:                     } else {
13058:                         $chgtext .= &mt('none').'</li>';
13059:                     }
13060:                 }
13061:                 $chgtext .= '</ul>';
13062:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
13063:                 $resulttext .= '</li>';
13064:             }
13065:             $resulttext .= '</ul>';
13066:         } else {
13067:             $resulttext = &mt('No changes made to user creation settings');
13068:         }
13069:     } else {
13070:         $resulttext = '<span class="LC_error">'.
13071:             &mt('An error occurred: [_1]',$putresult).'</span>';
13072:     }
13073:     if ($warningmsg ne '') {
13074:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
13075:     }
13076:     return $resulttext;
13077: }
13078: 
13079: sub modify_selfcreation {
13080:     my ($dom,$lastactref,%domconfig) = @_;
13081:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
13082:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
13083:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13084:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
13085:     if (ref($typesref) eq 'ARRAY') {
13086:         @types = @{$typesref};
13087:     }
13088:     if (ref($usertypesref) eq 'HASH') {
13089:         %usertypes = %{$usertypesref};
13090:     }
13091:     $usertypes{'default'} = $othertitle;
13092: #
13093: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
13094: #
13095:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
13096:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
13097:             if ($key eq 'cancreate') {
13098:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
13099:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
13100:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
13101:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
13102:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
13103:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
13104:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
13105:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
13106:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13107:                         } else {
13108:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
13109:                         }
13110:                     }
13111:                 }
13112:             } elsif ($key eq 'email_rule') {
13113:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
13114:             } else {
13115:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
13116:             }
13117:         }
13118:     }
13119: #
13120: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
13121: #
13122:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
13123:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
13124:             if ($key eq 'selfcreate') {
13125:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
13126:             } else {
13127:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
13128:             }
13129:         }
13130:     }
13131: #
13132: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
13133: #
13134:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
13135:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
13136:             if ($key eq 'inststatusguest') {
13137:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
13138:             } else {
13139:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
13140:             }
13141:         }
13142:     }
13143: 
13144:     my @contexts = ('selfcreate');
13145:     @{$cancreate{'selfcreate'}} = ();
13146:     %{$cancreate{'emailusername'}} = ();
13147:     if (@types) {
13148:         @{$cancreate{'statustocreate'}} = ();
13149:     }
13150:     %{$cancreate{'selfcreateprocessing'}} = ();
13151:     %{$cancreate{'shibenv'}} = ();
13152:     %{$cancreate{'emailverified'}} = ();
13153:     %{$cancreate{'emailoptions'}} = ();
13154:     %{$cancreate{'emaildomain'}} = ();
13155:     my %selfcreatetypes = (
13156:                              sso   => 'users authenticated by institutional single sign on',
13157:                              login => 'users authenticated by institutional log-in',
13158:                              email => 'users verified by e-mail',
13159:                           );
13160: #
13161: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
13162: # is permitted.
13163: #
13164: 
13165:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
13166: 
13167:     my (@statuses,%email_rule);
13168:     foreach my $item ('login','sso','email') {
13169:         if ($item eq 'email') {
13170:             if ($env{'form.cancreate_email'}) {
13171:                 if (@types) {
13172:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
13173:                     foreach my $status (@poss_statuses) {
13174:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
13175:                             push(@statuses,$status);
13176:                         }
13177:                     }
13178:                     $save_inststatus{'inststatusguest'} = \@statuses;
13179:                 } else {
13180:                     push(@statuses,'default');
13181:                 }
13182:                 if (@statuses) {
13183:                     my %curr_rule;
13184:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
13185:                         foreach my $type (@statuses) {
13186:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
13187:                         }
13188:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
13189:                         foreach my $type (@statuses) {
13190:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
13191:                         }
13192:                     }
13193:                     push(@{$cancreate{'selfcreate'}},'email');
13194:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
13195:                     my %curremaildom;
13196:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
13197:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
13198:                     }
13199:                     foreach my $type (@statuses) {
13200:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
13201:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
13202:                         }
13203:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
13204:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
13205:                         }
13206:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
13207: #
13208: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
13209: #
13210:                             my $chosen = $1;
13211:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
13212:                                 my $emaildom;
13213:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
13214:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
13215:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
13216:                                     if (ref($curremaildom{$type}) eq 'HASH') {
13217:                                         if (exists($curremaildom{$type}{$chosen})) {
13218:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
13219:                                                 push(@{$changes{'cancreate'}},'emaildomain');
13220:                                             }
13221:                                         } elsif ($emaildom ne '') {
13222:                                             push(@{$changes{'cancreate'}},'emaildomain');
13223:                                         }
13224:                                     } elsif ($emaildom ne '') {
13225:                                         push(@{$changes{'cancreate'}},'emaildomain');
13226:                                     }
13227:                                 }
13228:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13229:                             } elsif ($chosen eq 'custom') {
13230:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
13231:                                 $email_rule{$type} = [];
13232:                                 if (ref($emailrules) eq 'HASH') {
13233:                                     foreach my $rule (@possemail_rules) {
13234:                                         if (exists($emailrules->{$rule})) {
13235:                                             push(@{$email_rule{$type}},$rule);
13236:                                         }
13237:                                     }
13238:                                 }
13239:                                 if (@{$email_rule{$type}}) {
13240:                                     $cancreate{'emailoptions'}{$type} = 'custom';
13241:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
13242:                                         if (@{$curr_rule{$type}} > 0) {
13243:                                             foreach my $rule (@{$curr_rule{$type}}) {
13244:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
13245:                                                     push(@{$changes{'email_rule'}},$type);
13246:                                                 }
13247:                                             }
13248:                                         }
13249:                                         foreach my $type (@{$email_rule{$type}}) {
13250:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
13251:                                                 push(@{$changes{'email_rule'}},$type);
13252:                                             }
13253:                                         }
13254:                                     } else {
13255:                                         push(@{$changes{'email_rule'}},$type);
13256:                                     }
13257:                                 }
13258:                             } else {
13259:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
13260:                             }
13261:                         }
13262:                     }
13263:                     if (@types) {
13264:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13265:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
13266:                             if (@changed) {
13267:                                 push(@{$changes{'inststatus'}},'inststatusguest');
13268:                             }
13269:                         } else {
13270:                             push(@{$changes{'inststatus'}},'inststatusguest');
13271:                         }
13272:                     }
13273:                 } else {
13274:                     delete($env{'form.cancreate_email'});
13275:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13276:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13277:                             push(@{$changes{'inststatus'}},'inststatusguest');
13278:                         }
13279:                     }
13280:                 }
13281:             } else {
13282:                 $save_inststatus{'inststatusguest'} = [];
13283:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
13284:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
13285:                         push(@{$changes{'inststatus'}},'inststatusguest');
13286:                     }
13287:                 }
13288:             }
13289:         } else {
13290:             if ($env{'form.cancreate_'.$item}) {
13291:                 push(@{$cancreate{'selfcreate'}},$item);
13292:             }
13293:         }
13294:     }
13295:     my (%userinfo,%savecaptcha);
13296:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
13297: #
13298: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
13299: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
13300: #
13301: 
13302:     if ($env{'form.cancreate_email'}) {
13303:         push(@contexts,'emailusername');
13304:         if (@statuses) {
13305:             foreach my $type (@statuses) {
13306:                 if (ref($infofields) eq 'ARRAY') {
13307:                     foreach my $field (@{$infofields}) {
13308:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
13309:                             $cancreate{'emailusername'}{$type}{$field} = $1;
13310:                         }
13311:                     }
13312:                 }
13313:             }
13314:         }
13315: #
13316: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
13317: # queued requests for self-creation of account verified by e-mail.
13318: #
13319: 
13320:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
13321:         @approvalnotify = sort(@approvalnotify);
13322:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
13323:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13324:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
13325:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
13326:                     push(@{$changes{'cancreate'}},'notify');
13327:                 }
13328:             } else {
13329:                 if ($cancreate{'notify'}{'approval'}) {
13330:                     push(@{$changes{'cancreate'}},'notify');
13331:                 }
13332:             }
13333:         } elsif ($cancreate{'notify'}{'approval'}) {
13334:             push(@{$changes{'cancreate'}},'notify');
13335:         }
13336: 
13337:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
13338:     }
13339: #  
13340: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
13341: # institutional log-in.
13342: #
13343:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
13344:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
13345:                ($domdefaults{'auth_def'} eq 'localauth'))) {
13346:             $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.').' '.
13347:                           &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.');
13348:         }
13349:     }
13350:     my @fields = ('lastname','firstname','middlename','generation',
13351:                   'permanentemail','id');
13352:     my @shibfields = (@fields,'inststatus');
13353:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13354: #
13355: # Where usernames may created for institutional log-in and/or institutional single sign on:
13356: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
13357: # may self-create accounts 
13358: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
13359: # which the user may supply, if institutional data is unavailable.
13360: #
13361:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
13362:         if (@types) {
13363:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
13364:             push(@contexts,'statustocreate');
13365:             foreach my $type (@types) {
13366:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
13367:                 foreach my $field (@fields) {
13368:                     if (grep(/^\Q$field\E$/,@modifiable)) {
13369:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
13370:                     } else {
13371:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
13372:                     }
13373:                 }
13374:             }
13375:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
13376:                 foreach my $type (@types) {
13377:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
13378:                         foreach my $field (@fields) {
13379:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
13380:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
13381:                                 push(@{$changes{'selfcreate'}},$type);
13382:                                 last;
13383:                             }
13384:                         }
13385:                     }
13386:                 }
13387:             } else {
13388:                 foreach my $type (@types) {
13389:                     push(@{$changes{'selfcreate'}},$type);
13390:                 }
13391:             }
13392:         }
13393:         foreach my $field (@shibfields) {
13394:             if ($env{'form.shibenv_'.$field} ne '') {
13395:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
13396:             }
13397:         }
13398:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
13399:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
13400:                 foreach my $field (@shibfields) {
13401:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
13402:                         push(@{$changes{'cancreate'}},'shibenv');
13403:                     }
13404:                 }
13405:             } else {
13406:                 foreach my $field (@shibfields) {
13407:                     if ($env{'form.shibenv_'.$field}) {
13408:                         push(@{$changes{'cancreate'}},'shibenv');
13409:                         last;
13410:                     }
13411:                 }
13412:             }
13413:         }
13414:     }
13415:     foreach my $item (@contexts) {
13416:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
13417:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
13418:                 if (ref($cancreate{$item}) eq 'ARRAY') {
13419:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
13420:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13421:                             push(@{$changes{'cancreate'}},$item);
13422:                         }
13423:                     }
13424:                 }
13425:             }
13426:             if (ref($cancreate{$item}) eq 'ARRAY') {
13427:                 foreach my $type (@{$cancreate{$item}}) {
13428:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
13429:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13430:                             push(@{$changes{'cancreate'}},$item);
13431:                         }
13432:                     }
13433:                 }
13434:             }
13435:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
13436:             if (ref($cancreate{$item}) eq 'HASH') {
13437:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
13438:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13439:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
13440:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13441:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13442:                                     push(@{$changes{'cancreate'}},$item);
13443:                                 }
13444:                             }
13445:                         }
13446:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13447:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13448:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13449:                                 push(@{$changes{'cancreate'}},$item);
13450:                             }
13451:                         }
13452:                     }
13453:                 }
13454:                 foreach my $type (keys(%{$cancreate{$item}})) {
13455:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13456:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13457:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
13458:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
13459:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13460:                                         push(@{$changes{'cancreate'}},$item);
13461:                                     }
13462:                                 }
13463:                             } else {
13464:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13465:                                     push(@{$changes{'cancreate'}},$item);
13466:                                 }
13467:                             }
13468:                         }
13469:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13470:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
13471:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13472:                                 push(@{$changes{'cancreate'}},$item);
13473:                             }
13474:                         }
13475:                     }
13476:                 }
13477:             }
13478:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
13479:             if (ref($cancreate{$item}) eq 'ARRAY') {
13480:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
13481:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13482:                         push(@{$changes{'cancreate'}},$item);
13483:                     }
13484:                 }
13485:             }
13486:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
13487:             if (ref($cancreate{$item}) eq 'HASH') {
13488:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13489:                     push(@{$changes{'cancreate'}},$item);
13490:                 }
13491:             }
13492:         } elsif ($item eq 'emailusername') {
13493:             if (ref($cancreate{$item}) eq 'HASH') {
13494:                 foreach my $type (keys(%{$cancreate{$item}})) {
13495:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
13496:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
13497:                             if ($cancreate{$item}{$type}{$field}) {
13498:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
13499:                                     push(@{$changes{'cancreate'}},$item);
13500:                                 }
13501:                                 last;
13502:                             }
13503:                         }
13504:                     }
13505:                 }
13506:             }
13507:         }
13508:     }
13509: #
13510: # Populate %save_usercreate hash with updates to self-creation configuration.
13511: #
13512:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
13513:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
13514:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
13515:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
13516:     if (ref($cancreate{'notify'}) eq 'HASH') {
13517:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
13518:     }
13519:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
13520:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
13521:     }
13522:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
13523:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
13524:     }
13525:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
13526:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
13527:     }
13528:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
13529:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
13530:     }
13531:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13532:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
13533:     }
13534:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
13535:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
13536:     }
13537:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
13538:     $save_usercreate{'email_rule'} = \%email_rule;
13539: 
13540:     my %userconfig_hash = (
13541:             usercreation     => \%save_usercreate,
13542:             usermodification => \%save_usermodify,
13543:             inststatus       => \%save_inststatus,
13544:     );
13545: 
13546:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
13547:                                              $dom);
13548: #
13549: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
13550: #
13551:     if ($putresult eq 'ok') {
13552:         if (keys(%changes) > 0) {
13553:             $resulttext = &mt('Changes made:').'<ul>';
13554:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
13555:                 my %lt = &selfcreation_types();
13556:                 foreach my $type (@{$changes{'cancreate'}}) {
13557:                     my $chgtext = '';
13558:                     if ($type eq 'selfcreate') {
13559:                         if (@{$cancreate{$type}} == 0) {
13560:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
13561:                         } else {
13562:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
13563:                                         '<ul>';
13564:                             foreach my $case (@{$cancreate{$type}}) {
13565:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
13566:                             }
13567:                             $chgtext .= '</ul>';
13568:                             if (ref($cancreate{$type}) eq 'ARRAY') {
13569:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
13570:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
13571:                                         if (@{$cancreate{'statustocreate'}} == 0) {
13572:                                             $chgtext .= '<span class="LC_warning">'.
13573:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
13574:                                                         '</span><br />';
13575:                                         }
13576:                                     }
13577:                                 }
13578:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
13579:                                     if (!@statuses) {
13580:                                         $chgtext .= '<span class="LC_warning">'.
13581:                                                     &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.").
13582:                                                     '</span><br />';
13583: 
13584:                                     }
13585:                                 }
13586:                             }
13587:                         }
13588:                     } elsif ($type eq 'shibenv') {
13589:                         if (keys(%{$cancreate{$type}}) == 0) {
13590:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
13591:                         } else {
13592:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
13593:                                         '<ul>';
13594:                             foreach my $field (@shibfields) {
13595:                                 next if ($cancreate{$type}{$field} eq '');
13596:                                 if ($field eq 'inststatus') {
13597:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
13598:                                 } else {
13599:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
13600:                                 }
13601:                             }
13602:                             $chgtext .= '</ul>';
13603:                         }
13604:                     } elsif ($type eq 'statustocreate') {
13605:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
13606:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
13607:                             if (@{$cancreate{'selfcreate'}} > 0) {
13608:                                 if (@{$cancreate{'statustocreate'}} == 0) {
13609:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
13610:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
13611:                                         $chgtext .= '<br />'.
13612:                                                     '<span class="LC_warning">'.
13613:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
13614:                                                     '</span>';
13615:                                     }
13616:                                 } elsif (keys(%usertypes) > 0) {
13617:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13618:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
13619:                                     } else {
13620:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
13621:                                     }
13622:                                     $chgtext .= '<ul>';
13623:                                     foreach my $case (@{$cancreate{$type}}) {
13624:                                         if ($case eq 'default') {
13625:                                             $chgtext .= '<li>'.$othertitle.'</li>';
13626:                                         } else {
13627:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
13628:                                         }
13629:                                     }
13630:                                     $chgtext .= '</ul>';
13631:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
13632:                                         $chgtext .= '<span class="LC_warning">'.
13633:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
13634:                                                     '</span>';
13635:                                     }
13636:                                 }
13637:                             } else {
13638:                                 if (@{$cancreate{$type}} == 0) {
13639:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
13640:                                 } else {
13641:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
13642:                                 }
13643:                             }
13644:                             $chgtext .= '<br />';
13645:                         }
13646:                     } elsif ($type eq 'selfcreateprocessing') {
13647:                         my %choices = &Apache::lonlocal::texthash (
13648:                                                                     automatic => 'Automatic approval',
13649:                                                                     approval  => 'Queued for approval',
13650:                                                                   );
13651:                         if (@types) {
13652:                             if (@statuses) {
13653:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
13654:                                             '<ul>';
13655:                                 foreach my $status (@statuses) {
13656:                                     if ($status eq 'default') {
13657:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13658:                                     } else {
13659:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
13660:                                     }
13661:                                 }
13662:                                 $chgtext .= '</ul>';
13663:                             }
13664:                         } else {
13665:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
13666:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
13667:                         }
13668:                     } elsif ($type eq 'emailverified') {
13669:                         my %options = &Apache::lonlocal::texthash (
13670:                                                                     all   => 'Same as e-mail',
13671:                                                                     first => 'Omit @domain',
13672:                                                                     free  => 'Free to choose',
13673:                                                                   );
13674:                         if (@types) {
13675:                             if (@statuses) {
13676:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
13677:                                             '<ul>';
13678:                                 foreach my $status (@statuses) {
13679:                                     if ($status eq 'default') {
13680:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
13681:                                     } else {
13682:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
13683:                                     }
13684:                                 }
13685:                                 $chgtext .= '</ul>';
13686:                             }
13687:                         } else {
13688:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
13689:                                             $options{$cancreate{'emailverified'}{'default'}});
13690:                         }
13691:                     } elsif ($type eq 'emailoptions') {
13692:                         my %options = &Apache::lonlocal::texthash (
13693:                                                                     any     => 'Any e-mail',
13694:                                                                     inst    => 'Institutional only',
13695:                                                                     noninst => 'Non-institutional only',
13696:                                                                     custom  => 'Custom restrictions',
13697:                                                                   );
13698:                         if (@types) {
13699:                             if (@statuses) {
13700:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
13701:                                             '<ul>';
13702:                                 foreach my $status (@statuses) {
13703:                                     if ($type eq 'default') {
13704:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
13705:                                     } else {
13706:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
13707:                                     }
13708:                                 }
13709:                                 $chgtext .= '</ul>';
13710:                             }
13711:                         } else {
13712:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
13713:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
13714:                             } else {
13715:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
13716:                                                 $options{$cancreate{'emailoptions'}{'default'}});
13717:                             }
13718:                         }
13719:                     } elsif ($type eq 'emaildomain') {
13720:                         my $output;
13721:                         if (@statuses) {
13722:                             foreach my $type (@statuses) {
13723:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
13724:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
13725:                                         if ($type eq 'default') {
13726:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13727:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
13728:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
13729:                                             } else {
13730:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
13731:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
13732:                                             }
13733:                                         } else {
13734:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13735:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
13736:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
13737:                                             } else {
13738:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
13739:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
13740:                                             }
13741:                                         }
13742:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
13743:                                         if ($type eq 'default') {
13744:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13745:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
13746:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
13747:                                             } else {
13748:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
13749:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
13750:                                             }
13751:                                         } else {
13752:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
13753:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
13754:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
13755:                                             } else {
13756:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
13757:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
13758:                                             }
13759:                                         }
13760:                                     }
13761:                                 }
13762:                             }
13763:                         }
13764:                         if ($output ne '') {
13765:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
13766:                                         '<ul>'.$output.'</ul>';
13767:                         }
13768:                     } elsif ($type eq 'captcha') {
13769:                         if ($savecaptcha{$type} eq 'notused') {
13770:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
13771:                         } else {
13772:                             my %captchas = &captcha_phrases();
13773:                             if ($captchas{$savecaptcha{$type}}) {
13774:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
13775:                             } else {
13776:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
13777:                             }
13778:                         }
13779:                     } elsif ($type eq 'recaptchakeys') {
13780:                         my ($privkey,$pubkey);
13781:                         if (ref($savecaptcha{$type}) eq 'HASH') {
13782:                             $pubkey = $savecaptcha{$type}{'public'};
13783:                             $privkey = $savecaptcha{$type}{'private'};
13784:                         }
13785:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
13786:                         if (!$pubkey) {
13787:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
13788:                         } else {
13789:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
13790:                         }
13791:                         if (!$privkey) {
13792:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
13793:                         } else {
13794:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
13795:                         }
13796:                         $chgtext .= '</ul>';
13797:                     } elsif ($type eq 'recaptchaversion') {
13798:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
13799:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
13800:                         }
13801:                     } elsif ($type eq 'emailusername') {
13802:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
13803:                             if (@statuses) {
13804:                                 foreach my $type (@statuses) {
13805:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
13806:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
13807:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
13808:                                                     '<ul>';
13809:                                             foreach my $field (@{$infofields}) {
13810:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
13811:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
13812:                                                 }
13813:                                             }
13814:                                             $chgtext .= '</ul>';
13815:                                         } else {
13816:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
13817:                                         }
13818:                                     } else {
13819:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
13820:                                     }
13821:                                 }
13822:                             }
13823:                         }
13824:                     } elsif ($type eq 'notify') {
13825:                         my $numapprove = 0;
13826:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
13827:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
13828:                                 if ($cancreate{'notify'}{'approval'}) {
13829:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
13830:                                     $numapprove ++;
13831:                                 }
13832:                             }
13833:                         }
13834:                         unless ($numapprove) {
13835:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
13836:                         }
13837:                     }
13838:                     if ($chgtext) {
13839:                         $resulttext .= '<li>'.$chgtext.'</li>';
13840:                     }
13841:                 }
13842:             }
13843:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
13844:                 my ($emailrules,$emailruleorder) =
13845:                     &Apache::lonnet::inst_userrules($dom,'email');
13846:                 foreach my $type (@{$changes{'email_rule'}}) {
13847:                     if (ref($email_rule{$type}) eq 'ARRAY') {
13848:                         my $chgtext = '<ul>';
13849:                         foreach my $rule (@{$email_rule{$type}}) {
13850:                             if (ref($emailrules->{$rule}) eq 'HASH') {
13851:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
13852:                             }
13853:                         }
13854:                         $chgtext .= '</ul>';
13855:                         my $typename;
13856:                         if (@types) {
13857:                             if ($type eq 'default') {
13858:                                 $typename = $othertitle;
13859:                             } else {
13860:                                 $typename = $usertypes{$type};
13861:                             }
13862:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
13863:                         }
13864:                         if (@{$email_rule{$type}} > 0) {
13865:                             $resulttext .= '<li>'.
13866:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
13867:                                                $usertypes{$type}).
13868:                                            $chgtext.
13869:                                            '</li>';
13870:                         } else {
13871:                             $resulttext .= '<li>'.
13872:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
13873:                                            '</li>'.
13874:                                            &mt('(Affiliation: [_1])',$typename);
13875:                         }
13876:                     }
13877:                 }
13878:             }
13879:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
13880:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
13881:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
13882:                         my $chgtext = '<ul>';
13883:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
13884:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
13885:                         }
13886:                         $chgtext .= '</ul>';
13887:                         $resulttext .= '<li>'.
13888:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
13889:                                           $chgtext.
13890:                                        '</li>';
13891:                     } else {
13892:                         $resulttext .= '<li>'.
13893:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
13894:                                        '</li>';
13895:                     }
13896:                 }
13897:             }
13898:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
13899:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
13900:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13901:                 foreach my $type (@{$changes{'selfcreate'}}) {
13902:                     my $typename = $type;
13903:                     if (keys(%usertypes) > 0) {
13904:                         if ($usertypes{$type} ne '') {
13905:                             $typename = $usertypes{$type};
13906:                         }
13907:                     }
13908:                     my @modifiable;
13909:                     $resulttext .= '<li>'.
13910:                                     &mt('Self-creation of account by users with status: [_1]',
13911:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
13912:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
13913:                     foreach my $field (@fields) {
13914:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
13915:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
13916:                         }
13917:                     }
13918:                     if (@modifiable > 0) {
13919:                         $resulttext .= join(', ',@modifiable);
13920:                     } else {
13921:                         $resulttext .= &mt('none');
13922:                     }
13923:                     $resulttext .= '</li>';
13924:                 }
13925:                 $resulttext .= '</ul></li>';
13926:             }
13927:             $resulttext .= '</ul>';
13928:             my $cachetime = 24*60*60;
13929:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
13930:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13931:             if (ref($lastactref) eq 'HASH') {
13932:                 $lastactref->{'domdefaults'} = 1;
13933:             }
13934:         } else {
13935:             $resulttext = &mt('No changes made to self-creation settings');
13936:         }
13937:     } else {
13938:         $resulttext = '<span class="LC_error">'.
13939:             &mt('An error occurred: [_1]',$putresult).'</span>';
13940:     }
13941:     if ($warningmsg ne '') {
13942:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
13943:     }
13944:     return $resulttext;
13945: }
13946: 
13947: sub process_captcha {
13948:     my ($container,$changes,$newsettings,$currsettings) = @_;
13949:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
13950:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
13951:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
13952:         $newsettings->{'captcha'} = 'original';
13953:     }
13954:     my %current;
13955:     if (ref($currsettings) eq 'HASH') {
13956:         %current = %{$currsettings};
13957:     }
13958:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
13959:         if ($container eq 'cancreate') {
13960:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
13961:                 push(@{$changes->{'cancreate'}},'captcha');
13962:             } elsif (!defined($changes->{'cancreate'})) {
13963:                 $changes->{'cancreate'} = ['captcha'];
13964:             }
13965:         } elsif ($container eq 'passwords') {
13966:             $changes->{'reset'} = 1;
13967:         } else {
13968:             $changes->{'captcha'} = 1;
13969:         }
13970:     }
13971:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
13972:     if ($newsettings->{'captcha'} eq 'recaptcha') {
13973:         $newpub = $env{'form.'.$container.'_recaptchapub'};
13974:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
13975:         $newpub =~ s/[^\w\-]//g;
13976:         $newpriv =~ s/[^\w\-]//g;
13977:         $newsettings->{'recaptchakeys'} = {
13978:                                              public  => $newpub,
13979:                                              private => $newpriv,
13980:                                           };
13981:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
13982:         $newversion =~ s/\D//g;
13983:         if ($newversion ne '2') {
13984:             $newversion = 1;
13985:         }
13986:         $newsettings->{'recaptchaversion'} = $newversion;
13987:     }
13988:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
13989:         $currpub = $current{'recaptchakeys'}{'public'};
13990:         $currpriv = $current{'recaptchakeys'}{'private'};
13991:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
13992:             $newsettings->{'recaptchakeys'} = {
13993:                                                  public  => '',
13994:                                                  private => '',
13995:                                               }
13996:         }
13997:     }
13998:     if ($current{'captcha'} eq 'recaptcha') {
13999:         $currversion = $current{'recaptchaversion'};
14000:         if ($currversion ne '2') {
14001:             $currversion = 1;
14002:         }
14003:     }
14004:     if ($currversion ne $newversion) {
14005:         if ($container eq 'cancreate') {
14006:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14007:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
14008:             } elsif (!defined($changes->{'cancreate'})) {
14009:                 $changes->{'cancreate'} = ['recaptchaversion'];
14010:             }
14011:         } elsif ($container eq 'passwords') {
14012:             $changes->{'reset'} = 1;
14013:         } else {
14014:             $changes->{'recaptchaversion'} = 1;
14015:         }
14016:     }
14017:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
14018:         if ($container eq 'cancreate') {
14019:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
14020:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
14021:             } elsif (!defined($changes->{'cancreate'})) {
14022:                 $changes->{'cancreate'} = ['recaptchakeys'];
14023:             }
14024:         } elsif ($container eq 'passwords') {
14025:             $changes->{'reset'} = 1;
14026:         } else {
14027:             $changes->{'recaptchakeys'} = 1;
14028:         }
14029:     }
14030:     return;
14031: }
14032: 
14033: sub modify_usermodification {
14034:     my ($dom,%domconfig) = @_;
14035:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
14036:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
14037:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
14038:             if ($key eq 'selfcreate') {
14039:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
14040:             } else {  
14041:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
14042:             }
14043:         }
14044:     }
14045:     my @contexts = ('author','course');
14046:     my %context_title = (
14047:                            author => 'In author context',
14048:                            course => 'In course context',
14049:                         );
14050:     my @fields = ('lastname','firstname','middlename','generation',
14051:                   'permanentemail','id');
14052:     my %roles = (
14053:                   author => ['ca','aa'],
14054:                   course => ['st','ep','ta','in','cr'],
14055:                 );
14056:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14057:     foreach my $context (@contexts) {
14058:         foreach my $role (@{$roles{$context}}) {
14059:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
14060:             foreach my $item (@fields) {
14061:                 if (grep(/^\Q$item\E$/,@modifiable)) {
14062:                     $modifyhash{$context}{$role}{$item} = 1;
14063:                 } else {
14064:                     $modifyhash{$context}{$role}{$item} = 0;
14065:                 }
14066:             }
14067:         }
14068:         if (ref($curr_usermodification{$context}) eq 'HASH') {
14069:             foreach my $role (@{$roles{$context}}) {
14070:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
14071:                     foreach my $field (@fields) {
14072:                         if ($modifyhash{$context}{$role}{$field} ne 
14073:                                 $curr_usermodification{$context}{$role}{$field}) {
14074:                             push(@{$changes{$context}},$role);
14075:                             last;
14076:                         }
14077:                     }
14078:                 }
14079:             }
14080:         } else {
14081:             foreach my $context (@contexts) {
14082:                 foreach my $role (@{$roles{$context}}) {
14083:                     push(@{$changes{$context}},$role);
14084:                 }
14085:             }
14086:         }
14087:     }
14088:     my %usermodification_hash =  (
14089:                                    usermodification => \%modifyhash,
14090:                                  );
14091:     my $putresult = &Apache::lonnet::put_dom('configuration',
14092:                                              \%usermodification_hash,$dom);
14093:     if ($putresult eq 'ok') {
14094:         if (keys(%changes) > 0) {
14095:             $resulttext = &mt('Changes made: ').'<ul>';
14096:             foreach my $context (@contexts) {
14097:                 if (ref($changes{$context}) eq 'ARRAY') {
14098:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
14099:                     if (ref($changes{$context}) eq 'ARRAY') {
14100:                         foreach my $role (@{$changes{$context}}) {
14101:                             my $rolename;
14102:                             if ($role eq 'cr') {
14103:                                 $rolename = &mt('Custom');
14104:                             } else {
14105:                                 $rolename = &Apache::lonnet::plaintext($role);
14106:                             }
14107:                             my @modifiable;
14108:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
14109:                             foreach my $field (@fields) {
14110:                                 if ($modifyhash{$context}{$role}{$field}) {
14111:                                     push(@modifiable,$fieldtitles{$field});
14112:                                 }
14113:                             }
14114:                             if (@modifiable > 0) {
14115:                                 $resulttext .= join(', ',@modifiable);
14116:                             } else {
14117:                                 $resulttext .= &mt('none'); 
14118:                             }
14119:                             $resulttext .= '</li>';
14120:                         }
14121:                         $resulttext .= '</ul></li>';
14122:                     }
14123:                 }
14124:             }
14125:             $resulttext .= '</ul>';
14126:         } else {
14127:             $resulttext = &mt('No changes made to user modification settings');
14128:         }
14129:     } else {
14130:         $resulttext = '<span class="LC_error">'.
14131:             &mt('An error occurred: [_1]',$putresult).'</span>';
14132:     }
14133:     return $resulttext;
14134: }
14135: 
14136: sub modify_defaults {
14137:     my ($dom,$lastactref,%domconfig) = @_;
14138:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
14139:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14140:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
14141:                  'portal_def');
14142:     my @authtypes = ('internal','krb4','krb5','localauth');
14143:     foreach my $item (@items) {
14144:         $newvalues{$item} = $env{'form.'.$item};
14145:         if ($item eq 'auth_def') {
14146:             if ($newvalues{$item} ne '') {
14147:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
14148:                     push(@errors,$item);
14149:                 }
14150:             }
14151:         } elsif ($item eq 'lang_def') {
14152:             if ($newvalues{$item} ne '') {
14153:                 if ($newvalues{$item} =~ /^(\w+)/) {
14154:                     my $langcode = $1;
14155:                     if ($langcode ne 'x_chef') {
14156:                         if (code2language($langcode) eq '') {
14157:                             push(@errors,$item);
14158:                         }
14159:                     }
14160:                 } else {
14161:                     push(@errors,$item);
14162:                 }
14163:             }
14164:         } elsif ($item eq 'timezone_def') {
14165:             if ($newvalues{$item} ne '') {
14166:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
14167:                     push(@errors,$item);   
14168:                 }
14169:             }
14170:         } elsif ($item eq 'datelocale_def') {
14171:             if ($newvalues{$item} ne '') {
14172:                 my @datelocale_ids = DateTime::Locale->ids();
14173:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
14174:                     push(@errors,$item);
14175:                 }
14176:             }
14177:         } elsif ($item eq 'portal_def') {
14178:             if ($newvalues{$item} ne '') {
14179:                 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])\/?$/) {
14180:                     push(@errors,$item);
14181:                 }
14182:             }
14183:         }
14184:         if (grep(/^\Q$item\E$/,@errors)) {
14185:             $newvalues{$item} = $domdefaults{$item};
14186:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
14187:             $changes{$item} = 1;
14188:         }
14189:         $domdefaults{$item} = $newvalues{$item};
14190:     }
14191:     my %staticdefaults = (
14192:                            'intauth_cost'   => 10,
14193:                            'intauth_check'  => 0,
14194:                            'intauth_switch' => 0,
14195:                          );
14196:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
14197:         if (exists($domdefaults{$item})) {
14198:             $newvalues{$item} = $domdefaults{$item};
14199:         } else {
14200:             $newvalues{$item} = $staticdefaults{$item};
14201:         }
14202:     }
14203:     my %defaults_hash = (
14204:                          defaults => \%newvalues,
14205:                         );
14206:     my $title = &defaults_titles();
14207: 
14208:     my $currinststatus;
14209:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
14210:         $currinststatus = $domconfig{'inststatus'};
14211:     } else {
14212:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14213:         $currinststatus = {
14214:                              inststatustypes => $usertypes,
14215:                              inststatusorder => $types,
14216:                              inststatusguest => [],
14217:                           };
14218:     }
14219:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
14220:     my @allpos;
14221:     my %alltypes;
14222:     my @inststatusguest;
14223:     if (ref($currinststatus) eq 'HASH') {
14224:         if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
14225:             foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
14226:                 unless (grep(/^\Q$type\E$/,@todelete)) {
14227:                     push(@inststatusguest,$type);
14228:                 }
14229:             }
14230:         }
14231:     }
14232:     my ($currtitles,$currorder);
14233:     if (ref($currinststatus) eq 'HASH') {
14234:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
14235:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
14236:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
14237:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
14238:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
14239:                     }
14240:                 }
14241:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
14242:                     my $position = $env{'form.inststatus_pos_'.$type};
14243:                     $position =~ s/\D+//g;
14244:                     $allpos[$position] = $type;
14245:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
14246:                     $alltypes{$type} =~ s/`//g;
14247:                 }
14248:             }
14249:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
14250:             $currtitles =~ s/,$//;
14251:         }
14252:     }
14253:     if ($env{'form.addinststatus'}) {
14254:         my $newtype = $env{'form.addinststatus'};
14255:         $newtype =~ s/\W//g;
14256:         unless (exists($alltypes{$newtype})) {
14257:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
14258:             $alltypes{$newtype} =~ s/`//g; 
14259:             my $position = $env{'form.addinststatus_pos'};
14260:             $position =~ s/\D+//g;
14261:             if ($position ne '') {
14262:                 $allpos[$position] = $newtype;
14263:             }
14264:         }
14265:     }
14266:     my @orderedstatus;
14267:     foreach my $type (@allpos) {
14268:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
14269:             push(@orderedstatus,$type);
14270:         }
14271:     }
14272:     foreach my $type (keys(%alltypes)) {
14273:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
14274:             delete($alltypes{$type});
14275:         }
14276:     }
14277:     $defaults_hash{'inststatus'} = {
14278:                                      inststatustypes => \%alltypes,
14279:                                      inststatusorder => \@orderedstatus,
14280:                                      inststatusguest => \@inststatusguest,
14281:                                    };
14282:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
14283:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
14284:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
14285:         }
14286:     }
14287:     if ($currorder ne join(',',@orderedstatus)) {
14288:         $changes{'inststatus'}{'inststatusorder'} = 1;
14289:     }
14290:     my $newtitles;
14291:     foreach my $item (@orderedstatus) {
14292:         $newtitles .= $alltypes{$item}.',';
14293:     }
14294:     $newtitles =~ s/,$//;
14295:     if ($currtitles ne $newtitles) {
14296:         $changes{'inststatus'}{'inststatustypes'} = 1;
14297:     }
14298:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
14299:                                              $dom);
14300:     if ($putresult eq 'ok') {
14301:         if (keys(%changes) > 0) {
14302:             $resulttext = &mt('Changes made:').'<ul>';
14303:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
14304:             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";
14305:             foreach my $item (sort(keys(%changes))) {
14306:                 if ($item eq 'inststatus') {
14307:                     if (ref($changes{'inststatus'}) eq 'HASH') {
14308:                         if (@orderedstatus) {
14309:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
14310:                             foreach my $type (@orderedstatus) { 
14311:                                 $resulttext .= $alltypes{$type}.', ';
14312:                             }
14313:                             $resulttext =~ s/, $//;
14314:                             $resulttext .= '</li>';
14315:                         } else {
14316:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
14317:                         }
14318:                     }
14319:                 } else {
14320:                     my $value = $env{'form.'.$item};
14321:                     if ($value eq '') {
14322:                         $value = &mt('none');
14323:                     } elsif ($item eq 'auth_def') {
14324:                         my %authnames = &authtype_names();
14325:                         my %shortauth = (
14326:                                           internal   => 'int',
14327:                                           krb4       => 'krb4',
14328:                                           krb5       => 'krb5',
14329:                                           localauth  => 'loc',
14330:                         );
14331:                         $value = $authnames{$shortauth{$value}};
14332:                     }
14333:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
14334:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
14335:                 }
14336:             }
14337:             $resulttext .= '</ul>';
14338:             $mailmsgtext .= "\n";
14339:             my $cachetime = 24*60*60;
14340:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14341:             if (ref($lastactref) eq 'HASH') {
14342:                 $lastactref->{'domdefaults'} = 1;
14343:             }
14344:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
14345:                 my $notify = 1;
14346:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
14347:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
14348:                         $notify = 0;
14349:                     }
14350:                 }
14351:                 if ($notify) {
14352:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
14353:                                                "LON-CAPA Domain Settings Change - $dom",
14354:                                                $mailmsgtext);
14355:                 }
14356:             }
14357:         } else {
14358:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
14359:         }
14360:     } else {
14361:         $resulttext = '<span class="LC_error">'.
14362:             &mt('An error occurred: [_1]',$putresult).'</span>';
14363:     }
14364:     if (@errors > 0) {
14365:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
14366:         foreach my $item (@errors) {
14367:             $resulttext .= ' "'.$title->{$item}.'",';
14368:         }
14369:         $resulttext =~ s/,$//;
14370:     }
14371:     return $resulttext;
14372: }
14373: 
14374: sub modify_scantron {
14375:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
14376:     my ($resulttext,%confhash,%changes,$errors);
14377:     my $custom = 'custom.tab';
14378:     my $default = 'default.tab';
14379:     my $servadm = $r->dir_config('lonAdmEMail');
14380:     my ($configuserok,$author_ok,$switchserver) =
14381:         &config_check($dom,$confname,$servadm);
14382:     if ($env{'form.scantronformat.filename'} ne '') {
14383:         my $error;
14384:         if ($configuserok eq 'ok') {
14385:             if ($switchserver) {
14386:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
14387:             } else {
14388:                 if ($author_ok eq 'ok') {
14389:                     my ($result,$scantronurl) =
14390:                         &publishlogo($r,'upload','scantronformat',$dom,
14391:                                      $confname,'scantron','','',$custom);
14392:                     if ($result eq 'ok') {
14393:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
14394:                         $changes{'scantronformat'} = 1;
14395:                     } else {
14396:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
14397:                     }
14398:                 } else {
14399:                     $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);
14400:                 }
14401:             }
14402:         } else {
14403:             $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);
14404:         }
14405:         if ($error) {
14406:             &Apache::lonnet::logthis($error);
14407:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14408:         }
14409:     }
14410:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14411:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
14412:             if ($env{'form.scantronformat_del'}) {
14413:                 $confhash{'scantron'}{'scantronformat'} = '';
14414:                 $changes{'scantronformat'} = 1;
14415:             } else {
14416:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
14417:             }
14418:         }
14419:     }
14420:     my @options = ('hdr','pad','rem');
14421:     my @fields = &scantroncsv_fields();
14422:     my %titles = &scantronconfig_titles();
14423:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
14424:     my ($newdat,$currdat,%newcol,%currcol);
14425:     if (grep(/^dat$/,@formats)) {
14426:         $confhash{'scantron'}{config}{dat} = 1;
14427:         $newdat = 1;
14428:     } else {
14429:         $newdat = 0;
14430:     }
14431:     if (grep(/^csv$/,@formats)) {
14432:         my %bynum;
14433:         foreach my $field (@fields) {
14434:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
14435:                 my $posscol = $1;
14436:                 if (($posscol < 20) && (!$bynum{$posscol})) {
14437:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
14438:                     $bynum{$posscol} = $field;
14439:                     $newcol{$field} = $posscol;
14440:                 }
14441:             }
14442:         }
14443:         if (keys(%newcol)) {
14444:             foreach my $option (@options) {
14445:                 if ($env{'form.scantroncsv_'.$option}) {
14446:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
14447:                 }
14448:             }
14449:         }
14450:     }
14451:     $currdat = 1;
14452:     if (ref($domconfig{'scantron'}) eq 'HASH') {
14453:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
14454:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
14455:                 $currdat = 0;
14456:             }
14457:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
14458:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
14459:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
14460:                 }
14461:             }
14462:         }
14463:     }
14464:     if ($currdat != $newdat) {
14465:         $changes{'config'} = 1;
14466:     } else {
14467:         foreach my $field (@fields) {
14468:             if ($currcol{$field} ne '') {
14469:                 if ($currcol{$field} ne $newcol{$field}) {
14470:                     $changes{'config'} = 1;
14471:                     last;
14472:                 }
14473:             } elsif ($newcol{$field} ne '') {
14474:                 $changes{'config'} = 1;
14475:                 last;
14476:             }
14477:         }
14478:     }
14479:     if (keys(%confhash) > 0) {
14480:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
14481:                                                  $dom);
14482:         if ($putresult eq 'ok') {
14483:             if (keys(%changes) > 0) {
14484:                 if (ref($confhash{'scantron'}) eq 'HASH') {
14485:                     $resulttext = &mt('Changes made:').'<ul>';
14486:                     if ($changes{'scantronformat'}) {
14487:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
14488:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
14489:                         } else {
14490:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
14491:                         }
14492:                     }
14493:                     if ($changes{'config'}) {
14494:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
14495:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
14496:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
14497:                             }
14498:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
14499:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
14500:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
14501:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
14502:                                         foreach my $field (@fields) {
14503:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
14504:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
14505:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
14506:                                             }
14507:                                         }
14508:                                         $resulttext .= '</ul></li>';
14509:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
14510:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
14511:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
14512:                                                 foreach my $option (@options) {
14513:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
14514:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
14515:                                                     }
14516:                                                 }
14517:                                                 $resulttext .= '</ul></li>';
14518:                                             }
14519:                                         }
14520:                                     }
14521:                                 }
14522:                             }
14523:                         } else {
14524:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
14525:                         }
14526:                     }
14527:                     $resulttext .= '</ul>';
14528:                 } else {
14529:                     $resulttext = &mt('Changes made to bubblesheet format file.');
14530:                 }
14531:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14532:                 if (ref($lastactref) eq 'HASH') {
14533:                     $lastactref->{'domainconfig'} = 1;
14534:                 }
14535:             } else {
14536:                 $resulttext = &mt('No changes made to bubblesheet format settings');
14537:             }
14538:         } else {
14539:             $resulttext = '<span class="LC_error">'.
14540:                 &mt('An error occurred: [_1]',$putresult).'</span>';
14541:         }
14542:     } else {
14543:         $resulttext = &mt('No changes made to bubblesheet format settings');
14544:     }
14545:     if ($errors) {
14546:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14547:                        $errors.'</ul>';
14548:     }
14549:     return $resulttext;
14550: }
14551: 
14552: sub modify_coursecategories {
14553:     my ($dom,$lastactref,%domconfig) = @_;
14554:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
14555:         $cathash);
14556:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
14557:     my @catitems = ('unauth','auth');
14558:     my @cattypes = ('std','domonly','codesrch','none');
14559:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
14560:         $cathash = $domconfig{'coursecategories'}{'cats'};
14561:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
14562:             $changes{'togglecats'} = 1;
14563:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
14564:         }
14565:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
14566:             $changes{'categorize'} = 1;
14567:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
14568:         }
14569:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
14570:             $changes{'togglecatscomm'} = 1;
14571:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
14572:         }
14573:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
14574:             $changes{'categorizecomm'} = 1;
14575:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
14576:         }
14577:         foreach my $item (@catitems) {
14578:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14579:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
14580:                     $changes{$item} = 1;
14581:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14582:                 }
14583:             }
14584:         }
14585:     } else {
14586:         $changes{'togglecats'} = 1;
14587:         $changes{'categorize'} = 1;
14588:         $changes{'togglecatscomm'} = 1;
14589:         $changes{'categorizecomm'} = 1;
14590:         $domconfig{'coursecategories'} = {
14591:                                              togglecats => $env{'form.togglecats'},
14592:                                              categorize => $env{'form.categorize'},
14593:                                              togglecatscomm => $env{'form.togglecatscomm'},
14594:                                              categorizecomm => $env{'form.categorizecomm'},
14595:                                          };
14596:         foreach my $item (@catitems) {
14597:             if ($env{'form.coursecat_'.$item} ne 'std') {
14598:                 $changes{$item} = 1;
14599:             }
14600:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
14601:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
14602:             }
14603:         }
14604:     }
14605:     if (ref($cathash) eq 'HASH') {
14606:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
14607:             push (@deletecategory,'instcode::0');
14608:         }
14609:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
14610:             push(@deletecategory,'communities::0');
14611:         }
14612:     }
14613:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
14614:     if (ref($cathash) eq 'HASH') {
14615:         if (@deletecategory > 0) {
14616:             #FIXME Need to remove category from all courses using a deleted category 
14617:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
14618:             foreach my $item (@deletecategory) {
14619:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
14620:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
14621:                     $deletions{$item} = 1;
14622:                     &recurse_cat_deletes($item,$cathash,\%deletions);
14623:                 }
14624:             }
14625:         }
14626:         foreach my $item (keys(%{$cathash})) {
14627:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14628:             if ($cathash->{$item} ne $env{'form.'.$item}) {
14629:                 $reorderings{$item} = 1;
14630:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
14631:             }
14632:             if ($env{'form.addcategory_name_'.$item} ne '') {
14633:                 my $newcat = $env{'form.addcategory_name_'.$item};
14634:                 my $newdepth = $depth+1;
14635:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14636:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
14637:                 $adds{$newitem} = 1; 
14638:             }
14639:             if ($env{'form.subcat_'.$item} ne '') {
14640:                 my $newcat = $env{'form.subcat_'.$item};
14641:                 my $newdepth = $depth+1;
14642:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
14643:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
14644:                 $adds{$newitem} = 1;
14645:             }
14646:         }
14647:     }
14648:     if ($env{'form.instcode'} eq '1') {
14649:         if (ref($cathash) eq 'HASH') {
14650:             my $newitem = 'instcode::0';
14651:             if ($cathash->{$newitem} eq '') {  
14652:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14653:                 $adds{$newitem} = 1;
14654:             }
14655:         } else {
14656:             my $newitem = 'instcode::0';
14657:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
14658:             $adds{$newitem} = 1;
14659:         }
14660:     }
14661:     if ($env{'form.communities'} eq '1') {
14662:         if (ref($cathash) eq 'HASH') {
14663:             my $newitem = 'communities::0';
14664:             if ($cathash->{$newitem} eq '') {
14665:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
14666:                 $adds{$newitem} = 1;
14667:             }
14668:         } else {
14669:             my $newitem = 'communities::0';
14670:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
14671:             $adds{$newitem} = 1;
14672:         }
14673:     }
14674:     if ($env{'form.addcategory_name'} ne '') {
14675:         if (($env{'form.addcategory_name'} ne 'instcode') &&
14676:             ($env{'form.addcategory_name'} ne 'communities')) {
14677:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
14678:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
14679:             $adds{$newitem} = 1;
14680:         }
14681:     }
14682:     my $putresult;
14683:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14684:         if (keys(%deletions) > 0) {
14685:             foreach my $key (keys(%deletions)) {
14686:                 if ($predelallitems{$key} ne '') {
14687:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
14688:                 }
14689:             }
14690:         }
14691:         my (@chkcats,@chktrails,%chkallitems);
14692:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
14693:         if (ref($chkcats[0]) eq 'ARRAY') {
14694:             my $depth = 0;
14695:             my $chg = 0;
14696:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
14697:                 my $name = $chkcats[0][$i];
14698:                 my $item;
14699:                 if ($name eq '') {
14700:                     $chg ++;
14701:                 } else {
14702:                     $item = &escape($name).'::0';
14703:                     if ($chg) {
14704:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
14705:                     }
14706:                     $depth ++; 
14707:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
14708:                     $depth --;
14709:                 }
14710:             }
14711:         }
14712:     }
14713:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14714:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
14715:         if ($putresult eq 'ok') {
14716:             my %title = (
14717:                          togglecats     => 'Show/Hide a course in catalog',
14718:                          categorize     => 'Assign a category to a course',
14719:                          togglecatscomm => 'Show/Hide a community in catalog',
14720:                          categorizecomm => 'Assign a category to a community',
14721:                         );
14722:             my %level = (
14723:                          dom  => 'set in Domain ("Modify Course/Community")',
14724:                          crs  => 'set in Course ("Course Configuration")',
14725:                          comm => 'set in Community ("Community Configuration")',
14726:                          none     => 'No catalog',
14727:                          std      => 'Standard catalog',
14728:                          domonly  => 'Domain-only catalog',
14729:                          codesrch => 'Code search form',
14730:                         );
14731:             $resulttext = &mt('Changes made:').'<ul>';
14732:             if ($changes{'togglecats'}) {
14733:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
14734:             }
14735:             if ($changes{'categorize'}) {
14736:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
14737:             }
14738:             if ($changes{'togglecatscomm'}) {
14739:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
14740:             }
14741:             if ($changes{'categorizecomm'}) {
14742:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
14743:             }
14744:             if ($changes{'unauth'}) {
14745:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
14746:             }
14747:             if ($changes{'auth'}) {
14748:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
14749:             }
14750:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
14751:                 my $cathash;
14752:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
14753:                     $cathash = $domconfig{'coursecategories'}{'cats'};
14754:                 } else {
14755:                     $cathash = {};
14756:                 } 
14757:                 my (@cats,@trails,%allitems);
14758:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
14759:                 if (keys(%deletions) > 0) {
14760:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
14761:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
14762:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
14763:                     }
14764:                     $resulttext .= '</ul></li>';
14765:                 }
14766:                 if (keys(%reorderings) > 0) {
14767:                     my %sort_by_trail;
14768:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
14769:                     foreach my $key (keys(%reorderings)) {
14770:                         if ($allitems{$key} ne '') {
14771:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
14772:                         }
14773:                     }
14774:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
14775:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
14776:                     }
14777:                     $resulttext .= '</ul></li>';
14778:                 }
14779:                 if (keys(%adds) > 0) {
14780:                     my %sort_by_trail;
14781:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
14782:                     foreach my $key (keys(%adds)) {
14783:                         if ($allitems{$key} ne '') {
14784:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
14785:                         }
14786:                     }
14787:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
14788:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
14789:                     }
14790:                     $resulttext .= '</ul></li>';
14791:                 }
14792:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
14793:                 if (ref($lastactref) eq 'HASH') {
14794:                     $lastactref->{'cats'} = 1;
14795:                 }
14796:             }
14797:             $resulttext .= '</ul>';
14798:             if ($changes{'unauth'} || $changes{'auth'}) {
14799:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
14800:                 if ($changes{'auth'}) {
14801:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
14802:                 }
14803:                 if ($changes{'unauth'}) {
14804:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
14805:                 }
14806:                 my $cachetime = 24*60*60;
14807:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14808:                 if (ref($lastactref) eq 'HASH') {
14809:                     $lastactref->{'domdefaults'} = 1;
14810:                 }
14811:             }
14812:         } else {
14813:             $resulttext = '<span class="LC_error">'.
14814:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14815:         }
14816:     } else {
14817:         $resulttext = &mt('No changes made to course and community categories');
14818:     }
14819:     return $resulttext;
14820: }
14821: 
14822: sub modify_serverstatuses {
14823:     my ($dom,%domconfig) = @_;
14824:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
14825:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
14826:         %currserverstatus = %{$domconfig{'serverstatuses'}};
14827:     }
14828:     my @pages = &serverstatus_pages();
14829:     foreach my $type (@pages) {
14830:         $newserverstatus{$type}{'namedusers'} = '';
14831:         $newserverstatus{$type}{'machines'} = '';
14832:         if (defined($env{'form.'.$type.'_namedusers'})) {
14833:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
14834:             my @okusers;
14835:             foreach my $user (@users) {
14836:                 my ($uname,$udom) = split(/:/,$user);
14837:                 if (($udom =~ /^$match_domain$/) &&   
14838:                     (&Apache::lonnet::domain($udom)) &&
14839:                     ($uname =~ /^$match_username$/)) {
14840:                     if (!grep(/^\Q$user\E/,@okusers)) {
14841:                         push(@okusers,$user);
14842:                     }
14843:                 }
14844:             }
14845:             if (@okusers > 0) {
14846:                  @okusers = sort(@okusers);
14847:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
14848:             }
14849:         }
14850:         if (defined($env{'form.'.$type.'_machines'})) {
14851:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
14852:             my @okmachines;
14853:             foreach my $ip (@machines) {
14854:                 my @parts = split(/\./,$ip);
14855:                 next if (@parts < 4);
14856:                 my $badip = 0;
14857:                 for (my $i=0; $i<4; $i++) {
14858:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
14859:                         $badip = 1;
14860:                         last;
14861:                     }
14862:                 }
14863:                 if (!$badip) {
14864:                     push(@okmachines,$ip);     
14865:                 }
14866:             }
14867:             @okmachines = sort(@okmachines);
14868:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
14869:         }
14870:     }
14871:     my %serverstatushash =  (
14872:                                 serverstatuses => \%newserverstatus,
14873:                             );
14874:     foreach my $type (@pages) {
14875:         foreach my $setting ('namedusers','machines') {
14876:             my (@current,@new);
14877:             if (ref($currserverstatus{$type}) eq 'HASH') {
14878:                 if ($currserverstatus{$type}{$setting} ne '') { 
14879:                     @current = split(/,/,$currserverstatus{$type}{$setting});
14880:                 }
14881:             }
14882:             if ($newserverstatus{$type}{$setting} ne '') {
14883:                 @new = split(/,/,$newserverstatus{$type}{$setting});
14884:             }
14885:             if (@current > 0) {
14886:                 if (@new > 0) {
14887:                     foreach my $item (@current) {
14888:                         if (!grep(/^\Q$item\E$/,@new)) {
14889:                             $changes{$type}{$setting} = 1;
14890:                             last;
14891:                         }
14892:                     }
14893:                     foreach my $item (@new) {
14894:                         if (!grep(/^\Q$item\E$/,@current)) {
14895:                             $changes{$type}{$setting} = 1;
14896:                             last;
14897:                         }
14898:                     }
14899:                 } else {
14900:                     $changes{$type}{$setting} = 1;
14901:                 }
14902:             } elsif (@new > 0) {
14903:                 $changes{$type}{$setting} = 1;
14904:             }
14905:         }
14906:     }
14907:     if (keys(%changes) > 0) {
14908:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
14909:         my $putresult = &Apache::lonnet::put_dom('configuration',
14910:                                                  \%serverstatushash,$dom);
14911:         if ($putresult eq 'ok') {
14912:             $resulttext .= &mt('Changes made:').'<ul>';
14913:             foreach my $type (@pages) {
14914:                 if (ref($changes{$type}) eq 'HASH') {
14915:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
14916:                     if ($changes{$type}{'namedusers'}) {
14917:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
14918:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
14919:                         } else {
14920:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
14921:                         }
14922:                     }
14923:                     if ($changes{$type}{'machines'}) {
14924:                         if ($newserverstatus{$type}{'machines'} eq '') {
14925:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
14926:                         } else {
14927:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
14928:                         }
14929: 
14930:                     }
14931:                     $resulttext .= '</ul></li>';
14932:                 }
14933:             }
14934:             $resulttext .= '</ul>';
14935:         } else {
14936:             $resulttext = '<span class="LC_error">'.
14937:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
14938: 
14939:         }
14940:     } else {
14941:         $resulttext = &mt('No changes made to access to server status pages');
14942:     }
14943:     return $resulttext;
14944: }
14945: 
14946: sub modify_helpsettings {
14947:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
14948:     my ($resulttext,$errors,%changes,%helphash);
14949:     my %defaultchecked = ('submitbugs' => 'on');
14950:     my @offon = ('off','on');
14951:     my @toggles = ('submitbugs');
14952:     my %current = ('submitbugs' => '',
14953:                    'adhoc'      => {},
14954:                   );
14955:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
14956:         %current = %{$domconfig{'helpsettings'}};
14957:     }
14958:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14959:     foreach my $item (@toggles) {
14960:         if ($defaultchecked{$item} eq 'on') { 
14961:             if ($current{$item} eq '') {
14962:                 if ($env{'form.'.$item} eq '0') {
14963:                     $changes{$item} = 1;
14964:                 }
14965:             } elsif ($current{$item} ne $env{'form.'.$item}) {
14966:                 $changes{$item} = 1;
14967:             }
14968:         } elsif ($defaultchecked{$item} eq 'off') {
14969:             if ($current{$item} eq '') {
14970:                 if ($env{'form.'.$item} eq '1') {
14971:                     $changes{$item} = 1;
14972:                 }
14973:             } elsif ($current{$item} ne $env{'form.'.$item}) {
14974:                 $changes{$item} = 1;
14975:             }
14976:         }
14977:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
14978:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
14979:         }
14980:     }
14981:     my $maxnum = $env{'form.helproles_maxnum'};
14982:     my $confname = $dom.'-domainconfig';
14983:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
14984:     my (@allpos,%newsettings,%changedprivs,$newrole);
14985:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14986:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
14987:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
14988:     my %lt = &Apache::lonlocal::texthash(
14989:                     s      => 'system',
14990:                     d      => 'domain',
14991:                     order  => 'Display order',
14992:                     access => 'Role usage',
14993:                     all    => 'All with domain helpdesk or helpdesk assistant role',
14994:                     dh     => 'All with domain helpdesk role',
14995:                     da     => 'All with domain helpdesk assistant role',
14996:                     none   => 'None',
14997:                     status => 'Determined based on institutional status',
14998:                     inc    => 'Include all, but exclude specific personnel',
14999:                     exc    => 'Exclude all, but include specific personnel',
15000:     );
15001:     for (my $num=0; $num<=$maxnum; $num++) {
15002:         my ($prefix,$identifier,$rolename,%curr);
15003:         if ($num == $maxnum) {
15004:             next unless ($env{'form.newcusthelp'} == $maxnum);
15005:             $identifier = 'custhelp'.$num;
15006:             $prefix = 'helproles_'.$num;
15007:             $rolename = $env{'form.custhelpname'.$num};
15008:             $rolename=~s/[^A-Za-z0-9]//gs;
15009:             next if ($rolename eq '');
15010:             next if (exists($existing{'rolesdef_'.$rolename}));
15011:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15012:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15013:                                                      $newprivs{'c'},$confname,$dom);
15014:             if ($result ne 'ok') {
15015:                 $errors .= '<li><span class="LC_error">'.
15016:                            &mt('An error occurred storing the new custom role: [_1]',
15017:                            $result).'</span></li>';
15018:                 next;
15019:             } else {
15020:                 $changedprivs{$rolename} = \%newprivs;
15021:                 $newrole = $rolename;
15022:             }
15023:         } else {
15024:             $prefix = 'helproles_'.$num;
15025:             $rolename = $env{'form.'.$prefix};
15026:             next if ($rolename eq '');
15027:             next unless (exists($existing{'rolesdef_'.$rolename}));
15028:             $identifier = 'custhelp'.$num;
15029:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
15030:             my %currprivs;
15031:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
15032:                 split(/\_/,$existing{'rolesdef_'.$rolename});
15033:             foreach my $level ('c','d','s') {
15034:                 if ($newprivs{$level} ne $currprivs{$level}) {
15035:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
15036:                                                              $newprivs{'c'},$confname,$dom);
15037:                     if ($result ne 'ok') {
15038:                         $errors .= '<li><span class="LC_error">'.
15039:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
15040:                                        $rolename,$result).'</span></li>';
15041:                     } else {
15042:                         $changedprivs{$rolename} = \%newprivs;
15043:                     }
15044:                     last;
15045:                 }
15046:             }
15047:             if (ref($current{'adhoc'}) eq 'HASH') {
15048:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15049:                     %curr = %{$current{'adhoc'}{$rolename}};
15050:                 }
15051:             }
15052:         }
15053:         my $newpos = $env{'form.'.$prefix.'_pos'};
15054:         $newpos =~ s/\D+//g;
15055:         $allpos[$newpos] = $rolename;
15056:         my $newdesc = $env{'form.'.$prefix.'_desc'};
15057:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
15058:         if ($curr{'desc'}) {
15059:             if ($curr{'desc'} ne $newdesc) {
15060:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
15061:                 $newsettings{$rolename}{'desc'} = $newdesc;
15062:             }
15063:         } elsif ($newdesc ne '') {
15064:             $changes{'customrole'}{$rolename}{'desc'} = 1;
15065:             $newsettings{$rolename}{'desc'} = $newdesc;
15066:         }
15067:         my $access = $env{'form.'.$prefix.'_access'};
15068:         if (grep(/^\Q$access\E$/,@accesstypes)) {
15069:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
15070:             if ($access eq 'status') {
15071:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
15072:                 if (scalar(@statuses) == 0) {
15073:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
15074:                 } else {
15075:                     my (@shownstatus,$numtypes);
15076:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15077:                     if (ref($types) eq 'ARRAY') {
15078:                         $numtypes = scalar(@{$types});
15079:                         foreach my $type (sort(@statuses)) {
15080:                             if ($type eq 'default') {
15081:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15082:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
15083:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
15084:                                 push(@shownstatus,$usertypes->{$type});
15085:                             }
15086:                         }
15087:                     }
15088:                     if (grep(/^default$/,@statuses)) {
15089:                         push(@shownstatus,$othertitle);
15090:                     }
15091:                     if (scalar(@shownstatus) == 1+$numtypes) {
15092:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
15093:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
15094:                     } else {
15095:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
15096:                         if (ref($curr{'status'}) eq 'ARRAY') {
15097:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15098:                             if (@diffs) {
15099:                                 $changes{'customrole'}{$rolename}{$access} = 1;
15100:                             }
15101:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15102:                             $changes{'customrole'}{$rolename}{$access} = 1;
15103:                         }
15104:                     }
15105:                 }
15106:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
15107:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
15108:                 my @newspecstaff;
15109:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
15110:                 foreach my $person (sort(@personnel)) {
15111:                     if ($domhelpdesk{$person}) {
15112:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
15113:                     }
15114:                 }
15115:                 if (ref($curr{$access}) eq 'ARRAY') {
15116:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
15117:                     if (@diffs) {
15118:                         $changes{'customrole'}{$rolename}{$access} = 1;
15119:                     }
15120:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15121:                     $changes{'customrole'}{$rolename}{$access} = 1;
15122:                 }
15123:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
15124:                     my ($uname,$udom) = split(/:/,$person);
15125:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
15126:                 }
15127:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
15128:             }
15129:         } else {
15130:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
15131:         }
15132:         unless ($curr{'access'} eq $access) {
15133:             $changes{'customrole'}{$rolename}{'access'} = 1;
15134:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
15135:         }
15136:     }
15137:     if (@allpos > 0) {
15138:         my $idx = 0;
15139:         foreach my $rolename (@allpos) {
15140:             if ($rolename ne '') {
15141:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
15142:                 if (ref($current{'adhoc'}) eq 'HASH') {
15143:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
15144:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
15145:                             $changes{'customrole'}{$rolename}{'order'} = 1;
15146:                             $newsettings{$rolename}{'order'} = $idx+1;
15147:                         }
15148:                     }
15149:                 }
15150:                 $idx ++;
15151:             }
15152:         }
15153:     }
15154:     my $putresult;
15155:     if (keys(%changes) > 0) {
15156:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
15157:         if ($putresult eq 'ok') {
15158:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
15159:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
15160:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
15161:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
15162:                 }
15163:             }
15164:             my $cachetime = 24*60*60;
15165:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15166:             if (ref($lastactref) eq 'HASH') {
15167:                 $lastactref->{'domdefaults'} = 1;
15168:             }
15169:         } else {
15170:             $errors .= '<li><span class="LC_error">'.
15171:                        &mt('An error occurred storing the settings: [_1]',
15172:                            $putresult).'</span></li>';
15173:         }
15174:     }
15175:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
15176:         $resulttext = &mt('Changes made:').'<ul>';
15177:         my (%shownprivs,@levelorder);
15178:         @levelorder = ('c','d','s');
15179:         if ((keys(%changes)) && ($putresult eq 'ok')) {
15180:             foreach my $item (sort(keys(%changes))) {
15181:                 if ($item eq 'submitbugs') {
15182:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
15183:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
15184:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
15185:                 } elsif ($item eq 'customrole') {
15186:                     if (ref($changes{'customrole'}) eq 'HASH') {
15187:                         my @keyorder = ('order','desc','access','status','exc','inc');
15188:                         my %keytext = &Apache::lonlocal::texthash(
15189:                                                                    order  => 'Order',
15190:                                                                    desc   => 'Role description',
15191:                                                                    access => 'Role usage',
15192:                                                                    status => 'Allowed institutional types',
15193:                                                                    exc    => 'Allowed personnel',
15194:                                                                    inc    => 'Disallowed personnel',
15195:                         );
15196:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
15197:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
15198:                                 if ($role eq $newrole) {
15199:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
15200:                                                               $role).'<ul>';
15201:                                 } else {
15202:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15203:                                                               $role).'<ul>';
15204:                                 }
15205:                                 foreach my $key (@keyorder) {
15206:                                     if ($changes{'customrole'}{$role}{$key}) {
15207:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
15208:                                                                   $keytext{$key},$newsettings{$role}{$key}).
15209:                                                        '</li>';
15210:                                     }
15211:                                 }
15212:                                 if (ref($changedprivs{$role}) eq 'HASH') {
15213:                                     $shownprivs{$role} = 1;
15214:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
15215:                                     foreach my $level (@levelorder) {
15216:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15217:                                             next if ($item eq '');
15218:                                             my ($priv) = split(/\&/,$item,2);
15219:                                             if (&Apache::lonnet::plaintext($priv)) {
15220:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15221:                                                 unless ($level eq 'c') {
15222:                                                     $resulttext .= ' ('.$lt{$level}.')';
15223:                                                 }
15224:                                                 $resulttext .= '</li>';
15225:                                             }
15226:                                         }
15227:                                     }
15228:                                     $resulttext .= '</ul>';
15229:                                 }
15230:                                 $resulttext .= '</ul></li>';
15231:                             }
15232:                         }
15233:                     }
15234:                 }
15235:             }
15236:         }
15237:         if (keys(%changedprivs)) {
15238:             foreach my $role (sort(keys(%changedprivs))) {
15239:                 unless ($shownprivs{$role}) {
15240:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
15241:                                               $role).'<ul>'.
15242:                                    '<li>'.&mt('Privileges set to :').'<ul>';
15243:                     foreach my $level (@levelorder) {
15244:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
15245:                             next if ($item eq '');
15246:                             my ($priv) = split(/\&/,$item,2);
15247:                             if (&Apache::lonnet::plaintext($priv)) {
15248:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
15249:                                 unless ($level eq 'c') {
15250:                                     $resulttext .= ' ('.$lt{$level}.')';
15251:                                 }
15252:                                 $resulttext .= '</li>';
15253:                             }
15254:                         }
15255:                     }
15256:                     $resulttext .= '</ul></li></ul></li>';
15257:                 }
15258:             }
15259:         }
15260:         $resulttext .= '</ul>';
15261:     } else {
15262:         $resulttext = &mt('No changes made to help settings');
15263:     }
15264:     if ($errors) {
15265:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
15266:                                     $errors.'</ul>';
15267:     }
15268:     return $resulttext;
15269: }
15270: 
15271: sub modify_coursedefaults {
15272:     my ($dom,$lastactref,%domconfig) = @_;
15273:     my ($resulttext,$errors,%changes,%defaultshash);
15274:     my %defaultchecked = (
15275:                            'uselcmath'       => 'on',
15276:                            'usejsme'         => 'on'
15277:                          );
15278:     my @toggles = ('uselcmath','usejsme');
15279:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
15280:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
15281:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
15282:     my @types = ('official','unofficial','community','textbook');
15283:     my %staticdefaults = (
15284:                            anonsurvey_threshold => 10,
15285:                            uploadquota          => 500,
15286:                            postsubmit           => 60,
15287:                            mysqltables          => 172800,
15288:                          );
15289:     my %texoptions = (
15290:                         MathJax  => 'MathJax',
15291:                         mimetex  => &mt('Convert to Images'),
15292:                         tth      => &mt('TeX to HTML'),
15293:                      );
15294:     $defaultshash{'coursedefaults'} = {};
15295: 
15296:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
15297:         if ($domconfig{'coursedefaults'} eq '') {
15298:             $domconfig{'coursedefaults'} = {};
15299:         }
15300:     }
15301: 
15302:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
15303:         foreach my $item (@toggles) {
15304:             if ($defaultchecked{$item} eq 'on') {
15305:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
15306:                     ($env{'form.'.$item} eq '0')) {
15307:                     $changes{$item} = 1;
15308:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
15309:                     $changes{$item} = 1;
15310:                 }
15311:             } elsif ($defaultchecked{$item} eq 'off') {
15312:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
15313:                     ($env{'form.'.$item} eq '1')) {
15314:                     $changes{$item} = 1;
15315:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
15316:                     $changes{$item} = 1;
15317:                 }
15318:             }
15319:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
15320:         }
15321:         foreach my $item (@numbers) {
15322:             my ($currdef,$newdef);
15323:             $newdef = $env{'form.'.$item};
15324:             if ($item eq 'anonsurvey_threshold') {
15325:                 $currdef = $domconfig{'coursedefaults'}{$item};
15326:                 $newdef =~ s/\D//g;
15327:                 if ($newdef eq '' || $newdef < 1) {
15328:                     $newdef = 1;
15329:                 }
15330:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
15331:             } else {
15332:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
15333:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
15334:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
15335:                 }
15336:                 $newdef =~ s/[^\w.\-]//g;
15337:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
15338:             }
15339:             if ($currdef ne $newdef) {
15340:                 if ($item eq 'anonsurvey_threshold') {
15341:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
15342:                         $changes{$item} = 1;
15343:                     }
15344:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
15345:                     my $setting = $1;
15346:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
15347:                         $changes{$setting} = 1;
15348:                     }
15349:                 }
15350:             }
15351:         }
15352:         my $texengine;
15353:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
15354:             $texengine = $env{'form.texengine'};
15355:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
15356:             if ($currdef eq '') {
15357:                 unless ($texengine eq $Apache::lonnet::deftex) {
15358:                     $changes{'texengine'} = 1;
15359:                 }
15360:             } elsif ($currdef ne $texengine) {
15361:                 $changes{'texengine'} = 1;
15362:             }
15363:         }
15364:         if ($texengine ne '') {
15365:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
15366:         }
15367:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
15368:         my @currclonecode;
15369:         if (ref($currclone) eq 'HASH') {
15370:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
15371:                 @currclonecode = @{$currclone->{'instcode'}};
15372:             }
15373:         }
15374:         my $newclone;
15375:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
15376:             $newclone = $env{'form.canclone'};
15377:         }
15378:         if ($newclone eq 'instcode') {
15379:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
15380:             my (%codedefaults,@code_order,@clonecode);
15381:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
15382:                                                     \@code_order);
15383:             foreach my $item (@code_order) {
15384:                 if (grep(/^\Q$item\E$/,@newcodes)) {
15385:                     push(@clonecode,$item);
15386:                 }
15387:             }
15388:             if (@clonecode) {
15389:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
15390:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
15391:                 if (@diffs) {
15392:                     $changes{'canclone'} = 1;
15393:                 }
15394:             } else {
15395:                 $newclone eq '';
15396:             }
15397:         } elsif ($newclone ne '') {
15398:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
15399:         }
15400:         if ($newclone ne $currclone) {
15401:             $changes{'canclone'} = 1;
15402:         }
15403:         my %credits;
15404:         foreach my $type (@types) {
15405:             unless ($type eq 'community') {
15406:                 $credits{$type} = $env{'form.'.$type.'_credits'};
15407:                 $credits{$type} =~ s/[^\d.]+//g;
15408:             }
15409:         }
15410:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
15411:             ($env{'form.coursecredits'} eq '1')) {
15412:             $changes{'coursecredits'} = 1;
15413:             foreach my $type (keys(%credits)) {
15414:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15415:             }
15416:         } else {
15417:             if ($env{'form.coursecredits'} eq '1') {
15418:                 foreach my $type (@types) {
15419:                     unless ($type eq 'community') {
15420:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
15421:                             $changes{'coursecredits'} = 1;
15422:                         }
15423:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
15424:                     }
15425:                 }
15426:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15427:                 foreach my $type (@types) {
15428:                     unless ($type eq 'community') {
15429:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
15430:                             $changes{'coursecredits'} = 1;
15431:                             last;
15432:                         }
15433:                     }
15434:                 }
15435:             }
15436:         }
15437:         if ($env{'form.postsubmit'} eq '1') {
15438:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
15439:             my %currtimeout;
15440:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15441:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
15442:                     $changes{'postsubmit'} = 1;
15443:                 }
15444:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15445:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
15446:                 }
15447:             } else {
15448:                 $changes{'postsubmit'} = 1;
15449:             }
15450:             foreach my $type (@types) {
15451:                 my $timeout = $env{'form.'.$type.'_timeout'};
15452:                 $timeout =~ s/\D//g;
15453:                 if ($timeout == $staticdefaults{'postsubmit'}) {
15454:                     $timeout = '';
15455:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
15456:                     $timeout = '0';
15457:                 }
15458:                 unless ($timeout eq '') {
15459:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
15460:                 }
15461:                 if (exists($currtimeout{$type})) {
15462:                     if ($timeout ne $currtimeout{$type}) {
15463:                         $changes{'postsubmit'} = 1;
15464:                     }
15465:                 } elsif ($timeout ne '') {
15466:                     $changes{'postsubmit'} = 1;
15467:                 }
15468:             }
15469:         } else {
15470:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
15471:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15472:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
15473:                     $changes{'postsubmit'} = 1;
15474:                 }
15475:             } else {
15476:                 $changes{'postsubmit'} = 1;
15477:             }
15478:         }
15479:     }
15480:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15481:                                              $dom);
15482:     if ($putresult eq 'ok') {
15483:         if (keys(%changes) > 0) {
15484:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15485:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
15486:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
15487:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
15488:                 foreach my $item ('uselcmath','usejsme','texengine') {
15489:                     if ($changes{$item}) {
15490:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
15491:                     }
15492:                 }
15493:                 if ($changes{'coursecredits'}) {
15494:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15495:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
15496:                             $domdefaults{$type.'credits'} =
15497:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
15498:                         }
15499:                     }
15500:                 }
15501:                 if ($changes{'postsubmit'}) {
15502:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15503:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
15504:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15505:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
15506:                                 $domdefaults{$type.'postsubtimeout'} =
15507:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15508:                             }
15509:                         }
15510:                     }
15511:                 }
15512:                 if ($changes{'uploadquota'}) {
15513:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15514:                         foreach my $type (@types) {
15515:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
15516:                         }
15517:                     }
15518:                 }
15519:                 if ($changes{'canclone'}) {
15520:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15521:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15522:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
15523:                             if (@clonecodes) {
15524:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
15525:                             }
15526:                         }
15527:                     } else {
15528:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
15529:                     }
15530:                 }
15531:                 my $cachetime = 24*60*60;
15532:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15533:                 if (ref($lastactref) eq 'HASH') {
15534:                     $lastactref->{'domdefaults'} = 1;
15535:                 }
15536:             }
15537:             $resulttext = &mt('Changes made:').'<ul>';
15538:             foreach my $item (sort(keys(%changes))) {
15539:                 if ($item eq 'uselcmath') {
15540:                     if ($env{'form.'.$item} eq '1') {
15541:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
15542:                     } else {
15543:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
15544:                     }
15545:                 } elsif ($item eq 'usejsme') {
15546:                     if ($env{'form.'.$item} eq '1') {
15547:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
15548:                     } else {
15549:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
15550:                     }
15551:                 } elsif ($item eq 'texengine') {
15552:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
15553:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
15554:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
15555:                     }
15556:                 } elsif ($item eq 'anonsurvey_threshold') {
15557:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
15558:                 } elsif ($item eq 'uploadquota') {
15559:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
15560:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
15561:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
15562:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
15563:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
15564: 
15565:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
15566:                                        '</ul>'.
15567:                                        '</li>';
15568:                     } else {
15569:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
15570:                     }
15571:                 } elsif ($item eq 'mysqltables') {
15572:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
15573:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
15574:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
15575:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
15576:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
15577:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
15578:                                        '</ul>'.
15579:                                        '</li>';
15580:                     } else {
15581:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
15582:                     }
15583:                 } elsif ($item eq 'postsubmit') {
15584:                     if ($domdefaults{'postsubmit'} eq 'off') {
15585:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
15586:                     } else {
15587:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
15588:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
15589:                             $resulttext .= &mt('durations:').'<ul>';
15590:                             foreach my $type (@types) {
15591:                                 $resulttext .= '<li>';
15592:                                 my $timeout;
15593:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
15594:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
15595:                                 }
15596:                                 my $display;
15597:                                 if ($timeout eq '0') {
15598:                                     $display = &mt('unlimited');
15599:                                 } elsif ($timeout eq '') {
15600:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
15601:                                 } else {
15602:                                     $display = &mt('[quant,_1,second]',$timeout);
15603:                                 }
15604:                                 if ($type eq 'community') {
15605:                                     $resulttext .= &mt('Communities');
15606:                                 } elsif ($type eq 'official') {
15607:                                     $resulttext .= &mt('Official courses');
15608:                                 } elsif ($type eq 'unofficial') {
15609:                                     $resulttext .= &mt('Unofficial courses');
15610:                                 } elsif ($type eq 'textbook') {
15611:                                     $resulttext .= &mt('Textbook courses');
15612:                                 }
15613:                                 $resulttext .= ' -- '.$display.'</li>';
15614:                             }
15615:                             $resulttext .= '</ul>';
15616:                         }
15617:                         $resulttext .= '</li>';
15618:                     }
15619:                 } elsif ($item eq 'coursecredits') {
15620:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
15621:                         if (($domdefaults{'officialcredits'} eq '') &&
15622:                             ($domdefaults{'unofficialcredits'} eq '') &&
15623:                             ($domdefaults{'textbookcredits'} eq '')) {
15624:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15625:                         } else {
15626:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
15627:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
15628:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
15629:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
15630:                                            '</ul>'.
15631:                                            '</li>';
15632:                         }
15633:                     } else {
15634:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
15635:                     }
15636:                 } elsif ($item eq 'canclone') {
15637:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
15638:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
15639:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
15640:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
15641:                         }
15642:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
15643:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
15644:                     } else {
15645:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
15646:                     }
15647:                 }
15648:             }
15649:             $resulttext .= '</ul>';
15650:         } else {
15651:             $resulttext = &mt('No changes made to course defaults');
15652:         }
15653:     } else {
15654:         $resulttext = '<span class="LC_error">'.
15655:             &mt('An error occurred: [_1]',$putresult).'</span>';
15656:     }
15657:     return $resulttext;
15658: }
15659: 
15660: sub modify_selfenrollment {
15661:     my ($dom,$lastactref,%domconfig) = @_;
15662:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
15663:     my @types = ('official','unofficial','community','textbook');
15664:     my %titles = &tool_titles();
15665:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
15666:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
15667:     $ordered{'default'} = ['types','registered','approval','limit'];
15668: 
15669:     my (%roles,%shown,%toplevel);
15670:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
15671: 
15672:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
15673:         if ($domconfig{'selfenrollment'} eq '') {
15674:             $domconfig{'selfenrollment'} = {};
15675:         }
15676:     }
15677:     %toplevel = (
15678:                   admin      => 'Configuration Rights',
15679:                   default    => 'Default settings',
15680:                   validation => 'Validation of self-enrollment requests',
15681:                 );
15682:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
15683: 
15684:     if (ref($ordered{'admin'}) eq 'ARRAY') {
15685:         foreach my $item (@{$ordered{'admin'}}) {
15686:             foreach my $type (@types) {
15687:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
15688:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
15689:                 } else {
15690:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
15691:                 }
15692:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
15693:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
15694:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
15695:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
15696:                             push(@{$changes{'admin'}{$type}},$item);
15697:                         }
15698:                     } else {
15699:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
15700:                             push(@{$changes{'admin'}{$type}},$item);
15701:                         }
15702:                     }
15703:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
15704:                     push(@{$changes{'admin'}{$type}},$item);
15705:                 }
15706:             }
15707:         }
15708:     }
15709: 
15710:     foreach my $item (@{$ordered{'default'}}) {
15711:         foreach my $type (@types) {
15712:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
15713:             if ($item eq 'types') {
15714:                 unless (($value eq 'all') || ($value eq 'dom')) {
15715:                     $value = '';
15716:                 }
15717:             } elsif ($item eq 'registered') {
15718:                 unless ($value eq '1') {
15719:                     $value = 0;
15720:                 }
15721:             } elsif ($item eq 'approval') {
15722:                 unless ($value =~ /^[012]$/) {
15723:                     $value = 0;
15724:                 }
15725:             } else {
15726:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
15727:                     $value = 'none';
15728:                 }
15729:             }
15730:             $selfenrollhash{'default'}{$type}{$item} = $value;
15731:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
15732:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
15733:                     if ($selfenrollhash{'default'}{$type}{$item} ne
15734:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
15735:                          push(@{$changes{'default'}{$type}},$item);
15736:                     }
15737:                 } else {
15738:                     push(@{$changes{'default'}{$type}},$item);
15739:                 }
15740:             } else {
15741:                 push(@{$changes{'default'}{$type}},$item);
15742:             }
15743:             if ($item eq 'limit') {
15744:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
15745:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
15746:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
15747:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
15748:                     }
15749:                 } else {
15750:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
15751:                 }
15752:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
15753:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
15754:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
15755:                          push(@{$changes{'default'}{$type}},'cap');
15756:                     }
15757:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
15758:                     push(@{$changes{'default'}{$type}},'cap');
15759:                 }
15760:             }
15761:         }
15762:     }
15763: 
15764:     foreach my $item (@{$itemsref}) {
15765:         if ($item eq 'fields') {
15766:             my @changed;
15767:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
15768:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
15769:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
15770:             }
15771:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
15772:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
15773:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
15774:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
15775:                 } else {
15776:                     @changed = @{$selfenrollhash{'validation'}{$item}};
15777:                 }
15778:             } else {
15779:                 @changed = @{$selfenrollhash{'validation'}{$item}};
15780:             }
15781:             if (@changed) {
15782:                 if ($selfenrollhash{'validation'}{$item}) { 
15783:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
15784:                 } else {
15785:                     $changes{'validation'}{$item} = &mt('None');
15786:                 }
15787:             }
15788:         } else {
15789:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
15790:             if ($item eq 'markup') {
15791:                if ($env{'form.selfenroll_validation_'.$item}) {
15792:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
15793:                }
15794:             }
15795:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
15796:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
15797:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
15798:                 }
15799:             }
15800:         }
15801:     }
15802: 
15803:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
15804:                                              $dom);
15805:     if ($putresult eq 'ok') {
15806:         if (keys(%changes) > 0) {
15807:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15808:             $resulttext = &mt('Changes made:').'<ul>';
15809:             foreach my $key ('admin','default','validation') {
15810:                 if (ref($changes{$key}) eq 'HASH') {
15811:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
15812:                     if ($key eq 'validation') {
15813:                         foreach my $item (@{$itemsref}) {
15814:                             if (exists($changes{$key}{$item})) {
15815:                                 if ($item eq 'markup') {
15816:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
15817:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
15818:                                 } else {  
15819:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
15820:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
15821:                                 }
15822:                             }
15823:                         }
15824:                     } else {
15825:                         foreach my $type (@types) {
15826:                             if ($type eq 'community') {
15827:                                 $roles{'1'} = &mt('Community personnel');
15828:                             } else {
15829:                                 $roles{'1'} = &mt('Course personnel');
15830:                             }
15831:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
15832:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
15833:                                     if ($key eq 'admin') {
15834:                                         my @mgrdc = ();
15835:                                         if (ref($ordered{$key}) eq 'ARRAY') {
15836:                                             foreach my $item (@{$ordered{'admin'}}) {
15837:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
15838:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
15839:                                                         push(@mgrdc,$item);
15840:                                                     }
15841:                                                 }
15842:                                             }
15843:                                             if (@mgrdc) {
15844:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
15845:                                             } else {
15846:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
15847:                                             }
15848:                                         }
15849:                                     } else {
15850:                                         if (ref($ordered{$key}) eq 'ARRAY') {
15851:                                             foreach my $item (@{$ordered{$key}}) {
15852:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
15853:                                                     $domdefaults{$type.'selfenroll'.$item} =
15854:                                                         $selfenrollhash{$key}{$type}{$item};
15855:                                                 }
15856:                                             }
15857:                                         }
15858:                                     }
15859:                                 }
15860:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
15861:                                 foreach my $item (@{$ordered{$key}}) {
15862:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
15863:                                         $resulttext .= '<li>';
15864:                                         if ($key eq 'admin') {
15865:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
15866:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
15867:                                         } else {
15868:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
15869:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
15870:                                         }
15871:                                         $resulttext .= '</li>';
15872:                                     }
15873:                                 }
15874:                                 $resulttext .= '</ul></li>';
15875:                             }
15876:                         }
15877:                         $resulttext .= '</ul></li>'; 
15878:                     }
15879:                 }
15880:             }
15881:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
15882:                 my $cachetime = 24*60*60;
15883:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15884:                 if (ref($lastactref) eq 'HASH') {
15885:                     $lastactref->{'domdefaults'} = 1;
15886:                 }
15887:             }
15888:             $resulttext .= '</ul>';
15889:         } else {
15890:             $resulttext = &mt('No changes made to self-enrollment settings');
15891:         }
15892:     } else {
15893:         $resulttext = '<span class="LC_error">'.
15894:             &mt('An error occurred: [_1]',$putresult).'</span>';
15895:     }
15896:     return $resulttext;
15897: }
15898: 
15899: sub modify_usersessions {
15900:     my ($dom,$lastactref,%domconfig) = @_;
15901:     my @hostingtypes = ('version','excludedomain','includedomain');
15902:     my @offloadtypes = ('primary','default');
15903:     my %types = (
15904:                   remote => \@hostingtypes,
15905:                   hosted => \@hostingtypes,
15906:                   spares => \@offloadtypes,
15907:                 );
15908:     my @prefixes = ('remote','hosted','spares');
15909:     my @lcversions = &Apache::lonnet::all_loncaparevs();
15910:     my (%by_ip,%by_location,@intdoms);
15911:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
15912:     my @locations = sort(keys(%by_location));
15913:     my (%defaultshash,%changes);
15914:     foreach my $prefix (@prefixes) {
15915:         $defaultshash{'usersessions'}{$prefix} = {};
15916:     }
15917:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15918:     my $resulttext;
15919:     my %iphost = &Apache::lonnet::get_iphost();
15920:     foreach my $prefix (@prefixes) {
15921:         next if ($prefix eq 'spares');
15922:         foreach my $type (@{$types{$prefix}}) {
15923:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
15924:             if ($type eq 'version') {
15925:                 my $value = $env{'form.'.$prefix.'_'.$type};
15926:                 my $okvalue;
15927:                 if ($value ne '') {
15928:                     if (grep(/^\Q$value\E$/,@lcversions)) {
15929:                         $okvalue = $value;
15930:                     }
15931:                 }
15932:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
15933:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
15934:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
15935:                             if ($inuse == 0) {
15936:                                 $changes{$prefix}{$type} = 1;
15937:                             } else {
15938:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
15939:                                     $changes{$prefix}{$type} = 1;
15940:                                 }
15941:                                 if ($okvalue ne '') {
15942:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15943:                                 } 
15944:                             }
15945:                         } else {
15946:                             if (($inuse == 1) && ($okvalue ne '')) {
15947:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15948:                                 $changes{$prefix}{$type} = 1;
15949:                             }
15950:                         }
15951:                     } else {
15952:                         if (($inuse == 1) && ($okvalue ne '')) {
15953:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15954:                             $changes{$prefix}{$type} = 1;
15955:                         }
15956:                     }
15957:                 } else {
15958:                     if (($inuse == 1) && ($okvalue ne '')) {
15959:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
15960:                         $changes{$prefix}{$type} = 1;
15961:                     }
15962:                 }
15963:             } else {
15964:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
15965:                 my @okvals;
15966:                 foreach my $val (@vals) {
15967:                     if ($val =~ /:/) {
15968:                         my @items = split(/:/,$val);
15969:                         foreach my $item (@items) {
15970:                             if (ref($by_location{$item}) eq 'ARRAY') {
15971:                                 push(@okvals,$item);
15972:                             }
15973:                         }
15974:                     } else {
15975:                         if (ref($by_location{$val}) eq 'ARRAY') {
15976:                             push(@okvals,$val);
15977:                         }
15978:                     }
15979:                 }
15980:                 @okvals = sort(@okvals);
15981:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
15982:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
15983:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
15984:                             if ($inuse == 0) {
15985:                                 $changes{$prefix}{$type} = 1; 
15986:                             } else {
15987:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15988:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
15989:                                 if (@changed > 0) {
15990:                                     $changes{$prefix}{$type} = 1;
15991:                                 }
15992:                             }
15993:                         } else {
15994:                             if ($inuse == 1) {
15995:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
15996:                                 $changes{$prefix}{$type} = 1;
15997:                             }
15998:                         } 
15999:                     } else {
16000:                         if ($inuse == 1) {
16001:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16002:                             $changes{$prefix}{$type} = 1;
16003:                         }
16004:                     }
16005:                 } else {
16006:                     if ($inuse == 1) {
16007:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
16008:                         $changes{$prefix}{$type} = 1;
16009:                     }
16010:                 }
16011:             }
16012:         }
16013:     }
16014: 
16015:     my @alldoms = &Apache::lonnet::all_domains();
16016:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16017:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
16018:     my $savespares;
16019: 
16020:     foreach my $lonhost (sort(keys(%servers))) {
16021:         my $serverhomeID =
16022:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
16023:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
16024:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
16025:         my %spareschg;
16026:         foreach my $type (@{$types{'spares'}}) {
16027:             my @okspares;
16028:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
16029:             foreach my $server (@checked) {
16030:                 if (&Apache::lonnet::hostname($server) ne '') {
16031:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
16032:                         unless (grep(/^\Q$server\E$/,@okspares)) {
16033:                             push(@okspares,$server);
16034:                         }
16035:                     }
16036:                 }
16037:             }
16038:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
16039:             my $newspare;
16040:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
16041:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
16042:                     $newspare = $new;
16043:                 }
16044:             }
16045:             my @spares;
16046:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
16047:                 @spares = sort(@okspares,$newspare);
16048:             } else {
16049:                 @spares = sort(@okspares);
16050:             }
16051:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
16052:             if (ref($spareid{$lonhost}) eq 'HASH') {
16053:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
16054:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
16055:                     if (@diffs > 0) {
16056:                         $spareschg{$type} = 1;
16057:                     }
16058:                 }
16059:             }
16060:         }
16061:         if (keys(%spareschg) > 0) {
16062:             $changes{'spares'}{$lonhost} = \%spareschg;
16063:         }
16064:     }
16065:     $defaultshash{'usersessions'}{'offloadnow'} = {};
16066:     $defaultshash{'usersessions'}{'offloadoth'} = {};
16067:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
16068:     my @okoffload;
16069:     if (@offloadnow) {
16070:         foreach my $server (@offloadnow) {
16071:             if (&Apache::lonnet::hostname($server) ne '') {
16072:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
16073:                     push(@okoffload,$server);
16074:                 }
16075:             }
16076:         }
16077:         if (@okoffload) {
16078:             foreach my $lonhost (@okoffload) {
16079:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
16080:             }
16081:         }
16082:     }
16083:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
16084:     my @okoffloadoth;
16085:     if (@offloadoth) {
16086:         foreach my $server (@offloadoth) {
16087:             if (&Apache::lonnet::hostname($server) ne '') {
16088:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
16089:                     push(@okoffloadoth,$server);
16090:                 }
16091:             }
16092:         }
16093:         if (@okoffloadoth) {
16094:             foreach my $lonhost (@okoffloadoth) {
16095:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
16096:             }
16097:         }
16098:     }
16099:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
16100:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
16101:             if (ref($changes{'spares'}) eq 'HASH') {
16102:                 if (keys(%{$changes{'spares'}}) > 0) {
16103:                     $savespares = 1;
16104:                 }
16105:             }
16106:         } else {
16107:             $savespares = 1;
16108:         }
16109:         foreach my $offload ('offloadnow','offloadoth') {
16110:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
16111:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
16112:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
16113:                         $changes{$offload} = 1;
16114:                         last;
16115:                     }
16116:                 }
16117:                 unless ($changes{$offload}) {
16118:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
16119:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
16120:                             $changes{$offload} = 1;
16121:                             last;
16122:                         }
16123:                     }
16124:                 }
16125:             } else {
16126:                 if (($offload eq 'offloadnow') && (@okoffload)) {
16127:                      $changes{'offloadnow'} = 1;
16128:                 }
16129:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
16130:                     $changes{'offloadoth'} = 1;
16131:                 }
16132:             }
16133:         }
16134:     } else {
16135:         if (@okoffload) {
16136:             $changes{'offloadnow'} = 1;
16137:         }
16138:         if (@okoffloadoth) {
16139:             $changes{'offloadoth'} = 1;
16140:         }
16141:     }
16142:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
16143:     if ((keys(%changes) > 0) || ($savespares)) {
16144:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
16145:                                                  $dom);
16146:         if ($putresult eq 'ok') {
16147:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16148:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
16149:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
16150:                 }
16151:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
16152:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
16153:                 }
16154:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16155:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
16156:                 }
16157:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
16158:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
16159:                 }
16160:             }
16161:             my $cachetime = 24*60*60;
16162:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16163:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
16164:             if (ref($lastactref) eq 'HASH') {
16165:                 $lastactref->{'domdefaults'} = 1;
16166:                 $lastactref->{'usersessions'} = 1;
16167:             }
16168:             if (keys(%changes) > 0) {
16169:                 my %lt = &usersession_titles();
16170:                 $resulttext = &mt('Changes made:').'<ul>';
16171:                 foreach my $prefix (@prefixes) {
16172:                     if (ref($changes{$prefix}) eq 'HASH') {
16173:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
16174:                         if ($prefix eq 'spares') {
16175:                             if (ref($changes{$prefix}) eq 'HASH') {
16176:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
16177:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
16178:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
16179:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
16180:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
16181:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
16182:                                         foreach my $type (@{$types{$prefix}}) {
16183:                                             if ($changes{$prefix}{$lonhost}{$type}) {
16184:                                                 my $offloadto = &mt('None');
16185:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
16186:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
16187:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
16188:                                                     }
16189:                                                 }
16190:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
16191:                                             }
16192:                                         }
16193:                                     }
16194:                                     $resulttext .= '</li>';
16195:                                 }
16196:                             }
16197:                         } else {
16198:                             foreach my $type (@{$types{$prefix}}) {
16199:                                 if (defined($changes{$prefix}{$type})) {
16200:                                     my $newvalue;
16201:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
16202:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
16203:                                             if ($type eq 'version') {
16204:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
16205:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
16206:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
16207:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
16208:                                                 }
16209:                                             }
16210:                                         }
16211:                                     }
16212:                                     if ($newvalue eq '') {
16213:                                         if ($type eq 'version') {
16214:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
16215:                                         } else {
16216:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
16217:                                         }
16218:                                     } else {
16219:                                         if ($type eq 'version') {
16220:                                             $newvalue .= ' '.&mt('(or later)'); 
16221:                                         }
16222:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
16223:                                     }
16224:                                 }
16225:                             }
16226:                         }
16227:                         $resulttext .= '</ul>';
16228:                     }
16229:                 }
16230:                 if ($changes{'offloadnow'}) {
16231:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
16232:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
16233:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
16234:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
16235:                                 $resulttext .= '<li>'.$lonhost.'</li>';
16236:                             }
16237:                             $resulttext .= '</ul>';
16238:                         } else {
16239:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
16240:                         }
16241:                     } else {
16242:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
16243:                     }
16244:                 }
16245:                 if ($changes{'offloadoth'}) {
16246:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
16247:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
16248:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
16249:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
16250:                                 $resulttext .= '<li>'.$lonhost.'</li>';
16251:                             }
16252:                             $resulttext .= '</ul>';
16253:                         } else {
16254:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
16255:                         }
16256:                     } else {
16257:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
16258:                     }
16259:                 }
16260:                 $resulttext .= '</ul>';
16261:             } else {
16262:                 $resulttext = $nochgmsg;
16263:             }
16264:         } else {
16265:             $resulttext = '<span class="LC_error">'.
16266:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16267:         }
16268:     } else {
16269:         $resulttext = $nochgmsg;
16270:     }
16271:     return $resulttext;
16272: }
16273: 
16274: sub modify_loadbalancing {
16275:     my ($dom,%domconfig) = @_;
16276:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
16277:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
16278:     my ($othertitle,$usertypes,$types) =
16279:         &Apache::loncommon::sorted_inst_types($dom);
16280:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16281:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
16282:     my @sparestypes = ('primary','default');
16283:     my %typetitles = &sparestype_titles();
16284:     my $resulttext;
16285:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
16286:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
16287:         %existing = %{$domconfig{'loadbalancing'}};
16288:     }
16289:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
16290:                               \%currtargets,\%currrules,\%currcookies);
16291:     my ($saveloadbalancing,%defaultshash,%changes);
16292:     my ($alltypes,$othertypes,$titles) =
16293:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
16294:     my %ruletitles = &offloadtype_text();
16295:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
16296:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
16297:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
16298:         if ($balancer eq '') {
16299:             next;
16300:         }
16301:         if (!exists($servers{$balancer})) {
16302:             if (exists($currbalancer{$balancer})) {
16303:                 push(@{$changes{'delete'}},$balancer);
16304:             }
16305:             next;
16306:         }
16307:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
16308:             push(@{$changes{'delete'}},$balancer);
16309:             next;
16310:         }
16311:         if (!exists($currbalancer{$balancer})) {
16312:             push(@{$changes{'add'}},$balancer);
16313:         }
16314:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
16315:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
16316:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
16317:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
16318:             $saveloadbalancing = 1;
16319:         }
16320:         foreach my $sparetype (@sparestypes) {
16321:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
16322:             my @offloadto;
16323:             foreach my $target (@targets) {
16324:                 if (($servers{$target}) && ($target ne $balancer)) {
16325:                     if ($sparetype eq 'default') {
16326:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
16327:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
16328:                         }
16329:                     }
16330:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
16331:                         push(@offloadto,$target);
16332:                     }
16333:                 }
16334:             }
16335:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
16336:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
16337:                     push(@offloadto,$balancer);
16338:                 }
16339:             }
16340:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
16341:         }
16342:         if ($env{'form.loadbalancing_cookie_'.$i}) {
16343:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
16344:             if (exists($currbalancer{$balancer})) {
16345:                 unless ($currcookies{$balancer}) {
16346:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
16347:                 }
16348:             }
16349:         } elsif (exists($currbalancer{$balancer})) {
16350:             if ($currcookies{$balancer}) {
16351:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
16352:             }
16353:         }
16354:         if (ref($currtargets{$balancer}) eq 'HASH') {
16355:             foreach my $sparetype (@sparestypes) {
16356:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
16357:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
16358:                     if (@targetdiffs > 0) {
16359:                         $changes{'curr'}{$balancer}{'targets'} = 1;
16360:                     }
16361:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16362:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
16363:                         $changes{'curr'}{$balancer}{'targets'} = 1;
16364:                     }
16365:                 }
16366:             }
16367:         } else {
16368:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
16369:                 foreach my $sparetype (@sparestypes) {
16370:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16371:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
16372:                             $changes{'curr'}{$balancer}{'targets'} = 1;
16373:                         }
16374:                     }
16375:                 }
16376:             }
16377:         }
16378:         my $ishomedom;
16379:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
16380:             $ishomedom = 1;
16381:         }
16382:         if (ref($alltypes) eq 'ARRAY') {
16383:             foreach my $type (@{$alltypes}) {
16384:                 my $rule;
16385:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
16386:                          (!$ishomedom)) {
16387:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
16388:                 }
16389:                 if ($rule eq 'specific') {
16390:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
16391:                     if (exists($servers{$specifiedhost})) {
16392:                         $rule = $specifiedhost;
16393:                     }
16394:                 }
16395:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
16396:                 if (ref($currrules{$balancer}) eq 'HASH') {
16397:                     if ($rule ne $currrules{$balancer}{$type}) {
16398:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
16399:                     }
16400:                 } elsif ($rule ne '') {
16401:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
16402:                 }
16403:             }
16404:         }
16405:     }
16406:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
16407:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
16408:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
16409:             $defaultshash{'loadbalancing'} = {};
16410:         }
16411:         my $putresult = &Apache::lonnet::put_dom('configuration',
16412:                                                  \%defaultshash,$dom);
16413:         if ($putresult eq 'ok') {
16414:             if (keys(%changes) > 0) {
16415:                 my %toupdate;
16416:                 if (ref($changes{'delete'}) eq 'ARRAY') {
16417:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
16418:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
16419:                         $toupdate{$balancer} = 1;
16420:                     }
16421:                 }
16422:                 if (ref($changes{'add'}) eq 'ARRAY') {
16423:                     foreach my $balancer (sort(@{$changes{'add'}})) {
16424:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
16425:                         $toupdate{$balancer} = 1;
16426:                     }
16427:                 }
16428:                 if (ref($changes{'curr'}) eq 'HASH') {
16429:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
16430:                         $toupdate{$balancer} = 1;
16431:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
16432:                             if ($changes{'curr'}{$balancer}{'targets'}) {
16433:                                 my %offloadstr;
16434:                                 foreach my $sparetype (@sparestypes) {
16435:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16436:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
16437:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
16438:                                         }
16439:                                     }
16440:                                 }
16441:                                 if (keys(%offloadstr) == 0) {
16442:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
16443:                                 } else {
16444:                                     my $showoffload;
16445:                                     foreach my $sparetype (@sparestypes) {
16446:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
16447:                                         if (defined($offloadstr{$sparetype})) {
16448:                                             $showoffload .= $offloadstr{$sparetype};
16449:                                         } else {
16450:                                             $showoffload .= &mt('None');
16451:                                         }
16452:                                         $showoffload .= ('&nbsp;'x3);
16453:                                     }
16454:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
16455:                                 }
16456:                             }
16457:                         }
16458:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
16459:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
16460:                                 foreach my $type (@{$alltypes}) {
16461:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
16462:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
16463:                                         my $balancetext;
16464:                                         if ($rule eq '') {
16465:                                             $balancetext =  $ruletitles{'default'};
16466:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
16467:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
16468:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
16469:                                                 foreach my $sparetype (@sparestypes) {
16470:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
16471:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
16472:                                                     }
16473:                                                 }
16474:                                                 foreach my $item (@{$alltypes}) {
16475:                                                     next if ($item =~  /^_LC_ipchange/);
16476:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
16477:                                                     if ($hasrule eq 'homeserver') {
16478:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
16479:                                                     } else {
16480:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
16481:                                                             if ($servers{$hasrule}) {
16482:                                                                 $toupdate{$hasrule} = 1;
16483:                                                             }
16484:                                                         }
16485:                                                     }
16486:                                                 }
16487:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
16488:                                                     $balancetext =  $ruletitles{$rule};
16489:                                                 } else {
16490:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
16491:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
16492:                                                     if ($receiver) {
16493:                                                         $toupdate{$receiver};
16494:                                                     }
16495:                                                 }
16496:                                             } else {
16497:                                                 $balancetext =  $ruletitles{$rule};
16498:                                             }
16499:                                         } else {
16500:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
16501:                                         }
16502:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
16503:                                     }
16504:                                 }
16505:                             }
16506:                         }
16507:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
16508:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
16509:                                                       $balancer).'</li>';
16510:                         }
16511:                     }
16512:                 }
16513:                 if (keys(%toupdate)) {
16514:                     my %thismachine;
16515:                     my $updatedhere;
16516:                     my $cachetime = 60*60*24;
16517:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
16518:                     foreach my $lonhost (keys(%toupdate)) {
16519:                         if ($thismachine{$lonhost}) {
16520:                             unless ($updatedhere) {
16521:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
16522:                                                               $defaultshash{'loadbalancing'},
16523:                                                               $cachetime);
16524:                                 $updatedhere = 1;
16525:                             }
16526:                         } else {
16527:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
16528:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
16529:                         }
16530:                     }
16531:                 }
16532:                 if ($resulttext ne '') {
16533:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
16534:                 } else {
16535:                     $resulttext = $nochgmsg;
16536:                 }
16537:             } else {
16538:                 $resulttext = $nochgmsg;
16539:             }
16540:         } else {
16541:             $resulttext = '<span class="LC_error">'.
16542:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16543:         }
16544:     } else {
16545:         $resulttext = $nochgmsg;
16546:     }
16547:     return $resulttext;
16548: }
16549: 
16550: sub recurse_check {
16551:     my ($chkcats,$categories,$depth,$name) = @_;
16552:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
16553:         my $chg = 0;
16554:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
16555:             my $category = $chkcats->[$depth]{$name}[$j];
16556:             my $item;
16557:             if ($category eq '') {
16558:                 $chg ++;
16559:             } else {
16560:                 my $deeper = $depth + 1;
16561:                 $item = &escape($category).':'.&escape($name).':'.$depth;
16562:                 if ($chg) {
16563:                     $categories->{$item} -= $chg;
16564:                 }
16565:                 &recurse_check($chkcats,$categories,$deeper,$category);
16566:                 $deeper --;
16567:             }
16568:         }
16569:     }
16570:     return;
16571: }
16572: 
16573: sub recurse_cat_deletes {
16574:     my ($item,$coursecategories,$deletions) = @_;
16575:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
16576:     my $subdepth = $depth + 1;
16577:     if (ref($coursecategories) eq 'HASH') {
16578:         foreach my $subitem (keys(%{$coursecategories})) {
16579:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
16580:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
16581:                 delete($coursecategories->{$subitem});
16582:                 $deletions->{$subitem} = 1;
16583:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
16584:             }
16585:         }
16586:     }
16587:     return;
16588: }
16589: 
16590: sub active_dc_picker {
16591:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
16592:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16593:     my @domcoord = keys(%domcoords);
16594:     if (keys(%currhash)) {
16595:         foreach my $dc (keys(%currhash)) {
16596:             unless (exists($domcoords{$dc})) {
16597:                 push(@domcoord,$dc);
16598:             }
16599:         }
16600:     }
16601:     @domcoord = sort(@domcoord);
16602:     my $numdcs = scalar(@domcoord);
16603:     my $rows = 0;
16604:     my $table;
16605:     if ($numdcs > 1) {
16606:         $table = '<table>';
16607:         for (my $i=0; $i<@domcoord; $i++) {
16608:             my $rem = $i%($numinrow);
16609:             if ($rem == 0) {
16610:                 if ($i > 0) {
16611:                     $table .= '</tr>';
16612:                 }
16613:                 $table .= '<tr>';
16614:                 $rows ++;
16615:             }
16616:             my $check = '';
16617:             if ($inputtype eq 'radio') {
16618:                 if (keys(%currhash) == 0) {
16619:                     if (!$i) {
16620:                         $check = ' checked="checked"';
16621:                     }
16622:                 } elsif (exists($currhash{$domcoord[$i]})) {
16623:                     $check = ' checked="checked"';
16624:                 }
16625:             } else {
16626:                 if (exists($currhash{$domcoord[$i]})) {
16627:                     $check = ' checked="checked"';
16628:                 }
16629:             }
16630:             if ($i == @domcoord - 1) {
16631:                 my $colsleft = $numinrow - $rem;
16632:                 if ($colsleft > 1) {
16633:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
16634:                 } else {
16635:                     $table .= '<td class="LC_left_item">';
16636:                 }
16637:             } else {
16638:                 $table .= '<td class="LC_left_item">';
16639:             }
16640:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
16641:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
16642:             $table .= '<span class="LC_nobreak"><label>'.
16643:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
16644:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
16645:             if ($user ne $dcname.':'.$dcdom) {
16646:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16647:             }
16648:             $table .= '</label></span></td>';
16649:         }
16650:         $table .= '</tr></table>';
16651:     } elsif ($numdcs == 1) {
16652:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
16653:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
16654:         if ($inputtype eq 'radio') {
16655:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
16656:             if ($user ne $dcname.':'.$dcdom) {
16657:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16658:             }
16659:         } else {
16660:             my $check;
16661:             if (exists($currhash{$domcoord[0]})) {
16662:                 $check = ' checked="checked"';
16663:             }
16664:             $table = '<span class="LC_nobreak"><label>'.
16665:                      '<input type="checkbox" name="'.$name.'" '.
16666:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
16667:             if ($user ne $dcname.':'.$dcdom) {
16668:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
16669:             }
16670:             $table .= '</label></span>';
16671:             $rows ++;
16672:         }
16673:     }
16674:     return ($numdcs,$table,$rows);
16675: }
16676: 
16677: sub usersession_titles {
16678:     return &Apache::lonlocal::texthash(
16679:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
16680:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
16681:                spares => 'Servers offloaded to, when busy',
16682:                version => 'LON-CAPA version requirement',
16683:                excludedomain => 'Allow all, but exclude specific domains',
16684:                includedomain => 'Deny all, but include specific domains',
16685:                primary => 'Primary (checked first)',
16686:                default => 'Default',
16687:            );
16688: }
16689: 
16690: sub id_for_thisdom {
16691:     my (%servers) = @_;
16692:     my %altids;
16693:     foreach my $server (keys(%servers)) {
16694:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
16695:         if ($serverhome ne $server) {
16696:             $altids{$serverhome} = $server;
16697:         }
16698:     }
16699:     return %altids;
16700: }
16701: 
16702: sub count_servers {
16703:     my ($currbalancer,%servers) = @_;
16704:     my (@spares,$numspares);
16705:     foreach my $lonhost (sort(keys(%servers))) {
16706:         next if ($currbalancer eq $lonhost);
16707:         push(@spares,$lonhost);
16708:     }
16709:     if ($currbalancer) {
16710:         $numspares = scalar(@spares);
16711:     } else {
16712:         $numspares = scalar(@spares) - 1;
16713:     }
16714:     return ($numspares,@spares);
16715: }
16716: 
16717: sub lonbalance_targets_js {
16718:     my ($dom,$types,$servers,$settings) = @_;
16719:     my $select = &mt('Select');
16720:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
16721:     if (ref($servers) eq 'HASH') {
16722:         $alltargets = join("','",sort(keys(%{$servers})));
16723:         my @homedoms;
16724:         foreach my $server (sort(keys(%{$servers}))) {
16725:             if (&Apache::lonnet::host_domain($server) eq $dom) {
16726:                 push(@homedoms,'1');
16727:             } else {
16728:                 push(@homedoms,'0');
16729:             }
16730:         }
16731:         $allishome = join("','",@homedoms);
16732:     }
16733:     if (ref($types) eq 'ARRAY') {
16734:         if (@{$types} > 0) {
16735:             @alltypes = @{$types};
16736:         }
16737:     }
16738:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
16739:     $allinsttypes = join("','",@alltypes);
16740:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
16741:     if (ref($settings) eq 'HASH') {
16742:         %existing = %{$settings};
16743:     }
16744:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
16745:                               \%currtargets,\%currrules,\%currcookies);
16746:     my $balancers = join("','",sort(keys(%currbalancer)));
16747:     return <<"END";
16748: 
16749: <script type="text/javascript">
16750: // <![CDATA[
16751: 
16752: currBalancers = new Array('$balancers');
16753: 
16754: function toggleTargets(balnum) {
16755:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
16756:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
16757:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
16758:     var prevbalancer = prevhostitem.value;
16759:     var baltotal = document.getElementById('loadbalancing_total').value;
16760:     prevhostitem.value = balancer;
16761:     if (prevbalancer != '') {
16762:         var prevIdx = currBalancers.indexOf(prevbalancer);
16763:         if (prevIdx != -1) {
16764:             currBalancers.splice(prevIdx,1);
16765:         }
16766:     }
16767:     if (balancer == '') {
16768:         hideSpares(balnum);
16769:     } else {
16770:         var currIdx = currBalancers.indexOf(balancer);
16771:         if (currIdx == -1) {
16772:             currBalancers.push(balancer);
16773:         }
16774:         var homedoms = new Array('$allishome');
16775:         var ishomedom = homedoms[lonhostitem.selectedIndex];
16776:         showSpares(balancer,ishomedom,balnum);
16777:     }
16778:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
16779:     return;
16780: }
16781: 
16782: function showSpares(balancer,ishomedom,balnum) {
16783:     var alltargets = new Array('$alltargets');
16784:     var insttypes = new Array('$allinsttypes');
16785:     var offloadtypes = new Array('primary','default');
16786: 
16787:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
16788:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
16789:  
16790:     for (var i=0; i<offloadtypes.length; i++) {
16791:         var count = 0;
16792:         for (var j=0; j<alltargets.length; j++) {
16793:             if (alltargets[j] != balancer) {
16794:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
16795:                 item.value = alltargets[j];
16796:                 item.style.textAlign='left';
16797:                 item.style.textFace='normal';
16798:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
16799:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
16800:                     item.disabled = '';
16801:                 } else {
16802:                     item.disabled = 'disabled';
16803:                     item.checked = false;
16804:                 }
16805:                 count ++;
16806:             }
16807:         }
16808:     }
16809:     for (var k=0; k<insttypes.length; k++) {
16810:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
16811:             if (ishomedom == 1) {
16812:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
16813:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
16814:             } else {
16815:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
16816:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
16817:             }
16818:         } else {
16819:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
16820:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
16821:         }
16822:         if ((insttypes[k] != '_LC_external') && 
16823:             ((insttypes[k] != '_LC_internetdom') ||
16824:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
16825:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
16826:             item.options.length = 0;
16827:             item.options[0] = new Option("","",true,true);
16828:             var idx = 0;
16829:             for (var m=0; m<alltargets.length; m++) {
16830:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
16831:                     idx ++;
16832:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
16833:                 }
16834:             }
16835:         }
16836:     }
16837:     return;
16838: }
16839: 
16840: function hideSpares(balnum) {
16841:     var alltargets = new Array('$alltargets');
16842:     var insttypes = new Array('$allinsttypes');
16843:     var offloadtypes = new Array('primary','default');
16844: 
16845:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
16846:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
16847: 
16848:     var total = alltargets.length - 1;
16849:     for (var i=0; i<offloadtypes; i++) {
16850:         for (var j=0; j<total; j++) {
16851:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
16852:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
16853:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
16854:         }
16855:     }
16856:     for (var k=0; k<insttypes.length; k++) {
16857:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
16858:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
16859:         if (insttypes[k] != '_LC_external') {
16860:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
16861:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
16862:         }
16863:     }
16864:     return;
16865: }
16866: 
16867: function checkOffloads(item,balnum,type) {
16868:     var alltargets = new Array('$alltargets');
16869:     var offloadtypes = new Array('primary','default');
16870:     if (item.checked) {
16871:         var total = alltargets.length - 1;
16872:         var other;
16873:         if (type == offloadtypes[0]) {
16874:             other = offloadtypes[1];
16875:         } else {
16876:             other = offloadtypes[0];
16877:         }
16878:         for (var i=0; i<total; i++) {
16879:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
16880:             if (server == item.value) {
16881:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
16882:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
16883:                 }
16884:             }
16885:         }
16886:     }
16887:     return;
16888: }
16889: 
16890: function singleServerToggle(balnum,type) {
16891:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
16892:     if (offloadtoSelIdx == 0) {
16893:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
16894:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
16895: 
16896:     } else {
16897:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
16898:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
16899:     }
16900:     return;
16901: }
16902: 
16903: function balanceruleChange(formname,balnum,type) {
16904:     if (type == '_LC_external') {
16905:         return;
16906:     }
16907:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
16908:     for (var i=0; i<typesRules.length; i++) {
16909:         if (formname.elements[typesRules[i]].checked) {
16910:             if (formname.elements[typesRules[i]].value != 'specific') {
16911:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
16912:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
16913:             } else {
16914:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
16915:             }
16916:         }
16917:     }
16918:     return;
16919: }
16920: 
16921: function balancerDeleteChange(balnum) {
16922:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
16923:     var baltotal = document.getElementById('loadbalancing_total').value;
16924:     var addtarget;
16925:     var removetarget;
16926:     var action = 'delete';
16927:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
16928:         var lonhost = hostitem.value;
16929:         var currIdx = currBalancers.indexOf(lonhost);
16930:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
16931:             if (currIdx != -1) {
16932:                 currBalancers.splice(currIdx,1);
16933:             }
16934:             addtarget = lonhost;
16935:         } else {
16936:             if (currIdx == -1) {
16937:                 currBalancers.push(lonhost);
16938:             }
16939:             removetarget = lonhost;
16940:             action = 'undelete';
16941:         }
16942:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
16943:     }
16944:     return;
16945: }
16946: 
16947: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
16948:     if (baltotal > 1) {
16949:         var offloadtypes = new Array('primary','default');
16950:         var alltargets = new Array('$alltargets');
16951:         var insttypes = new Array('$allinsttypes');
16952:         for (var i=0; i<baltotal; i++) {
16953:             if (i != balnum) {
16954:                 for (var j=0; j<offloadtypes.length; j++) {
16955:                     var total = alltargets.length - 1;
16956:                     for (var k=0; k<total; k++) {
16957:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
16958:                         var server = serveritem.value;
16959:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
16960:                             if (server == addtarget) {
16961:                                 serveritem.disabled = '';
16962:                             }
16963:                         }
16964:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
16965:                             if (server == removetarget) {
16966:                                 serveritem.disabled = 'disabled';
16967:                                 serveritem.checked = false;
16968:                             }
16969:                         }
16970:                     }
16971:                 }
16972:                 for (var j=0; j<insttypes.length; j++) {
16973:                     if (insttypes[j] != '_LC_external') {
16974:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
16975:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
16976:                             var currSel = singleserver.selectedIndex;
16977:                             var currVal = singleserver.options[currSel].value;
16978:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
16979:                                 var numoptions = singleserver.options.length;
16980:                                 var needsnew = 1;
16981:                                 for (var k=0; k<numoptions; k++) {
16982:                                     if (singleserver.options[k] == addtarget) {
16983:                                         needsnew = 0;
16984:                                         break;
16985:                                     }
16986:                                 }
16987:                                 if (needsnew == 1) {
16988:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
16989:                                 }
16990:                             }
16991:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
16992:                                 singleserver.options.length = 0;
16993:                                 if ((currVal) && (currVal != removetarget)) {
16994:                                     singleserver.options[0] = new Option("","",false,false);
16995:                                 } else {
16996:                                     singleserver.options[0] = new Option("","",true,true);
16997:                                 }
16998:                                 var idx = 0;
16999:                                 for (var m=0; m<alltargets.length; m++) {
17000:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
17001:                                         idx ++;
17002:                                         if (currVal == alltargets[m]) {
17003:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
17004:                                         } else {
17005:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
17006:                                         }
17007:                                     }
17008:                                 }
17009:                             }
17010:                         }
17011:                     }
17012:                 }
17013:             }
17014:         }
17015:     }
17016:     return;
17017: }
17018: 
17019: // ]]>
17020: </script>
17021: 
17022: END
17023: }
17024: 
17025: sub new_spares_js {
17026:     my @sparestypes = ('primary','default');
17027:     my $types = join("','",@sparestypes);
17028:     my $select = &mt('Select');
17029:     return <<"END";
17030: 
17031: <script type="text/javascript">
17032: // <![CDATA[
17033: 
17034: function updateNewSpares(formname,lonhost) {
17035:     var types = new Array('$types');
17036:     var include = new Array();
17037:     var exclude = new Array();
17038:     for (var i=0; i<types.length; i++) {
17039:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
17040:         for (var j=0; j<spareboxes.length; j++) {
17041:             if (formname.elements[spareboxes[j]].checked) {
17042:                 exclude.push(formname.elements[spareboxes[j]].value);
17043:             } else {
17044:                 include.push(formname.elements[spareboxes[j]].value);
17045:             }
17046:         }
17047:     }
17048:     for (var i=0; i<types.length; i++) {
17049:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
17050:         var selIdx = newSpare.selectedIndex;
17051:         var currnew = newSpare.options[selIdx].value;
17052:         var okSpares = new Array();
17053:         for (var j=0; j<newSpare.options.length; j++) {
17054:             var possible = newSpare.options[j].value;
17055:             if (possible != '') {
17056:                 if (exclude.indexOf(possible) == -1) {
17057:                     okSpares.push(possible);
17058:                 } else {
17059:                     if (currnew == possible) {
17060:                         selIdx = 0;
17061:                     }
17062:                 }
17063:             }
17064:         }
17065:         for (var k=0; k<include.length; k++) {
17066:             if (okSpares.indexOf(include[k]) == -1) {
17067:                 okSpares.push(include[k]);
17068:             }
17069:         }
17070:         okSpares.sort();
17071:         newSpare.options.length = 0;
17072:         if (selIdx == 0) {
17073:             newSpare.options[0] = new Option("$select","",true,true);
17074:         } else {
17075:             newSpare.options[0] = new Option("$select","",false,false);
17076:         }
17077:         for (var m=0; m<okSpares.length; m++) {
17078:             var idx = m+1;
17079:             var selThis = 0;
17080:             if (selIdx != 0) {
17081:                 if (okSpares[m] == currnew) {
17082:                     selThis = 1;
17083:                 }
17084:             }
17085:             if (selThis == 1) {
17086:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
17087:             } else {
17088:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
17089:             }
17090:         }
17091:     }
17092:     return;
17093: }
17094: 
17095: function checkNewSpares(lonhost,type) {
17096:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
17097:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
17098:     if (chosen != '') { 
17099:         var othertype;
17100:         var othernewSpare;
17101:         if (type == 'primary') {
17102:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
17103:         }
17104:         if (type == 'default') {
17105:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
17106:         }
17107:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
17108:             othernewSpare.selectedIndex = 0;
17109:         }
17110:     }
17111:     return;
17112: }
17113: 
17114: // ]]>
17115: </script>
17116: 
17117: END
17118: 
17119: }
17120: 
17121: sub common_domprefs_js {
17122:     return <<"END";
17123: 
17124: <script type="text/javascript">
17125: // <![CDATA[
17126: 
17127: function getIndicesByName(formname,item) {
17128:     var group = new Array();
17129:     for (var i=0;i<formname.elements.length;i++) {
17130:         if (formname.elements[i].name == item) {
17131:             group.push(formname.elements[i].id);
17132:         }
17133:     }
17134:     return group;
17135: }
17136: 
17137: // ]]>
17138: </script>
17139: 
17140: END
17141: 
17142: }
17143: 
17144: sub recaptcha_js {
17145:     my %lt = &captcha_phrases();
17146:     return <<"END";
17147: 
17148: <script type="text/javascript">
17149: // <![CDATA[
17150: 
17151: function updateCaptcha(caller,context) {
17152:     var privitem;
17153:     var pubitem;
17154:     var privtext;
17155:     var pubtext;
17156:     var versionitem;
17157:     var versiontext;
17158:     if (document.getElementById(context+'_recaptchapub')) {
17159:         pubitem = document.getElementById(context+'_recaptchapub');
17160:     } else {
17161:         return;
17162:     }
17163:     if (document.getElementById(context+'_recaptchapriv')) {
17164:         privitem = document.getElementById(context+'_recaptchapriv');
17165:     } else {
17166:         return;
17167:     }
17168:     if (document.getElementById(context+'_recaptchapubtxt')) {
17169:         pubtext = document.getElementById(context+'_recaptchapubtxt');
17170:     } else {
17171:         return;
17172:     }
17173:     if (document.getElementById(context+'_recaptchaprivtxt')) {
17174:         privtext = document.getElementById(context+'_recaptchaprivtxt');
17175:     } else {
17176:         return;
17177:     }
17178:     if (document.getElementById(context+'_recaptchaversion')) {
17179:         versionitem = document.getElementById(context+'_recaptchaversion');
17180:     } else {
17181:         return;
17182:     }
17183:     if (document.getElementById(context+'_recaptchavertxt')) {
17184:         versiontext = document.getElementById(context+'_recaptchavertxt');
17185:     } else {
17186:         return;
17187:     }
17188:     if (caller.checked) {
17189:         if (caller.value == 'recaptcha') {
17190:             pubitem.type = 'text';
17191:             privitem.type = 'text';
17192:             pubitem.size = '40';
17193:             privitem.size = '40';
17194:             pubtext.innerHTML = "$lt{'pub'}";
17195:             privtext.innerHTML = "$lt{'priv'}";
17196:             versionitem.type = 'text';
17197:             versionitem.size = '3';
17198:             versiontext.innerHTML = "$lt{'ver'}";
17199:         } else {
17200:             pubitem.type = 'hidden';
17201:             privitem.type = 'hidden';
17202:             versionitem.type = 'hidden';
17203:             pubtext.innerHTML = '';
17204:             privtext.innerHTML = '';
17205:             versiontext.innerHTML = '';
17206:         }
17207:     }
17208:     return;
17209: }
17210: 
17211: // ]]>
17212: </script>
17213: 
17214: END
17215: 
17216: }
17217: 
17218: sub toggle_display_js {
17219:     return <<"END";
17220: 
17221: <script type="text/javascript">
17222: // <![CDATA[
17223: 
17224: function toggleDisplay(domForm,caller) {
17225:     if (document.getElementById(caller)) {
17226:         var divitem = document.getElementById(caller);
17227:         var optionsElement = domForm.coursecredits;
17228:         var checkval = 1;
17229:         var dispval = 'block';
17230:         var selfcreateRegExp = /^cancreate_emailverified/;
17231:         if (caller == 'emailoptions') {
17232:             optionsElement = domForm.cancreate_email; 
17233:         }
17234:         if (caller == 'studentsubmission') {
17235:             optionsElement = domForm.postsubmit;
17236:         }
17237:         if (caller == 'cloneinstcode') {
17238:             optionsElement = domForm.canclone;
17239:             checkval = 'instcode';
17240:         }
17241:         if (selfcreateRegExp.test(caller)) {
17242:             optionsElement = domForm.elements[caller];
17243:             checkval = 'other';
17244:             dispval = 'inline'
17245:         }
17246:         if (optionsElement.length) {
17247:             var currval;
17248:             for (var i=0; i<optionsElement.length; i++) {
17249:                 if (optionsElement[i].checked) {
17250:                    currval = optionsElement[i].value;
17251:                 }
17252:             }
17253:             if (currval == checkval) {
17254:                 divitem.style.display = dispval;
17255:             } else {
17256:                 divitem.style.display = 'none';
17257:             }
17258:         }
17259:     }
17260:     return;
17261: }
17262: 
17263: // ]]>
17264: </script>
17265: 
17266: END
17267: 
17268: }
17269: 
17270: sub captcha_phrases {
17271:     return &Apache::lonlocal::texthash (
17272:                  priv => 'Private key',
17273:                  pub  => 'Public key',
17274:                  original  => 'original (CAPTCHA)',
17275:                  recaptcha => 'successor (ReCAPTCHA)',
17276:                  notused   => 'unused',
17277:                  ver => 'ReCAPTCHA version (1 or 2)',
17278:     );
17279: }
17280: 
17281: sub devalidate_remote_domconfs {
17282:     my ($dom,$cachekeys) = @_;
17283:     return unless (ref($cachekeys) eq 'HASH');
17284:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
17285:     my %thismachine;
17286:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
17287:     my @posscached = ('domainconfig','domdefaults','usersessions',
17288:                       'ltitools','directorysrch','passwdconf','cats');
17289:     if (keys(%servers)) {
17290:         foreach my $server (keys(%servers)) {
17291:             next if ($thismachine{$server});
17292:             my @cached;
17293:             foreach my $name (@posscached) {
17294:                 if ($cachekeys->{$name}) {
17295:                     push(@cached,&escape($name).':'.&escape($dom));
17296:                 }
17297:             }
17298:             if (@cached) {
17299:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
17300:             }
17301:         }
17302:     }
17303:     return;
17304: }
17305: 
17306: 1;

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