File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.367: download - view: text, annotated - select for diffs
Sun Aug 25 14:15:20 2019 UTC (4 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- javascript function:  warnIntPass() warns about invalid value for
  number of previous passwords saved in domain config for passwords

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.367 2019/08/25 14:15:20 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, textbook, placement, and lti).  
  108: In each case the radio buttons 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 LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: 
  180: my $registered_cleanup;
  181: my $modified_urls;
  182: 
  183: sub handler {
  184:     my $r=shift;
  185:     if ($r->header_only) {
  186:         &Apache::loncommon::content_type($r,'text/html');
  187:         $r->send_http_header;
  188:         return OK;
  189:     }
  190: 
  191:     my $context = 'domain';
  192:     my $dom = $env{'request.role.domain'};
  193:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  194:     if (&Apache::lonnet::allowed('mau',$dom)) {
  195:         &Apache::loncommon::content_type($r,'text/html');
  196:         $r->send_http_header;
  197:     } else {
  198:         $env{'user.error.msg'}=
  199:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  200:         return HTTP_NOT_ACCEPTABLE;
  201:     }
  202: 
  203:     $registered_cleanup=0;
  204:     @{$modified_urls}=();
  205: 
  206:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  207:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  208:                                             ['phase','actions']);
  209:     my $phase = 'pickactions';
  210:     if ( exists($env{'form.phase'}) ) {
  211:         $phase = $env{'form.phase'};
  212:     }
  213:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  214:     my %domconfig =
  215:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  216:                 'quotas','autoenroll','autoupdate','autocreate',
  217:                 'directorysrch','usercreation','usermodification',
  218:                 'contacts','defaults','scantron','coursecategories',
  219:                 'serverstatuses','requestcourses','helpsettings',
  220:                 'coursedefaults','usersessions','loadbalancing',
  221:                 'requestauthor','selfenrollment','inststatus',
  222:                 'ltitools','ssl','trust','lti','privacy','passwords'],$dom);
  223:     my %encconfig =
  224:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti'],$dom);
  225:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  226:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  227:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  228:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  229:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  230:                     foreach my $item ('key','secret') {
  231:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  232:                     }
  233:                 }
  234:             }
  235:         }
  236:     }
  237:     if (ref($domconfig{'lti'}) eq 'HASH') {
  238:         if (ref($encconfig{'lti'}) eq 'HASH') {
  239:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  240:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  241:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  242:                     foreach my $item ('key','secret') {
  243:                         $domconfig{'lti'}{$id}{$item} = $encconfig{'lti'}{$id}{$item};
  244:                     }
  245:                 }
  246:             }
  247:         }
  248:     }
  249:     my @prefs_order = ('rolecolors','login','defaults','passwords','quotas','autoenroll',
  250:                        'autoupdate','autocreate','directorysrch','contacts','privacy',
  251:                        'usercreation','selfcreation','usermodification','scantron',
  252:                        'requestcourses','requestauthor','coursecategories',
  253:                        'serverstatuses','helpsettings','coursedefaults',
  254:                        'ltitools','selfenrollment','usersessions','ssl','trust','lti');
  255:     my %existing;
  256:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  257:         %existing = %{$domconfig{'loadbalancing'}};
  258:     }
  259:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  260:         push(@prefs_order,'loadbalancing');
  261:     }
  262:     my %prefs = (
  263:         'rolecolors' =>
  264:                    { text => 'Default color schemes',
  265:                      help => 'Domain_Configuration_Color_Schemes',
  266:                      header => [{col1 => 'Student Settings',
  267:                                  col2 => '',},
  268:                                 {col1 => 'Coordinator Settings',
  269:                                  col2 => '',},
  270:                                 {col1 => 'Author Settings',
  271:                                  col2 => '',},
  272:                                 {col1 => 'Administrator Settings',
  273:                                  col2 => '',}],
  274:                       print => \&print_rolecolors,
  275:                       modify => \&modify_rolecolors,
  276:                     },
  277:         'login' =>
  278:                     { text => 'Log-in page options',
  279:                       help => 'Domain_Configuration_Login_Page',
  280:                       header => [{col1 => 'Log-in Page Items',
  281:                                   col2 => '',},
  282:                                  {col1 => 'Log-in Help',
  283:                                   col2 => 'Value'},
  284:                                  {col1 => 'Custom HTML in document head',
  285:                                   col2 => 'Value'}],
  286:                       print => \&print_login,
  287:                       modify => \&modify_login,
  288:                     },
  289:         'defaults' => 
  290:                     { text => 'Default authentication/language/timezone/portal/types',
  291:                       help => 'Domain_Configuration_LangTZAuth',
  292:                       header => [{col1 => 'Setting',
  293:                                   col2 => 'Value'},
  294:                                  {col1 => 'Institutional user types',
  295:                                   col2 => 'Name displayed'}],
  296:                       print => \&print_defaults,
  297:                       modify => \&modify_defaults,
  298:                     },
  299:         'passwords' =>
  300:                     { text => 'Passwords (Internal authentication)',
  301:                       help => 'Domain_Configuration_Passwords',
  302:                       header => [{col1 => 'Resetting Forgotten Password',
  303:                                   col2 => 'Settings'},
  304:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  305:                                   col2 => 'Settings'},
  306:                                  {col1 => 'Rules for LON-CAPA Passwords',
  307:                                   col2 => 'Settings'},
  308:                                  {col1 => 'Course Owner Changing Student Passwords',
  309:                                   col2 => 'Settings'}],
  310:                       print => \&print_passwords,
  311:                       modify => \&modify_passwords,
  312:                     },
  313:         'quotas' => 
  314:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  315:                       help => 'Domain_Configuration_Quotas',
  316:                       header => [{col1 => 'User affiliation',
  317:                                   col2 => 'Available tools',
  318:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  319:                       print => \&print_quotas,
  320:                       modify => \&modify_quotas,
  321:                     },
  322:         'autoenroll' =>
  323:                    { text => 'Auto-enrollment settings',
  324:                      help => 'Domain_Configuration_Auto_Enrollment',
  325:                      header => [{col1 => 'Configuration setting',
  326:                                  col2 => 'Value(s)'}],
  327:                      print => \&print_autoenroll,
  328:                      modify => \&modify_autoenroll,
  329:                    },
  330:         'autoupdate' => 
  331:                    { text => 'Auto-update settings',
  332:                      help => 'Domain_Configuration_Auto_Updates',
  333:                      header => [{col1 => 'Setting',
  334:                                  col2 => 'Value',},
  335:                                 {col1 => 'Setting',
  336:                                  col2 => 'Affiliation'},
  337:                                 {col1 => 'User population',
  338:                                  col2 => 'Updatable user data'}],
  339:                      print => \&print_autoupdate,
  340:                      modify => \&modify_autoupdate,
  341:                   },
  342:         'autocreate' => 
  343:                   { text => 'Auto-course creation settings',
  344:                      help => 'Domain_Configuration_Auto_Creation',
  345:                      header => [{col1 => 'Configuration Setting',
  346:                                  col2 => 'Value',}],
  347:                      print => \&print_autocreate,
  348:                      modify => \&modify_autocreate,
  349:                   },
  350:         'directorysrch' => 
  351:                   { text => 'Directory searches',
  352:                     help => 'Domain_Configuration_InstDirectory_Search',
  353:                     header => [{col1 => 'Institutional Directory Setting',
  354:                                 col2 => 'Value',},
  355:                                {col1 => 'LON-CAPA Directory Setting',
  356:                                 col2 => 'Value',}],
  357:                     print => \&print_directorysrch,
  358:                     modify => \&modify_directorysrch,
  359:                   },
  360:         'contacts' =>
  361:                   { text => 'E-mail addresses and helpform',
  362:                     help => 'Domain_Configuration_Contact_Info',
  363:                     header => [{col1 => 'Default e-mail addresses',
  364:                                 col2 => 'Value',},
  365:                                {col1 => 'Recipient(s) for notifications',
  366:                                 col2 => 'Value',},
  367:                                {col1 => 'Nightly status check e-mail',
  368:                                 col2 => 'Settings',},
  369:                                {col1 => 'Ask helpdesk form settings',
  370:                                 col2 => 'Value',},],
  371:                     print => \&print_contacts,
  372:                     modify => \&modify_contacts,
  373:                   },
  374:         'usercreation' => 
  375:                   { text => 'User creation',
  376:                     help => 'Domain_Configuration_User_Creation',
  377:                     header => [{col1 => 'Format rule type',
  378:                                 col2 => 'Format rules in force'},
  379:                                {col1 => 'User account creation',
  380:                                 col2 => 'Usernames which may be created',},
  381:                                {col1 => 'Context',
  382:                                 col2 => 'Assignable authentication types'}],
  383:                     print => \&print_usercreation,
  384:                     modify => \&modify_usercreation,
  385:                   },
  386:         'selfcreation' => 
  387:                   { text => 'Users self-creating accounts',
  388:                     help => 'Domain_Configuration_Self_Creation', 
  389:                     header => [{col1 => 'Self-creation with institutional username',
  390:                                 col2 => 'Enabled?'},
  391:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  392:                                 col2 => 'Information user can enter'},
  393:                                {col1 => 'Self-creation with e-mail verification',
  394:                                 col2 => 'Settings'}],
  395:                     print => \&print_selfcreation,
  396:                     modify => \&modify_selfcreation,
  397:                   },
  398:         'usermodification' =>
  399:                   { text => 'User modification',
  400:                     help => 'Domain_Configuration_User_Modification',
  401:                     header => [{col1 => 'Target user has role',
  402:                                 col2 => 'User information updatable in author context'},
  403:                                {col1 => 'Target user has role',
  404:                                 col2 => 'User information updatable in course context'}],
  405:                     print => \&print_usermodification,
  406:                     modify => \&modify_usermodification,
  407:                   },
  408:         'scantron' =>
  409:                   { text => 'Bubblesheet format',
  410:                     help => 'Domain_Configuration_Scantron_Format',
  411:                     header => [ {col1 => 'Bubblesheet format file',
  412:                                  col2 => ''},
  413:                                 {col1 => 'Bubblesheet data upload formats',
  414:                                  col2 => 'Settings'}],
  415:                     print => \&print_scantron,
  416:                     modify => \&modify_scantron,
  417:                   },
  418:         'requestcourses' => 
  419:                  {text => 'Request creation of courses',
  420:                   help => 'Domain_Configuration_Request_Courses',
  421:                   header => [{col1 => 'User affiliation',
  422:                               col2 => 'Availability/Processing of requests',},
  423:                              {col1 => 'Setting',
  424:                               col2 => 'Value'},
  425:                              {col1 => 'Available textbooks',
  426:                               col2 => ''},
  427:                              {col1 => 'Available templates',
  428:                               col2 => ''},
  429:                              {col1 => 'Validation (not official courses)',
  430:                               col2 => 'Value'},],
  431:                   print => \&print_quotas,
  432:                   modify => \&modify_quotas,
  433:                  },
  434:         'requestauthor' =>
  435:                  {text => 'Request Authoring Space',
  436:                   help => 'Domain_Configuration_Request_Author',
  437:                   header => [{col1 => 'User affiliation',
  438:                               col2 => 'Availability/Processing of requests',},
  439:                              {col1 => 'Setting',
  440:                               col2 => 'Value'}],
  441:                   print => \&print_quotas,
  442:                   modify => \&modify_quotas,
  443:                  },
  444:         'coursecategories' =>
  445:                   { text => 'Cataloging of courses/communities',
  446:                     help => 'Domain_Configuration_Cataloging_Courses',
  447:                     header => [{col1 => 'Catalog type/availability',
  448:                                 col2 => '',},
  449:                                {col1 => 'Category settings for standard catalog',
  450:                                 col2 => '',},
  451:                                {col1 => 'Categories',
  452:                                 col2 => '',
  453:                                }],
  454:                     print => \&print_coursecategories,
  455:                     modify => \&modify_coursecategories,
  456:                   },
  457:         'serverstatuses' =>
  458:                  {text   => 'Access to server status pages',
  459:                   help   => 'Domain_Configuration_Server_Status',
  460:                   header => [{col1 => 'Status Page',
  461:                               col2 => 'Other named users',
  462:                               col3 => 'Specific IPs',
  463:                             }],
  464:                   print => \&print_serverstatuses,
  465:                   modify => \&modify_serverstatuses,
  466:                  },
  467:         'helpsettings' =>
  468:                  {text   => 'Support settings',
  469:                   help   => 'Domain_Configuration_Help_Settings',
  470:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  471:                               col2 => 'Value'},
  472:                              {col1 => 'Helpdesk Roles',
  473:                               col2 => 'Settings'},],
  474:                   print  => \&print_helpsettings,
  475:                   modify => \&modify_helpsettings,
  476:                  },
  477:         'coursedefaults' => 
  478:                  {text => 'Course/Community defaults',
  479:                   help => 'Domain_Configuration_Course_Defaults',
  480:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  481:                               col2 => 'Value',},
  482:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  483:                               col2 => 'Value',},],
  484:                   print => \&print_coursedefaults,
  485:                   modify => \&modify_coursedefaults,
  486:                  },
  487:         'selfenrollment' => 
  488:                  {text   => 'Self-enrollment in Course/Community',
  489:                   help   => 'Domain_Configuration_Selfenrollment',
  490:                   header => [{col1 => 'Configuration Rights',
  491:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  492:                              {col1 => 'Defaults',
  493:                               col2 => 'Value'},
  494:                              {col1 => 'Self-enrollment validation (optional)',
  495:                               col2 => 'Value'},],
  496:                   print => \&print_selfenrollment,
  497:                   modify => \&modify_selfenrollment,
  498:                  },
  499:         'privacy' => 
  500:                  {text   => 'Availability of User Information',
  501:                   help   => 'Domain_Configuration_User_Privacy',
  502:                   header => [{col1 => 'Role assigned in different domain',
  503:                               col2 => 'Approval options'},
  504:                              {col1 => 'Role assigned in different domain to user of type',
  505:                               col2 => 'User information available in that domain'},
  506:                              {col1 => "Role assigned in user's domain",
  507:                               col2 => 'Information viewable by privileged user'},
  508:                              {col1 => "Role assigned in user's domain",
  509:                               col2 => 'Information viewable by unprivileged user'}],
  510:                   print => \&print_privacy,
  511:                   modify => \&modify_privacy,
  512:                  },
  513:         'usersessions' =>
  514:                  {text  => 'User session hosting/offloading',
  515:                   help  => 'Domain_Configuration_User_Sessions',
  516:                   header => [{col1 => 'Domain server',
  517:                               col2 => 'Servers to offload sessions to when busy'},
  518:                              {col1 => 'Hosting of users from other domains',
  519:                               col2 => 'Rules'},
  520:                              {col1 => "Hosting domain's own users elsewhere",
  521:                               col2 => 'Rules'}],
  522:                   print => \&print_usersessions,
  523:                   modify => \&modify_usersessions,
  524:                  },
  525:         'loadbalancing' =>
  526:                  {text  => 'Dedicated Load Balancer(s)',
  527:                   help  => 'Domain_Configuration_Load_Balancing',
  528:                   header => [{col1 => 'Balancers',
  529:                               col2 => 'Default destinations',
  530:                               col3 => 'User affiliation',
  531:                               col4 => 'Overrides'},
  532:                             ],
  533:                   print => \&print_loadbalancing,
  534:                   modify => \&modify_loadbalancing,
  535:                  },
  536:         'ltitools' => 
  537:                  {text => 'External Tools (LTI)',
  538:                   help => 'Domain_Configuration_LTI_Tools',
  539:                   header => [{col1 => 'Setting',
  540:                               col2 => 'Value',}],
  541:                   print => \&print_ltitools,
  542:                   modify => \&modify_ltitools,
  543:                  },
  544:         'ssl' =>
  545:                  {text  => 'LON-CAPA Network (SSL)',
  546:                   help  => 'Domain_Configuration_Network_SSL',
  547:                   header => [{col1 => 'Server',
  548:                               col2 => 'Certificate Status'},
  549:                              {col1 => 'Connections to other servers',
  550:                               col2 => 'Rules'},
  551:                              {col1 => 'Connections from other servers',
  552:                               col2 => 'Rules'},
  553:                              {col1 => "Replicating domain's published content",
  554:                               col2 => 'Rules'}],
  555:                   print => \&print_ssl,
  556:                   modify => \&modify_ssl,
  557:                  },
  558:         'trust' =>
  559:                  {text   => 'Trust Settings',
  560:                   help   => 'Domain_Configuration_Trust',
  561:                   header => [{col1 => "Access to this domain's content by others",
  562:                               col2 => 'Rules'},
  563:                              {col1 => "Access to other domain's content by this domain",
  564:                               col2 => 'Rules'},
  565:                              {col1 => "Enrollment in this domain's courses by others",
  566:                               col2 => 'Rules',},
  567:                              {col1 => "Co-author roles in this domain for others",
  568:                               col2 => 'Rules',},
  569:                              {col1 => "Co-author roles for this domain's users elsewhere",
  570:                               col2 => 'Rules',},
  571:                              {col1 => "Domain roles in this domain assignable to others",
  572:                               col2 => 'Rules'},
  573:                              {col1 => "Course catalog for this domain displayed elsewhere",
  574:                               col2 => 'Rules'},
  575:                              {col1 => "Requests for creation of courses in this domain by others",
  576:                               col2 => 'Rules'},
  577:                              {col1 => "Users in other domains can send messages to this domain",
  578:                               col2 => 'Rules'},],
  579:                   print => \&print_trust,
  580:                   modify => \&modify_trust,
  581:                  },
  582:           'lti' =>
  583:                  {text => 'LTI Provider',
  584:                   help => 'Domain_Configuration_LTI_Provider',
  585:                   header => [{col1 => 'Setting',
  586:                               col2 => 'Value',}],
  587:                   print => \&print_lti,
  588:                   modify => \&modify_lti,
  589:                  },
  590:     );
  591:     if (keys(%servers) > 1) {
  592:         $prefs{'login'}  = { text   => 'Log-in page options',
  593:                              help   => 'Domain_Configuration_Login_Page',
  594:                             header => [{col1 => 'Log-in Service',
  595:                                         col2 => 'Server Setting',},
  596:                                        {col1 => 'Log-in Page Items',
  597:                                         col2 => ''},
  598:                                        {col1 => 'Log-in Help',
  599:                                         col2 => 'Value'},
  600:                                        {col1 => 'Custom HTML in document head',
  601:                                         col2 => 'Value'}],
  602:                             print => \&print_login,
  603:                             modify => \&modify_login,
  604:                            };
  605:     }
  606: 
  607:     my @roles = ('student','coordinator','author','admin');
  608:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  609:     &Apache::lonhtmlcommon::add_breadcrumb
  610:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  611:       text=>"Settings to display/modify"});
  612:     my $confname = $dom.'-domainconfig';
  613: 
  614:     if ($phase eq 'process') {
  615:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  616:                                                               \%prefs,\%domconfig,$confname,\@roles);
  617:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  618:             $r->rflush();
  619:             &devalidate_remote_domconfs($dom,$result);
  620:         }
  621:     } elsif ($phase eq 'display') {
  622:         my $js = &recaptcha_js().
  623:                  &toggle_display_js();
  624:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  625:             my ($othertitle,$usertypes,$types) =
  626:                 &Apache::loncommon::sorted_inst_types($dom);
  627:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  628:                                           $domconfig{'loadbalancing'}).
  629:                    &new_spares_js().
  630:                    &common_domprefs_js().
  631:                    &Apache::loncommon::javascript_array_indexof();
  632:         }
  633:         if (grep(/^requestcourses$/,@actions)) {
  634:             my $javascript_validations;
  635:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  636:             $js .= <<END;
  637: <script type="text/javascript">
  638: $javascript_validations
  639: </script>
  640: $coursebrowserjs
  641: END
  642:         }
  643:         if (grep(/^selfcreation$/,@actions)) {
  644:             $js .= &selfcreate_javascript();
  645:         }
  646:         if (grep(/^contacts$/,@actions)) {
  647:             $js .= &contacts_javascript();
  648:         }
  649:         if (grep(/^scantron$/,@actions)) {
  650:             $js .= &scantron_javascript();
  651:         }
  652:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  653:     } else {
  654: # check if domconfig user exists for the domain.
  655:         my $servadm = $r->dir_config('lonAdmEMail');
  656:         my ($configuserok,$author_ok,$switchserver) =
  657:             &config_check($dom,$confname,$servadm);
  658:         unless ($configuserok eq 'ok') {
  659:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  660:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  661:                           $confname).
  662:                       '<br />'
  663:             );
  664:             if ($switchserver) {
  665:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  666:                           '<br />'.
  667:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  668:                           '<br />'.
  669:                           &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).
  670:                           '<br />'.
  671:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  672:                 );
  673:             } else {
  674:                 $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.').
  675:                           '<br />'.
  676:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  677:                 );
  678:             }
  679:             $r->print(&Apache::loncommon::end_page());
  680:             return OK;
  681:         }
  682:         if (keys(%domconfig) == 0) {
  683:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  684:             my @ids=&Apache::lonnet::current_machine_ids();
  685:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  686:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  687:                 my @loginimages = ('img','logo','domlogo','login');
  688:                 my $custom_img_count = 0;
  689:                 foreach my $img (@loginimages) {
  690:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  691:                         $custom_img_count ++;
  692:                     }
  693:                 }
  694:                 foreach my $role (@roles) {
  695:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  696:                         $custom_img_count ++;
  697:                     }
  698:                 }
  699:                 if ($custom_img_count > 0) {
  700:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  701:                     my $switch_server = &check_switchserver($dom,$confname);
  702:                     $r->print(
  703:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  704:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  705:     &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 />'.
  706:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  707:                     if ($switch_server) {
  708:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  709:                     }
  710:                     $r->print(&Apache::loncommon::end_page());
  711:                     return OK;
  712:                 }
  713:             }
  714:         }
  715:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  716:     }
  717:     return OK;
  718: }
  719: 
  720: sub process_changes {
  721:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  722:     my %domconfig;
  723:     if (ref($values) eq 'HASH') {
  724:         %domconfig = %{$values};
  725:     }
  726:     my $output;
  727:     if ($action eq 'login') {
  728:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  729:     } elsif ($action eq 'rolecolors') {
  730:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  731:                                      $lastactref,%domconfig);
  732:     } elsif ($action eq 'quotas') {
  733:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  734:     } elsif ($action eq 'autoenroll') {
  735:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  736:     } elsif ($action eq 'autoupdate') {
  737:         $output = &modify_autoupdate($dom,%domconfig);
  738:     } elsif ($action eq 'autocreate') {
  739:         $output = &modify_autocreate($dom,%domconfig);
  740:     } elsif ($action eq 'directorysrch') {
  741:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  742:     } elsif ($action eq 'usercreation') {
  743:         $output = &modify_usercreation($dom,%domconfig);
  744:     } elsif ($action eq 'selfcreation') {
  745:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  746:     } elsif ($action eq 'usermodification') {
  747:         $output = &modify_usermodification($dom,%domconfig);
  748:     } elsif ($action eq 'contacts') {
  749:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  750:     } elsif ($action eq 'defaults') {
  751:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  752:     } elsif ($action eq 'scantron') {
  753:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  754:     } elsif ($action eq 'coursecategories') {
  755:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  756:     } elsif ($action eq 'serverstatuses') {
  757:         $output = &modify_serverstatuses($dom,%domconfig);
  758:     } elsif ($action eq 'requestcourses') {
  759:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  760:     } elsif ($action eq 'requestauthor') {
  761:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  762:     } elsif ($action eq 'helpsettings') {
  763:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  764:     } elsif ($action eq 'coursedefaults') {
  765:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  766:     } elsif ($action eq 'selfenrollment') {
  767:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  768:     } elsif ($action eq 'usersessions') {
  769:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  770:     } elsif ($action eq 'loadbalancing') {
  771:         $output = &modify_loadbalancing($dom,%domconfig);
  772:     } elsif ($action eq 'ltitools') {
  773:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  774:     } elsif ($action eq 'ssl') {
  775:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  776:     } elsif ($action eq 'trust') {
  777:         $output = &modify_trust($dom,$lastactref,%domconfig);
  778:     } elsif ($action eq 'lti') {
  779:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  780:     } elsif ($action eq 'privacy') {
  781:         $output = &modify_privacy($dom,%domconfig);
  782:     } elsif ($action eq 'passwords') {
  783:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  784:     }
  785:     return $output;
  786: }
  787: 
  788: sub print_config_box {
  789:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  790:     my $rowtotal = 0;
  791:     my $output;
  792:     if ($action eq 'coursecategories') {
  793:         $output = &coursecategories_javascript($settings);
  794:     } elsif ($action eq 'defaults') {
  795:         $output = &defaults_javascript($settings); 
  796:     } elsif ($action eq 'passwords') {
  797:         $output = &passwords_javascript();
  798:     } elsif ($action eq 'helpsettings') {
  799:         my (%privs,%levelscurrent);
  800:         my %full=();
  801:         my %levels=(
  802:                      course => {},
  803:                      domain => {},
  804:                      system => {},
  805:                    );
  806:         my $context = 'domain';
  807:         my $crstype = 'Course';
  808:         my $formname = 'display';
  809:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  810:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  811:         $output =
  812:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  813:                                                       \@templateroles);
  814:     } elsif ($action eq 'ltitools') {
  815:         $output .= &ltitools_javascript($settings);
  816:     } elsif ($action eq 'lti') {
  817:         $output .= &lti_javascript($settings);
  818:     }
  819:     $output .=
  820:          '<table class="LC_nested_outer">
  821:           <tr>
  822:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  823:            &mt($item->{text}).'&nbsp;'.
  824:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  825:           '</tr>';
  826:     $rowtotal ++;
  827:     my $numheaders = 1;
  828:     if (ref($item->{'header'}) eq 'ARRAY') {
  829:         $numheaders = scalar(@{$item->{'header'}});
  830:     }
  831:     if ($numheaders > 1) {
  832:         my $colspan = '';
  833:         my $rightcolspan = '';
  834:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  835:             ($action eq 'directorysrch') ||
  836:             (($action eq 'login') && ($numheaders < 4))) {
  837:             $colspan = ' colspan="2"';
  838:         }
  839:         if ($action eq 'usersessions') {
  840:             $rightcolspan = ' colspan="3"'; 
  841:         }
  842:         $output .= '
  843:           <tr>
  844:            <td>
  845:             <table class="LC_nested">
  846:              <tr class="LC_info_row">
  847:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  848:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  849:              </tr>';
  850:         $rowtotal ++;
  851:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  852:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  853:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  854:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  855:             ($action eq 'contacts') || ($action eq 'privacy')) {
  856:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  857:         } elsif ($action eq 'passwords') {
  858:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  859:         } elsif ($action eq 'coursecategories') {
  860:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  861:         } elsif ($action eq 'scantron') {
  862:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  863:         } elsif ($action eq 'login') {
  864:             if ($numheaders == 4) {
  865:                 $colspan = ' colspan="2"';
  866:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  867:             } else {
  868:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  869:             }
  870:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  871:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  872:         } elsif ($action eq 'rolecolors') {
  873:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  874:         }
  875:         $output .= '
  876:            </table>
  877:           </td>
  878:          </tr>
  879:          <tr>
  880:            <td>
  881:             <table class="LC_nested">
  882:              <tr class="LC_info_row">
  883:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  884:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  885:              </tr>';
  886:             $rowtotal ++;
  887:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  888:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  889:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  890:             ($action eq 'trust') || ($action eq 'contacts') ||
  891:             ($action eq 'privacy') || ($action eq 'passwords')) {
  892:             if ($action eq 'coursecategories') {
  893:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  894:                 $colspan = ' colspan="2"';
  895:             } elsif ($action eq 'trust') {
  896:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  897:             } elsif ($action eq 'passwords') {
  898:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  899:             } else {
  900:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  901:             }
  902:             if ($action eq 'trust') {
  903:                 $output .= '
  904:             </table>
  905:           </td>
  906:          </tr>';
  907:                 my @trusthdrs = qw(2 3 4 5 6 7);
  908:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  909:                 for (my $i=0; $i<@trusthdrs; $i++) {
  910:                     $output .= '
  911:          <tr>
  912:            <td>
  913:             <table class="LC_nested">
  914:              <tr class="LC_info_row">
  915:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  916:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  917:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  918:             </table>
  919:           </td>
  920:          </tr>';
  921:                 }
  922:                 $output .= '
  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'}->[8]->{'col1'}).'</td>
  928:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  929:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  930:             } else {
  931:                 $output .= '
  932:            </table>
  933:           </td>
  934:          </tr>
  935:          <tr>
  936:            <td>
  937:             <table class="LC_nested">
  938:              <tr class="LC_info_row">
  939:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  940:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  941:              </tr>'."\n";
  942:                 if ($action eq 'coursecategories') {
  943:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  944:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'passwords')) {
  945:                     if ($action eq 'passwords') {
  946:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
  947:                     } else {
  948:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
  949:                     }
  950:                     $output .= '
  951:              </tr>
  952:             </table>
  953:            </td>
  954:           </tr>
  955:           <tr>
  956:            <td>
  957:             <table class="LC_nested">
  958:              <tr class="LC_info_row">
  959:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  960:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
  961:                     if ($action eq 'passwords') {
  962:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
  963:                     } else {
  964:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  965:                     }
  966:                     $output .= '
  967:             </table>
  968:           </td>
  969:          </tr>
  970:          <tr>';
  971:                 } else {
  972:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  973:                 }
  974:             }
  975:             $rowtotal ++;
  976:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  977:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  978:                  ($action eq 'helpsettings')) {
  979:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  980:         } elsif ($action eq 'scantron') {
  981:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
  982:         } elsif ($action eq 'ssl') {
  983:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  984:             </table>
  985:           </td>
  986:          </tr>
  987:          <tr>
  988:            <td>
  989:             <table class="LC_nested">
  990:              <tr class="LC_info_row">
  991:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  992:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  993:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  994:             </table>
  995:           </td>
  996:          </tr>
  997:          <tr>
  998:            <td>
  999:             <table class="LC_nested">
 1000:              <tr class="LC_info_row">
 1001:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1002:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1003:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1004:         } elsif ($action eq 'login') {
 1005:             if ($numheaders == 4) {
 1006:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1007:            </table>
 1008:           </td>
 1009:          </tr>
 1010:          <tr>
 1011:            <td>
 1012:             <table class="LC_nested">
 1013:              <tr class="LC_info_row">
 1014:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1015:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1016:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1017:                 $rowtotal ++;
 1018:             } else {
 1019:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1020:             }
 1021:             $output .= '
 1022:            </table>
 1023:           </td>
 1024:          </tr>
 1025:          <tr>
 1026:            <td>
 1027:             <table class="LC_nested">
 1028:              <tr class="LC_info_row">';
 1029:             if ($numheaders == 4) {
 1030:                 $output .= '
 1031:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1032:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1033:              </tr>';
 1034:             } else {
 1035:                 $output .= '
 1036:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1037:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1038:              </tr>';
 1039:             }
 1040:             $rowtotal ++;
 1041:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
 1042:         } elsif ($action eq 'requestcourses') {
 1043:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1044:             $rowtotal ++;
 1045:             $output .= &print_studentcode($settings,\$rowtotal).'
 1046:            </table>
 1047:           </td>
 1048:          </tr>
 1049:          <tr>
 1050:            <td>
 1051:             <table class="LC_nested">
 1052:              <tr class="LC_info_row">
 1053:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1054:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1055:                        &textbookcourses_javascript($settings).
 1056:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1057:             </table>
 1058:            </td>
 1059:           </tr>
 1060:          <tr>
 1061:            <td>
 1062:             <table class="LC_nested">
 1063:              <tr class="LC_info_row">
 1064:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1065:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1066:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1067:             </table>
 1068:            </td>
 1069:           </tr>
 1070:           <tr>
 1071:            <td>
 1072:             <table class="LC_nested">
 1073:              <tr class="LC_info_row">
 1074:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1075:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1076:              </tr>'.
 1077:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1078:         } elsif ($action eq 'requestauthor') {
 1079:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1080:             $rowtotal ++;
 1081:         } elsif ($action eq 'rolecolors') {
 1082:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1083:            </table>
 1084:           </td>
 1085:          </tr>
 1086:          <tr>
 1087:            <td>
 1088:             <table class="LC_nested">
 1089:              <tr class="LC_info_row">
 1090:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1091:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1092:               <td class="LC_right_item" style="vertical-align: top">'.
 1093:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1094:              </tr>'.
 1095:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1096:            </table>
 1097:           </td>
 1098:          </tr>
 1099:          <tr>
 1100:            <td>
 1101:             <table class="LC_nested">
 1102:              <tr class="LC_info_row">
 1103:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1104:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1105:              </tr>'.
 1106:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1107:             $rowtotal += 2;
 1108:         }
 1109:     } else {
 1110:         $output .= '
 1111:           <tr>
 1112:            <td>
 1113:             <table class="LC_nested">
 1114:              <tr class="LC_info_row">';
 1115:         if ($action eq 'login') {
 1116:             $output .= '  
 1117:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1118:         } elsif ($action eq 'serverstatuses') {
 1119:             $output .= '
 1120:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1121:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1122: 
 1123:         } else {
 1124:             $output .= '
 1125:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1126:         }
 1127:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1128:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1129:                        &mt($item->{'header'}->[0]->{'col2'});
 1130:             if ($action eq 'serverstatuses') {
 1131:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1132:             } 
 1133:         } else {
 1134:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1135:                        &mt($item->{'header'}->[0]->{'col2'});
 1136:         }
 1137:         $output .= '</td>';
 1138:         if ($item->{'header'}->[0]->{'col3'}) {
 1139:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1140:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1141:                             &mt($item->{'header'}->[0]->{'col3'});
 1142:             } else {
 1143:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1144:                            &mt($item->{'header'}->[0]->{'col3'});
 1145:             }
 1146:             if ($action eq 'serverstatuses') {
 1147:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1148:             }
 1149:             $output .= '</td>';
 1150:         }
 1151:         if ($item->{'header'}->[0]->{'col4'}) {
 1152:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1153:                        &mt($item->{'header'}->[0]->{'col4'});
 1154:         }
 1155:         $output .= '</tr>';
 1156:         $rowtotal ++;
 1157:         if ($action eq 'quotas') {
 1158:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1159:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1160:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1161:                  ($action eq 'ltitools') || ($action eq 'lti')) {
 1162:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1163:         }
 1164:     }
 1165:     $output .= '
 1166:    </table>
 1167:   </td>
 1168:  </tr>
 1169: </table><br />';
 1170:     return ($output,$rowtotal);
 1171: }
 1172: 
 1173: sub print_login {
 1174:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1175:     my ($css_class,$datatable);
 1176:     my %choices = &login_choices();
 1177: 
 1178:     if ($caller eq 'service') {
 1179:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1180:         my $choice = $choices{'disallowlogin'};
 1181:         $css_class = ' class="LC_odd_row"';
 1182:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1183:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1184:                       '<th>'.$choices{'server'}.'</th>'.
 1185:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1186:                       '<th>'.$choices{'custompath'}.'</th>'.
 1187:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1188:         my %disallowed;
 1189:         if (ref($settings) eq 'HASH') {
 1190:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1191:                %disallowed = %{$settings->{'loginvia'}};
 1192:             }
 1193:         }
 1194:         foreach my $lonhost (sort(keys(%servers))) {
 1195:             my $direct = 'selected="selected"';
 1196:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1197:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1198:                     $direct = '';
 1199:                 }
 1200:             }
 1201:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1202:                           '<td><select name="'.$lonhost.'_server">'.
 1203:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1204:                           '</option>';
 1205:             foreach my $hostid (sort(keys(%servers))) {
 1206:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1207:                 my $selected = '';
 1208:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1209:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1210:                         $selected = 'selected="selected"';
 1211:                     }
 1212:                 }
 1213:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1214:                               $servers{$hostid}.'</option>';
 1215:             }
 1216:             $datatable .= '</select></td>'.
 1217:                           '<td><select name="'.$lonhost.'_serverpath">';
 1218:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1219:                 my $pathname = $path;
 1220:                 if ($path eq 'custom') {
 1221:                     $pathname = &mt('Custom Path').' ->';
 1222:                 }
 1223:                 my $selected = '';
 1224:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1225:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1226:                         $selected = 'selected="selected"';
 1227:                     }
 1228:                 } elsif ($path eq '') {
 1229:                     $selected = 'selected="selected"';
 1230:                 }
 1231:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1232:             }
 1233:             $datatable .= '</select></td>';
 1234:             my ($custom,$exempt);
 1235:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1236:                 $custom = $disallowed{$lonhost}{'custompath'};
 1237:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1238:             }
 1239:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1240:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1241:                           '</tr>';
 1242:         }
 1243:         $datatable .= '</table></td></tr>';
 1244:         return $datatable;
 1245:     } elsif ($caller eq 'page') {
 1246:         my %defaultchecked = ( 
 1247:                                'coursecatalog' => 'on',
 1248:                                'helpdesk'      => 'on',
 1249:                                'adminmail'     => 'off',
 1250:                                'newuser'       => 'off',
 1251:                              );
 1252:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1253:         my (%checkedon,%checkedoff);
 1254:         foreach my $item (@toggles) {
 1255:             if ($defaultchecked{$item} eq 'on') { 
 1256:                 $checkedon{$item} = ' checked="checked" ';
 1257:                 $checkedoff{$item} = ' ';
 1258:             } elsif ($defaultchecked{$item} eq 'off') {
 1259:                 $checkedoff{$item} = ' checked="checked" ';
 1260:                 $checkedon{$item} = ' ';
 1261:             }
 1262:         }
 1263:         my @images = ('img','logo','domlogo','login');
 1264:         my @logintext = ('textcol','bgcol');
 1265:         my @bgs = ('pgbg','mainbg','sidebg');
 1266:         my @links = ('link','alink','vlink');
 1267:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1268:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1269:         my (%is_custom,%designs);
 1270:         my %defaults = (
 1271:                        font => $defaultdesign{'login.font'},
 1272:                        );
 1273:         foreach my $item (@images) {
 1274:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1275:             $defaults{'showlogo'}{$item} = 1;
 1276:         }
 1277:         foreach my $item (@bgs) {
 1278:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1279:         }
 1280:         foreach my $item (@logintext) {
 1281:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1282:         }
 1283:         foreach my $item (@links) {
 1284:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1285:         }
 1286:         if (ref($settings) eq 'HASH') {
 1287:             foreach my $item (@toggles) {
 1288:                 if ($settings->{$item} eq '1') {
 1289:                     $checkedon{$item} =  ' checked="checked" ';
 1290:                     $checkedoff{$item} = ' ';
 1291:                 } elsif ($settings->{$item} eq '0') {
 1292:                     $checkedoff{$item} =  ' checked="checked" ';
 1293:                     $checkedon{$item} = ' ';
 1294:                 }
 1295:             }
 1296:             foreach my $item (@images) {
 1297:                 if (defined($settings->{$item})) {
 1298:                     $designs{$item} = $settings->{$item};
 1299:                     $is_custom{$item} = 1;
 1300:                 }
 1301:                 if (defined($settings->{'showlogo'}{$item})) {
 1302:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1303:                 }
 1304:             }
 1305:             foreach my $item (@logintext) {
 1306:                 if ($settings->{$item} ne '') {
 1307:                     $designs{'logintext'}{$item} = $settings->{$item};
 1308:                     $is_custom{$item} = 1;
 1309:                 }
 1310:             }
 1311:             if ($settings->{'font'} ne '') {
 1312:                 $designs{'font'} = $settings->{'font'};
 1313:                 $is_custom{'font'} = 1;
 1314:             }
 1315:             foreach my $item (@bgs) {
 1316:                 if ($settings->{$item} ne '') {
 1317:                     $designs{'bgs'}{$item} = $settings->{$item};
 1318:                     $is_custom{$item} = 1;
 1319:                 }
 1320:             }
 1321:             foreach my $item (@links) {
 1322:                 if ($settings->{$item} ne '') {
 1323:                     $designs{'links'}{$item} = $settings->{$item};
 1324:                     $is_custom{$item} = 1;
 1325:                 }
 1326:             }
 1327:         } else {
 1328:             if ($designhash{$dom.'.login.font'} ne '') {
 1329:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1330:                 $is_custom{'font'} = 1;
 1331:             }
 1332:             foreach my $item (@images) {
 1333:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1334:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1335:                     $is_custom{$item} = 1;
 1336:                 }
 1337:             }
 1338:             foreach my $item (@bgs) {
 1339:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1340:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1341:                     $is_custom{$item} = 1;
 1342:                 }
 1343:             }
 1344:             foreach my $item (@links) {
 1345:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1346:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1347:                     $is_custom{$item} = 1;
 1348:                 }
 1349:             }
 1350:         }
 1351:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1352:                                                       logo => 'Institution Logo',
 1353:                                                       domlogo => 'Domain Logo',
 1354:                                                       login => 'Login box');
 1355:         my $itemcount = 1;
 1356:         foreach my $item (@toggles) {
 1357:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1358:             $datatable .=  
 1359:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1360:                 '</td><td>'.
 1361:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1362:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1363:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1364:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1365:                 '</tr>';
 1366:             $itemcount ++;
 1367:         }
 1368:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1369:         $datatable .= '</tr></table></td></tr>';
 1370:     } elsif ($caller eq 'help') {
 1371:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1372:         my $switchserver = &check_switchserver($dom,$confname);
 1373:         my $itemcount = 1;
 1374:         $defaulturl = '/adm/loginproblems.html';
 1375:         $defaulttype = 'default';
 1376:         %lt = &Apache::lonlocal::texthash (
 1377:                      del     => 'Delete?',
 1378:                      rep     => 'Replace:',
 1379:                      upl     => 'Upload:',
 1380:                      default => 'Default',
 1381:                      custom  => 'Custom',
 1382:                                              );
 1383:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1384:         my @currlangs;
 1385:         if (ref($settings) eq 'HASH') {
 1386:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1387:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1388:                     next if ($settings->{'helpurl'}{$key} eq '');
 1389:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1390:                     $type{$key} = 'custom';
 1391:                     unless ($key eq 'nolang') {
 1392:                         push(@currlangs,$key);
 1393:                     }
 1394:                 }
 1395:             } elsif ($settings->{'helpurl'} ne '') {
 1396:                 $type{'nolang'} = 'custom';
 1397:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1398:             }
 1399:         }
 1400:         foreach my $lang ('nolang',sort(@currlangs)) {
 1401:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1402:             $datatable .= '<tr'.$css_class.'>';
 1403:             if ($url{$lang} eq '') {
 1404:                 $url{$lang} = $defaulturl;
 1405:             }
 1406:             if ($type{$lang} eq '') {
 1407:                 $type{$lang} = $defaulttype;
 1408:             }
 1409:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1410:             if ($lang eq 'nolang') {
 1411:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1412:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1413:             } else {
 1414:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1415:                                   $langchoices{$lang},
 1416:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1417:             }
 1418:             $datatable .= '</span></td>'."\n".
 1419:                           '<td class="LC_left_item">';
 1420:             if ($type{$lang} eq 'custom') {
 1421:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1422:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1423:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1424:             } else {
 1425:                 $datatable .= $lt{'upl'};
 1426:             }
 1427:             $datatable .='<br />';
 1428:             if ($switchserver) {
 1429:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1430:             } else {
 1431:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1432:             }
 1433:             $datatable .= '</td></tr>';
 1434:             $itemcount ++;
 1435:         }
 1436:         my @addlangs;
 1437:         foreach my $lang (sort(keys(%langchoices))) {
 1438:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1439:             push(@addlangs,$lang);
 1440:         }
 1441:         if (@addlangs > 0) {
 1442:             my %toadd;
 1443:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1444:             $toadd{''} = &mt('Select');
 1445:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1446:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1447:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1448:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1449:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1450:             if ($switchserver) {
 1451:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1452:             } else {
 1453:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1454:             }
 1455:             $datatable .= '</td></tr>';
 1456:             $itemcount ++;
 1457:         }
 1458:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1459:     } elsif ($caller eq 'headtag') {
 1460:         my %domservers = &Apache::lonnet::get_servers($dom);
 1461:         my $choice = $choices{'headtag'};
 1462:         $css_class = ' class="LC_odd_row"';
 1463:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1464:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1465:                       '<th>'.$choices{'current'}.'</th>'.
 1466:                       '<th>'.$choices{'action'}.'</th>'.
 1467:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1468:         my (%currurls,%currexempt);
 1469:         if (ref($settings) eq 'HASH') {
 1470:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1471:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1472:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1473:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1474:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1475:                     }
 1476:                 }
 1477:             }
 1478:         }
 1479:         my %lt = &Apache::lonlocal::texthash(
 1480:                                                del  => 'Delete?',
 1481:                                                rep  => 'Replace:',
 1482:                                                upl  => 'Upload:',
 1483:                                                curr => 'View contents',
 1484:                                                none => 'None',
 1485:         );
 1486:         my $switchserver = &check_switchserver($dom,$confname);
 1487:         foreach my $lonhost (sort(keys(%domservers))) {
 1488:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1489:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1490:             if ($currurls{$lonhost}) {
 1491:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1492:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1493:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1494:                               '">'.$lt{'curr'}.'</a></td>'.
 1495:                               '<td><span class="LC_nobreak"><label>'.
 1496:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1497:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1498:             } else {
 1499:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1500:             }
 1501:             $datatable .='<br />';
 1502:             if ($switchserver) {
 1503:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1504:             } else {
 1505:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1506:             }
 1507:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1508:         }
 1509:         $datatable .= '</table></td></tr>';
 1510:     }
 1511:     return $datatable;
 1512: }
 1513: 
 1514: sub login_choices {
 1515:     my %choices =
 1516:         &Apache::lonlocal::texthash (
 1517:             coursecatalog => 'Display Course/Community Catalog link?',
 1518:             adminmail     => "Display Administrator's E-mail Address?",
 1519:             helpdesk      => 'Display "Contact Helpdesk" link',
 1520:             disallowlogin => "Login page requests redirected",
 1521:             hostid        => "Server",
 1522:             server        => "Redirect to:",
 1523:             serverpath    => "Path",
 1524:             custompath    => "Custom", 
 1525:             exempt        => "Exempt IP(s)",
 1526:             directlogin   => "No redirect",
 1527:             newuser       => "Link to create a user account",
 1528:             img           => "Header",
 1529:             logo          => "Main Logo",
 1530:             domlogo       => "Domain Logo",
 1531:             login         => "Log-in Header", 
 1532:             textcol       => "Text color",
 1533:             bgcol         => "Box color",
 1534:             bgs           => "Background colors",
 1535:             links         => "Link colors",
 1536:             font          => "Font color",
 1537:             pgbg          => "Header",
 1538:             mainbg        => "Page",
 1539:             sidebg        => "Login box",
 1540:             link          => "Link",
 1541:             alink         => "Active link",
 1542:             vlink         => "Visited link",
 1543:             headtag       => "Custom markup",
 1544:             action        => "Action",
 1545:             current       => "Current",
 1546:         );
 1547:     return %choices;
 1548: }
 1549: 
 1550: sub print_rolecolors {
 1551:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1552:     my %choices = &color_font_choices();
 1553:     my @bgs = ('pgbg','tabbg','sidebg');
 1554:     my @links = ('link','alink','vlink');
 1555:     my @images = ('img');
 1556:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1557:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1558:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1559:     my (%is_custom,%designs);
 1560:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1561:     if (ref($settings) eq 'HASH') {
 1562:         if (ref($settings->{$role}) eq 'HASH') {
 1563:             if ($settings->{$role}->{'img'} ne '') {
 1564:                 $designs{'img'} = $settings->{$role}->{'img'};
 1565:                 $is_custom{'img'} = 1;
 1566:             }
 1567:             if ($settings->{$role}->{'font'} ne '') {
 1568:                 $designs{'font'} = $settings->{$role}->{'font'};
 1569:                 $is_custom{'font'} = 1;
 1570:             }
 1571:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1572:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1573:                 $is_custom{'fontmenu'} = 1;
 1574:             }
 1575:             foreach my $item (@bgs) {
 1576:                 if ($settings->{$role}->{$item} ne '') {
 1577:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1578:                     $is_custom{$item} = 1;
 1579:                 }
 1580:             }
 1581:             foreach my $item (@links) {
 1582:                 if ($settings->{$role}->{$item} ne '') {
 1583:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1584:                     $is_custom{$item} = 1;
 1585:                 }
 1586:             }
 1587:         }
 1588:     } else {
 1589:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1590:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1591:             $is_custom{'img'} = 1;
 1592:         }
 1593:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1594:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1595:             $is_custom{'fontmenu'} = 1; 
 1596:         }
 1597:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1598:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1599:             $is_custom{'font'} = 1;
 1600:         }
 1601:         foreach my $item (@bgs) {
 1602:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1603:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1604:                 $is_custom{$item} = 1;
 1605:             
 1606:             }
 1607:         }
 1608:         foreach my $item (@links) {
 1609:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1610:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1611:                 $is_custom{$item} = 1;
 1612:             }
 1613:         }
 1614:     }
 1615:     my $itemcount = 1;
 1616:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1617:     $datatable .= '</tr></table></td></tr>';
 1618:     return $datatable;
 1619: }
 1620: 
 1621: sub role_defaults {
 1622:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1623:     my %defaults;
 1624:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1625:         return %defaults;
 1626:     }
 1627:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1628:     if ($role eq 'login') {
 1629:         %defaults = (
 1630:                        font => $defaultdesign{$role.'.font'},
 1631:                     );
 1632:         if (ref($logintext) eq 'ARRAY') {
 1633:             foreach my $item (@{$logintext}) {
 1634:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1635:             }
 1636:         }
 1637:         foreach my $item (@{$images}) {
 1638:             $defaults{'showlogo'}{$item} = 1;
 1639:         }
 1640:     } else {
 1641:         %defaults = (
 1642:                        img => $defaultdesign{$role.'.img'},
 1643:                        font => $defaultdesign{$role.'.font'},
 1644:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1645:                     );
 1646:     }
 1647:     foreach my $item (@{$bgs}) {
 1648:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1649:     }
 1650:     foreach my $item (@{$links}) {
 1651:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1652:     }
 1653:     foreach my $item (@{$images}) {
 1654:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1655:     }
 1656:     return %defaults;
 1657: }
 1658: 
 1659: sub display_color_options {
 1660:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1661:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1662:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1663:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1664:     my $datatable = '<tr'.$css_class.'>'.
 1665:         '<td>'.$choices->{'font'}.'</td>';
 1666:     if (!$is_custom->{'font'}) {
 1667:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1668:     } else {
 1669:         $datatable .= '<td>&nbsp;</td>';
 1670:     }
 1671:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1672: 
 1673:     $datatable .= '<td><span class="LC_nobreak">'.
 1674:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1675:                   ' value="'.$current_color.'" />&nbsp;'.
 1676:                   '&nbsp;</span></td></tr>';
 1677:     unless ($role eq 'login') { 
 1678:         $datatable .= '<tr'.$css_class.'>'.
 1679:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1680:         if (!$is_custom->{'fontmenu'}) {
 1681:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1682:         } else {
 1683:             $datatable .= '<td>&nbsp;</td>';
 1684:         }
 1685: 	$current_color = $designs->{'fontmenu'} ?
 1686: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1687:         $datatable .= '<td><span class="LC_nobreak">'.
 1688:                       '<input class="colorchooser" type="text" size="10" name="'
 1689: 		      .$role.'_fontmenu"'.
 1690:                       ' value="'.$current_color.'" />&nbsp;'.
 1691:                       '&nbsp;</span></td></tr>';
 1692:     }
 1693:     my $switchserver = &check_switchserver($dom,$confname);
 1694:     foreach my $img (@{$images}) {
 1695: 	$itemcount ++;
 1696:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1697:         $datatable .= '<tr'.$css_class.'>'.
 1698:                       '<td>'.$choices->{$img};
 1699:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1700:         if ($role eq 'login') {
 1701:             if ($img eq 'login') {
 1702:                 $login_hdr_pick =
 1703:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1704:                 $logincolors =
 1705:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1706:                                        $designs,$defaults);
 1707:             } elsif ($img ne 'domlogo') {
 1708:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1709:             }
 1710:         }
 1711:         $datatable .= '</td>';
 1712:         if ($designs->{$img} ne '') {
 1713:             $imgfile = $designs->{$img};
 1714: 	    $img_import = ($imgfile =~ m{^/adm/});
 1715:         } else {
 1716:             $imgfile = $defaults->{$img};
 1717:         }
 1718:         if ($imgfile) {
 1719:             my ($showfile,$fullsize);
 1720:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1721:                 my $urldir = $1;
 1722:                 my $filename = $2;
 1723:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1724:                 if (@info) {
 1725:                     my $thumbfile = 'tn-'.$filename;
 1726:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1727:                     if (@thumb) {
 1728:                         $showfile = $urldir.'/'.$thumbfile;
 1729:                     } else {
 1730:                         $showfile = $imgfile;
 1731:                     }
 1732:                 } else {
 1733:                     $showfile = '';
 1734:                 }
 1735:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1736:                 $showfile = $imgfile;
 1737:                 my $imgdir = $1;
 1738:                 my $filename = $2;
 1739:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1740:                     $showfile = "/$imgdir/tn-".$filename;
 1741:                 } else {
 1742:                     my $input = $londocroot.$imgfile;
 1743:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1744:                     if (!-e $output) {
 1745:                         my ($width,$height) = &thumb_dimensions();
 1746:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1747:                         if ($fullwidth ne '' && $fullheight ne '') {
 1748:                             if ($fullwidth > $width && $fullheight > $height) { 
 1749:                                 my $size = $width.'x'.$height;
 1750:                                 my @args = ('convert','-sample',$size,$input,$output);
 1751:                                 system({$args[0]} @args);
 1752:                                 $showfile = "/$imgdir/tn-".$filename;
 1753:                             }
 1754:                         }
 1755:                     }
 1756:                 }
 1757:             }
 1758:             if ($showfile) {
 1759:                 if ($showfile =~ m{^/(adm|res)/}) {
 1760:                     if ($showfile =~ m{^/res/}) {
 1761:                         my $local_showfile =
 1762:                             &Apache::lonnet::filelocation('',$showfile);
 1763:                         &Apache::lonnet::repcopy($local_showfile);
 1764:                     }
 1765:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1766:                 }
 1767:                 if ($imgfile) {
 1768:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1769:                         if ($imgfile =~ m{^/res/}) {
 1770:                             my $local_imgfile =
 1771:                                 &Apache::lonnet::filelocation('',$imgfile);
 1772:                             &Apache::lonnet::repcopy($local_imgfile);
 1773:                         }
 1774:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1775:                     } else {
 1776:                         $fullsize = $imgfile;
 1777:                     }
 1778:                 }
 1779:                 $datatable .= '<td>';
 1780:                 if ($img eq 'login') {
 1781:                     $datatable .= $login_hdr_pick;
 1782:                 } 
 1783:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1784:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1785:             } else {
 1786:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1787:                               &mt('Upload:').'<br />';
 1788:             }
 1789:         } else {
 1790:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1791:                           &mt('Upload:').'<br />';
 1792:         }
 1793:         if ($switchserver) {
 1794:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1795:         } else {
 1796:             if ($img ne 'login') { # suppress file selection for Log-in header
 1797:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1798:             }
 1799:         }
 1800:         $datatable .= '</td></tr>';
 1801:     }
 1802:     $itemcount ++;
 1803:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1804:     $datatable .= '<tr'.$css_class.'>'.
 1805:                   '<td>'.$choices->{'bgs'}.'</td>';
 1806:     my $bgs_def;
 1807:     foreach my $item (@{$bgs}) {
 1808:         if (!$is_custom->{$item}) {
 1809:             $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>';
 1810:         }
 1811:     }
 1812:     if ($bgs_def) {
 1813:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1814:     } else {
 1815:         $datatable .= '<td>&nbsp;</td>';
 1816:     }
 1817:     $datatable .= '<td class="LC_right_item">'.
 1818:                   '<table border="0"><tr>';
 1819: 
 1820:     foreach my $item (@{$bgs}) {
 1821:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1822: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1823:         if ($designs->{'bgs'}{$item}) {
 1824:             $datatable .= '&nbsp;';
 1825:         }
 1826:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1827:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1828:     }
 1829:     $datatable .= '</tr></table></td></tr>';
 1830:     $itemcount ++;
 1831:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1832:     $datatable .= '<tr'.$css_class.'>'.
 1833:                   '<td>'.$choices->{'links'}.'</td>';
 1834:     my $links_def;
 1835:     foreach my $item (@{$links}) {
 1836:         if (!$is_custom->{$item}) {
 1837:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1838:         }
 1839:     }
 1840:     if ($links_def) {
 1841:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1842:     } else {
 1843:         $datatable .= '<td>&nbsp;</td>';
 1844:     }
 1845:     $datatable .= '<td class="LC_right_item">'.
 1846:                   '<table border="0"><tr>';
 1847:     foreach my $item (@{$links}) {
 1848: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1849:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1850:         if ($designs->{'links'}{$item}) {
 1851:             $datatable.='&nbsp;';
 1852:         }
 1853:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1854:                       '" /></td>';
 1855:     }
 1856:     $$rowtotal += $itemcount;
 1857:     return $datatable;
 1858: }
 1859: 
 1860: sub logo_display_options {
 1861:     my ($img,$defaults,$designs) = @_;
 1862:     my $checkedon;
 1863:     if (ref($defaults) eq 'HASH') {
 1864:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1865:             if ($defaults->{'showlogo'}{$img}) {
 1866:                 $checkedon = 'checked="checked" ';     
 1867:             }
 1868:         } 
 1869:     }
 1870:     if (ref($designs) eq 'HASH') {
 1871:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1872:             if (defined($designs->{'showlogo'}{$img})) {
 1873:                 if ($designs->{'showlogo'}{$img} == 0) {
 1874:                     $checkedon = '';
 1875:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1876:                     $checkedon = 'checked="checked" ';
 1877:                 }
 1878:             }
 1879:         }
 1880:     }
 1881:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1882:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1883:            &mt('show').'</label>'."\n";
 1884: }
 1885: 
 1886: sub login_header_options  {
 1887:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1888:     my $output = '';
 1889:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1890:         $output .= &mt('Text default(s):').'<br />';
 1891:         if (!$is_custom->{'textcol'}) {
 1892:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1893:                        '&nbsp;&nbsp;&nbsp;';
 1894:         }
 1895:         if (!$is_custom->{'bgcol'}) {
 1896:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1897:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1898:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1899:         }
 1900:         $output .= '<br />';
 1901:     }
 1902:     $output .='<br />';
 1903:     return $output;
 1904: }
 1905: 
 1906: sub login_text_colors {
 1907:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1908:     my $color_menu = '<table border="0"><tr>';
 1909:     foreach my $item (@{$logintext}) {
 1910:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1911:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1912:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1913:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1914:     }
 1915:     $color_menu .= '</tr></table><br />';
 1916:     return $color_menu;
 1917: }
 1918: 
 1919: sub image_changes {
 1920:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1921:     my $output;
 1922:     if ($img eq 'login') {
 1923:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1924:     } elsif (!$is_custom) {
 1925:         if ($img ne 'domlogo') {
 1926:             $output = &mt('Default image:').'<br />';
 1927:         } else {
 1928:             $output = &mt('Default in use:').'<br />';
 1929:         }
 1930:     }
 1931:     if ($img ne 'login') {
 1932:         if ($img_import) {
 1933:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1934:         }
 1935:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1936:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1937:         if ($is_custom) {
 1938:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1939:                        '<input type="checkbox" name="'.
 1940:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1941:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1942:         } else {
 1943:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1944:         }
 1945:     }
 1946:     return $output;
 1947: }
 1948: 
 1949: sub print_quotas {
 1950:     my ($dom,$settings,$rowtotal,$action) = @_;
 1951:     my $context;
 1952:     if ($action eq 'quotas') {
 1953:         $context = 'tools';
 1954:     } else {
 1955:         $context = $action;
 1956:     }
 1957:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1958:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1959:     my $typecount = 0;
 1960:     my ($css_class,%titles);
 1961:     if ($context eq 'requestcourses') {
 1962:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 1963:         @options =('norequest','approval','validate','autolimit');
 1964:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1965:         %titles = &courserequest_titles();
 1966:     } elsif ($context eq 'requestauthor') {
 1967:         @usertools = ('author');
 1968:         @options = ('norequest','approval','automatic');
 1969:         %titles = &authorrequest_titles();
 1970:     } else {
 1971:         @usertools = ('aboutme','blog','webdav','portfolio');
 1972:         %titles = &tool_titles();
 1973:     }
 1974:     if (ref($types) eq 'ARRAY') {
 1975:         foreach my $type (@{$types}) {
 1976:             my ($currdefquota,$currauthorquota);
 1977:             unless (($context eq 'requestcourses') ||
 1978:                     ($context eq 'requestauthor')) {
 1979:                 if (ref($settings) eq 'HASH') {
 1980:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1981:                         $currdefquota = $settings->{defaultquota}->{$type};
 1982:                     } else {
 1983:                         $currdefquota = $settings->{$type};
 1984:                     }
 1985:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1986:                         $currauthorquota = $settings->{authorquota}->{$type};
 1987:                     }
 1988:                 }
 1989:             }
 1990:             if (defined($usertypes->{$type})) {
 1991:                 $typecount ++;
 1992:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1993:                 $datatable .= '<tr'.$css_class.'>'.
 1994:                               '<td>'.$usertypes->{$type}.'</td>'.
 1995:                               '<td class="LC_left_item">';
 1996:                 if ($context eq 'requestcourses') {
 1997:                     $datatable .= '<table><tr>';
 1998:                 }
 1999:                 my %cell;  
 2000:                 foreach my $item (@usertools) {
 2001:                     if ($context eq 'requestcourses') {
 2002:                         my ($curroption,$currlimit);
 2003:                         if (ref($settings) eq 'HASH') {
 2004:                             if (ref($settings->{$item}) eq 'HASH') {
 2005:                                 $curroption = $settings->{$item}->{$type};
 2006:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2007:                                     $currlimit = $1; 
 2008:                                 }
 2009:                             }
 2010:                         }
 2011:                         if (!$curroption) {
 2012:                             $curroption = 'norequest';
 2013:                         }
 2014:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2015:                         foreach my $option (@options) {
 2016:                             my $val = $option;
 2017:                             if ($option eq 'norequest') {
 2018:                                 $val = 0;  
 2019:                             }
 2020:                             if ($option eq 'validate') {
 2021:                                 my $canvalidate = 0;
 2022:                                 if (ref($validations{$item}) eq 'HASH') { 
 2023:                                     if ($validations{$item}{$type}) {
 2024:                                         $canvalidate = 1;
 2025:                                     }
 2026:                                 }
 2027:                                 next if (!$canvalidate);
 2028:                             }
 2029:                             my $checked = '';
 2030:                             if ($option eq $curroption) {
 2031:                                 $checked = ' checked="checked"';
 2032:                             } elsif ($option eq 'autolimit') {
 2033:                                 if ($curroption =~ /^autolimit/) {
 2034:                                     $checked = ' checked="checked"';
 2035:                                 }                       
 2036:                             } 
 2037:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2038:                                   '<input type="radio" name="crsreq_'.$item.
 2039:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2040:                                   $titles{$option}.'</label>';
 2041:                             if ($option eq 'autolimit') {
 2042:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2043:                                                 $item.'_limit_'.$type.'" size="1" '.
 2044:                                                 'value="'.$currlimit.'" />';
 2045:                             }
 2046:                             $cell{$item} .= '</span> ';
 2047:                             if ($option eq 'autolimit') {
 2048:                                 $cell{$item} .= $titles{'unlimited'};
 2049:                             }
 2050:                         }
 2051:                     } elsif ($context eq 'requestauthor') {
 2052:                         my $curroption;
 2053:                         if (ref($settings) eq 'HASH') {
 2054:                             $curroption = $settings->{$type};
 2055:                         }
 2056:                         if (!$curroption) {
 2057:                             $curroption = 'norequest';
 2058:                         }
 2059:                         foreach my $option (@options) {
 2060:                             my $val = $option;
 2061:                             if ($option eq 'norequest') {
 2062:                                 $val = 0;
 2063:                             }
 2064:                             my $checked = '';
 2065:                             if ($option eq $curroption) {
 2066:                                 $checked = ' checked="checked"';
 2067:                             }
 2068:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2069:                                   '<input type="radio" name="authorreq_'.$type.
 2070:                                   '" value="'.$val.'"'.$checked.' />'.
 2071:                                   $titles{$option}.'</label></span>&nbsp; ';
 2072:                         }
 2073:                     } else {
 2074:                         my $checked = 'checked="checked" ';
 2075:                         if (ref($settings) eq 'HASH') {
 2076:                             if (ref($settings->{$item}) eq 'HASH') {
 2077:                                 if ($settings->{$item}->{$type} == 0) {
 2078:                                     $checked = '';
 2079:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2080:                                     $checked =  'checked="checked" ';
 2081:                                 }
 2082:                             }
 2083:                         }
 2084:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2085:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2086:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2087:                                       '</label></span>&nbsp; ';
 2088:                     }
 2089:                 }
 2090:                 if ($context eq 'requestcourses') {
 2091:                     $datatable .= '</tr><tr>';
 2092:                     foreach my $item (@usertools) {
 2093:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2094:                     }
 2095:                     $datatable .= '</tr></table>';
 2096:                 }
 2097:                 $datatable .= '</td>';
 2098:                 unless (($context eq 'requestcourses') ||
 2099:                         ($context eq 'requestauthor')) {
 2100:                     $datatable .= 
 2101:                               '<td class="LC_right_item">'.
 2102:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2103:                               '<input type="text" name="quota_'.$type.
 2104:                               '" value="'.$currdefquota.
 2105:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2106:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2107:                               '<input type="text" name="authorquota_'.$type.
 2108:                               '" value="'.$currauthorquota.
 2109:                               '" size="5" /></span></td>';
 2110:                 }
 2111:                 $datatable .= '</tr>';
 2112:             }
 2113:         }
 2114:     }
 2115:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2116:         $defaultquota = '20';
 2117:         $authorquota = '500';
 2118:         if (ref($settings) eq 'HASH') {
 2119:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2120:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2121:             } elsif (defined($settings->{'default'})) {
 2122:                 $defaultquota = $settings->{'default'};
 2123:             }
 2124:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2125:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2126:             }
 2127:         }
 2128:     }
 2129:     $typecount ++;
 2130:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2131:     $datatable .= '<tr'.$css_class.'>'.
 2132:                   '<td>'.$othertitle.'</td>'.
 2133:                   '<td class="LC_left_item">';
 2134:     if ($context eq 'requestcourses') {
 2135:         $datatable .= '<table><tr>';
 2136:     }
 2137:     my %defcell;
 2138:     foreach my $item (@usertools) {
 2139:         if ($context eq 'requestcourses') {
 2140:             my ($curroption,$currlimit);
 2141:             if (ref($settings) eq 'HASH') {
 2142:                 if (ref($settings->{$item}) eq 'HASH') {
 2143:                     $curroption = $settings->{$item}->{'default'};
 2144:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2145:                         $currlimit = $1;
 2146:                     }
 2147:                 }
 2148:             }
 2149:             if (!$curroption) {
 2150:                 $curroption = 'norequest';
 2151:             }
 2152:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2153:             foreach my $option (@options) {
 2154:                 my $val = $option;
 2155:                 if ($option eq 'norequest') {
 2156:                     $val = 0;
 2157:                 }
 2158:                 if ($option eq 'validate') {
 2159:                     my $canvalidate = 0;
 2160:                     if (ref($validations{$item}) eq 'HASH') {
 2161:                         if ($validations{$item}{'default'}) {
 2162:                             $canvalidate = 1;
 2163:                         }
 2164:                     }
 2165:                     next if (!$canvalidate);
 2166:                 }
 2167:                 my $checked = '';
 2168:                 if ($option eq $curroption) {
 2169:                     $checked = ' checked="checked"';
 2170:                 } elsif ($option eq 'autolimit') {
 2171:                     if ($curroption =~ /^autolimit/) {
 2172:                         $checked = ' checked="checked"';
 2173:                     }
 2174:                 }
 2175:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2176:                                   '<input type="radio" name="crsreq_'.$item.
 2177:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2178:                                   $titles{$option}.'</label>';
 2179:                 if ($option eq 'autolimit') {
 2180:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2181:                                        $item.'_limit_default" size="1" '.
 2182:                                        'value="'.$currlimit.'" />';
 2183:                 }
 2184:                 $defcell{$item} .= '</span> ';
 2185:                 if ($option eq 'autolimit') {
 2186:                     $defcell{$item} .= $titles{'unlimited'};
 2187:                 }
 2188:             }
 2189:         } elsif ($context eq 'requestauthor') {
 2190:             my $curroption;
 2191:             if (ref($settings) eq 'HASH') {
 2192:                 $curroption = $settings->{'default'};
 2193:             }
 2194:             if (!$curroption) {
 2195:                 $curroption = 'norequest';
 2196:             }
 2197:             foreach my $option (@options) {
 2198:                 my $val = $option;
 2199:                 if ($option eq 'norequest') {
 2200:                     $val = 0;
 2201:                 }
 2202:                 my $checked = '';
 2203:                 if ($option eq $curroption) {
 2204:                     $checked = ' checked="checked"';
 2205:                 }
 2206:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2207:                               '<input type="radio" name="authorreq_default"'.
 2208:                               ' value="'.$val.'"'.$checked.' />'.
 2209:                               $titles{$option}.'</label></span>&nbsp; ';
 2210:             }
 2211:         } else {
 2212:             my $checked = 'checked="checked" ';
 2213:             if (ref($settings) eq 'HASH') {
 2214:                 if (ref($settings->{$item}) eq 'HASH') {
 2215:                     if ($settings->{$item}->{'default'} == 0) {
 2216:                         $checked = '';
 2217:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2218:                         $checked = 'checked="checked" ';
 2219:                     }
 2220:                 }
 2221:             }
 2222:             $datatable .= '<span class="LC_nobreak"><label>'.
 2223:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2224:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2225:                           '</label></span>&nbsp; ';
 2226:         }
 2227:     }
 2228:     if ($context eq 'requestcourses') {
 2229:         $datatable .= '</tr><tr>';
 2230:         foreach my $item (@usertools) {
 2231:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2232:         }
 2233:         $datatable .= '</tr></table>';
 2234:     }
 2235:     $datatable .= '</td>';
 2236:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2237:         $datatable .= '<td class="LC_right_item">'.
 2238:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2239:                       '<input type="text" name="defaultquota" value="'.
 2240:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2241:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2242:                       '<input type="text" name="authorquota" value="'.
 2243:                       $authorquota.'" size="5" /></span></td>';
 2244:     }
 2245:     $datatable .= '</tr>';
 2246:     $typecount ++;
 2247:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2248:     $datatable .= '<tr'.$css_class.'>'.
 2249:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2250:     if ($context eq 'requestcourses') {
 2251:         $datatable .= &mt('(overrides affiliation, if set)').
 2252:                       '</td>'.
 2253:                       '<td class="LC_left_item">'.
 2254:                       '<table><tr>';
 2255:     } else {
 2256:         $datatable .= &mt('(overrides affiliation, if checked)').
 2257:                       '</td>'.
 2258:                       '<td class="LC_left_item" colspan="2">'.
 2259:                       '<br />';
 2260:     }
 2261:     my %advcell;
 2262:     foreach my $item (@usertools) {
 2263:         if ($context eq 'requestcourses') {
 2264:             my ($curroption,$currlimit);
 2265:             if (ref($settings) eq 'HASH') {
 2266:                 if (ref($settings->{$item}) eq 'HASH') {
 2267:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2268:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2269:                         $currlimit = $1;
 2270:                     }
 2271:                 }
 2272:             }
 2273:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2274:             my $checked = '';
 2275:             if ($curroption eq '') {
 2276:                 $checked = ' checked="checked"';
 2277:             }
 2278:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2279:                                '<input type="radio" name="crsreq_'.$item.
 2280:                                '__LC_adv" value=""'.$checked.' />'.
 2281:                                &mt('No override set').'</label></span>&nbsp; ';
 2282:             foreach my $option (@options) {
 2283:                 my $val = $option;
 2284:                 if ($option eq 'norequest') {
 2285:                     $val = 0;
 2286:                 }
 2287:                 if ($option eq 'validate') {
 2288:                     my $canvalidate = 0;
 2289:                     if (ref($validations{$item}) eq 'HASH') {
 2290:                         if ($validations{$item}{'_LC_adv'}) {
 2291:                             $canvalidate = 1;
 2292:                         }
 2293:                     }
 2294:                     next if (!$canvalidate);
 2295:                 }
 2296:                 my $checked = '';
 2297:                 if ($val eq $curroption) {
 2298:                     $checked = ' checked="checked"';
 2299:                 } elsif ($option eq 'autolimit') {
 2300:                     if ($curroption =~ /^autolimit/) {
 2301:                         $checked = ' checked="checked"';
 2302:                     }
 2303:                 }
 2304:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2305:                                   '<input type="radio" name="crsreq_'.$item.
 2306:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2307:                                   $titles{$option}.'</label>';
 2308:                 if ($option eq 'autolimit') {
 2309:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2310:                                        $item.'_limit__LC_adv" size="1" '.
 2311:                                        'value="'.$currlimit.'" />';
 2312:                 }
 2313:                 $advcell{$item} .= '</span> ';
 2314:                 if ($option eq 'autolimit') {
 2315:                     $advcell{$item} .= $titles{'unlimited'};
 2316:                 }
 2317:             }
 2318:         } elsif ($context eq 'requestauthor') {
 2319:             my $curroption;
 2320:             if (ref($settings) eq 'HASH') {
 2321:                 $curroption = $settings->{'_LC_adv'};
 2322:             }
 2323:             my $checked = '';
 2324:             if ($curroption eq '') {
 2325:                 $checked = ' checked="checked"';
 2326:             }
 2327:             $datatable .= '<span class="LC_nobreak"><label>'.
 2328:                           '<input type="radio" name="authorreq__LC_adv"'.
 2329:                           ' value=""'.$checked.' />'.
 2330:                           &mt('No override set').'</label></span>&nbsp; ';
 2331:             foreach my $option (@options) {
 2332:                 my $val = $option;
 2333:                 if ($option eq 'norequest') {
 2334:                     $val = 0;
 2335:                 }
 2336:                 my $checked = '';
 2337:                 if ($val eq $curroption) {
 2338:                     $checked = ' checked="checked"';
 2339:                 }
 2340:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2341:                               '<input type="radio" name="authorreq__LC_adv"'.
 2342:                               ' value="'.$val.'"'.$checked.' />'.
 2343:                               $titles{$option}.'</label></span>&nbsp; ';
 2344:             }
 2345:         } else {
 2346:             my $checked = 'checked="checked" ';
 2347:             if (ref($settings) eq 'HASH') {
 2348:                 if (ref($settings->{$item}) eq 'HASH') {
 2349:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2350:                         $checked = '';
 2351:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2352:                         $checked = 'checked="checked" ';
 2353:                     }
 2354:                 }
 2355:             }
 2356:             $datatable .= '<span class="LC_nobreak"><label>'.
 2357:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2358:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2359:                           '</label></span>&nbsp; ';
 2360:         }
 2361:     }
 2362:     if ($context eq 'requestcourses') {
 2363:         $datatable .= '</tr><tr>';
 2364:         foreach my $item (@usertools) {
 2365:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2366:         }
 2367:         $datatable .= '</tr></table>';
 2368:     }
 2369:     $datatable .= '</td></tr>';
 2370:     $$rowtotal += $typecount;
 2371:     return $datatable;
 2372: }
 2373: 
 2374: sub print_requestmail {
 2375:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2376:     my ($now,$datatable,%currapp);
 2377:     $now = time;
 2378:     if (ref($settings) eq 'HASH') {
 2379:         if (ref($settings->{'notify'}) eq 'HASH') {
 2380:             if ($settings->{'notify'}{'approval'} ne '') {
 2381:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2382:             }
 2383:         }
 2384:     }
 2385:     my $numinrow = 2;
 2386:     my $css_class;
 2387:     if ($$rowtotal%2) {
 2388:         $css_class = 'LC_odd_row';
 2389:     }
 2390:     if ($customcss) {
 2391:         $css_class .= " $customcss";
 2392:     }
 2393:     $css_class =~ s/^\s+//;
 2394:     if ($css_class) {
 2395:         $css_class = ' class="'.$css_class.'"';
 2396:     }
 2397:     if ($rowstyle) {
 2398:         $css_class .= ' style="'.$rowstyle.'"';
 2399:     }
 2400:     my $text;
 2401:     if ($action eq 'requestcourses') {
 2402:         $text = &mt('Receive notification of course requests requiring approval');
 2403:     } elsif ($action eq 'requestauthor') {
 2404:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2405:     } else {
 2406:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2407:     }
 2408:     $datatable = '<tr'.$css_class.'>'.
 2409:                  ' <td>'.$text.'</td>'.
 2410:                  ' <td class="LC_left_item">';
 2411:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2412:                                                  $action.'notifyapproval',%currapp);
 2413:     if ($numdc > 0) {
 2414:         $datatable .= $table;
 2415:     } else {
 2416:         $datatable .= &mt('There are no active Domain Coordinators');
 2417:     }
 2418:     $datatable .='</td></tr>';
 2419:     return $datatable;
 2420: }
 2421: 
 2422: sub print_studentcode {
 2423:     my ($settings,$rowtotal) = @_;
 2424:     my $rownum = 0; 
 2425:     my ($output,%current);
 2426:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2427:     if (ref($settings) eq 'HASH') {
 2428:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2429:             foreach my $type (@crstypes) {
 2430:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2431:             }
 2432:         }
 2433:     }
 2434:     $output .= '<tr>'.
 2435:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2436:                '<td class="LC_left_item">';
 2437:     foreach my $type (@crstypes) {
 2438:         my $check = ' ';
 2439:         if ($current{$type}) {
 2440:             $check = ' checked="checked" ';
 2441:         }
 2442:         $output .= '<span class="LC_nobreak"><label>'.
 2443:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2444:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2445:     }
 2446:     $output .= '</td></tr>';
 2447:     $$rowtotal ++;
 2448:     return $output;
 2449: }
 2450: 
 2451: sub print_textbookcourses {
 2452:     my ($dom,$type,$settings,$rowtotal) = @_;
 2453:     my $rownum = 0;
 2454:     my $css_class;
 2455:     my $itemcount = 1;
 2456:     my $maxnum = 0;
 2457:     my $bookshash;
 2458:     if (ref($settings) eq 'HASH') {
 2459:         $bookshash = $settings->{$type};
 2460:     }
 2461:     my %ordered;
 2462:     if (ref($bookshash) eq 'HASH') {
 2463:         foreach my $item (keys(%{$bookshash})) {
 2464:             if (ref($bookshash->{$item}) eq 'HASH') {
 2465:                 my $num = $bookshash->{$item}{'order'};
 2466:                 $ordered{$num} = $item;
 2467:             }
 2468:         }
 2469:     }
 2470:     my $confname = $dom.'-domainconfig';
 2471:     my $switchserver = &check_switchserver($dom,$confname);
 2472:     my $maxnum = scalar(keys(%ordered));
 2473:     my $datatable;
 2474:     if (keys(%ordered)) {
 2475:         my @items = sort { $a <=> $b } keys(%ordered);
 2476:         for (my $i=0; $i<@items; $i++) {
 2477:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2478:             my $key = $ordered{$items[$i]};
 2479:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2480:             my $coursetitle = $coursehash{'description'};
 2481:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2482:             if (ref($bookshash->{$key}) eq 'HASH') {
 2483:                 $subject = $bookshash->{$key}->{'subject'};
 2484:                 $title = $bookshash->{$key}->{'title'};
 2485:                 if ($type eq 'textbooks') {
 2486:                     $publisher = $bookshash->{$key}->{'publisher'};
 2487:                     $author = $bookshash->{$key}->{'author'};
 2488:                     $image = $bookshash->{$key}->{'image'};
 2489:                     if ($image ne '') {
 2490:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2491:                         my $imagethumb = "$path/tn-".$imagefile;
 2492:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2493:                     }
 2494:                 }
 2495:             }
 2496:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2497:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2498:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2499:             for (my $k=0; $k<=$maxnum; $k++) {
 2500:                 my $vpos = $k+1;
 2501:                 my $selstr;
 2502:                 if ($k == $i) {
 2503:                     $selstr = ' selected="selected" ';
 2504:                 }
 2505:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2506:             }
 2507:             $datatable .= '</select>'.('&nbsp;'x2).
 2508:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2509:                 &mt('Delete?').'</label></span></td>'.
 2510:                 '<td colspan="2">'.
 2511:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2512:                 ('&nbsp;'x2).
 2513:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2514:             if ($type eq 'textbooks') {
 2515:                 $datatable .= ('&nbsp;'x2).
 2516:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2517:                               ('&nbsp;'x2).
 2518:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2519:                               ('&nbsp;'x2).
 2520:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2521:                 if ($image) {
 2522:                     $datatable .= $imgsrc.
 2523:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2524:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2525:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2526:                 }
 2527:                 if ($switchserver) {
 2528:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2529:                 } else {
 2530:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2531:                 }
 2532:             }
 2533:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2534:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2535:                           $coursetitle.'</span></td></tr>'."\n";
 2536:             $itemcount ++;
 2537:         }
 2538:     }
 2539:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2540:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2541:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2542:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2543:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2544:     for (my $k=0; $k<$maxnum+1; $k++) {
 2545:         my $vpos = $k+1;
 2546:         my $selstr;
 2547:         if ($k == $maxnum) {
 2548:             $selstr = ' selected="selected" ';
 2549:         }
 2550:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2551:     }
 2552:     $datatable .= '</select>&nbsp;'."\n".
 2553:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2554:                   '<td colspan="2">'.
 2555:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2556:                   ('&nbsp;'x2).
 2557:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2558:                   ('&nbsp;'x2);
 2559:     if ($type eq 'textbooks') {
 2560:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2561:                       ('&nbsp;'x2).
 2562:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2563:                       ('&nbsp;'x2).
 2564:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2565:         if ($switchserver) {
 2566:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2567:         } else {
 2568:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2569:         }
 2570:         $datatable .= '</span>'."\n";
 2571:     }
 2572:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2573:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2574:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2575:                   &Apache::loncommon::selectcourse_link
 2576:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2577:                   '</span></td>'."\n".
 2578:                   '</tr>'."\n";
 2579:     $itemcount ++;
 2580:     return $datatable;
 2581: }
 2582: 
 2583: sub textbookcourses_javascript {
 2584:     my ($settings) = @_;
 2585:     return unless(ref($settings) eq 'HASH');
 2586:     my (%ordered,%total,%jstext);
 2587:     foreach my $type ('textbooks','templates') {
 2588:         $total{$type} = 0;
 2589:         if (ref($settings->{$type}) eq 'HASH') {
 2590:             foreach my $item (keys(%{$settings->{$type}})) {
 2591:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2592:                     my $num = $settings->{$type}->{$item}{'order'};
 2593:                     $ordered{$type}{$num} = $item;
 2594:                 }
 2595:             }
 2596:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2597:         }
 2598:         my @jsarray = ();
 2599:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2600:             push(@jsarray,$ordered{$type}{$item});
 2601:         }
 2602:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2603:     }
 2604:     return <<"ENDSCRIPT";
 2605: <script type="text/javascript">
 2606: // <![CDATA[
 2607: function reorderBooks(form,item,caller) {
 2608:     var changedVal;
 2609: $jstext{'textbooks'};
 2610: $jstext{'templates'};
 2611:     var newpos;
 2612:     var maxh;
 2613:     if (caller == 'textbooks') {  
 2614:         newpos = 'textbooks_addbook_pos';
 2615:         maxh = 1 + $total{'textbooks'};
 2616:     } else {
 2617:         newpos = 'templates_addbook_pos';
 2618:         maxh = 1 + $total{'templates'};
 2619:     }
 2620:     var current = new Array;
 2621:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2622:     if (item == newpos) {
 2623:         changedVal = newitemVal;
 2624:     } else {
 2625:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2626:         current[newitemVal] = newpos;
 2627:     }
 2628:     if (caller == 'textbooks') {
 2629:         for (var i=0; i<textbooks.length; i++) {
 2630:             var elementName = 'textbooks_'+textbooks[i];
 2631:             if (elementName != item) {
 2632:                 if (form.elements[elementName]) {
 2633:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2634:                     current[currVal] = elementName;
 2635:                 }
 2636:             }
 2637:         }
 2638:     }
 2639:     if (caller == 'templates') {
 2640:         for (var i=0; i<templates.length; i++) {
 2641:             var elementName = 'templates_'+templates[i];
 2642:             if (elementName != item) {
 2643:                 if (form.elements[elementName]) {
 2644:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2645:                     current[currVal] = elementName;
 2646:                 }
 2647:             }
 2648:         }
 2649:     }
 2650:     var oldVal;
 2651:     for (var j=0; j<maxh; j++) {
 2652:         if (current[j] == undefined) {
 2653:             oldVal = j;
 2654:         }
 2655:     }
 2656:     if (oldVal < changedVal) {
 2657:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2658:            var elementName = current[k];
 2659:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2660:         }
 2661:     } else {
 2662:         for (var k=changedVal; k<oldVal; k++) {
 2663:             var elementName = current[k];
 2664:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2665:         }
 2666:     }
 2667:     return;
 2668: }
 2669: 
 2670: // ]]>
 2671: </script>
 2672: 
 2673: ENDSCRIPT
 2674: }
 2675: 
 2676: sub ltitools_javascript {
 2677:     my ($settings) = @_;
 2678:     my $togglejs = &ltitools_toggle_js();
 2679:     unless (ref($settings) eq 'HASH') {
 2680:         return $togglejs;
 2681:     }
 2682:     my (%ordered,$total,%jstext);
 2683:     $total = 0;
 2684:     foreach my $item (keys(%{$settings})) {
 2685:         if (ref($settings->{$item}) eq 'HASH') {
 2686:             my $num = $settings->{$item}{'order'};
 2687:             $ordered{$num} = $item;
 2688:         }
 2689:     }
 2690:     $total = scalar(keys(%{$settings}));
 2691:     my @jsarray = ();
 2692:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2693:         push(@jsarray,$ordered{$item});
 2694:     }
 2695:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2696:     return <<"ENDSCRIPT";
 2697: <script type="text/javascript">
 2698: // <![CDATA[
 2699: function reorderLTITools(form,item) {
 2700:     var changedVal;
 2701: $jstext
 2702:     var newpos = 'ltitools_add_pos';
 2703:     var maxh = 1 + $total;
 2704:     var current = new Array;
 2705:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2706:     if (item == newpos) {
 2707:         changedVal = newitemVal;
 2708:     } else {
 2709:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2710:         current[newitemVal] = newpos;
 2711:     }
 2712:     for (var i=0; i<ltitools.length; i++) {
 2713:         var elementName = 'ltitools_'+ltitools[i];
 2714:         if (elementName != item) {
 2715:             if (form.elements[elementName]) {
 2716:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2717:                 current[currVal] = elementName;
 2718:             }
 2719:         }
 2720:     }
 2721:     var oldVal;
 2722:     for (var j=0; j<maxh; j++) {
 2723:         if (current[j] == undefined) {
 2724:             oldVal = j;
 2725:         }
 2726:     }
 2727:     if (oldVal < changedVal) {
 2728:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2729:            var elementName = current[k];
 2730:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2731:         }
 2732:     } else {
 2733:         for (var k=changedVal; k<oldVal; k++) {
 2734:             var elementName = current[k];
 2735:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2736:         }
 2737:     }
 2738:     return;
 2739: }
 2740: 
 2741: // ]]>
 2742: </script>
 2743: 
 2744: $togglejs
 2745: 
 2746: ENDSCRIPT
 2747: }
 2748: 
 2749: sub ltitools_toggle_js {
 2750:     return <<"ENDSCRIPT";
 2751: <script type="text/javascript">
 2752: // <![CDATA[
 2753: 
 2754: function toggleLTITools(form,setting,item) {
 2755:     var radioname = '';
 2756:     var divid = '';
 2757:     if ((setting == 'passback') || (setting == 'roster')) {
 2758:         radioname = 'ltitools_'+setting+'_'+item;
 2759:         divid = 'ltitools_'+setting+'time_'+item;
 2760:         var num = form.elements[radioname].length;
 2761:         if (num) {
 2762:             var setvis = '';
 2763:             for (var i=0; i<num; i++) {
 2764:                 if (form.elements[radioname][i].checked) {
 2765:                     if (form.elements[radioname][i].value == '1') {
 2766:                         if (document.getElementById(divid)) {
 2767:                             document.getElementById(divid).style.display = 'inline-block';
 2768:                         }
 2769:                         setvis = 1;
 2770:                     }
 2771:                     break;
 2772:                 }
 2773:             }
 2774:         }
 2775:         if (!setvis) {
 2776:             if (document.getElementById(divid)) {
 2777:                 document.getElementById(divid).style.display = 'none';
 2778:             }
 2779:         }
 2780:     }
 2781:     if (setting == 'user') {
 2782:         divid = 'ltitools_'+setting+'_div_'+item;
 2783:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2784:         if (document.getElementById(divid)) {
 2785:             if (document.getElementById(checkid)) {
 2786:                 if (document.getElementById(checkid).checked) {
 2787:                     document.getElementById(divid).style.display = 'inline-block';
 2788:                 } else {
 2789:                     document.getElementById(divid).style.display = 'none';
 2790:                 }
 2791:             }
 2792:         }
 2793:     }
 2794:     return;
 2795: }
 2796: // ]]>
 2797: </script>
 2798: 
 2799: ENDSCRIPT
 2800: }
 2801: 
 2802: sub lti_javascript {
 2803:     my ($settings) = @_;
 2804:     my $togglejs = &lti_toggle_js();
 2805:     unless (ref($settings) eq 'HASH') {
 2806:         return $togglejs;
 2807:     }
 2808:     my (%ordered,$total,%jstext);
 2809:     $total = 0;
 2810:     foreach my $item (keys(%{$settings})) {
 2811:         if (ref($settings->{$item}) eq 'HASH') {
 2812:             my $num = $settings->{$item}{'order'};
 2813:             $ordered{$num} = $item;
 2814:         }
 2815:     }
 2816:     $total = scalar(keys(%{$settings}));
 2817:     my @jsarray = ();
 2818:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2819:         push(@jsarray,$ordered{$item});
 2820:     }
 2821:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 2822:     return <<"ENDSCRIPT";
 2823: <script type="text/javascript">
 2824: // <![CDATA[
 2825: function reorderLTI(form,item) {
 2826:     var changedVal;
 2827: $jstext
 2828:     var newpos = 'lti_pos_add';
 2829:     var maxh = 1 + $total;
 2830:     var current = new Array;
 2831:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2832:     if (item == newpos) {
 2833:         changedVal = newitemVal;
 2834:     } else {
 2835:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2836:         current[newitemVal] = newpos;
 2837:     }
 2838:     for (var i=0; i<lti.length; i++) {
 2839:         var elementName = 'lti_pos_'+lti[i];
 2840:         if (elementName != item) {
 2841:             if (form.elements[elementName]) {
 2842:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2843:                 current[currVal] = elementName;
 2844:             }
 2845:         }
 2846:     }
 2847:     var oldVal;
 2848:     for (var j=0; j<maxh; j++) {
 2849:         if (current[j] == undefined) {
 2850:             oldVal = j;
 2851:         }
 2852:     }
 2853:     if (oldVal < changedVal) {
 2854:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2855:            var elementName = current[k];
 2856:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2857:         }
 2858:     } else {
 2859:         for (var k=changedVal; k<oldVal; k++) {
 2860:             var elementName = current[k];
 2861:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2862:         }
 2863:     }
 2864:     return;
 2865: }
 2866: // ]]>
 2867: </script>
 2868: 
 2869: $togglejs
 2870: 
 2871: ENDSCRIPT
 2872: }
 2873: 
 2874: sub lti_toggle_js {
 2875:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 2876:                             localauth => 'Local auth argument',
 2877:                             krb       => 'Kerberos domain',
 2878:                          );
 2879:     return <<"ENDSCRIPT";
 2880: <script type="text/javascript">
 2881: // <![CDATA[
 2882: 
 2883: function toggleLTI(form,setting,item) {
 2884:     if (setting == 'requser') {
 2885:         var fieldsets = document.getElementsByClassName('ltioption_'+item);
 2886:         if (fieldsets.length) {
 2887:             var radioname = 'lti_'+setting+'_'+item;
 2888:             var num = form.elements[radioname].length;
 2889:             if (num) {
 2890:                 var setvis = '';
 2891:                 for (var i=0; i<num; i++) {
 2892:                     if (form.elements[radioname][i].checked) {
 2893:                         if (form.elements[radioname][i].value == '1') {
 2894:                            setvis = 1;
 2895:                            break;
 2896:                         }
 2897:                     }
 2898:                 }
 2899:                 for (var j=0; j<fieldsets.length; j++) {
 2900:                     if (setvis) {
 2901:                         fieldsets[j].style.display = 'block';
 2902:                     } else {
 2903:                         fieldsets[j].style.display = 'none';
 2904:                     }
 2905:                 }
 2906:             }
 2907:         }
 2908:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 2909:         var radioname = '';
 2910:         var divid = '';
 2911:         if (setting == 'user') {
 2912:             radioname = 'lti_mapuser_'+item;
 2913:             divid = 'lti_userfield_'+item;
 2914:         } else if (setting == 'crs') {
 2915:             radioname = 'lti_mapcrs_'+item;
 2916:             divid = 'lti_crsfield_'+item;
 2917:         } else if (setting == 'callback') {
 2918:             radioname = 'lti_callback_'+item;
 2919:             divid = 'lti_callbackfield_'+item;
 2920:         } else {
 2921:             radioname = 'lti_passback_'+item;
 2922:             divid =  'lti_passback_'+item;
 2923:         }
 2924:         var num = form.elements[radioname].length;
 2925:         if (num) {
 2926:             var setvis = '';
 2927:             for (var i=0; i<num; i++) {
 2928:                if (form.elements[radioname][i].checked) {
 2929:                    if ((setting == 'passback') || (setting == 'callback')) {
 2930:                        if (form.elements[radioname][i].value == '1') {
 2931:                            if (document.getElementById(divid)) {
 2932:                                document.getElementById(divid).style.display = 'inline-block';
 2933:                            }
 2934:                            setvis = 1;
 2935:                            break;
 2936:                        }
 2937:                    } else {
 2938:                        if (form.elements[radioname][i].value == 'other') {
 2939:                            if (document.getElementById(divid)) {
 2940:                                document.getElementById(divid).style.display = 'inline-block';
 2941:                            }
 2942:                            setvis = 1;
 2943:                            break;
 2944:                        }
 2945:                    }
 2946:                } 
 2947:             }
 2948:             if (!setvis) {
 2949:                 if (document.getElementById(divid)) {
 2950:                     document.getElementById(divid).style.display = 'none';
 2951:                 }
 2952:             }
 2953:         }
 2954:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 2955:         var numsec = form.elements['lti_crssec_'+item].length;
 2956:         if (numsec) {
 2957:             var setvis = '';
 2958:             for (var i=0; i<numsec; i++) {
 2959:                 if (form.elements['lti_crssec_'+item][i].checked) {
 2960:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 2961:                         if (document.getElementById('lti_crssecfield_'+item)) {
 2962:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 2963:                             setvis = 1;
 2964:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 2965:                             if (numsrcsec) {
 2966:                                 var setsrcvis = '';
 2967:                                 for (var j=0; j<numsrcsec; j++) {
 2968:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 2969:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 2970:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 2971:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 2972:                                                 setsrcvis = 1;
 2973:                                             }
 2974:                                         }
 2975:                                     }
 2976:                                 }
 2977:                                 if (!setsrcvis) {
 2978:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 2979:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2980:                                     }
 2981:                                 }
 2982:                             }
 2983:                         }
 2984:                     }
 2985:                 }
 2986:             }
 2987:             if (!setvis) {
 2988:                 if (document.getElementById('lti_crssecfield_'+item)) {
 2989:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 2990:                 }
 2991:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 2992:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 2993:                 }
 2994:             }
 2995:         }
 2996:     } else if (setting == 'lcauth') {
 2997:         var numauth = form.elements['lti_lcauth_'+item].length;
 2998:         if (numauth) {
 2999:             for (var i=0; i<numauth; i++) {
 3000:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3001:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3002:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3003:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3004:                         } else {
 3005:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3006:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3007:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3008:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3009:                                 } else {
 3010:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3011:                                 }
 3012:                             }
 3013:                         }
 3014:                     }
 3015:                 }
 3016:             }
 3017:         }
 3018:     } else if (setting == 'lcmenu') {
 3019:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3020:         var divid = 'lti_menufield_'+item;
 3021:         var setvis = '';
 3022:         for (var i=0; i<menus.length; i++) {
 3023:             var radioname = menus[i];  
 3024:             var num = form.elements[radioname].length;
 3025:             if (num) {
 3026:                 for (var j=0; j<num; j++) {
 3027:                     if (form.elements[radioname][j].checked) {
 3028:                         if (form.elements[radioname][j].value == '1') {
 3029:                             if (document.getElementById(divid)) {
 3030:                                 document.getElementById(divid).style.display = 'inline-block';
 3031:                             }
 3032:                             setvis = 1;
 3033:                             break;
 3034:                         }
 3035:                     }
 3036:                 }
 3037:             }
 3038:             if (setvis == 1) {
 3039:                 break;
 3040:             }
 3041:         }
 3042:         if (!setvis) {
 3043:             if (document.getElementById(divid)) {
 3044:                 document.getElementById(divid).style.display = 'none';
 3045:             }
 3046:         }
 3047:     }
 3048:     return;
 3049: }
 3050: // ]]>
 3051: </script>
 3052: 
 3053: ENDSCRIPT
 3054: }
 3055: 
 3056: sub print_autoenroll {
 3057:     my ($dom,$settings,$rowtotal) = @_;
 3058:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3059:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3060:     if (ref($settings) eq 'HASH') {
 3061:         if (exists($settings->{'run'})) {
 3062:             if ($settings->{'run'} eq '0') {
 3063:                 $runoff = ' checked="checked" ';
 3064:                 $runon = ' ';
 3065:             } else {
 3066:                 $runon = ' checked="checked" ';
 3067:                 $runoff = ' ';
 3068:             }
 3069:         } else {
 3070:             if ($autorun) {
 3071:                 $runon = ' checked="checked" ';
 3072:                 $runoff = ' ';
 3073:             } else {
 3074:                 $runoff = ' checked="checked" ';
 3075:                 $runon = ' ';
 3076:             }
 3077:         }
 3078:         if (exists($settings->{'co-owners'})) {
 3079:             if ($settings->{'co-owners'} eq '0') {
 3080:                 $coownersoff = ' checked="checked" ';
 3081:                 $coownerson = ' ';
 3082:             } else {
 3083:                 $coownerson = ' checked="checked" ';
 3084:                 $coownersoff = ' ';
 3085:             }
 3086:         } else {
 3087:             $coownersoff = ' checked="checked" ';
 3088:             $coownerson = ' ';
 3089:         }
 3090:         if (exists($settings->{'sender_domain'})) {
 3091:             $defdom = $settings->{'sender_domain'};
 3092:         }
 3093:         if (exists($settings->{'autofailsafe'})) {
 3094:             $failsafe = $settings->{'autofailsafe'};
 3095:         }
 3096:     } else {
 3097:         if ($autorun) {
 3098:             $runon = ' checked="checked" ';
 3099:             $runoff = ' ';
 3100:         } else {
 3101:             $runoff = ' checked="checked" ';
 3102:             $runon = ' ';
 3103:         }
 3104:     }
 3105:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3106:     my $notif_sender;
 3107:     if (ref($settings) eq 'HASH') {
 3108:         $notif_sender = $settings->{'sender_uname'};
 3109:     }
 3110:     my $datatable='<tr class="LC_odd_row">'.
 3111:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3112:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3113:                   '<input type="radio" name="autoenroll_run"'.
 3114:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3115:                   '<label><input type="radio" name="autoenroll_run"'.
 3116:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3117:                   '</tr><tr>'.
 3118:                   '<td>'.&mt('Notification messages - sender').
 3119:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3120:                   &mt('username').':&nbsp;'.
 3121:                   '<input type="text" name="sender_uname" value="'.
 3122:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3123:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3124:                   '<tr class="LC_odd_row">'.
 3125:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3126:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3127:                   '<input type="radio" name="autoassign_coowners"'.
 3128:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3129:                   '<label><input type="radio" name="autoassign_coowners"'.
 3130:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3131:                   '</tr><tr>'.
 3132:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3133:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3134:                   '<input type="text" name="autoenroll_failsafe"'.
 3135:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3136:     $$rowtotal += 4;
 3137:     return $datatable;
 3138: }
 3139: 
 3140: sub print_autoupdate {
 3141:     my ($position,$dom,$settings,$rowtotal) = @_;
 3142:     my $datatable;
 3143:     if ($position eq 'top') {
 3144:         my $updateon = ' ';
 3145:         my $updateoff = ' checked="checked" ';
 3146:         my $classlistson = ' ';
 3147:         my $classlistsoff = ' checked="checked" ';
 3148:         if (ref($settings) eq 'HASH') {
 3149:             if ($settings->{'run'} eq '1') {
 3150:                 $updateon = $updateoff;
 3151:                 $updateoff = ' ';
 3152:             }
 3153:             if ($settings->{'classlists'} eq '1') {
 3154:                 $classlistson = $classlistsoff;
 3155:                 $classlistsoff = ' ';
 3156:             }
 3157:         }
 3158:         my %title = (
 3159:                    run => 'Auto-update active?',
 3160:                    classlists => 'Update information in classlists?',
 3161:                     );
 3162:         $datatable = '<tr class="LC_odd_row">'. 
 3163:                   '<td>'.&mt($title{'run'}).'</td>'.
 3164:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3165:                   '<input type="radio" name="autoupdate_run"'.
 3166:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3167:                   '<label><input type="radio" name="autoupdate_run"'.
 3168:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3169:                   '</tr><tr>'.
 3170:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 3171:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3172:                   '<label><input type="radio" name="classlists"'.
 3173:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3174:                   '<label><input type="radio" name="classlists"'.
 3175:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3176:                   '</tr>';
 3177:         $$rowtotal += 2;
 3178:     } elsif ($position eq 'middle') {
 3179:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3180:         my $numinrow = 3;
 3181:         my $locknamesettings;
 3182:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3183:                                      $dom,$numinrow,$othertitle,
 3184:                                     'lockablenames',$rowtotal);
 3185:         $$rowtotal ++;
 3186:     } else {
 3187:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3188:         my @fields = ('lastname','firstname','middlename','generation',
 3189:                       'permanentemail','id');
 3190:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3191:         my $numrows = 0;
 3192:         if (ref($types) eq 'ARRAY') {
 3193:             if (@{$types} > 0) {
 3194:                 $datatable = 
 3195:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3196:                                          \@fields,$types,\$numrows);
 3197:                     $$rowtotal += @{$types}; 
 3198:             }
 3199:         }
 3200:         $datatable .= 
 3201:             &usertype_update_row($settings,{'default' => $othertitle},
 3202:                                  \%fieldtitles,\@fields,['default'],
 3203:                                  \$numrows);
 3204:         $$rowtotal ++;     
 3205:     }
 3206:     return $datatable;
 3207: }
 3208: 
 3209: sub print_autocreate {
 3210:     my ($dom,$settings,$rowtotal) = @_;
 3211:     my (%createon,%createoff,%currhash);
 3212:     my @types = ('xml','req');
 3213:     if (ref($settings) eq 'HASH') {
 3214:         foreach my $item (@types) {
 3215:             $createoff{$item} = ' checked="checked" ';
 3216:             $createon{$item} = ' ';
 3217:             if (exists($settings->{$item})) {
 3218:                 if ($settings->{$item}) {
 3219:                     $createon{$item} = ' checked="checked" ';
 3220:                     $createoff{$item} = ' ';
 3221:                 }
 3222:             }
 3223:         }
 3224:         if ($settings->{'xmldc'} ne '') {
 3225:             $currhash{$settings->{'xmldc'}} = 1;
 3226:         }
 3227:     } else {
 3228:         foreach my $item (@types) {
 3229:             $createoff{$item} = ' checked="checked" ';
 3230:             $createon{$item} = ' ';
 3231:         }
 3232:     }
 3233:     $$rowtotal += 2;
 3234:     my $numinrow = 2;
 3235:     my $datatable='<tr class="LC_odd_row">'.
 3236:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3237:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3238:                   '<input type="radio" name="autocreate_xml"'.
 3239:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3240:                   '<label><input type="radio" name="autocreate_xml"'.
 3241:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3242:                   '</td></tr><tr>'.
 3243:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3244:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3245:                   '<input type="radio" name="autocreate_req"'.
 3246:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3247:                   '<label><input type="radio" name="autocreate_req"'.
 3248:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3249:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3250:                                                    'autocreate_xmldc',%currhash);
 3251:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3252:     if ($numdc > 1) {
 3253:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3254:                       '</td><td class="LC_left_item">';
 3255:     } else {
 3256:         $datatable .= &mt('Course creation processed as:').
 3257:                       '</td><td class="LC_right_item">';
 3258:     }
 3259:     $datatable .= $dctable.'</td></tr>';
 3260:     $$rowtotal += $rows;
 3261:     return $datatable;
 3262: }
 3263: 
 3264: sub print_directorysrch {
 3265:     my ($position,$dom,$settings,$rowtotal) = @_;
 3266:     my $datatable;
 3267:     if ($position eq 'top') {
 3268:         my $instsrchon = ' ';
 3269:         my $instsrchoff = ' checked="checked" ';
 3270:         my ($exacton,$containson,$beginson);
 3271:         my $instlocalon = ' ';
 3272:         my $instlocaloff = ' checked="checked" ';
 3273:         if (ref($settings) eq 'HASH') {
 3274:             if ($settings->{'available'} eq '1') {
 3275:                 $instsrchon = $instsrchoff;
 3276:                 $instsrchoff = ' ';
 3277:             }
 3278:             if ($settings->{'localonly'} eq '1') {
 3279:                 $instlocalon = $instlocaloff;
 3280:                 $instlocaloff = ' ';
 3281:             }
 3282:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3283:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3284:                     if ($type eq 'exact') {
 3285:                         $exacton = ' checked="checked" ';
 3286:                     } elsif ($type eq 'contains') {
 3287:                         $containson = ' checked="checked" ';
 3288:                     } elsif ($type eq 'begins') {
 3289:                         $beginson = ' checked="checked" ';
 3290:                     }
 3291:                 }
 3292:             } else {
 3293:                 if ($settings->{'searchtypes'} eq 'exact') {
 3294:                     $exacton = ' checked="checked" ';
 3295:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3296:                     $containson = ' checked="checked" ';
 3297:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3298:                     $exacton = ' checked="checked" ';
 3299:                     $containson = ' checked="checked" ';
 3300:                 }
 3301:             }
 3302:         }
 3303:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3304:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3305: 
 3306:         my $numinrow = 4;
 3307:         my $cansrchrow = 0;
 3308:         $datatable='<tr class="LC_odd_row">'.
 3309:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3310:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3311:                    '<input type="radio" name="dirsrch_available"'.
 3312:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3313:                    '<label><input type="radio" name="dirsrch_available"'.
 3314:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3315:                    '</tr><tr>'.
 3316:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3317:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3318:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3319:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3320:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3321:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3322:                    '</tr>';
 3323:         $$rowtotal += 2;
 3324:         if (ref($usertypes) eq 'HASH') {
 3325:             if (keys(%{$usertypes}) > 0) {
 3326:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3327:                                              $numinrow,$othertitle,'cansearch',
 3328:                                              $rowtotal);
 3329:                 $cansrchrow = 1;
 3330:             }
 3331:         }
 3332:         if ($cansrchrow) {
 3333:             $$rowtotal ++;
 3334:             $datatable .= '<tr>';
 3335:         } else {
 3336:             $datatable .= '<tr class="LC_odd_row">';
 3337:         }
 3338:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3339:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3340:         foreach my $title (@{$titleorder}) {
 3341:             if (defined($searchtitles->{$title})) {
 3342:                 my $check = ' ';
 3343:                 if (ref($settings) eq 'HASH') {
 3344:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3345:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3346:                             $check = ' checked="checked" ';
 3347:                         }
 3348:                     }
 3349:                 }
 3350:                 $datatable .= '<td class="LC_left_item">'.
 3351:                               '<span class="LC_nobreak"><label>'.
 3352:                               '<input type="checkbox" name="searchby" '.
 3353:                               'value="'.$title.'"'.$check.'/>'.
 3354:                               $searchtitles->{$title}.'</label></span></td>';
 3355:             }
 3356:         }
 3357:         $datatable .= '</tr></table></td></tr>';
 3358:         $$rowtotal ++;
 3359:         if ($cansrchrow) {
 3360:             $datatable .= '<tr class="LC_odd_row">';
 3361:         } else {
 3362:             $datatable .= '<tr>';
 3363:         }
 3364:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3365:                       '<td class="LC_left_item" colspan="2">'.
 3366:                       '<span class="LC_nobreak"><label>'.
 3367:                       '<input type="checkbox" name="searchtypes" '.
 3368:                       $exacton.' value="exact" />'.&mt('Exact match').
 3369:                       '</label>&nbsp;'.
 3370:                       '<label><input type="checkbox" name="searchtypes" '.
 3371:                       $beginson.' value="begins" />'.&mt('Begins with').
 3372:                       '</label>&nbsp;'.
 3373:                       '<label><input type="checkbox" name="searchtypes" '.
 3374:                       $containson.' value="contains" />'.&mt('Contains').
 3375:                       '</label></span></td></tr>';
 3376:         $$rowtotal ++;
 3377:     } else {
 3378:         my $domsrchon = ' checked="checked" ';
 3379:         my $domsrchoff = ' ';
 3380:         my $domlocalon = ' ';
 3381:         my $domlocaloff = ' checked="checked" ';
 3382:         if (ref($settings) eq 'HASH') {
 3383:             if ($settings->{'lclocalonly'} eq '1') {
 3384:                 $domlocalon = $domlocaloff;
 3385:                 $domlocaloff = ' ';
 3386:             }
 3387:             if ($settings->{'lcavailable'} eq '0') {
 3388:                 $domsrchoff = $domsrchon;
 3389:                 $domsrchon = ' ';
 3390:             }
 3391:         }
 3392:         $datatable='<tr class="LC_odd_row">'.
 3393:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3394:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3395:                       '<input type="radio" name="dirsrch_domavailable"'.
 3396:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3397:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3398:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3399:                       '</tr><tr>'.
 3400:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3401:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3402:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3403:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3404:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3405:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3406:                       '</tr>';
 3407:         $$rowtotal += 2;
 3408:     }
 3409:     return $datatable;
 3410: }
 3411: 
 3412: sub print_contacts {
 3413:     my ($position,$dom,$settings,$rowtotal) = @_;
 3414:     my $datatable;
 3415:     my @contacts = ('adminemail','supportemail');
 3416:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3417:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3418:     if ($position eq 'top') {
 3419:         if (ref($settings) eq 'HASH') {
 3420:             foreach my $item (@contacts) {
 3421:                 if (exists($settings->{$item})) {
 3422:                     $to{$item} = $settings->{$item};
 3423:                 }
 3424:             }
 3425:         }
 3426:     } elsif ($position eq 'middle') {
 3427:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3428:                      'updatesmail','idconflictsmail','hostipmail');
 3429:         foreach my $type (@mailings) {
 3430:             $otheremails{$type} = '';
 3431:         }
 3432:     } elsif ($position eq 'lower') {
 3433:         if (ref($settings) eq 'HASH') {
 3434:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3435:                 %lonstatus = %{$settings->{'lonstatus'}};
 3436:             }
 3437:         }
 3438:     } else {
 3439:         @mailings = ('helpdeskmail','otherdomsmail');
 3440:         foreach my $type (@mailings) {
 3441:             $otheremails{$type} = '';
 3442:         }
 3443:         $bccemails{'helpdeskmail'} = '';
 3444:         $bccemails{'otherdomsmail'} = '';
 3445:         $includestr{'helpdeskmail'} = '';
 3446:         $includestr{'otherdomsmail'} = '';
 3447:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3448:     }
 3449:     if (ref($settings) eq 'HASH') {
 3450:         unless (($position eq 'top') || ($position eq 'lower')) {
 3451:             foreach my $type (@mailings) {
 3452:                 if (exists($settings->{$type})) {
 3453:                     if (ref($settings->{$type}) eq 'HASH') {
 3454:                         foreach my $item (@contacts) {
 3455:                             if ($settings->{$type}{$item}) {
 3456:                                 $checked{$type}{$item} = ' checked="checked" ';
 3457:                             }
 3458:                         }
 3459:                         $otheremails{$type} = $settings->{$type}{'others'};
 3460:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3461:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3462:                             if ($settings->{$type}{'include'} ne '') {
 3463:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3464:                                 $includestr{$type} = &unescape($includestr{$type});
 3465:                             }
 3466:                         }
 3467:                     }
 3468:                 } elsif ($type eq 'lonstatusmail') {
 3469:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3470:                 }
 3471:             }
 3472:         }
 3473:         if ($position eq 'bottom') {
 3474:             foreach my $type (@mailings) {
 3475:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3476:                 if ($settings->{$type}{'include'} ne '') {
 3477:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3478:                     $includestr{$type} = &unescape($includestr{$type});
 3479:                 }
 3480:             }
 3481:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3482:                 if (ref($fields) eq 'ARRAY') {
 3483:                     foreach my $field (@{$fields}) {
 3484:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3485:                     }
 3486:                 }
 3487:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3488:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3489:                 } else {
 3490:                     $maxsize = '1.0';
 3491:                 }
 3492:             } else {
 3493:                 if (ref($fields) eq 'ARRAY') {
 3494:                     foreach my $field (@{$fields}) {
 3495:                         $currfield{$field} = 'yes';
 3496:                     }
 3497:                 }
 3498:                 $maxsize = '1.0';
 3499:             }
 3500:         }
 3501:     } else {
 3502:         if ($position eq 'top') {
 3503:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3504:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3505:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3506:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3507:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3508:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3509:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3510:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3511:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3512:         } elsif ($position eq 'bottom') {
 3513:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3514:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3515:             if (ref($fields) eq 'ARRAY') {
 3516:                 foreach my $field (@{$fields}) {
 3517:                     $currfield{$field} = 'yes';
 3518:                 }
 3519:             }
 3520:             $maxsize = '1.0';
 3521:         }
 3522:     }
 3523:     my ($titles,$short_titles) = &contact_titles();
 3524:     my $rownum = 0;
 3525:     my $css_class;
 3526:     if ($position eq 'top') {
 3527:         foreach my $item (@contacts) {
 3528:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3529:             $datatable .= '<tr'.$css_class.'>'. 
 3530:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3531:                           '</span></td><td class="LC_right_item">'.
 3532:                           '<input type="text" name="'.$item.'" value="'.
 3533:                           $to{$item}.'" /></td></tr>';
 3534:             $rownum ++;
 3535:         }
 3536:     } elsif ($position eq 'bottom') {
 3537:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3538:         $datatable .= '<tr'.$css_class.'>'.
 3539:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3540:                       &mt('(e-mail, subject, and description always shown)').
 3541:                       '</td><td class="LC_left_item">';
 3542:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3543:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3544:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3545:             foreach my $field (@{$fields}) {
 3546:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3547:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3548:                     $datatable .= ' '.&mt('(logged-in users)');
 3549:                 }
 3550:                 $datatable .='</td><td>';
 3551:                 my $clickaction;
 3552:                 if ($field eq 'screenshot') {
 3553:                     $clickaction = ' onclick="screenshotSize(this);"';
 3554:                 }
 3555:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3556:                     foreach my $option (@{$possoptions->{$field}}) {
 3557:                         my $checked;
 3558:                         if ($currfield{$field} eq $option) {
 3559:                             $checked = ' checked="checked"';
 3560:                         }
 3561:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3562:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3563:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3564:                                       '</label></span>'.('&nbsp;'x2);
 3565:                     }
 3566:                 }
 3567:                 if ($field eq 'screenshot') {
 3568:                     my $display;
 3569:                     if ($currfield{$field} eq 'no') {
 3570:                         $display = ' style="display:none"';
 3571:                     }
 3572:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3573:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3574:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3575:                 }
 3576:                 $datatable .= '</td></tr>';
 3577:             }
 3578:             $datatable .= '</table>';
 3579:         }
 3580:         $datatable .= '</td></tr>'."\n";
 3581:         $rownum ++;
 3582:     }
 3583:     unless (($position eq 'top') || ($position eq 'lower')) {
 3584:         foreach my $type (@mailings) {
 3585:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3586:             $datatable .= '<tr'.$css_class.'>'.
 3587:                           '<td><span class="LC_nobreak">'.
 3588:                           $titles->{$type}.': </span></td>'.
 3589:                           '<td class="LC_left_item">';
 3590:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3591:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3592:             }
 3593:             $datatable .= '<span class="LC_nobreak">';
 3594:             foreach my $item (@contacts) {
 3595:                 $datatable .= '<label>'.
 3596:                               '<input type="checkbox" name="'.$type.'"'.
 3597:                               $checked{$type}{$item}.
 3598:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3599:                               '</label>&nbsp;';
 3600:             }
 3601:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3602:                           '<input type="text" name="'.$type.'_others" '.
 3603:                           'value="'.$otheremails{$type}.'"  />';
 3604:             my %locchecked;
 3605:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3606:                 foreach my $loc ('s','b') {
 3607:                     if ($includeloc{$type} eq $loc) {
 3608:                         $locchecked{$loc} = ' checked="checked"';
 3609:                         last;
 3610:                     }
 3611:                 }
 3612:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3613:                               '<input type="text" name="'.$type.'_bcc" '.
 3614:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3615:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3616:                               &mt('Text automatically added to e-mail:').' '.
 3617:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3618:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3619:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3620:                               ('&nbsp;'x2).
 3621:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3622:                               '</span></fieldset>';
 3623:             }
 3624:             $datatable .= '</td></tr>'."\n";
 3625:             $rownum ++;
 3626:         }
 3627:     }
 3628:     if ($position eq 'middle') {
 3629:         my %choices;
 3630:         my $corelink = &core_link_msu();
 3631:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3632:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3633:                                         $corelink);
 3634:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3635:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3636:         my %defaultchecked = ('reporterrors'  => 'on',
 3637:                               'reportupdates' => 'on',
 3638:                               'reportstatus'  => 'on');
 3639:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3640:                                                    \%choices,$rownum);
 3641:         $datatable .= $reports;
 3642:     } elsif ($position eq 'lower') {
 3643:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3644:         my ($threshold,$sysmail,%excluded,%weights);
 3645:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3646:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3647:             $threshold = $lonstatus{'threshold'};
 3648:         } else {
 3649:             $threshold = $defaults->{'threshold'};
 3650:         }
 3651:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3652:             $sysmail = $lonstatus{'sysmail'};
 3653:         } else {
 3654:             $sysmail = $defaults->{'sysmail'};
 3655:         }
 3656:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3657:             foreach my $type ('E','W','N','U') {
 3658:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3659:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3660:                 } else {
 3661:                     $weights{$type} = $defaults->{$type};
 3662:                 }
 3663:             }
 3664:         } else {
 3665:             foreach my $type ('E','W','N','U') {
 3666:                 $weights{$type} = $defaults->{$type};
 3667:             }
 3668:         }
 3669:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3670:             if (@{$lonstatus{'excluded'}} > 0) {
 3671:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3672:             }
 3673:         }
 3674:         $datatable .= '<tr'.$css_class.'>'.
 3675:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3676:                       $titles->{'errorthreshold'}.
 3677:                       '</span></td><td class="LC_left_item">'.
 3678:                       '<input type="text" name="errorthreshold" value="'.
 3679:                       $threshold.'" size="5" /></td></tr>';
 3680:         $rownum ++;
 3681:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3682:         $datatable .= '<tr'.$css_class.'>'.
 3683:                       '<td class="LC_left_item">'.
 3684:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 3685:                       '</span></td><td class="LC_left_item"><table><tr>';
 3686:         foreach my $type ('E','W','N','U') {
 3687:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 3688:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 3689:                           $weights{$type}.'" size="5" /></td>';
 3690:         }
 3691:         $datatable .= '</tr></table></tr>';
 3692:         $rownum ++;
 3693:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3694:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 3695:                       $titles->{'errorexcluded'}.'</td>'.
 3696:                       '<td class="LC_left_item"><table>';
 3697:         my $numinrow = 4;
 3698:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 3699:         for (my $i=0; $i<@ids; $i++) {
 3700:             my $rem = $i%($numinrow);
 3701:             if ($rem == 0) {
 3702:                 if ($i > 0) {
 3703:                     $datatable .= '</tr>';
 3704:                 }
 3705:                 $datatable .= '<tr>';
 3706:             }
 3707:             my $check;
 3708:             if ($excluded{$ids[$i]}) {
 3709:                 $check = ' checked="checked" ';
 3710:             }
 3711:             $datatable .= '<td class="LC_left_item">'.
 3712:                           '<span class="LC_nobreak"><label>'.
 3713:                           '<input type="checkbox" name="errorexcluded" '.
 3714:                           'value="'.$ids[$i].'"'.$check.' />'.
 3715:                           $ids[$i].'</label></span></td>';
 3716:         }
 3717:         my $colsleft = $numinrow - @ids%($numinrow);
 3718:         if ($colsleft > 1 ) {
 3719:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3720:                           '&nbsp;</td>';
 3721:         } elsif ($colsleft == 1) {
 3722:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3723:         }
 3724:         $datatable .= '</tr></table></td></tr>';
 3725:         $rownum ++;
 3726:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3727:         $datatable .= '<tr'.$css_class.'>'.
 3728:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3729:                       $titles->{'errorsysmail'}.
 3730:                       '</span></td><td class="LC_left_item">'.
 3731:                       '<input type="text" name="errorsysmail" value="'.
 3732:                       $sysmail.'" size="5" /></td></tr>';
 3733:         $rownum ++;
 3734:     } elsif ($position eq 'bottom') {
 3735:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3736:         my (@posstypes,%usertypeshash);
 3737:         if (ref($types) eq 'ARRAY') {
 3738:             @posstypes = @{$types};
 3739:         }
 3740:         if (@posstypes) {
 3741:             if (ref($usertypes) eq 'HASH') {
 3742:                 %usertypeshash = %{$usertypes};
 3743:             }
 3744:             my @overridden;
 3745:             my $numinrow = 4;
 3746:             if (ref($settings) eq 'HASH') {
 3747:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3748:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3749:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3750:                             push(@overridden,$key);
 3751:                             foreach my $item (@contacts) {
 3752:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3753:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3754:                                 }
 3755:                             }
 3756:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3757:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3758:                             $includeloc{'override_'.$key} = '';
 3759:                             $includestr{'override_'.$key} = '';
 3760:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3761:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 3762:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3763:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3764:                             }
 3765:                         }
 3766:                     }
 3767:                 }
 3768:             }
 3769:             my $customclass = 'LC_helpdesk_override';
 3770:             my $optionsprefix = 'LC_options_helpdesk_';
 3771: 
 3772:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3773:  
 3774:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3775:                                          $numinrow,$othertitle,'overrides',
 3776:                                          \$rownum,$onclicktypes,$customclass);
 3777:             $rownum ++;
 3778:             $usertypeshash{'default'} = $othertitle;
 3779:             foreach my $status (@posstypes) {
 3780:                 my $css_class;
 3781:                 if ($rownum%2) {
 3782:                     $css_class = 'LC_odd_row ';
 3783:                 }
 3784:                 $css_class .= $customclass;
 3785:                 my $rowid = $optionsprefix.$status;
 3786:                 my $hidden = 1;
 3787:                 my $currstyle = 'display:none';
 3788:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3789:                     $currstyle = 'display:table-row';
 3790:                     $hidden = 0;
 3791:                 }
 3792:                 my $key = 'override_'.$status;
 3793:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3794:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3795:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3796:                                                   \@contacts,$short_titles);
 3797:                 unless ($hidden) {
 3798:                     $rownum ++;
 3799:                 }
 3800:             }
 3801:         }
 3802:     }
 3803:     $$rowtotal += $rownum;
 3804:     return $datatable;
 3805: }
 3806: 
 3807: sub core_link_msu {
 3808:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3809:                                           &mt('LON-CAPA core group - MSU'),600,500);
 3810: }
 3811: 
 3812: sub overridden_helpdesk {
 3813:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3814:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3815:     my $class = 'LC_left_item';
 3816:     if ($css_class) {
 3817:         $css_class = ' class="'.$css_class.'"';
 3818:     }
 3819:     if ($rowid) {
 3820:         $rowid = ' id="'.$rowid.'"';
 3821:     }
 3822:     if ($rowstyle) {
 3823:         $rowstyle = ' style="'.$rowstyle.'"';
 3824:     }
 3825:     my ($output,$description);
 3826:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3827:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3828:               "<td>$description</td>\n".
 3829:               '<td class="'.$class.'" colspan="2">'.
 3830:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3831:               '<span class="LC_nobreak">';
 3832:     if (ref($contacts) eq 'ARRAY') {
 3833:         foreach my $item (@{$contacts}) {
 3834:             my $check;
 3835:             if (ref($checked) eq 'HASH') {
 3836:                $check = $checked->{$item};
 3837:             }
 3838:             my $title;
 3839:             if (ref($short_titles) eq 'HASH') {
 3840:                 $title = $short_titles->{$item}; 
 3841:             }
 3842:             $output .= '<label>'.
 3843:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3844:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3845:         }
 3846:     }
 3847:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3848:                '<input type="text" name="override_'.$type.'_others" '.
 3849:                'value="'.$otheremails.'"  />';
 3850:     my %locchecked;
 3851:     foreach my $loc ('s','b') {
 3852:         if ($includeloc eq $loc) {
 3853:             $locchecked{$loc} = ' checked="checked"';
 3854:             last;
 3855:         }
 3856:     }
 3857:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3858:                '<input type="text" name="override_'.$type.'_bcc" '.
 3859:                'value="'.$bccemails.'"  /></fieldset>'.
 3860:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3861:                &mt('Text automatically added to e-mail:').' '.
 3862:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3863:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3864:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3865:                ('&nbsp;'x2).
 3866:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3867:                '</span></fieldset>'.
 3868:                '</td></tr>'."\n";
 3869:     return $output;
 3870: }
 3871: 
 3872: sub contacts_javascript {
 3873:     return <<"ENDSCRIPT";
 3874: 
 3875: <script type="text/javascript">
 3876: // <![CDATA[
 3877: 
 3878: function screenshotSize(field) {
 3879:     if (document.getElementById('help_screenshotsize')) {
 3880:         if (field.value == 'no') {
 3881:             document.getElementById('help_screenshotsize').style.display="none";
 3882:         } else {
 3883:             document.getElementById('help_screenshotsize').style.display="";
 3884:         }
 3885:     }
 3886:     return;
 3887: }
 3888: 
 3889: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3890:     if (form.elements[checkbox].length != undefined) {
 3891:         var count = 0;
 3892:         if (docount) {
 3893:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3894:                 if (form.elements[checkbox][i].checked) {
 3895:                     count ++;
 3896:                 }
 3897:             }
 3898:         }
 3899:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3900:             var type = form.elements[checkbox][i].value;
 3901:             if (document.getElementById(prefix+type)) {
 3902:                 if (form.elements[checkbox][i].checked) {
 3903:                     document.getElementById(prefix+type).style.display = 'table-row';
 3904:                     if (count % 2 == 1) {
 3905:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3906:                     } else {
 3907:                         document.getElementById(prefix+type).className = target;
 3908:                     }
 3909:                     count ++;
 3910:                 } else {
 3911:                     document.getElementById(prefix+type).style.display = 'none';
 3912:                 }
 3913:             }
 3914:         }
 3915:     }
 3916:     return;
 3917: }
 3918: 
 3919: 
 3920: // ]]>
 3921: </script>
 3922: 
 3923: ENDSCRIPT
 3924: }
 3925: 
 3926: sub print_helpsettings {
 3927:     my ($position,$dom,$settings,$rowtotal) = @_;
 3928:     my $confname = $dom.'-domainconfig';
 3929:     my $formname = 'display';
 3930:     my ($datatable,$itemcount);
 3931:     if ($position eq 'top') {
 3932:         $itemcount = 1;
 3933:         my (%choices,%defaultchecked,@toggles);
 3934:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3935:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3936:                                      &mt('LON-CAPA bug tracker'),600,500));
 3937:         %defaultchecked = ('submitbugs' => 'on');
 3938:         @toggles = ('submitbugs');
 3939:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3940:                                                      \%choices,$itemcount);
 3941:         $$rowtotal ++;
 3942:     } else {
 3943:         my $css_class;
 3944:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3945:         my (%customroles,%ordered,%current);
 3946:         if (ref($settings) eq 'HASH') {
 3947:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3948:                 %current = %{$settings->{'adhoc'}};
 3949:             }
 3950:         }
 3951:         my $count = 0;
 3952:         foreach my $key (sort(keys(%existing))) {
 3953:             if ($key=~/^rolesdef\_(\w+)$/) {
 3954:                 my $rolename = $1;
 3955:                 my (%privs,$order);
 3956:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3957:                 $customroles{$rolename} = \%privs;
 3958:                 if (ref($current{$rolename}) eq 'HASH') {
 3959:                     $order = $current{$rolename}{'order'};
 3960:                 }
 3961:                 if ($order eq '') {
 3962:                     $order = $count;
 3963:                 }
 3964:                 $ordered{$order} = $rolename;
 3965:                 $count++;
 3966:             }
 3967:         }
 3968:         my $maxnum = scalar(keys(%ordered));
 3969:         my @roles_by_num = ();
 3970:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3971:             push(@roles_by_num,$item);
 3972:         }
 3973:         my $context = 'domprefs';
 3974:         my $crstype = 'Course';
 3975:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3976:         my @accesstypes = ('all','dh','da','none');
 3977:         my ($numstatustypes,@jsarray);
 3978:         if (ref($types) eq 'ARRAY') {
 3979:             if (@{$types} > 0) {
 3980:                 $numstatustypes = scalar(@{$types});
 3981:                 push(@accesstypes,'status');
 3982:                 @jsarray = ('bystatus');
 3983:             }
 3984:         }
 3985:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3986:         if (keys(%domhelpdesk)) {
 3987:             push(@accesstypes,('inc','exc'));
 3988:             push(@jsarray,('notinc','notexc'));
 3989:         }
 3990:         my $hiddenstr = join("','",@jsarray);
 3991:         my $context = 'domprefs';
 3992:         my $crstype = 'Course';
 3993:         my $prefix = 'helproles_';
 3994:         my $add_class = 'LC_hidden';
 3995:         foreach my $num (@roles_by_num) {
 3996:             my $role = $ordered{$num};
 3997:             my ($desc,$access,@statuses);
 3998:             if (ref($current{$role}) eq 'HASH') {
 3999:                 $desc = $current{$role}{'desc'};
 4000:                 $access = $current{$role}{'access'};
 4001:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4002:                     @statuses = @{$current{$role}{'insttypes'}};
 4003:                 }
 4004:             }
 4005:             if ($desc eq '') {
 4006:                 $desc = $role;
 4007:             }
 4008:             my $identifier = 'custhelp'.$num;
 4009:             my %full=();
 4010:             my %levels= (
 4011:                          course => {},
 4012:                          domain => {},
 4013:                          system => {},
 4014:                         );
 4015:             my %levelscurrent=(
 4016:                                course => {},
 4017:                                domain => {},
 4018:                                system => {},
 4019:                               );
 4020:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4021:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4022:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4023:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4024:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4025:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4026:             for (my $k=0; $k<=$maxnum; $k++) {
 4027:                 my $vpos = $k+1;
 4028:                 my $selstr;
 4029:                 if ($k == $num) {
 4030:                     $selstr = ' selected="selected" ';
 4031:                 }
 4032:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4033:             }
 4034:             $datatable .= '</select>'.('&nbsp;'x2).
 4035:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4036:                           '</td>'.
 4037:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4038:                           &mt('Name shown to users:').
 4039:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4040:                           '</fieldset>'.
 4041:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4042:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4043:                           '<fieldset>'.
 4044:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4045:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4046:                                                                    \%levelscurrent,$identifier,
 4047:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4048:                           '</fieldset></td>';
 4049:             $itemcount ++;
 4050:         }
 4051:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4052:         my $newcust = 'custhelp'.$count;
 4053:         my (%privs,%levelscurrent);
 4054:         my %full=();
 4055:         my %levels= (
 4056:                      course => {},
 4057:                      domain => {},
 4058:                      system => {},
 4059:                     );
 4060:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4061:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4062:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4063:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4064:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4065:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4066:         for (my $k=0; $k<$maxnum+1; $k++) {
 4067:             my $vpos = $k+1;
 4068:             my $selstr;
 4069:             if ($k == $maxnum) {
 4070:                 $selstr = ' selected="selected" ';
 4071:             }
 4072:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4073:         }
 4074:         $datatable .= '</select>&nbsp;'."\n".
 4075:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4076:                       '</label></span></td>'.
 4077:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4078:                       '<span class="LC_nobreak">'.
 4079:                       &mt('Internal name:').
 4080:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4081:                       '</span>'.('&nbsp;'x4).
 4082:                       '<span class="LC_nobreak">'.
 4083:                       &mt('Name shown to users:').
 4084:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4085:                       '</span></fieldset>'.
 4086:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4087:                                              $usertypes,$types,\%domhelpdesk).
 4088:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4089:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4090:                                                                 \@templateroles,$newcust).
 4091:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4092:                                                                \%levelscurrent,$newcust).
 4093:                       '</fieldset>'.
 4094:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4095:                       '</td></tr>';
 4096:         $count ++;
 4097:         $$rowtotal += $count;
 4098:     }
 4099:     return $datatable;
 4100: }
 4101: 
 4102: sub adhocbutton {
 4103:     my ($prefix,$num,$field,$visibility) = @_;
 4104:     my %lt = &Apache::lonlocal::texthash(
 4105:                                           show => 'Show details',
 4106:                                           hide => 'Hide details',
 4107:                                         );
 4108:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4109:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4110:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4111:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4112: }
 4113: 
 4114: sub helpsettings_javascript {
 4115:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4116:     return unless(ref($roles_by_num) eq 'ARRAY');
 4117:     my %html_js_lt = &Apache::lonlocal::texthash(
 4118:                                           show => 'Show details',
 4119:                                           hide => 'Hide details',
 4120:                                         );
 4121:     &html_escape(\%html_js_lt);
 4122:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4123:     return <<"ENDSCRIPT";
 4124: <script type="text/javascript">
 4125: // <![CDATA[
 4126: 
 4127: function reorderHelpRoles(form,item) {
 4128:     var changedVal;
 4129: $jstext
 4130:     var newpos = 'helproles_${total}_pos';
 4131:     var maxh = 1 + $total;
 4132:     var current = new Array();
 4133:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4134:     if (item == newpos) {
 4135:         changedVal = newitemVal;
 4136:     } else {
 4137:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4138:         current[newitemVal] = newpos;
 4139:     }
 4140:     for (var i=0; i<helproles.length; i++) {
 4141:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4142:         if (elementName != item) {
 4143:             if (form.elements[elementName]) {
 4144:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4145:                 current[currVal] = elementName;
 4146:             }
 4147:         }
 4148:     }
 4149:     var oldVal;
 4150:     for (var j=0; j<maxh; j++) {
 4151:         if (current[j] == undefined) {
 4152:             oldVal = j;
 4153:         }
 4154:     }
 4155:     if (oldVal < changedVal) {
 4156:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4157:            var elementName = current[k];
 4158:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4159:         }
 4160:     } else {
 4161:         for (var k=changedVal; k<oldVal; k++) {
 4162:             var elementName = current[k];
 4163:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4164:         }
 4165:     }
 4166:     return;
 4167: }
 4168: 
 4169: function helpdeskAccess(num) {
 4170:     var curraccess = null;
 4171:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4172:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4173:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4174:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4175:             }
 4176:         }
 4177:     }
 4178:     var shown = Array();
 4179:     var hidden = Array();
 4180:     if (curraccess == 'none') {
 4181:         hidden = Array('$hiddenstr');
 4182:     } else {
 4183:         if (curraccess == 'status') {
 4184:             shown = Array('bystatus');
 4185:             hidden = Array('notinc','notexc');
 4186:         } else {
 4187:             if (curraccess == 'exc') {
 4188:                 shown = Array('notexc');
 4189:                 hidden = Array('notinc','bystatus');
 4190:             }
 4191:             if (curraccess == 'inc') {
 4192:                 shown = Array('notinc');
 4193:                 hidden = Array('notexc','bystatus');
 4194:             }
 4195:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4196:                 hidden = Array('notinc','notexc','bystatus');
 4197:             }
 4198:         }
 4199:     }
 4200:     if (hidden.length > 0) {
 4201:         for (var i=0; i<hidden.length; i++) {
 4202:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4203:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4204:             }
 4205:         }
 4206:     }
 4207:     if (shown.length > 0) {
 4208:         for (var i=0; i<shown.length; i++) {
 4209:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4210:                 if (shown[i] == 'privs') {
 4211:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4212:                 } else {
 4213:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4214:                 }
 4215:             }
 4216:         }
 4217:     }
 4218:     return;
 4219: }
 4220: 
 4221: function toggleHelpdeskItem(num,field) {
 4222:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4223:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4224:             document.getElementById('helproles_'+num+'_'+field).className =
 4225:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4226:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4227:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4228:             }
 4229:         } else {
 4230:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4231:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4232:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4233:             }
 4234:         }
 4235:     }
 4236:     return;
 4237: }
 4238: 
 4239: // ]]>
 4240: </script>
 4241: 
 4242: ENDSCRIPT
 4243: }
 4244: 
 4245: sub helpdeskroles_access {
 4246:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4247:         $usertypes,$types,$domhelpdesk) = @_;
 4248:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4249:     my %lt = &Apache::lonlocal::texthash(
 4250:                     'rou'    => 'Role usage',
 4251:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4252:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4253:                     'dh'     => 'All with domain helpdesk role',
 4254:                     'da'     => 'All with domain helpdesk assistant role',
 4255:                     'none'   => 'None',
 4256:                     'status' => 'Determined based on institutional status',
 4257:                     'inc'    => 'Include all, but exclude specific personnel',
 4258:                     'exc'    => 'Exclude all, but include specific personnel',
 4259:                   );
 4260:     my %usecheck = (
 4261:                      all => ' checked="checked"',
 4262:                    );
 4263:     my %displaydiv = (
 4264:                       status => 'none',
 4265:                       inc    => 'none',
 4266:                       exc    => 'none',
 4267:                       priv   => 'block',
 4268:                      );
 4269:     my $output;
 4270:     if (ref($current) eq 'HASH') {
 4271:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4272:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4273:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4274:                 delete($usecheck{'all'});
 4275:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4276:                     my $access = $1;
 4277:                     $displaydiv{$access} = 'inline';
 4278:                 } elsif ($current->{access} eq 'none') {
 4279:                     $displaydiv{'priv'} = 'none';
 4280:                 }
 4281:             }
 4282:         }
 4283:     }
 4284:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4285:               '<p>'.$lt{'whi'}.'</p>';
 4286:     foreach my $access (@{$accesstypes}) {
 4287:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4288:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4289:                    $lt{$access}.'</label>';
 4290:         if ($access eq 'status') {
 4291:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4292:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4293:                                                                  $othertitle,$usertypes,$types).
 4294:                        '</div>';
 4295:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4296:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4297:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4298:                        '</div>';
 4299:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4300:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4301:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4302:                        '</div>';
 4303:         }
 4304:         $output .= '</p>';
 4305:     }
 4306:     $output .= '</fieldset>';
 4307:     return $output;
 4308: }
 4309: 
 4310: sub radiobutton_prefs {
 4311:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4312:         $additional,$align) = @_;
 4313:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4314:                    (ref($choices) eq 'HASH'));
 4315: 
 4316:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4317: 
 4318:     foreach my $item (@{$toggles}) {
 4319:         if ($defaultchecked->{$item} eq 'on') {
 4320:             $checkedon{$item} = ' checked="checked" ';
 4321:             $checkedoff{$item} = ' ';
 4322:         } elsif ($defaultchecked->{$item} eq 'off') {
 4323:             $checkedoff{$item} = ' checked="checked" ';
 4324:             $checkedon{$item} = ' ';
 4325:         }
 4326:     }
 4327:     if (ref($settings) eq 'HASH') {
 4328:         foreach my $item (@{$toggles}) {
 4329:             if ($settings->{$item} eq '1') {
 4330:                 $checkedon{$item} =  ' checked="checked" ';
 4331:                 $checkedoff{$item} = ' ';
 4332:             } elsif ($settings->{$item} eq '0') {
 4333:                 $checkedoff{$item} =  ' checked="checked" ';
 4334:                 $checkedon{$item} = ' ';
 4335:             }
 4336:         }
 4337:     }
 4338:     if ($onclick) {
 4339:         $onclick = ' onclick="'.$onclick.'"';
 4340:     }
 4341:     foreach my $item (@{$toggles}) {
 4342:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4343:         $datatable .=
 4344:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4345:             '<span class="LC_nobreak">'.$choices->{$item}.
 4346:             '</span></td>';
 4347:         if ($align eq 'left') {
 4348:             $datatable .= '<td class="LC_left_item">';
 4349:         } else {
 4350:             $datatable .= '<td class="LC_right_item">';
 4351:         }
 4352:         $datatable .=
 4353:             '<span class="LC_nobreak">'.
 4354:             '<label><input type="radio" name="'.
 4355:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4356:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4357:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4358:             '</span>'.$additional.
 4359:             '</td>'.
 4360:             '</tr>';
 4361:         $itemcount ++;
 4362:     }
 4363:     return ($datatable,$itemcount);
 4364: }
 4365: 
 4366: sub print_ltitools {
 4367:     my ($dom,$settings,$rowtotal) = @_;
 4368:     my $rownum = 0;
 4369:     my $css_class;
 4370:     my $itemcount = 1;
 4371:     my $maxnum = 0;
 4372:     my %ordered;
 4373:     if (ref($settings) eq 'HASH') {
 4374:         foreach my $item (keys(%{$settings})) {
 4375:             if (ref($settings->{$item}) eq 'HASH') {
 4376:                 my $num = $settings->{$item}{'order'};
 4377:                 $ordered{$num} = $item;
 4378:             }
 4379:         }
 4380:     }
 4381:     my $confname = $dom.'-domainconfig';
 4382:     my $switchserver = &check_switchserver($dom,$confname);
 4383:     my $maxnum = scalar(keys(%ordered));
 4384:     my $datatable;
 4385:     my %lt = &ltitools_names();
 4386:     my @courseroles = ('cc','in','ta','ep','st');
 4387:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4388:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4389:     if (keys(%ordered)) {
 4390:         my @items = sort { $a <=> $b } keys(%ordered);
 4391:         for (my $i=0; $i<@items; $i++) {
 4392:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4393:             my $item = $ordered{$items[$i]};
 4394:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4395:             if (ref($settings->{$item}) eq 'HASH') {
 4396:                 $title = $settings->{$item}->{'title'};
 4397:                 $url = $settings->{$item}->{'url'};
 4398:                 $key = $settings->{$item}->{'key'};
 4399:                 $secret = $settings->{$item}->{'secret'};
 4400:                 $lifetime = $settings->{$item}->{'lifetime'};
 4401:                 my $image = $settings->{$item}->{'image'};
 4402:                 if ($image ne '') {
 4403:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4404:                 }
 4405:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4406:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4407:                 } else {
 4408:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4409:                 }
 4410:             }
 4411:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4412:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4413:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4414:             for (my $k=0; $k<=$maxnum; $k++) {
 4415:                 my $vpos = $k+1;
 4416:                 my $selstr;
 4417:                 if ($k == $i) {
 4418:                     $selstr = ' selected="selected" ';
 4419:                 }
 4420:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4421:             }
 4422:             $datatable .= '</select>'.('&nbsp;'x2).
 4423:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4424:                 &mt('Delete?').'</label></span></td>'.
 4425:                 '<td colspan="2">'.
 4426:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4427:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4428:                 ('&nbsp;'x2).
 4429:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4430:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4431:                 ('&nbsp;'x2).
 4432:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4433:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4434:                 ('&nbsp;'x2).
 4435:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4436:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4437:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4438:                 '<br /><br />'.
 4439:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4440:                 ' value="'.$url.'" /></span>'.
 4441:                 ('&nbsp;'x2).
 4442:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4443:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4444:                 ('&nbsp;'x2).
 4445:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4446:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4447:                 ('&nbsp;'x2).
 4448:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4449:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4450:                 '<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>'.
 4451:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4452:                 '</fieldset>'.
 4453:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4454:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4455:             my %currdisp;
 4456:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4457:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4458:                     $currdisp{'window'} = ' checked="checked"';
 4459:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4460:                     $currdisp{'tab'} = ' checked="checked"';
 4461:                 } else {
 4462:                     $currdisp{'iframe'} = ' checked="checked"';
 4463:                 }
 4464:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4465:                     $currdisp{'width'} = $1;
 4466:                 }
 4467:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4468:                      $currdisp{'height'} = $1;
 4469:                 }
 4470:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4471:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4472:             } else {
 4473:                 $currdisp{'iframe'} = ' checked="checked"';
 4474:             }
 4475:             foreach my $disp ('iframe','tab','window') {
 4476:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4477:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4478:             }
 4479:             $datatable .= ('&nbsp;'x4);
 4480:             foreach my $dimen ('width','height') {
 4481:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4482:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4483:                               ('&nbsp;'x2);
 4484:             }
 4485:             $datatable .= '</span><br />'.
 4486:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4487:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4488:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4489:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4490:                           '</textarea></div><div style=""></div><br />';
 4491:             my %units = (
 4492:                           'passback' => 'days',
 4493:                           'roster'   => 'seconds',
 4494:                         );
 4495:             foreach my $extra ('passback','roster') {
 4496:                 my $validsty = 'none';
 4497:                 my $currvalid;
 4498:                 my $checkedon = '';
 4499:                 my $checkedoff = ' checked="checked"';
 4500:                 if ($settings->{$item}->{$extra}) {
 4501:                     $checkedon = $checkedoff;
 4502:                     $checkedoff = '';
 4503:                     $validsty = 'inline-block';
 4504:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 4505:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 4506:                     }
 4507:                 }
 4508:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 4509:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4510:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 4511:                               &mt('No').'</label>'.('&nbsp;'x2).
 4512:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 4513:                               &mt('Yes').'</label></span></div>'.
 4514:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 4515:                               '<span class="LC_nobreak">'.
 4516:                               &mt("at least [_1] $units{$extra} after launch",
 4517:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 4518:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4519:             }
 4520:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4521:             if ($imgsrc) {
 4522:                 $datatable .= $imgsrc.
 4523:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4524:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4525:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4526:             } else {
 4527:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4528:             }
 4529:             if ($switchserver) {
 4530:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4531:             } else {
 4532:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4533:             }
 4534:             $datatable .= '</span></fieldset>';
 4535:             my (%checkedfields,%rolemaps,$userincdom);
 4536:             if (ref($settings->{$item}) eq 'HASH') {
 4537:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4538:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4539:                 }
 4540:                 $userincdom = $settings->{$item}->{'incdom'};
 4541:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4542:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4543:                     $checkedfields{'roles'} = 1;
 4544:                 }
 4545:             }
 4546:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4547:                           '<span class="LC_nobreak">';
 4548:             my $userfieldstyle = 'display:none;';
 4549:             my $seluserdom = '';
 4550:             my $unseluserdom = ' selected="selected"';
 4551:             foreach my $field (@fields) {
 4552:                 my ($checked,$onclick,$id,$spacer);
 4553:                 if ($checkedfields{$field}) {
 4554:                     $checked = ' checked="checked"';
 4555:                 }
 4556:                 if ($field eq 'user') {
 4557:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4558:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4559:                     if ($checked) {
 4560:                         $userfieldstyle = 'display:inline-block';
 4561:                         if ($userincdom) {
 4562:                             $seluserdom = $unseluserdom;
 4563:                             $unseluserdom = '';
 4564:                         }
 4565:                     }
 4566:                 } else {
 4567:                     $spacer = ('&nbsp;' x2);
 4568:                 }
 4569:                 $datatable .= '<label>'.
 4570:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4571:                               $lt{$field}.'</label>'.$spacer;
 4572:             }
 4573:             $datatable .= '</span>';
 4574:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4575:                           '<span class="LC_nobreak"> : '.
 4576:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4577:                           '<option value="">'.&mt('Select').'</option>'.
 4578:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4579:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4580:                           '</select></span></div>';
 4581:             $datatable .= '</fieldset>'.
 4582:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4583:             foreach my $role (@courseroles) {
 4584:                 my ($selected,$selectnone);
 4585:                 if (!$rolemaps{$role}) {
 4586:                     $selectnone = ' selected="selected"';
 4587:                 }
 4588:                 $datatable .= '<td style="text-align: center">'. 
 4589:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4590:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4591:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4592:                 foreach my $ltirole (@ltiroles) {
 4593:                     unless ($selectnone) {
 4594:                         if ($rolemaps{$role} eq $ltirole) {
 4595:                             $selected = ' selected="selected"';
 4596:                         } else {
 4597:                             $selected = '';
 4598:                         }
 4599:                     }
 4600:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4601:                 }
 4602:                 $datatable .= '</select></td>';
 4603:             }
 4604:             $datatable .= '</tr></table></fieldset>';
 4605:             my %courseconfig;
 4606:             if (ref($settings->{$item}) eq 'HASH') {
 4607:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4608:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4609:                 }
 4610:             }
 4611:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4612:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4613:                 my $checked;
 4614:                 if ($courseconfig{$item}) {
 4615:                     $checked = ' checked="checked"';
 4616:                 }
 4617:                 $datatable .= '<label>'.
 4618:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4619:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4620:             }
 4621:             $datatable .= '</span></fieldset>'.
 4622:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4623:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4624:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4625:                 my %custom = %{$settings->{$item}->{'custom'}};
 4626:                 if (keys(%custom) > 0) {
 4627:                     foreach my $key (sort(keys(%custom))) {
 4628:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4629:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4630:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4631:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4632:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4633:                     }
 4634:                 }
 4635:             }
 4636:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4637:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4638:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4639:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4640:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4641:             $itemcount ++;
 4642:         }
 4643:     }
 4644:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4645:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4646:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4647:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4648:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4649:     for (my $k=0; $k<$maxnum+1; $k++) {
 4650:         my $vpos = $k+1;
 4651:         my $selstr;
 4652:         if ($k == $maxnum) {
 4653:             $selstr = ' selected="selected" ';
 4654:         }
 4655:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4656:     }
 4657:     $datatable .= '</select>&nbsp;'."\n".
 4658:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4659:                   '<td colspan="2">'.
 4660:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4661:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4662:                   ('&nbsp;'x2).
 4663:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4664:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4665:                   ('&nbsp;'x2).
 4666:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4667:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4668:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4669:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4670:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4671:                   '<br />'.
 4672:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4673:                   ('&nbsp;'x2).
 4674:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4675:                   ('&nbsp;'x2).
 4676:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4677:                   ('&nbsp;'x2).
 4678:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4679:                   '<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".
 4680:                   '</fieldset>'.
 4681:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4682:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4683:     my %defaultdisp;
 4684:     $defaultdisp{'iframe'} = ' checked="checked"';
 4685:     foreach my $disp ('iframe','tab','window') {
 4686:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4687:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4688:     }
 4689:     $datatable .= ('&nbsp;'x4);
 4690:     foreach my $dimen ('width','height') {
 4691:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4692:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4693:                       ('&nbsp;'x2);
 4694:     }
 4695:     $datatable .= '</span><br />'.
 4696:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4697:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 4698:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4699:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4700:                   '</div><div style=""></div><br />';
 4701:     my %units = (
 4702:                   'passback' => 'days',
 4703:                   'roster'   => 'seconds',
 4704:                 );
 4705:     my %defaulttimes = (
 4706:                      'passback' => '7',
 4707:                      'roster'   => '300',
 4708:                    );
 4709:     foreach my $extra ('passback','roster') {
 4710:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 4711:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4712:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 4713:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 4714:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 4715:                       &mt('Yes').'</label></span></div>'.
 4716:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 4717:                       '<span class="LC_nobreak">'.
 4718:                       &mt("at least [_1] $units{$extra} after launch",
 4719:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 4720:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4721:     }
 4722:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4723:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4724:     if ($switchserver) {
 4725:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4726:     } else {
 4727:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4728:     }
 4729:     $datatable .= '</span></fieldset>'.
 4730:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4731:                   '<span class="LC_nobreak">';
 4732:     foreach my $field (@fields) {
 4733:         my ($id,$onclick,$spacer);
 4734:         if ($field eq 'user') {
 4735:             $id = ' id="ltitools_user_field_add"';
 4736:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 4737:         } else {
 4738:             $spacer = ('&nbsp;' x2);
 4739:         }
 4740:         $datatable .= '<label>'.
 4741:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 4742:                       $lt{$field}.'</label>'.$spacer;
 4743:     }
 4744:     $datatable .= '</span>'.
 4745:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 4746:                   '<span class="LC_nobreak"> : '.
 4747:                   '<select name="ltitools_userincdom_add">'.
 4748:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 4749:                   '<option value="0">'.&mt('username').'</option>'.
 4750:                   '<option value="1">'.&mt('username:domain').'</option>'.
 4751:                   '</select></span></div></fieldset>';
 4752:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4753:     foreach my $role (@courseroles) {
 4754:         my ($checked,$checkednone);
 4755:         $datatable .= '<td style="text-align: center">'.
 4756:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4757:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4758:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4759:         foreach my $ltirole (@ltiroles) {
 4760:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4761:         }
 4762:         $datatable .= '</select></td>';
 4763:     }
 4764:     $datatable .= '</tr></table></fieldset>'.
 4765:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4766:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4767:         $datatable .= '<label>'.
 4768:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4769:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4770:     }
 4771:     $datatable .= '</span></fieldset>'.
 4772:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4773:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4774:                   '<tr><td><span class="LC_nobreak">'.
 4775:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4776:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4777:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4778:                   '</table></fieldset>'."\n".
 4779:                   '</td>'."\n".
 4780:                   '</tr>'."\n";
 4781:     $itemcount ++;
 4782:     return $datatable;
 4783: }
 4784: 
 4785: sub ltitools_names {
 4786:     my %lt = &Apache::lonlocal::texthash(
 4787:                                           'title'          => 'Title',
 4788:                                           'version'        => 'Version',
 4789:                                           'msgtype'        => 'Message Type',
 4790:                                           'sigmethod'      => 'Signature Method',
 4791:                                           'url'            => 'URL',
 4792:                                           'key'            => 'Key',
 4793:                                           'lifetime'       => 'Nonce lifetime (s)',
 4794:                                           'secret'         => 'Secret',
 4795:                                           'icon'           => 'Icon',   
 4796:                                           'user'           => 'User',
 4797:                                           'fullname'       => 'Full Name',
 4798:                                           'firstname'      => 'First Name',
 4799:                                           'lastname'       => 'Last Name',
 4800:                                           'email'          => 'E-mail',
 4801:                                           'roles'          => 'Role',
 4802:                                           'window'         => 'Window',
 4803:                                           'tab'            => 'Tab',
 4804:                                           'iframe'         => 'iFrame',
 4805:                                           'height'         => 'Height',
 4806:                                           'width'          => 'Width',
 4807:                                           'linktext'       => 'Default Link Text',
 4808:                                           'explanation'    => 'Default Explanation',
 4809:                                           'passback'       => 'Tool can return grades:',
 4810:                                           'roster'         => 'Tool can retrieve roster:',
 4811:                                           'crstarget'      => 'Display target',
 4812:                                           'crslabel'       => 'Course label',
 4813:                                           'crstitle'       => 'Course title', 
 4814:                                           'crslinktext'    => 'Link Text',
 4815:                                           'crsexplanation' => 'Explanation',
 4816:                                           'crsappend'      => 'Provider URL',
 4817:                                         );
 4818:     return %lt;
 4819: }
 4820: 
 4821: sub print_lti {
 4822:     my ($dom,$settings,$rowtotal) = @_;
 4823:     my $itemcount = 1;
 4824:     my $maxnum = 0;
 4825:     my $css_class;
 4826:     my %ordered;
 4827:     if (ref($settings) eq 'HASH') {
 4828:         foreach my $item (keys(%{$settings})) {
 4829:             if (ref($settings->{$item}) eq 'HASH') {
 4830:                 my $num = $settings->{$item}{'order'};
 4831:                 $ordered{$num} = $item;
 4832:             }
 4833:         }
 4834:     }
 4835:     my $maxnum = scalar(keys(%ordered));
 4836:     my $datatable;
 4837:     my %lt = &lti_names();
 4838:     if (keys(%ordered)) {
 4839:         my @items = sort { $a <=> $b } keys(%ordered);
 4840:         for (my $i=0; $i<@items; $i++) {
 4841:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4842:             my $item = $ordered{$items[$i]};
 4843:             my ($key,$secret,$lifetime,$consumer,$requser,$current);
 4844:             if (ref($settings->{$item}) eq 'HASH') {
 4845:                 $key = $settings->{$item}->{'key'};
 4846:                 $secret = $settings->{$item}->{'secret'};
 4847:                 $lifetime = $settings->{$item}->{'lifetime'};
 4848:                 $consumer = $settings->{$item}->{'consumer'};
 4849:                 $requser = $settings->{$item}->{'requser'};
 4850:                 $current = $settings->{$item};
 4851:             }
 4852:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 4853:             my %checkedrequser = (
 4854:                                    yes => ' checked="checked"',
 4855:                                    no  => '',
 4856:                                  );
 4857:             if (!$requser) {
 4858:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 4859:                 $checkedrequser{'yes'} = '';
 4860:             }
 4861:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 4862:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4863:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 4864:             for (my $k=0; $k<=$maxnum; $k++) {
 4865:                 my $vpos = $k+1;
 4866:                 my $selstr;
 4867:                 if ($k == $i) {
 4868:                     $selstr = ' selected="selected" ';
 4869:                 }
 4870:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4871:             }
 4872:             $datatable .= '</select>'.('&nbsp;'x2).
 4873:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 4874:                 &mt('Delete?').'</label></span></td>'.
 4875:                 '<td colspan="2">'.
 4876:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4877:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 4878:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 4879:                 ('&nbsp;'x2).
 4880:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 4881:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4882:                 ('&nbsp;'x2).
 4883:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 4884:                 'value="'.$lifetime.'" size="3" /></span>'.
 4885:                 ('&nbsp;'x2).
 4886:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4887:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 4888:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 4889:                 '<br /><br />'.
 4890:                 '<span class="LC_nobreak">'.$lt{'key'}.
 4891:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 4892:                 ('&nbsp;'x2).
 4893:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4894:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 4895:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.lti_secret_'.$i.'.type='."'text'".' } else { this.form.lti_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>'.
 4896:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 4897:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 4898:             $itemcount ++;
 4899:         }
 4900:     }
 4901:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4902:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 4903:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4904:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 4905:                   '<select name="lti_pos_add"'.$chgstr.'>';
 4906:     for (my $k=0; $k<$maxnum+1; $k++) {
 4907:         my $vpos = $k+1;
 4908:         my $selstr;
 4909:         if ($k == $maxnum) {
 4910:             $selstr = ' selected="selected" ';
 4911:         }
 4912:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4913:     }
 4914:     $datatable .= '</select>&nbsp;'."\n".
 4915:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4916:                   '<td colspan="2">'.
 4917:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4918:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 4919:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 4920:                   ('&nbsp;'x2).
 4921:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 4922:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4923:                   ('&nbsp;'x2).
 4924:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 4925:                   ('&nbsp;'x2).
 4926:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 4927:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 4928:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 4929:                   '<br /><br />'.
 4930:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 4931:                   ('&nbsp;'x2).
 4932:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 4933:                   '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.lti_secret_add.type='."'text'".' } else { this.form.lti_secret_add.type='."'password'".' }" />'.&mt('Visible input').'</label></span> '."\n".
 4934:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 4935:                   '</td>'."\n".
 4936:                   '</tr>'."\n";
 4937:     $$rowtotal ++;
 4938:     return $datatable;;
 4939: }
 4940: 
 4941: sub lti_names {
 4942:     my %lt = &Apache::lonlocal::texthash(
 4943:                                           'version'   => 'LTI Version',
 4944:                                           'url'       => 'URL',
 4945:                                           'key'       => 'Key',
 4946:                                           'lifetime'  => 'Nonce lifetime (s)',
 4947:                                           'consumer'  => 'Consumer',
 4948:                                           'secret'    => 'Secret',
 4949:                                           'requser'   => "User's identity sent",
 4950:                                           'email'     => 'Email address',
 4951:                                           'sourcedid' => 'User ID',
 4952:                                           'other'     => 'Other',
 4953:                                           'passback'  => 'Can return grades to Consumer:',
 4954:                                           'roster'    => 'Can retrieve roster from Consumer:',
 4955:                                           'topmenu'   => 'Display LON-CAPA page header',
 4956:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 4957:                                         );
 4958:     return %lt;
 4959: }
 4960: 
 4961: sub lti_options {
 4962:     my ($num,$current,$itemcount,%lt) = @_;
 4963:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 4964:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 4965:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 4966:     $checked{'makecrs'}{'N'} = '  checked="checked"';
 4967:     $checked{'mapcrstype'} = {};
 4968:     $checked{'makeuser'} = {};
 4969:     $checked{'selfenroll'} = {};
 4970:     $checked{'crssec'} = {};
 4971:     $checked{'crssecsrc'} = {};
 4972:     $checked{'lcauth'} = {};
 4973:     $checked{'menuitem'} = {};
 4974:     if ($num eq 'add') {
 4975:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 4976:     }
 4977:     my $userfieldsty = 'none';
 4978:     my $crsfieldsty = 'none';
 4979:     my $crssecfieldsty = 'none';
 4980:     my $secsrcfieldsty = 'none';
 4981:     my $callbacksty = 'none';
 4982:     my $passbacksty = 'none';
 4983:     my $optionsty = 'block';
 4984:     my $lcauthparm;
 4985:     my $lcauthparmstyle = 'display:none';
 4986:     my $lcauthparmtext;
 4987:     my $menusty;
 4988:     my $numinrow = 4;
 4989:     my %menutitles = &ltimenu_titles();
 4990: 
 4991:     if (ref($current) eq 'HASH') {
 4992:         if (!$current->{'requser'}) {
 4993:             $optionsty = 'none';
 4994:         }
 4995:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 4996:             $checked{'mapuser'}{'sourcedid'} = '';
 4997:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 4998:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 4999:             } else {
 5000:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 5001:                 $userfield = $current->{'mapuser'};
 5002:                 $userfieldsty = 'inline-block';
 5003:             }
 5004:         }
 5005:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 5006:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 5007:             if ($current->{'mapcrs'} eq 'context_id') {
 5008:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 5009:             } else {
 5010:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 5011:                 $cidfield = $current->{'mapcrs'};
 5012:                 $crsfieldsty = 'inline-block';
 5013:             }
 5014:         }
 5015:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 5016:             foreach my $type (@{$current->{'mapcrstype'}}) {
 5017:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 5018:             }
 5019:         }
 5020:         if ($current->{'makecrs'}) {
 5021:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 5022:         }
 5023:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 5024:             foreach my $role (@{$current->{'makeuser'}}) {
 5025:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 5026:             }
 5027:         }
 5028:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 5029:             $checked{'lcauth'}{$1} = ' checked="checked"';
 5030:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 5031:                 $lcauthparm = $current->{'lcauthparm'};
 5032:                 $lcauthparmstyle = 'display:table-row'; 
 5033:                 if ($current->{'lcauth'} eq 'localauth') {
 5034:                     $lcauthparmtext = &mt('Local auth argument');
 5035:                 } else {
 5036:                     $lcauthparmtext = &mt('Kerberos domain');
 5037:                 }
 5038:             }
 5039:         }
 5040:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 5041:             foreach my $role (@{$current->{'selfenroll'}}) {
 5042:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 5043:             }
 5044:         }
 5045:         if (ref($current->{'maproles'}) eq 'HASH') {
 5046:             %rolemaps = %{$current->{'maproles'}};
 5047:         }
 5048:         if ($current->{'section'} ne '') {
 5049:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 5050:             $crssecfieldsty = 'inline-block';
 5051:             if ($current->{'section'} eq 'course_section_sourcedid') {
 5052:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 5053:             } else {
 5054:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 5055:                 $crssecsrc = $current->{'section'};
 5056:                 $secsrcfieldsty = 'inline-block';
 5057:             }
 5058:         } else {
 5059:             $checked{'crssec'}{'N'} = ' checked="checked"';
 5060:         }
 5061:         if ($current->{'callback'} ne '') {
 5062:             $callback = $current->{'callback'};
 5063:             $checked{'callback'}{'Y'} = ' checked="checked"';
 5064:             $callbacksty = 'inline-block';
 5065:         } else {
 5066:             $checked{'callback'}{'N'} = ' checked="checked"';
 5067:         }
 5068:         if ($current->{'topmenu'}) {
 5069:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 5070:         } else {
 5071:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 5072:         }
 5073:         if ($current->{'inlinemenu'}) {
 5074:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5075:         } else {
 5076:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 5077:         }
 5078:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 5079:             $menusty = 'inline-block';
 5080:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 5081:                 foreach my $item (@{$current->{'lcmenu'}}) {
 5082:                     if (exists($menutitles{$item})) {
 5083:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 5084:                     }
 5085:                 }
 5086:             }
 5087:         } else {
 5088:             $menusty = 'none';
 5089:         }
 5090:     } else {
 5091:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 5092:         $checked{'crssec'}{'N'} = ' checked="checked"';
 5093:         $checked{'callback'}{'N'} = ' checked="checked"';
 5094:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 5095:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 5096:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 5097:         $menusty = 'inline-block'; 
 5098:     }
 5099:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 5100:     my %coursetypetitles = &Apache::lonlocal::texthash (
 5101:                                official   => 'Official',
 5102:                                unofficial => 'Unofficial',
 5103:                                community  => 'Community',
 5104:                                textbook   => 'Textbook',
 5105:                                placement  => 'Placement Test',
 5106:                                lti        => 'LTI Provider',
 5107:     );
 5108:     my @authtypes = ('internal','krb4','krb5','localauth');
 5109:     my %shortauth = (
 5110:                      internal => 'int',
 5111:                      krb4 => 'krb4',
 5112:                      krb5 => 'krb5',
 5113:                      localauth  => 'loc'
 5114:                     );
 5115:     my %authnames = &authtype_names();
 5116:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 5117:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 5118:     my @courseroles = ('cc','in','ta','ep','st');
 5119:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 5120:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 5121:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 5122:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 5123:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 5124:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 5125:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 5126:     my $output = '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 5127:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 5128:     foreach my $option ('sourcedid','email','other') {
 5129:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 5130:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 5131:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5132:     }
 5133:     $output .= '</span></div>'.
 5134:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 5135:                '<input type="text" name="lti_customuser_'.$num.'" '.
 5136:                'value="'.$userfield.'" /></div></fieldset>'. 
 5137:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 5138:     foreach my $ltirole (@lticourseroles) {
 5139:         my ($selected,$selectnone);
 5140:         if ($rolemaps{$ltirole} eq '') {
 5141:             $selectnone = ' selected="selected"';
 5142:         }
 5143:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 5144:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 5145:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5146:         foreach my $role (@courseroles) {
 5147:             unless ($selectnone) {
 5148:                 if ($rolemaps{$ltirole} eq $role) {
 5149:                     $selected = ' selected="selected"';
 5150:                 } else {
 5151:                     $selected = '';
 5152:                 }
 5153:             }
 5154:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 5155:                        &Apache::lonnet::plaintext($role,'Course').
 5156:                        '</option>';
 5157:         }
 5158:         $output .= '</select></td>';
 5159:     }
 5160:     $output .= '</tr></table></fieldset>'.
 5161:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 5162:     foreach my $ltirole (@ltiroles) {
 5163:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 5164:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 5165:     }
 5166:     $output .= '</fieldset>'.
 5167:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 5168:                '<table>'.
 5169:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 5170:                '</table>'.
 5171:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 5172:                '<td class="LC_left_item">';
 5173:     foreach my $auth ('lti',@authtypes) {
 5174:         my $authtext;
 5175:         if ($auth eq 'lti') {
 5176:             $authtext = &mt('None');
 5177:         } else {
 5178:             $authtext = $authnames{$shortauth{$auth}};
 5179:         }
 5180:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 5181:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 5182:                    $authtext.'</label></span> &nbsp;';
 5183:     }
 5184:     $output .= '</td></tr>'.
 5185:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 5186:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 5187:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 5188:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 5189:                '</table></fieldset>'.
 5190:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 5191:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 5192:                &mt('Unique course identifier').':&nbsp;';
 5193:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 5194:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 5195:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 5196:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5197:     }
 5198:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 5199:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 5200:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5201:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 5202:     foreach my $type (@coursetypes) {
 5203:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 5204:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 5205:                    ('&nbsp;'x2);
 5206:     }
 5207:     $output .= '</span></fieldset>'.
 5208:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Creating courses').'</legend>'.
 5209:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 5210:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 5211:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5212:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 5213:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5214:                '</fieldset>'.
 5215:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 5216:     foreach my $lticrsrole (@lticourseroles) {
 5217:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 5218:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 5219:     }
 5220:     $output .= '</fieldset>'.
 5221:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course options').'</legend>'.
 5222:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 5223:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 5224:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5225:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 5226:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 5227:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 5228:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 5229:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 5230:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 5231:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 5232:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 5233:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 5234:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 5235:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 5236:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5237:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 5238:     foreach my $extra ('roster','passback') {
 5239:         my $checkedon = '';
 5240:         my $checkedoff = ' checked="checked"';
 5241:         if ($extra eq 'passback') {
 5242:             $pb1p1chk = ' checked="checked"';
 5243:             $pb1p0chk = '';
 5244:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 5245:         } else {
 5246:             $onclickpb = ''; 
 5247:         }
 5248:         if (ref($current) eq 'HASH') {
 5249:             if (($current->{$extra})) {
 5250:                 $checkedon = $checkedoff;
 5251:                 $checkedoff = '';
 5252:                 if ($extra eq 'passback') {
 5253:                     $passbacksty = 'inline-block';
 5254:                 }
 5255:                 if ($current->{'passbackformat'} eq '1.0') {
 5256:                     $pb1p0chk =  ' checked="checked"';
 5257:                     $pb1p1chk = '';
 5258:                 }
 5259:             }
 5260:         }
 5261:         $output .= $lt{$extra}.'&nbsp;'.
 5262:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 5263:                    &mt('No').'</label>'.('&nbsp;'x2).
 5264:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 5265:                    &mt('Yes').'</label><br />';
 5266:     }
 5267:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 5268:                '<span class="LC_nobreak">'.&mt('Grade format').
 5269:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 5270:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 5271:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 5272:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 5273:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5274:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback on logout').':&nbsp;'.
 5275:                '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 5276:                $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5277:                '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 5278:                $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 5279:                '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 5280:                '<span class="LC_nobreak">'.&mt('Parameter').': '.
 5281:                '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 5282:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 5283:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course defaults (Course Coordinator can override)').'</legend>'.
 5284:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 5285:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 5286:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5287:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 5288:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 5289:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5290:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 5291:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 5292:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5293:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 5294:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 5295:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'. 
 5296:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 5297:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 5298:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 5299:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 5300:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 5301:                    ('&nbsp;'x2);
 5302:     }
 5303:     $output .= '</span></div></fieldset>';
 5304: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 5305: #
 5306: #    $output .= '</fieldset>'.
 5307: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 5308:     return $output;
 5309: }
 5310: 
 5311: sub ltimenu_titles {
 5312:     return &Apache::lonlocal::texthash(
 5313:                                         fullname    => 'Full name',
 5314:                                         coursetitle => 'Course title',
 5315:                                         role        => 'Role',
 5316:                                         logout      => 'Logout',
 5317:                                         grades      => 'Grades',
 5318:     );
 5319: }
 5320: 
 5321: sub print_coursedefaults {
 5322:     my ($position,$dom,$settings,$rowtotal) = @_;
 5323:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 5324:     my $itemcount = 1;
 5325:     my %choices =  &Apache::lonlocal::texthash (
 5326:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 5327:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 5328:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 5329:         coursecredits        => 'Credits can be specified for courses',
 5330:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 5331:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 5332:         texengine            => 'Default method to display mathematics',
 5333:         postsubmit           => 'Disable submit button/keypress following student submission',
 5334:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 5335:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 5336:     );
 5337:     my %staticdefaults = (
 5338:                            anonsurvey_threshold => 10,
 5339:                            uploadquota          => 500,
 5340:                            postsubmit           => 60,
 5341:                            mysqltables          => 172800,
 5342:                          );
 5343:     if ($position eq 'top') {
 5344:         %defaultchecked = (
 5345:                             'canuse_pdfforms' => 'off',
 5346:                             'uselcmath'       => 'on',
 5347:                             'usejsme'         => 'on',
 5348:                             'canclone'        => 'none',
 5349:                           );
 5350:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 5351:         my $deftex = $Apache::lonnet::deftex;
 5352:         if (ref($settings) eq 'HASH') {
 5353:             if ($settings->{'texengine'}) {
 5354:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 5355:                     $deftex = $settings->{'texengine'};
 5356:                 }
 5357:             }
 5358:         }
 5359:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5360:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 5361:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 5362:                        '</span></td><td class="LC_right_item">'.
 5363:                        '<select name="texengine">'."\n";
 5364:         my %texoptions = (
 5365:                             MathJax  => 'MathJax',
 5366:                             mimetex  => &mt('Convert to Images'),
 5367:                             tth      => &mt('TeX to HTML'),
 5368:                          );
 5369:         foreach my $renderer ('MathJax','mimetex','tth') {
 5370:             my $selected = '';
 5371:             if ($renderer eq $deftex) {
 5372:                 $selected = ' selected="selected"';
 5373:             }
 5374:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 5375:         }
 5376:         $mathdisp .= '</select></td></tr>'."\n";
 5377:         $itemcount ++;
 5378:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 5379:                                                      \%choices,$itemcount);
 5380:         $datatable = $mathdisp.$datatable;
 5381:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5382:         $datatable .=
 5383:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5384:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 5385:             '</span></td><td class="LC_left_item">';
 5386:         my $currcanclone = 'none';
 5387:         my $onclick;
 5388:         my @cloneoptions = ('none','domain');
 5389:         my %clonetitles = (
 5390:                              none     => 'No additional course requesters',
 5391:                              domain   => "Any course requester in course's domain",
 5392:                              instcode => 'Course requests for official courses ...',
 5393:                           );
 5394:         my (%codedefaults,@code_order,@posscodes);
 5395:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 5396:                                                     \@code_order) eq 'ok') {
 5397:             if (@code_order > 0) {
 5398:                 push(@cloneoptions,'instcode');
 5399:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 5400:             }
 5401:         }
 5402:         if (ref($settings) eq 'HASH') {
 5403:             if ($settings->{'canclone'}) {
 5404:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 5405:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 5406:                         if (@code_order > 0) {
 5407:                             $currcanclone = 'instcode';
 5408:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 5409:                         }
 5410:                     }
 5411:                 } elsif ($settings->{'canclone'} eq 'domain') {
 5412:                     $currcanclone = $settings->{'canclone'};
 5413:                 }
 5414:             }
 5415:         }
 5416:         foreach my $option (@cloneoptions) {
 5417:             my ($checked,$additional);
 5418:             if ($currcanclone eq $option) {
 5419:                 $checked = ' checked="checked"';
 5420:             }
 5421:             if ($option eq 'instcode') {
 5422:                 if (@code_order) {
 5423:                     my $show = 'none';
 5424:                     if ($checked) {
 5425:                         $show = 'block';
 5426:                     }
 5427:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 5428:                                   &mt('Institutional codes for new and cloned course have identical:').
 5429:                                   '<br />';
 5430:                     foreach my $item (@code_order) {
 5431:                         my $codechk;
 5432:                         if ($checked) {
 5433:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 5434:                                 $codechk = ' checked="checked"';
 5435:                             }
 5436:                         }
 5437:                         $additional .= '<label>'.
 5438:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 5439:                                        $item.'</label>';
 5440:                     }
 5441:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 5442:                 }
 5443:             }
 5444:             $datatable .=
 5445:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 5446:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 5447:                 '</label>&nbsp;'.$additional.'</span><br />';
 5448:         }
 5449:         $datatable .= '</td>'.
 5450:                       '</tr>';
 5451:         $itemcount ++;
 5452:     } else {
 5453:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5454:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 5455:         my $currusecredits = 0;
 5456:         my $postsubmitclient = 1;
 5457:         my @types = ('official','unofficial','community','textbook','placement');
 5458:         if (ref($settings) eq 'HASH') {
 5459:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 5460:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 5461:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 5462:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 5463:                 }
 5464:             }
 5465:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 5466:                 foreach my $type (@types) {
 5467:                     next if ($type eq 'community');
 5468:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 5469:                     if ($defcredits{$type} ne '') {
 5470:                         $currusecredits = 1;
 5471:                     }
 5472:                 }
 5473:             }
 5474:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 5475:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 5476:                     $postsubmitclient = 0;
 5477:                     foreach my $type (@types) {
 5478:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5479:                     }
 5480:                 } else {
 5481:                     foreach my $type (@types) {
 5482:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 5483:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 5484:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 5485:                             } else {
 5486:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5487:                             }
 5488:                         } else {
 5489:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5490:                         }
 5491:                     }
 5492:                 }
 5493:             } else {
 5494:                 foreach my $type (@types) {
 5495:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5496:                 }
 5497:             }
 5498:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 5499:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 5500:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 5501:                 }
 5502:             } else {
 5503:                 foreach my $type (@types) {
 5504:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 5505:                 }
 5506:             }
 5507:         } else {
 5508:             foreach my $type (@types) {
 5509:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 5510:             }
 5511:         }
 5512:         if (!$currdefresponder) {
 5513:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 5514:         } elsif ($currdefresponder < 1) {
 5515:             $currdefresponder = 1;
 5516:         }
 5517:         foreach my $type (@types) {
 5518:             if ($curruploadquota{$type} eq '') {
 5519:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 5520:             }
 5521:         }
 5522:         $datatable .=
 5523:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5524:                 $choices{'anonsurvey_threshold'}.
 5525:                 '</span></td>'.
 5526:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 5527:                 '<input type="text" name="anonsurvey_threshold"'.
 5528:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 5529:                 '</td></tr>'."\n";
 5530:         $itemcount ++;
 5531:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5532:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5533:                       $choices{'uploadquota'}.
 5534:                       '</span></td>'.
 5535:                       '<td style="text-align: right" class="LC_right_item">'.
 5536:                       '<table><tr>';
 5537:         foreach my $type (@types) {
 5538:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5539:                            '<input type="text" name="uploadquota_'.$type.'"'.
 5540:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 5541:         }
 5542:         $datatable .= '</tr></table></td></tr>'."\n";
 5543:         $itemcount ++;
 5544:         my $onclick = "toggleDisplay(this.form,'credits');";
 5545:         my $display = 'none';
 5546:         if ($currusecredits) {
 5547:             $display = 'block';
 5548:         }
 5549:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 5550:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 5551:         foreach my $type (@types) {
 5552:             next if ($type eq 'community');
 5553:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5554:                            '<input type="text" name="'.$type.'_credits"'.
 5555:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 5556:         }
 5557:         $additional .= '</tr></table></div>'."\n";
 5558:         %defaultchecked = ('coursecredits' => 'off');
 5559:         @toggles = ('coursecredits');
 5560:         my $current = {
 5561:                         'coursecredits' => $currusecredits,
 5562:                       };
 5563:         (my $table,$itemcount) =
 5564:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5565:                                \%choices,$itemcount,$onclick,$additional,'left');
 5566:         $datatable .= $table;
 5567:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 5568:         my $display = 'none';
 5569:         if ($postsubmitclient) {
 5570:             $display = 'block';
 5571:         }
 5572:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 5573:                       &mt('Number of seconds submit is disabled').'<br />'.
 5574:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 5575:                       '<table><tr>';
 5576:         foreach my $type (@types) {
 5577:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5578:                            '<input type="text" name="'.$type.'_timeout" value="'.
 5579:                            $deftimeout{$type}.'" size="5" /></td>';
 5580:         }
 5581:         $additional .= '</tr></table></div>'."\n";
 5582:         %defaultchecked = ('postsubmit' => 'on');
 5583:         @toggles = ('postsubmit');
 5584:         $current = {
 5585:                        'postsubmit' => $postsubmitclient,
 5586:                    };
 5587:         ($table,$itemcount) =
 5588:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 5589:                                \%choices,$itemcount,$onclick,$additional,'left');
 5590:         $datatable .= $table;
 5591:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5592:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5593:                       $choices{'mysqltables'}.
 5594:                       '</span></td>'.
 5595:                       '<td style="text-align: right" class="LC_right_item">'.
 5596:                       '<table><tr>';
 5597:         foreach my $type (@types) {
 5598:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 5599:                            '<input type="text" name="mysqltables_'.$type.'"'.
 5600:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 5601:         }
 5602:         $datatable .= '</tr></table></td></tr>'."\n";
 5603:         $itemcount ++;
 5604: 
 5605:     }
 5606:     $$rowtotal += $itemcount;
 5607:     return $datatable;
 5608: }
 5609: 
 5610: sub print_selfenrollment {
 5611:     my ($position,$dom,$settings,$rowtotal) = @_;
 5612:     my ($css_class,$datatable);
 5613:     my $itemcount = 1;
 5614:     my @types = ('official','unofficial','community','textbook','placement');
 5615:     if (($position eq 'top') || ($position eq 'middle')) {
 5616:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 5617:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 5618:         my @rows;
 5619:         my $key;
 5620:         if ($position eq 'top') {
 5621:             $key = 'admin'; 
 5622:             if (ref($rowsref) eq 'ARRAY') {
 5623:                 @rows = @{$rowsref};
 5624:             }
 5625:         } elsif ($position eq 'middle') {
 5626:             $key = 'default';
 5627:             @rows = ('types','registered','approval','limit');
 5628:         }
 5629:         foreach my $row (@rows) {
 5630:             if (defined($titlesref->{$row})) {
 5631:                 $itemcount ++;
 5632:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5633:                 $datatable .= '<tr'.$css_class.'>'.
 5634:                               '<td>'.$titlesref->{$row}.'</td>'.
 5635:                               '<td class="LC_left_item">'.
 5636:                               '<table><tr>';
 5637:                 my (%current,%currentcap);
 5638:                 if (ref($settings) eq 'HASH') {
 5639:                     if (ref($settings->{$key}) eq 'HASH') {
 5640:                         foreach my $type (@types) {
 5641:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5642:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 5643:                             }
 5644:                             if (($row eq 'limit') && ($key eq 'default')) {
 5645:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 5646:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 5647:                                 }
 5648:                             }
 5649:                         }
 5650:                     }
 5651:                 }
 5652:                 my %roles = (
 5653:                              '0' => &Apache::lonnet::plaintext('dc'),
 5654:                             ); 
 5655:             
 5656:                 foreach my $type (@types) {
 5657:                     unless (($row eq 'registered') && ($key eq 'default')) {
 5658:                         $datatable .= '<th>'.&mt($type).'</th>';
 5659:                     }
 5660:                 }
 5661:                 unless (($row eq 'registered') && ($key eq 'default')) {
 5662:                     $datatable .= '</tr><tr>';
 5663:                 }
 5664:                 foreach my $type (@types) {
 5665:                     if ($type eq 'community') {
 5666:                         $roles{'1'} = &mt('Community personnel');
 5667:                     } else {
 5668:                         $roles{'1'} = &mt('Course personnel');
 5669:                     }
 5670:                     $datatable .= '<td style="vertical-align: top">';
 5671:                     if ($position eq 'top') {
 5672:                         my %checked;
 5673:                         if ($current{$type} eq '0') {
 5674:                             $checked{'0'} = ' checked="checked"';
 5675:                         } else {
 5676:                             $checked{'1'} = ' checked="checked"';
 5677:                         }
 5678:                         foreach my $role ('1','0') {
 5679:                             $datatable .= '<span class="LC_nobreak"><label>'.
 5680:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 5681:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 5682:                                           $roles{$role}.'</label></span> ';
 5683:                         }
 5684:                     } else {
 5685:                         if ($row eq 'types') {
 5686:                             my %checked;
 5687:                             if ($current{$type} =~ /^(all|dom)$/) {
 5688:                                 $checked{$1} = ' checked="checked"';
 5689:                             } else {
 5690:                                 $checked{''} = ' checked="checked"';
 5691:                             }
 5692:                             foreach my $val ('','dom','all') {
 5693:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5694:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5695:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5696:                             }
 5697:                         } elsif ($row eq 'registered') {
 5698:                             my %checked;
 5699:                             if ($current{$type} eq '1') {
 5700:                                 $checked{'1'} = ' checked="checked"';
 5701:                             } else {
 5702:                                 $checked{'0'} = ' checked="checked"';
 5703:                             }
 5704:                             foreach my $val ('0','1') {
 5705:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5706:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5707:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5708:                             }
 5709:                         } elsif ($row eq 'approval') {
 5710:                             my %checked;
 5711:                             if ($current{$type} =~ /^([12])$/) {
 5712:                                 $checked{$1} = ' checked="checked"';
 5713:                             } else {
 5714:                                 $checked{'0'} = ' checked="checked"';
 5715:                             }
 5716:                             for my $val (0..2) {
 5717:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5718:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5719:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5720:                             }
 5721:                         } elsif ($row eq 'limit') {
 5722:                             my %checked;
 5723:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 5724:                                 $checked{$1} = ' checked="checked"';
 5725:                             } else {
 5726:                                 $checked{'none'} = ' checked="checked"';
 5727:                             }
 5728:                             my $cap;
 5729:                             if ($currentcap{$type} =~ /^\d+$/) {
 5730:                                 $cap = $currentcap{$type};
 5731:                             }
 5732:                             foreach my $val ('none','allstudents','selfenrolled') {
 5733:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 5734:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 5735:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 5736:                             }
 5737:                             $datatable .= '<br />'.
 5738:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 5739:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 5740:                                           '</span>'; 
 5741:                         }
 5742:                     }
 5743:                     $datatable .= '</td>';
 5744:                 }
 5745:                 $datatable .= '</tr>';
 5746:             }
 5747:             $datatable .= '</table></td></tr>';
 5748:         }
 5749:     } elsif ($position eq 'bottom') {
 5750:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 5751:     }
 5752:     $$rowtotal += $itemcount;
 5753:     return $datatable;
 5754: }
 5755: 
 5756: sub print_validation_rows {
 5757:     my ($caller,$dom,$settings,$rowtotal) = @_;
 5758:     my ($itemsref,$namesref,$fieldsref);
 5759:     if ($caller eq 'selfenroll') { 
 5760:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 5761:     } elsif ($caller eq 'requestcourses') {
 5762:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 5763:     }
 5764:     my %currvalidation;
 5765:     if (ref($settings) eq 'HASH') {
 5766:         if (ref($settings->{'validation'}) eq 'HASH') {
 5767:             %currvalidation = %{$settings->{'validation'}};
 5768:         }
 5769:     }
 5770:     my $datatable;
 5771:     my $itemcount = 0;
 5772:     foreach my $item (@{$itemsref}) {
 5773:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5774:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 5775:                       $namesref->{$item}.
 5776:                       '</span></td>'.
 5777:                       '<td class="LC_left_item">';
 5778:         if (($item eq 'url') || ($item eq 'button')) {
 5779:             $datatable .= '<span class="LC_nobreak">'.
 5780:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 5781:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 5782:         } elsif ($item eq 'fields') {
 5783:             my @currfields;
 5784:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 5785:                 @currfields = @{$currvalidation{$item}};
 5786:             }
 5787:             foreach my $field (@{$fieldsref}) {
 5788:                 my $check = '';
 5789:                 if (grep(/^\Q$field\E$/,@currfields)) {
 5790:                     $check = ' checked="checked"';
 5791:                 }
 5792:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5793:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 5794:                               ' value="'.$field.'"'.$check.' />'.$field.
 5795:                               '</label></span> ';
 5796:             }
 5797:         } elsif ($item eq 'markup') {
 5798:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 5799:                            $currvalidation{$item}.
 5800:                               '</textarea>';
 5801:         }
 5802:         $datatable .= '</td></tr>'."\n";
 5803:         if (ref($rowtotal)) {
 5804:             $itemcount ++;
 5805:         }
 5806:     }
 5807:     if ($caller eq 'requestcourses') {
 5808:         my %currhash;
 5809:         if (ref($settings) eq 'HASH') {
 5810:             if (ref($settings->{'validation'}) eq 'HASH') {
 5811:                 if ($settings->{'validation'}{'dc'} ne '') {
 5812:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 5813:                 }
 5814:             }
 5815:         }
 5816:         my $numinrow = 2;
 5817:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 5818:                                                        'validationdc',%currhash);
 5819:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5820:         $datatable .= '<tr'.$css_class.'><td>';
 5821:         if ($numdc > 1) {
 5822:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 5823:         } else {
 5824:             $datatable .=  &mt('Course creation processed as: ');
 5825:         }
 5826:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 5827:         $itemcount ++;
 5828:     }
 5829:     if (ref($rowtotal)) {
 5830:         $$rowtotal += $itemcount;
 5831:     }
 5832:     return $datatable;
 5833: }
 5834: 
 5835: sub print_privacy {
 5836:     my ($position,$dom,$settings,$rowtotal) = @_;
 5837:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 5838:     my $itemcount = 0;
 5839:     unless ($position eq 'top') {
 5840:         @items = ('domain','author','course','community');
 5841:         %names = &Apache::lonlocal::texthash (
 5842:                      domain => 'Assigned domain role(s)',
 5843:                      author => 'Assigned co-author role(s)',
 5844:                      course => 'Assigned course role(s)',
 5845:                      community => 'Assigned community role',
 5846:                  );
 5847:         $numinrow = 4;
 5848:         ($othertitle,$usertypes,$types) =
 5849:             &Apache::loncommon::sorted_inst_types($dom);
 5850:     }
 5851:     if (($position eq 'top') || ($position eq 'middle')) {
 5852:         my (%by_ip,%by_location,@intdoms,@instdoms);
 5853:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 5854:         if ($position eq 'top') {
 5855:             my %curr;
 5856:             my @options = ('none','user','domain','auto');
 5857:             my %titles = &Apache::lonlocal::texthash (
 5858:                 none   => 'Not allowed',
 5859:                 user   => 'User authorizes',
 5860:                 domain => 'DC authorizes',
 5861:                 auto   => 'Unrestricted',
 5862:                 instdom => 'Other domain shares institution/provider',
 5863:                 extdom => 'Other domain has different institution/provider',
 5864:             );
 5865:             my %names = &Apache::lonlocal::texthash (
 5866:                 domain => 'Domain role',
 5867:                 author => 'Co-author role',
 5868:                 course => 'Course role',
 5869:                 community => 'Community role',
 5870:             );
 5871:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5872:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5873:             foreach my $domtype ('instdom','extdom') {
 5874:                 my (%checked,$skip);
 5875:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5876:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 5877:                               '<td class="LC_left_item">';
 5878:                 if ($domtype eq 'instdom') {
 5879:                     unless (@instdoms > 1) {
 5880:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 5881:                         $skip = 1;
 5882:                     }
 5883:                 } elsif ($domtype eq 'extdom') {
 5884:                     if (keys(%by_location) == 0) {
 5885:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 5886:                         $skip = 1;
 5887:                     }
 5888:                 }
 5889:                 unless ($skip) {
 5890:                     foreach my $roletype ('domain','author','course','community') {
 5891:                         $checked{'auto'} = ' checked="checked"';
 5892:                         if (ref($settings) eq 'HASH') {
 5893:                             if (ref($settings->{approval}) eq 'HASH') {
 5894:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 5895:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 5896:                                         $checked{$1} = ' checked="checked"';
 5897:                                         $checked{'auto'} = '';
 5898:                                     }
 5899:                                 }
 5900:                             }
 5901:                         }
 5902:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 5903:                         foreach my $option (@options) {
 5904:                             $datatable .= '<span class="LC_nobreak"><label>'.
 5905:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 5906:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 5907:                                           '</label></span>&nbsp; ';
 5908:                         }
 5909:                         $datatable .= '</fieldset>';
 5910:                     }
 5911:                 }
 5912:                 $datatable .= '</td></tr>';
 5913:                 $itemcount ++;
 5914:             }
 5915:         } elsif ($position eq 'middle') {
 5916:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 5917:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5918:                     foreach my $item (@{$types}) {
 5919:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 5920:                                                                $numinrow,$itemcount,'','','','','',
 5921:                                                                '',$usertypes->{$item});
 5922:                         $itemcount ++;
 5923:                     }
 5924:                 }
 5925:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 5926:                                                        $numinrow,$itemcount,'','','','','',
 5927:                                                        '',$othertitle);
 5928:                 $itemcount ++;
 5929:             } else {
 5930:                 my (@insttypes,%insttitles);
 5931:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 5932:                     @insttypes = @{$types};
 5933:                     %insttitles = %{$usertypes};
 5934:                 }
 5935:                 foreach my $item (@insttypes,'default') {
 5936:                     my $title;
 5937:                     if ($item eq 'default') {
 5938:                         $title = $othertitle;
 5939:                     } else {
 5940:                         $title = $insttitles{$item};
 5941:                     }
 5942:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5943:                     $datatable .= '<tr'.$css_class.'>'.
 5944:                                   '<td class="LC_left_item">'.$title.'</td>'.
 5945:                                   '<td class="LC_left_item">'.
 5946:                                   &mt('Nothing to set here, as there are no other domains').
 5947:                                   '</td></tr>';
 5948:                     $itemcount ++;
 5949:                 }
 5950:             }
 5951:         }
 5952:     } else {
 5953:         my $prefix;
 5954:         if ($position eq 'lower') {
 5955:             $prefix = 'priv';
 5956:         } else {
 5957:             $prefix = 'unpriv';
 5958:         }
 5959:         foreach my $item (@items) {
 5960:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 5961:                                                    $numinrow,$itemcount,'','','','','',
 5962:                                                    '',$names{$item});
 5963:             $itemcount ++;
 5964:         }
 5965:     }
 5966:     if (ref($rowtotal)) {
 5967:         $$rowtotal += $itemcount;
 5968:     }
 5969:     return $datatable;
 5970: }
 5971: 
 5972: sub print_passwords {
 5973:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 5974:     my ($datatable,$css_class);
 5975:     my $itemcount = 0;
 5976:     my %titles = &Apache::lonlocal::texthash (
 5977:         captcha        => '"Forgot Password" CAPTCHA validation',
 5978:         link           => 'Reset link expiration (hours)',
 5979:         case           => 'Case-sensitive usernames/e-mail',
 5980:         prelink        => 'Information required (form 1)',
 5981:         postlink       => 'Information required (form 2)',
 5982:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 5983:         customtext     => 'Domain specific text (HTML)',
 5984:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 5985:         intauth_check  => 'Check bcrypt cost if authenticated',
 5986:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 5987:         permanent      => 'Permanent e-mail address',
 5988:         critical       => 'Critical notification address',
 5989:         notify         => 'Notification address',
 5990:         min            => 'Minimum password length',
 5991:         max            => 'Maximum password length',
 5992:         chars          => 'Required characters',
 5993:         expire         => 'Password expiration (days)',
 5994:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 5995:     );
 5996:     if ($position eq 'top') {
 5997:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5998:         my $shownlinklife = 2;
 5999:         my $prelink = 'both';
 6000:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 6001:         if (ref($settings) eq 'HASH') {
 6002:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 6003:                 $shownlinklife = $settings->{resetlink};
 6004:             }
 6005:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 6006:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 6007:             }
 6008:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 6009:                 $prelink = $settings->{resetprelink};
 6010:             }
 6011:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 6012:                 %postlink = %{$settings->{resetpostlink}};
 6013:             }
 6014:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 6015:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 6016:             }
 6017:             if ($settings->{resetremove}) {
 6018:                 $nostdtext = 1;
 6019:             }
 6020:             if ($settings->{resetcustom}) {
 6021:                 $customurl = $settings->{resetcustom};
 6022:             }
 6023:         } else {
 6024:             if (ref($types) eq 'ARRAY') {
 6025:                 foreach my $item (@{$types}) {
 6026:                     $casesens{$item} = 1;
 6027:                     $postlink{$item} = ['username','email'];
 6028:                 }
 6029:             }
 6030:             $casesens{'default'} = 1;
 6031:             $postlink{'default'} = ['username','email'];
 6032:             $prelink = 'both';
 6033:             %emailsrc = (
 6034:                           permanent => 1,
 6035:                           critical  => 1,
 6036:                           notify    => 1,
 6037:             );
 6038:         }
 6039:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 6040:         $itemcount ++;
 6041:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6042:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 6043:                       '<td class="LC_left_item">'.
 6044:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 6045:                       'name="passwords_link" size="3" /></td></tr>';
 6046:         $itemcount ++;
 6047:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6048:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 6049:                       '<td class="LC_left_item">';
 6050:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6051:             foreach my $item (@{$types}) {
 6052:                 my $checkedcase;
 6053:                 if ($casesens{$item}) {
 6054:                     $checkedcase = ' checked="checked"';
 6055:                 }
 6056:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6057:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 6058:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 6059:                               '<span>&nbsp;&nbsp; ';
 6060:             }
 6061:         }
 6062:         my $checkedcase;
 6063:         if ($casesens{'default'}) {
 6064:             $checkedcase = ' checked="checked"';
 6065:         }
 6066:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 6067:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 6068:                       $othertitle.'</label></span></td>';
 6069:         $itemcount ++;
 6070:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6071:         my %checkedpre = (
 6072:                              both => ' checked="checked"',
 6073:                              either => '',
 6074:                          );
 6075:         if ($prelink eq 'either') {
 6076:             $checkedpre{either} = ' checked="checked"';
 6077:             $checkedpre{both} = '';
 6078:         }
 6079:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 6080:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6081:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 6082:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 6083:                       '<span class="LC_nobreak"><label>'.
 6084:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 6085:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 6086:         $itemcount ++;
 6087:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6088:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 6089:                       '<td class="LC_left_item">';
 6090:         my %postlinked;
 6091:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6092:             foreach my $item (@{$types}) {
 6093:                 undef(%postlinked);
 6094:                 $datatable .= '<fieldset style="display: inline-block;">'.
 6095:                               '<legend>'.$usertypes->{$item}.'</legend>';
 6096:                 if (ref($postlink{$item}) eq 'ARRAY') {
 6097:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 6098:                 }
 6099:                 foreach my $field ('email','username') {
 6100:                     my $checked;
 6101:                     if ($postlinked{$field}) {
 6102:                         $checked = ' checked="checked"';
 6103:                     }
 6104:                     $datatable .= '<span class="LC_nobreak"><label>'.
 6105:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 6106:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 6107:                                   '<span>&nbsp;&nbsp; ';
 6108:                 }
 6109:                 $datatable .= '</fieldset>';
 6110:             }
 6111:         }
 6112:         if (ref($postlink{'default'}) eq 'ARRAY') {
 6113:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 6114:         }
 6115:         $datatable .= '<fieldset style="display: inline-block;">'.
 6116:                       '<legend>'.$othertitle.'</legend>';
 6117:         foreach my $field ('email','username') {
 6118:             my $checked;
 6119:             if ($postlinked{$field}) {
 6120:                 $checked = ' checked="checked"';
 6121:             }
 6122:             $datatable .= '<span class="LC_nobreak"><label>'.
 6123:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 6124:                           $field.'"'.$checked.' />'.$field.'</label>'.
 6125:                           '<span>&nbsp;&nbsp; ';
 6126:         }
 6127:         $datatable .= '</fieldset></td></tr>';
 6128:         $itemcount ++;
 6129:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6130:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 6131:                       '<td class="LC_left_item">';
 6132:         foreach my $type ('permanent','critical','notify') {
 6133:             my $checkedemail;
 6134:             if ($emailsrc{$type}) {
 6135:                 $checkedemail = ' checked="checked"';
 6136:             }
 6137:             $datatable .= '<span class="LC_nobreak"><label>'.
 6138:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 6139:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 6140:                           '<span>&nbsp;&nbsp; ';
 6141:         }
 6142:         $datatable .= '</td></tr>';
 6143:         $itemcount ++;
 6144:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6145:         my $switchserver = &check_switchserver($dom,$confname);
 6146:         my ($showstd,$noshowstd);
 6147:         if ($nostdtext) {
 6148:             $noshowstd = ' checked="checked"';
 6149:         } else {
 6150:             $showstd = ' checked="checked"';
 6151:         }
 6152:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 6153:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6154:                       &mt('Retain standard text:').
 6155:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 6156:                       &mt('Yes').'</label>'.'&nbsp;'.
 6157:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 6158:                       &mt('No').'</label></span><br />'.
 6159:                       '<span class="LC_fontsize_small">'.
 6160:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 6161:                       &mt('Include custom text:');
 6162:         if ($customurl) {
 6163:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('Custom text file'),600,500,
 6164:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6165:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 6166:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 6167:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 6168:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 6169:         }
 6170:         if ($switchserver) {
 6171:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 6172:         } else {
 6173:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6174:                          '<input type="file" name="passwords_customfile" /></span>';
 6175:         }
 6176:         $datatable .= '</td></tr>';
 6177:     } elsif ($position eq 'middle') {
 6178:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 6179:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6180:         my %defaults;
 6181:         if (ref($domconf{'defaults'}) eq 'HASH') {
 6182:             %defaults = %{$domconf{'defaults'}};
 6183:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6184:                 $defaults{'intauth_cost'} = 10;
 6185:             }
 6186:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6187:                 $defaults{'intauth_check'} = 0;
 6188:             }
 6189:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6190:                 $defaults{'intauth_switch'} = 0;
 6191:             }
 6192:         } else {
 6193:             %defaults = (
 6194:                           'intauth_cost'   => 10,
 6195:                           'intauth_check'  => 0,
 6196:                           'intauth_switch' => 0,
 6197:                         );
 6198:         }
 6199:         foreach my $item (@items) {
 6200:             if ($itemcount%2) {
 6201:                 $css_class = '';
 6202:             } else {
 6203:                 $css_class = ' class="LC_odd_row" ';
 6204:             }
 6205:             $datatable .= '<tr'.$css_class.'>'.
 6206:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 6207:                           '</span></td><td class="LC_left_item" colspan="3">';
 6208:             if ($item eq 'intauth_switch') {
 6209:                 my @options = (0,1,2);
 6210:                 my %optiondesc = &Apache::lonlocal::texthash (
 6211:                                    0 => 'No',
 6212:                                    1 => 'Yes',
 6213:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6214:                                  );
 6215:                 $datatable .= '<table width="100%">';
 6216:                 foreach my $option (@options) {
 6217:                     my $checked = ' ';
 6218:                     if ($defaults{$item} eq $option) {
 6219:                         $checked = ' checked="checked"';
 6220:                     }
 6221:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6222:                                   '<label><input type="radio" name="'.$item.
 6223:                                   '" value="'.$option.'"'.$checked.' />'.
 6224:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6225:                 }
 6226:                 $datatable .= '</table>';
 6227:             } elsif ($item eq 'intauth_check') {
 6228:                 my @options = (0,1,2);
 6229:                 my %optiondesc = &Apache::lonlocal::texthash (
 6230:                                    0 => 'No',
 6231:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6232:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6233:                                  );
 6234:                 $datatable .= '<table width="100%">';
 6235:                 foreach my $option (@options) {
 6236:                     my $checked = ' ';
 6237:                     my $onclick;
 6238:                     if ($defaults{$item} eq $option) {
 6239:                         $checked = ' checked="checked"';
 6240:                     }
 6241:                     if ($option == 2) {
 6242:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6243:                     }
 6244:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6245:                                   '<label><input type="radio" name="'.$item.
 6246:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6247:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6248:                 }
 6249:                 $datatable .= '</table>';
 6250:             } else {
 6251:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6252:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 6253:             }
 6254:             $datatable .= '</td></tr>';
 6255:             $itemcount ++;
 6256:         }
 6257:     } elsif ($position eq 'lower') {
 6258:         my ($min,$max,%chars,$expire,$numsaved);
 6259:         $min = $Apache::lonnet::passwdmin;
 6260:         if (ref($settings) eq 'HASH') {
 6261:             if ($settings->{min}) {
 6262:                 $min = $settings->{min};
 6263:             }
 6264:             if ($settings->{max}) {
 6265:                 $max = $settings->{max};
 6266:             }
 6267:             if (ref($settings->{chars}) eq 'ARRAY') {
 6268:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 6269:             }
 6270:             if ($settings->{expire}) {
 6271:                 $expire = $settings->{expire};
 6272:             }
 6273:             if ($settings->{numsaved}) {
 6274:                 $numsaved = $settings->{numsaved};
 6275:             }
 6276:         }
 6277:         my %rulenames = &Apache::lonlocal::texthash(
 6278:                                                      uc => 'At least one upper case letter',
 6279:                                                      lc => 'At least one lower case letter',
 6280:                                                      num => 'At least one number',
 6281:                                                      spec => 'At least one non-alphanumeric',
 6282:                                                    );
 6283:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6284:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 6285:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6286:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 6287:                       'onblur="javascript:warnIntPass(this);" />'.
 6288:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 6289:                       '</span></td></tr>';
 6290:         $itemcount ++;
 6291:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6292:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 6293:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6294:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 6295:                       'onblur="javascript:warnIntPass(this);" />'.
 6296:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 6297:                       '</span></td></tr>';
 6298:         $itemcount ++;
 6299:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6300:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 6301:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 6302:                       '</span></td>';
 6303:         my $numinrow = 2;
 6304:         my @possrules = ('uc','lc','num','spec');
 6305:         $datatable .= '<td class="LC_left_item"><table>';
 6306:         for (my $i=0; $i<@possrules; $i++) {
 6307:             my ($rem,$checked);
 6308:             if ($chars{$possrules[$i]}) {
 6309:                 $checked = ' checked="checked"';
 6310:             }
 6311:             $rem = $i%($numinrow);
 6312:             if ($rem == 0) {
 6313:                 if ($i > 0) {
 6314:                     $datatable .= '</tr>';
 6315:                 }
 6316:                 $datatable .= '<tr>';
 6317:             }
 6318:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 6319:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 6320:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 6321:         }
 6322:         my $rem = @possrules%($numinrow);
 6323:         my $colsleft = $numinrow - $rem;
 6324:         if ($colsleft > 1 ) {
 6325:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6326:                           '&nbsp;</td>';
 6327:         } elsif ($colsleft == 1) {
 6328:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6329:         }
 6330:         $datatable .='</table></td></tr>';
 6331:         $itemcount ++;
 6332:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6333:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 6334:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6335:                       '<input type="text" name="passwords_expire" value="'.$expire.'" size="4" '.
 6336:                       'onblur="javascript:warnIntPass(this);" />'.
 6337:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 6338:                       '</span></td></tr>';
 6339:         $itemcount ++;
 6340:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6341:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 6342:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6343:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 6344:                       'onblur="javascript:warnIntPass(this);" />'. 
 6345:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 6346:                       '</span></td></tr>';
 6347:     } else {
 6348:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6349:         my %ownerchg = (
 6350:                           by  => {},
 6351:                           for => {},
 6352:                        );
 6353:         my %ownertitles = &Apache::lonlocal::texthash (
 6354:                             by  => 'Course owner status(es) allowed',
 6355:                             for => 'Student status(es) allowed',
 6356:                           );
 6357:         if (ref($settings) eq 'HASH') {
 6358:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 6359:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 6360:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 6361:                 }
 6362:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 6363:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 6364:                 }
 6365:             }
 6366:         }
 6367:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6368:         $datatable .= '<tr '.$css_class.'>'.
 6369:                       '<td>'.
 6370:                       &mt('Requirements').'<ul>'.
 6371:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 6372:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 6373:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 6374:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 6375:                       '</ul>'.
 6376:                       '</td>'.
 6377:                       '<td class="LC_left_item">';
 6378:         foreach my $item ('by','for') {
 6379:             $datatable .= '<fieldset style="display: inline-block;">'.
 6380:                           '<legend>'.$ownertitles{$item}.'</legend>';
 6381:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6382:                 foreach my $type (@{$types}) {
 6383:                     my $checked;
 6384:                     if ($ownerchg{$item}{$type}) {
 6385:                         $checked = ' checked="checked"';
 6386:                     }
 6387:                     $datatable .= '<span class="LC_nobreak"><label>'.
 6388:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 6389:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 6390:                                   '<span>&nbsp;&nbsp; ';
 6391:                 }
 6392:             }
 6393:             my $checked;
 6394:             if ($ownerchg{$item}{'default'}) {
 6395:                 $checked = ' checked="checked"';
 6396:             }
 6397:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 6398:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 6399:                           $othertitle.'</label></span></fieldset>';
 6400:         }
 6401:         $datatable .= '</td></tr>';
 6402:     }
 6403:     return $datatable;
 6404: }
 6405: 
 6406: sub print_usersessions {
 6407:     my ($position,$dom,$settings,$rowtotal) = @_;
 6408:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 6409:     my (%by_ip,%by_location,@intdoms,@instdoms);
 6410:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6411: 
 6412:     my @alldoms = &Apache::lonnet::all_domains();
 6413:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 6414:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6415:     my %altids = &id_for_thisdom(%servers);
 6416:     if ($position eq 'top') {
 6417:         if (keys(%serverhomes) > 1) {
 6418:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 6419:             my $curroffloadnow;
 6420:             if (ref($settings) eq 'HASH') {
 6421:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 6422:                     $curroffloadnow = $settings->{'offloadnow'};
 6423:                 }
 6424:             }
 6425:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 6426:         } else {
 6427:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 6428:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 6429:                           '</td></tr>';
 6430:         }
 6431:     } else {
 6432:         my %titles = &usersession_titles();
 6433:         my ($prefix,@types);
 6434:         if ($position eq 'bottom') {
 6435:             $prefix = 'remote';
 6436:             @types = ('version','excludedomain','includedomain');
 6437:         } else {
 6438:             $prefix = 'hosted';
 6439:             @types = ('excludedomain','includedomain');
 6440:         }
 6441:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6442:     }
 6443:     $$rowtotal += $itemcount;
 6444:     return $datatable;
 6445: }
 6446: 
 6447: sub rules_by_location {
 6448:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 6449:     my ($datatable,$itemcount,$css_class);
 6450:     if (keys(%{$by_location}) == 0) {
 6451:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6452:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 6453:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 6454:                      '</td></tr>';
 6455:         $itemcount = 1;
 6456:     } else {
 6457:         $itemcount = 0;
 6458:         my $numinrow = 5;
 6459:         my (%current,%checkedon,%checkedoff);
 6460:         my @locations = sort(keys(%{$by_location}));
 6461:         foreach my $type (@{$types}) {
 6462:             $checkedon{$type} = '';
 6463:             $checkedoff{$type} = ' checked="checked"';
 6464:         }
 6465:         if (ref($settings) eq 'HASH') {
 6466:             if (ref($settings->{$prefix}) eq 'HASH') {
 6467:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 6468:                     $current{$key} = $settings->{$prefix}{$key};
 6469:                     if ($key eq 'version') {
 6470:                         if ($current{$key} ne '') {
 6471:                             $checkedon{$key} = ' checked="checked"';
 6472:                             $checkedoff{$key} = '';
 6473:                         }
 6474:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 6475:                         $checkedon{$key} = ' checked="checked"';
 6476:                         $checkedoff{$key} = '';
 6477:                     }
 6478:                 }
 6479:             }
 6480:         }
 6481:         foreach my $type (@{$types}) {
 6482:             next if ($type ne 'version' && !@locations);
 6483:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6484:             $datatable .= '<tr'.$css_class.'>
 6485:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 6486:                            <span class="LC_nobreak">&nbsp;
 6487:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 6488:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 6489:             if ($type eq 'version') {
 6490:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 6491:                 my $selector = '<select name="'.$prefix.'_version">';
 6492:                 foreach my $version (@lcversions) {
 6493:                     my $selected = '';
 6494:                     if ($current{'version'} eq $version) {
 6495:                         $selected = ' selected="selected"';
 6496:                     }
 6497:                     $selector .= ' <option value="'.$version.'"'.
 6498:                                  $selected.'>'.$version.'</option>';
 6499:                 }
 6500:                 $selector .= '</select> ';
 6501:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 6502:             } else {
 6503:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 6504:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 6505:                              ' />'.('&nbsp;'x2).
 6506:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 6507:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 6508:                              "\n".
 6509:                              '</div><div><table>';
 6510:                 my $rem;
 6511:                 for (my $i=0; $i<@locations; $i++) {
 6512:                     my ($showloc,$value,$checkedtype);
 6513:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 6514:                         my $ip = $by_location->{$locations[$i]}->[0];
 6515:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6516:                             $value = join(':',@{$by_ip->{$ip}});
 6517:                             $showloc = join(', ',@{$by_ip->{$ip}});
 6518:                             if (ref($current{$type}) eq 'ARRAY') {
 6519:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 6520:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 6521:                                         $checkedtype = ' checked="checked"';
 6522:                                         last;
 6523:                                     }
 6524:                                 }
 6525:                             }
 6526:                         }
 6527:                     }
 6528:                     $rem = $i%($numinrow);
 6529:                     if ($rem == 0) {
 6530:                         if ($i > 0) {
 6531:                             $datatable .= '</tr>';
 6532:                         }
 6533:                         $datatable .= '<tr>';
 6534:                     }
 6535:                     $datatable .= '<td class="LC_left_item">'.
 6536:                                   '<span class="LC_nobreak"><label>'.
 6537:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 6538:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 6539:                                   '</label></span></td>';
 6540:                 }
 6541:                 $rem = @locations%($numinrow);
 6542:                 my $colsleft = $numinrow - $rem;
 6543:                 if ($colsleft > 1 ) {
 6544:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6545:                                   '&nbsp;</td>';
 6546:                 } elsif ($colsleft == 1) {
 6547:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6548:                 }
 6549:                 $datatable .= '</tr></table>';
 6550:             }
 6551:             $datatable .= '</td></tr>';
 6552:             $itemcount ++;
 6553:         }
 6554:     }
 6555:     return ($datatable,$itemcount);
 6556: }
 6557: 
 6558: sub print_ssl {
 6559:     my ($position,$dom,$settings,$rowtotal) = @_;
 6560:     my ($css_class,$datatable);
 6561:     my $itemcount = 1;
 6562:     if ($position eq 'top') {
 6563:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6564:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6565:         my $same_institution;
 6566:         if ($intdom ne '') {
 6567:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 6568:             if (ref($internet_names) eq 'ARRAY') {
 6569:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 6570:                     $same_institution = 1;
 6571:                 }
 6572:             }
 6573:         }
 6574:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6575:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 6576:         if ($same_institution) {
 6577:             my %domservers = &Apache::lonnet::get_servers($dom);
 6578:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 6579:         } else {
 6580:             $datatable .= &mt("You need to be logged into one of your own domain's servers to display information about the status of LON-CAPA SSL certificates.");
 6581:         }
 6582:         $datatable .= '</td></tr>';
 6583:         $itemcount ++;
 6584:     } else {
 6585:         my %titles = &ssl_titles();
 6586:         my (%by_ip,%by_location,@intdoms,@instdoms);
 6587:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6588:         my @alldoms = &Apache::lonnet::all_domains();
 6589:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 6590:         my @domservers = &Apache::lonnet::get_servers($dom);
 6591:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6592:         my %altids = &id_for_thisdom(%servers);
 6593:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 6594:             my $legacy;
 6595:             unless (ref($settings) eq 'HASH') {
 6596:                 my $name;
 6597:                 if ($position eq 'connto') {
 6598:                     $name = 'loncAllowInsecure';
 6599:                 } else {
 6600:                     $name = 'londAllowInsecure';
 6601:                 }
 6602:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 6603:                 my @ids=&Apache::lonnet::current_machine_ids();
 6604:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 6605:                     my %what = (
 6606:                                    $name => 1,
 6607:                                );
 6608:                     my ($result,$returnhash) =
 6609:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 6610:                     if ($result eq 'ok') {
 6611:                         if (ref($returnhash) eq 'HASH') {
 6612:                             $legacy = $returnhash->{$name};
 6613:                         }
 6614:                     }
 6615:                 } else {
 6616:                     $legacy = $Apache::lonnet::perlvar{$name};
 6617:                 }
 6618:             }
 6619:             foreach my $type ('dom','intdom','other') {
 6620:                 my %checked;
 6621:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6622:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 6623:                               '<td class="LC_right_item">';
 6624:                 my $skip; 
 6625:                 if ($type eq 'dom') {
 6626:                     unless (keys(%servers) > 1) {
 6627:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 6628:                         $skip = 1;
 6629:                     }
 6630:                 }
 6631:                 if ($type eq 'intdom') {
 6632:                     unless (@instdoms > 1) {
 6633:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 6634:                         $skip = 1;
 6635:                     } 
 6636:                 } elsif ($type eq 'other') {
 6637:                     if (keys(%by_location) == 0) {
 6638:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 6639:                         $skip = 1;
 6640:                     }
 6641:                 }
 6642:                 unless ($skip) {
 6643:                     $checked{'yes'} = ' checked="checked"'; 
 6644:                     if (ref($settings) eq 'HASH') {
 6645:                         if (ref($settings->{$position}) eq 'HASH') {
 6646:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 6647:                                 $checked{$1} = $checked{'yes'};
 6648:                                 delete($checked{'yes'}); 
 6649:                             }
 6650:                         }
 6651:                     } else {
 6652:                         if ($legacy == 0) {
 6653:                             $checked{'req'} = $checked{'yes'};
 6654:                             delete($checked{'yes'});    
 6655:                         }
 6656:                     }
 6657:                     foreach my $option ('no','yes','req') {
 6658:                         $datatable .= '<span class="LC_nobreak"><label>'.
 6659:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 6660:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 6661:                                       '</label></span>'.('&nbsp;'x2);
 6662:                     }
 6663:                 }
 6664:                 $datatable .= '</td></tr>';
 6665:                 $itemcount ++; 
 6666:             }
 6667:         } else {
 6668:             my $prefix = 'replication';
 6669:             my @types = ('certreq','nocertreq');
 6670:             if (keys(%by_location) == 0) {
 6671:                 $datatable .= '<tr'.$css_class.'><td>'.
 6672:                               &mt('Nothing to set here, as there are no other institutions').
 6673:                               '</td></tr>';
 6674:                 $itemcount ++;
 6675:             } else {
 6676:                 ($datatable,$itemcount) = 
 6677:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6678:             }
 6679:         }
 6680:     }
 6681:     $$rowtotal += $itemcount;
 6682:     return $datatable;
 6683: }
 6684: 
 6685: sub ssl_titles {
 6686:     return &Apache::lonlocal::texthash (
 6687:                dom           => 'LON-CAPA servers/VMs from same domain',
 6688:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 6689:                other         => 'External LON-CAPA servers/VMs',
 6690:                connto        => 'Connections to other servers',
 6691:                connfrom      => 'Connections from other servers',
 6692:                replication   => 'Replicating content to other institutions',
 6693:                certreq       => 'Client certificate required, but specific domains exempt',
 6694:                nocertreq     => 'No client certificate required, except for specific domains',
 6695:                no            => 'SSL not used',
 6696:                yes           => 'SSL Optional (used if available)',
 6697:                req           => 'SSL Required',
 6698:     );
 6699: }
 6700: 
 6701: sub print_trust {
 6702:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 6703:     my ($css_class,$datatable,%checked,%choices);
 6704:     my (%by_ip,%by_location,@intdoms,@instdoms);
 6705:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6706:     my $itemcount = 1;
 6707:     my %titles = &trust_titles();
 6708:     my @types = ('exc','inc');
 6709:     if ($prefix eq 'top') {
 6710:         $prefix = 'content';
 6711:     } elsif ($prefix eq 'bottom') {
 6712:         $prefix = 'msg';
 6713:     }
 6714:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 6715:     $$rowtotal += $itemcount;
 6716:     return $datatable;
 6717: }
 6718: 
 6719: sub trust_titles {
 6720:     return &Apache::lonlocal::texthash(
 6721:                content  => "Access to this domain's content by others",
 6722:                shared   => "Access to other domain's content by this domain",
 6723:                enroll   => "Enrollment in this domain's courses by others", 
 6724:                othcoau  => "Co-author roles in this domain for others",
 6725:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 6726:                domroles => "Domain roles in this domain assignable to others",
 6727:                catalog  => "Course Catalog for this domain displayed elsewhere",
 6728:                reqcrs   => "Requests for creation of courses in this domain by others",
 6729:                msg      => "Users in other domains can send messages to this domain",
 6730:                exc      => "Allow all, but exclude specific domains",
 6731:                inc      => "Deny all, but include specific domains",
 6732:            );
 6733: } 
 6734: 
 6735: sub build_location_hashes {
 6736:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 6737:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 6738:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 6739:     my %iphost = &Apache::lonnet::get_iphost();
 6740:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 6741:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 6742:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 6743:         foreach my $id (@{$iphost{$primary_ip}}) {
 6744:             my $intdom = &Apache::lonnet::internet_dom($id);
 6745:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 6746:                 push(@{$intdoms},$intdom);
 6747:             }
 6748:         }
 6749:     }
 6750:     foreach my $ip (keys(%iphost)) {
 6751:         if (ref($iphost{$ip}) eq 'ARRAY') {
 6752:             foreach my $id (@{$iphost{$ip}}) {
 6753:                 my $location = &Apache::lonnet::internet_dom($id);
 6754:                 if ($location) {
 6755:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 6756:                         my $dom = &Apache::lonnet::host_domain($id);
 6757:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 6758:                             push(@{$instdoms},$dom);
 6759:                         }
 6760:                         next;
 6761:                     }
 6762:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6763:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 6764:                             push(@{$by_ip->{$ip}},$location);
 6765:                         }
 6766:                     } else {
 6767:                         $by_ip->{$ip} = [$location];
 6768:                     }
 6769:                 }
 6770:             }
 6771:         }
 6772:     }
 6773:     foreach my $ip (sort(keys(%{$by_ip}))) {
 6774:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 6775:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 6776:             my $first = $by_ip->{$ip}->[0];
 6777:             if (ref($by_location->{$first}) eq 'ARRAY') {
 6778:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 6779:                     push(@{$by_location->{$first}},$ip);
 6780:                 }
 6781:             } else {
 6782:                 $by_location->{$first} = [$ip];
 6783:             }
 6784:         }
 6785:     }
 6786:     return;
 6787: }
 6788: 
 6789: sub current_offloads_to {
 6790:     my ($dom,$settings,$servers) = @_;
 6791:     my (%spareid,%otherdomconfigs);
 6792:     if (ref($servers) eq 'HASH') {
 6793:         foreach my $lonhost (sort(keys(%{$servers}))) {
 6794:             my $gotspares;
 6795:             if (ref($settings) eq 'HASH') {
 6796:                 if (ref($settings->{'spares'}) eq 'HASH') {
 6797:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 6798:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 6799:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 6800:                         $gotspares = 1;
 6801:                     }
 6802:                 }
 6803:             }
 6804:             unless ($gotspares) {
 6805:                 my $gotspares;
 6806:                 my $serverhomeID =
 6807:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 6808:                 my $serverhomedom =
 6809:                     &Apache::lonnet::host_domain($serverhomeID);
 6810:                 if ($serverhomedom ne $dom) {
 6811:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 6812:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 6813:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 6814:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 6815:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 6816:                                 $gotspares = 1;
 6817:                             }
 6818:                         }
 6819:                     } else {
 6820:                         $otherdomconfigs{$serverhomedom} =
 6821:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 6822:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 6823:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 6824:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 6825:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 6826:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 6827:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 6828:                                         $gotspares = 1;
 6829:                                     }
 6830:                                 }
 6831:                             }
 6832:                         }
 6833:                     }
 6834:                 }
 6835:             }
 6836:             unless ($gotspares) {
 6837:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 6838:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 6839:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 6840:                } else {
 6841:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 6842:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 6843:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 6844:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 6845:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 6846:                     } else {
 6847:                         my %what = (
 6848:                              spareid => 1,
 6849:                         );
 6850:                         my ($result,$returnhash) = 
 6851:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 6852:                         if ($result eq 'ok') { 
 6853:                             if (ref($returnhash) eq 'HASH') {
 6854:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 6855:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 6856:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 6857:                                 }
 6858:                             }
 6859:                         }
 6860:                     }
 6861:                 }
 6862:             }
 6863:         }
 6864:     }
 6865:     return %spareid;
 6866: }
 6867: 
 6868: sub spares_row {
 6869:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 6870:     my $css_class;
 6871:     my $numinrow = 4;
 6872:     my $itemcount = 1;
 6873:     my $datatable;
 6874:     my %typetitles = &sparestype_titles();
 6875:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 6876:         foreach my $server (sort(keys(%{$servers}))) {
 6877:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 6878:             my ($othercontrol,$serverdom);
 6879:             if ($serverhome ne $server) {
 6880:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 6881:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6882:             } else {
 6883:                 $serverdom = &Apache::lonnet::host_domain($server);
 6884:                 if ($serverdom ne $dom) {
 6885:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 6886:                 }
 6887:             }
 6888:             next unless (ref($spareid->{$server}) eq 'HASH');
 6889:             my $checkednow;
 6890:             if (ref($curroffloadnow) eq 'HASH') {
 6891:                 if ($curroffloadnow->{$server}) {
 6892:                     $checkednow = ' checked="checked"';
 6893:                 }
 6894:             }
 6895:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6896:             $datatable .= '<tr'.$css_class.'>
 6897:                            <td rowspan="2">
 6898:                             <span class="LC_nobreak">'.
 6899:                           &mt('[_1] when busy, offloads to:'
 6900:                               ,'<b>'.$server.'</b>').'</span><br />'.
 6901:                           '<span class="LC_nobreak">'."\n".
 6902:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 6903:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 6904:                           "\n";
 6905:             my (%current,%canselect);
 6906:             my @choices = 
 6907:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 6908:             foreach my $type ('primary','default') {
 6909:                 if (ref($spareid->{$server}) eq 'HASH') {
 6910:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 6911:                         my @spares = @{$spareid->{$server}{$type}};
 6912:                         if (@spares > 0) {
 6913:                             if ($othercontrol) {
 6914:                                 $current{$type} = join(', ',@spares);
 6915:                             } else {
 6916:                                 $current{$type} .= '<table>';
 6917:                                 my $numspares = scalar(@spares);
 6918:                                 for (my $i=0;  $i<@spares; $i++) {
 6919:                                     my $rem = $i%($numinrow);
 6920:                                     if ($rem == 0) {
 6921:                                         if ($i > 0) {
 6922:                                             $current{$type} .= '</tr>';
 6923:                                         }
 6924:                                         $current{$type} .= '<tr>';
 6925:                                     }
 6926:                                     $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;'.
 6927:                                                        $spareid->{$server}{$type}[$i].
 6928:                                                        '</label></td>'."\n";
 6929:                                 }
 6930:                                 my $rem = @spares%($numinrow);
 6931:                                 my $colsleft = $numinrow - $rem;
 6932:                                 if ($colsleft > 1 ) {
 6933:                                     $current{$type} .= '<td colspan="'.$colsleft.
 6934:                                                        '" class="LC_left_item">'.
 6935:                                                        '&nbsp;</td>';
 6936:                                 } elsif ($colsleft == 1) {
 6937:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 6938:                                 }
 6939:                                 $current{$type} .= '</tr></table>';
 6940:                             }
 6941:                         }
 6942:                     }
 6943:                     if ($current{$type} eq '') {
 6944:                         $current{$type} = &mt('None specified');
 6945:                     }
 6946:                     if ($othercontrol) {
 6947:                         if ($type eq 'primary') {
 6948:                             $canselect{$type} = $othercontrol;
 6949:                         }
 6950:                     } else {
 6951:                         $canselect{$type} = 
 6952:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 6953:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 6954:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 6955:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 6956:                         if (@choices > 0) {
 6957:                             foreach my $lonhost (@choices) {
 6958:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 6959:                             }
 6960:                         }
 6961:                         $canselect{$type} .= '</select>'."\n";
 6962:                     }
 6963:                 } else {
 6964:                     $current{$type} = &mt('Could not be determined');
 6965:                     if ($type eq 'primary') {
 6966:                         $canselect{$type} =  $othercontrol;
 6967:                     }
 6968:                 }
 6969:                 if ($type eq 'default') {
 6970:                     $datatable .= '<tr'.$css_class.'>';
 6971:                 }
 6972:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 6973:                               '<td>'.$current{$type}.'</td>'."\n".
 6974:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 6975:             }
 6976:             $itemcount ++;
 6977:         }
 6978:     }
 6979:     $$rowtotal += $itemcount;
 6980:     return $datatable;
 6981: }
 6982: 
 6983: sub possible_newspares {
 6984:     my ($server,$currspares,$serverhomes,$altids) = @_;
 6985:     my $serverhostname = &Apache::lonnet::hostname($server);
 6986:     my %excluded;
 6987:     if ($serverhostname ne '') {
 6988:         %excluded = (
 6989:                        $serverhostname => 1,
 6990:                     );
 6991:     }
 6992:     if (ref($currspares) eq 'HASH') {
 6993:         foreach my $type (keys(%{$currspares})) {
 6994:             if (ref($currspares->{$type}) eq 'ARRAY') {
 6995:                 if (@{$currspares->{$type}} > 0) {
 6996:                     foreach my $curr (@{$currspares->{$type}}) {
 6997:                         my $hostname = &Apache::lonnet::hostname($curr);
 6998:                         $excluded{$hostname} = 1;
 6999:                     }
 7000:                 }
 7001:             }
 7002:         }
 7003:     }
 7004:     my @choices;
 7005:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 7006:         if (keys(%{$serverhomes}) > 1) {
 7007:             foreach my $name (sort(keys(%{$serverhomes}))) {
 7008:                 unless ($excluded{$name}) {
 7009:                     if (exists($altids->{$serverhomes->{$name}})) {
 7010:                         push(@choices,$altids->{$serverhomes->{$name}});
 7011:                     } else {
 7012:                         push(@choices,$serverhomes->{$name});
 7013:                     }
 7014:                 }
 7015:             }
 7016:         }
 7017:     }
 7018:     return sort(@choices);
 7019: }
 7020: 
 7021: sub print_loadbalancing {
 7022:     my ($dom,$settings,$rowtotal) = @_;
 7023:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7024:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7025:     my $numinrow = 1;
 7026:     my $datatable;
 7027:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7028:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 7029:     if (ref($settings) eq 'HASH') {
 7030:         %existing = %{$settings};
 7031:     }
 7032:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 7033:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 7034:                                   \%currtargets,\%currrules,\%currcookies);
 7035:     } else {
 7036:         return;
 7037:     }
 7038:     my ($othertitle,$usertypes,$types) =
 7039:         &Apache::loncommon::sorted_inst_types($dom);
 7040:     my $rownum = 8;
 7041:     if (ref($types) eq 'ARRAY') {
 7042:         $rownum += scalar(@{$types});
 7043:     }
 7044:     my @css_class = ('LC_odd_row','LC_even_row');
 7045:     my $balnum = 0;
 7046:     my $islast;
 7047:     my (@toshow,$disabledtext);
 7048:     if (keys(%currbalancer) > 0) {
 7049:         @toshow = sort(keys(%currbalancer));
 7050:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 7051:             push(@toshow,'');
 7052:         }
 7053:     } else {
 7054:         @toshow = ('');
 7055:         $disabledtext = &mt('No existing load balancer');
 7056:     }
 7057:     foreach my $lonhost (@toshow) {
 7058:         if ($balnum == scalar(@toshow)-1) {
 7059:             $islast = 1;
 7060:         } else {
 7061:             $islast = 0;
 7062:         }
 7063:         my $cssidx = $balnum%2;
 7064:         my $targets_div_style = 'display: none';
 7065:         my $disabled_div_style = 'display: block';
 7066:         my $homedom_div_style = 'display: none';
 7067:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 7068:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 7069:                       '<p>';
 7070:         if ($lonhost eq '') {
 7071:             $datatable .= '<span class="LC_nobreak">';
 7072:             if (keys(%currbalancer) > 0) {
 7073:                 $datatable .= &mt('Add balancer:');
 7074:             } else {
 7075:                 $datatable .= &mt('Enable balancer:');
 7076:             }
 7077:             $datatable .= '&nbsp;'.
 7078:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 7079:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 7080:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 7081:                           '<option value="" selected="selected">'.&mt('None').
 7082:                           '</option>'."\n";
 7083:             foreach my $server (sort(keys(%servers))) {
 7084:                 next if ($currbalancer{$server});
 7085:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 7086:             }
 7087:             $datatable .=
 7088:                 '</select>'."\n".
 7089:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 7090:         } else {
 7091:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 7092:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 7093:                            &mt('Stop balancing').'</label>'.
 7094:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 7095:             $targets_div_style = 'display: block';
 7096:             $disabled_div_style = 'display: none';
 7097:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 7098:                 $homedom_div_style = 'display: block';
 7099:             }
 7100:         }
 7101:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 7102:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 7103:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 7104:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 7105:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 7106:         my @sparestypes = ('primary','default');
 7107:         my %typetitles = &sparestype_titles();
 7108:         my %hostherechecked = (
 7109:                                   no => ' checked="checked"',
 7110:                               );
 7111:         my %balcookiechecked = (
 7112:                                   no => ' checked="checked"', 
 7113:                                );
 7114:         foreach my $sparetype (@sparestypes) {
 7115:             my $targettable;
 7116:             for (my $i=0; $i<$numspares; $i++) {
 7117:                 my $checked;
 7118:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 7119:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 7120:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 7121:                             $checked = ' checked="checked"';
 7122:                         }
 7123:                     }
 7124:                 }
 7125:                 my ($chkboxval,$disabled);
 7126:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 7127:                     $chkboxval = $spares[$i];
 7128:                 }
 7129:                 if (exists($currbalancer{$spares[$i]})) {
 7130:                     $disabled = ' disabled="disabled"';
 7131:                 }
 7132:                 $targettable .=
 7133:                     '<td><span class="LC_nobreak"><label>'.
 7134:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 7135:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 7136:                     '</span></label></span></td>';
 7137:                 my $rem = $i%($numinrow);
 7138:                 if ($rem == 0) {
 7139:                     if (($i > 0) && ($i < $numspares-1)) {
 7140:                         $targettable .= '</tr>';
 7141:                     }
 7142:                     if ($i < $numspares-1) {
 7143:                         $targettable .= '<tr>';
 7144:                     }
 7145:                 }
 7146:             }
 7147:             if ($targettable ne '') {
 7148:                 my $rem = $numspares%($numinrow);
 7149:                 my $colsleft = $numinrow - $rem;
 7150:                 if ($colsleft > 1 ) {
 7151:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7152:                                     '&nbsp;</td>';
 7153:                 } elsif ($colsleft == 1) {
 7154:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 7155:                 }
 7156:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 7157:                                '<table><tr>'.$targettable.'</tr></table><br />';
 7158:             }
 7159:             $hostherechecked{$sparetype} = '';
 7160:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 7161:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 7162:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 7163:                         $hostherechecked{$sparetype} = ' checked="checked"';
 7164:                         $hostherechecked{'no'} = '';
 7165:                     }
 7166:                 }
 7167:             }
 7168:         }
 7169:         if ($currcookies{$lonhost}) {
 7170:             %balcookiechecked = (
 7171:                                     yes => ' checked="checked"',
 7172:                                 );
 7173:         }
 7174:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 7175:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 7176:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 7177:         foreach my $sparetype (@sparestypes) {
 7178:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 7179:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 7180:                           '</i></label><br />';
 7181:         }
 7182:         $datatable .= &mt('Use balancer cookie').'<br />'.
 7183:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 7184:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 7185:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 7186:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 7187:                       '</div></td></tr>'.
 7188:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 7189:                                            $othertitle,$usertypes,$types,\%servers,
 7190:                                            \%currbalancer,$lonhost,
 7191:                                            $targets_div_style,$homedom_div_style,
 7192:                                            $css_class[$cssidx],$balnum,$islast);
 7193:         $$rowtotal += $rownum;
 7194:         $balnum ++;
 7195:     }
 7196:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 7197:     return $datatable;
 7198: }
 7199: 
 7200: sub get_loadbalancers_config {
 7201:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 7202:     return unless ((ref($servers) eq 'HASH') &&
 7203:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 7204:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 7205:                    (ref($currcookies) eq 'HASH'));
 7206:     if (keys(%{$existing}) > 0) {
 7207:         my $oldlonhost;
 7208:         foreach my $key (sort(keys(%{$existing}))) {
 7209:             if ($key eq 'lonhost') {
 7210:                 $oldlonhost = $existing->{'lonhost'};
 7211:                 $currbalancer->{$oldlonhost} = 1;
 7212:             } elsif ($key eq 'targets') {
 7213:                 if ($oldlonhost) {
 7214:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 7215:                 }
 7216:             } elsif ($key eq 'rules') {
 7217:                 if ($oldlonhost) {
 7218:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 7219:                 }
 7220:             } elsif (ref($existing->{$key}) eq 'HASH') {
 7221:                 $currbalancer->{$key} = 1;
 7222:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 7223:                 $currrules->{$key} = $existing->{$key}{'rules'};
 7224:                 if ($existing->{$key}{'cookie'}) {
 7225:                     $currcookies->{$key} = 1;
 7226:                 }
 7227:             }
 7228:         }
 7229:     } else {
 7230:         my ($balancerref,$targetsref) =
 7231:                 &Apache::lonnet::get_lonbalancer_config($servers);
 7232:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 7233:             foreach my $server (sort(keys(%{$balancerref}))) {
 7234:                 $currbalancer->{$server} = 1;
 7235:                 $currtargets->{$server} = $targetsref->{$server};
 7236:             }
 7237:         }
 7238:     }
 7239:     return;
 7240: }
 7241: 
 7242: sub loadbalancing_rules {
 7243:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 7244:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 7245:         $css_class,$balnum,$islast) = @_;
 7246:     my $output;
 7247:     my $num = 0;
 7248:     my ($alltypes,$othertypes,$titles) =
 7249:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 7250:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 7251:         foreach my $type (@{$alltypes}) {
 7252:             $num ++;
 7253:             my $current;
 7254:             if (ref($currrules) eq 'HASH') {
 7255:                 $current = $currrules->{$type};
 7256:             }
 7257:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 7258:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 7259:                     $current = '';
 7260:                 }
 7261:             }
 7262:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 7263:                                              $servers,$currbalancer,$lonhost,$dom,
 7264:                                              $targets_div_style,$homedom_div_style,
 7265:                                              $css_class,$balnum,$num,$islast);
 7266:         }
 7267:     }
 7268:     return $output;
 7269: }
 7270: 
 7271: sub loadbalancing_titles {
 7272:     my ($dom,$intdom,$usertypes,$types) = @_;
 7273:     my %othertypes = (
 7274:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 7275:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 7276:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 7277:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 7278:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 7279:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 7280:                      );
 7281:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 7282:     my @available;
 7283:     if (ref($types) eq 'ARRAY') {
 7284:         @available = @{$types};
 7285:     }
 7286:     unless (grep(/^default$/,@available)) {
 7287:         push(@available,'default');
 7288:     }
 7289:     unshift(@alltypes,@available);
 7290:     my %titles;
 7291:     foreach my $type (@alltypes) {
 7292:         if ($type =~ /^_LC_/) {
 7293:             $titles{$type} = $othertypes{$type};
 7294:         } elsif ($type eq 'default') {
 7295:             $titles{$type} = &mt('All users from [_1]',$dom);
 7296:             if (ref($types) eq 'ARRAY') {
 7297:                 if (@{$types} > 0) {
 7298:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 7299:                 }
 7300:             }
 7301:         } elsif (ref($usertypes) eq 'HASH') {
 7302:             $titles{$type} = $usertypes->{$type};
 7303:         }
 7304:     }
 7305:     return (\@alltypes,\%othertypes,\%titles);
 7306: }
 7307: 
 7308: sub loadbalance_rule_row {
 7309:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 7310:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 7311:     my @rulenames;
 7312:     my %ruletitles = &offloadtype_text();
 7313:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 7314:         @rulenames = ('balancer','offloadedto','specific');
 7315:     } else {
 7316:         @rulenames = ('default','homeserver');
 7317:         if ($type eq '_LC_external') {
 7318:             push(@rulenames,'externalbalancer');
 7319:         } else {
 7320:             push(@rulenames,'specific');
 7321:         }
 7322:         push(@rulenames,'none');
 7323:     }
 7324:     my $style = $targets_div_style;
 7325:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 7326:         $style = $homedom_div_style;
 7327:     }
 7328:     my $space;
 7329:     if ($islast && $num == 1) {
 7330:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 7331:     }
 7332:     my $output =
 7333:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 7334:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 7335:         '<td valaign="top">'.$space.
 7336:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 7337:     for (my $i=0; $i<@rulenames; $i++) {
 7338:         my $rule = $rulenames[$i];
 7339:         my ($checked,$extra);
 7340:         if ($rulenames[$i] eq 'default') {
 7341:             $rule = '';
 7342:         }
 7343:         if ($rulenames[$i] eq 'specific') {
 7344:             if (ref($servers) eq 'HASH') {
 7345:                 my $default;
 7346:                 if (($current ne '') && (exists($servers->{$current}))) {
 7347:                     $checked = ' checked="checked"';
 7348:                 }
 7349:                 unless ($checked) {
 7350:                     $default = ' selected="selected"';
 7351:                 }
 7352:                 $extra =
 7353:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 7354:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 7355:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 7356:                     '<option value=""'.$default.'></option>'."\n";
 7357:                 foreach my $server (sort(keys(%{$servers}))) {
 7358:                     if (ref($currbalancer) eq 'HASH') {
 7359:                         next if (exists($currbalancer->{$server}));
 7360:                     }
 7361:                     my $selected;
 7362:                     if ($server eq $current) {
 7363:                         $selected = ' selected="selected"';
 7364:                     }
 7365:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 7366:                 }
 7367:                 $extra .= '</select>';
 7368:             }
 7369:         } elsif ($rule eq $current) {
 7370:             $checked = ' checked="checked"';
 7371:         }
 7372:         $output .= '<span class="LC_nobreak"><label>'.
 7373:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 7374:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 7375:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 7376:                    ')"'.$checked.' />&nbsp;';
 7377:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 7378:             $output .= $ruletitles{'particular'};
 7379:         } else {
 7380:             $output .= $ruletitles{$rulenames[$i]};
 7381:         }
 7382:         $output .= '</label>'.$extra.'</span><br />'."\n";
 7383:     }
 7384:     $output .= '</div></td></tr>'."\n";
 7385:     return $output;
 7386: }
 7387: 
 7388: sub offloadtype_text {
 7389:     my %ruletitles = &Apache::lonlocal::texthash (
 7390:            'default'          => 'Offloads to default destinations',
 7391:            'homeserver'       => "Offloads to user's home server",
 7392:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 7393:            'specific'         => 'Offloads to specific server',
 7394:            'none'             => 'No offload',
 7395:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 7396:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 7397:            'particular'       => 'Session hosted (after re-auth) on server:',
 7398:     );
 7399:     return %ruletitles;
 7400: }
 7401: 
 7402: sub sparestype_titles {
 7403:     my %typestitles = &Apache::lonlocal::texthash (
 7404:                           'primary' => 'primary',
 7405:                           'default' => 'default',
 7406:                       );
 7407:     return %typestitles;
 7408: }
 7409: 
 7410: sub contact_titles {
 7411:     my %titles = &Apache::lonlocal::texthash (
 7412:                    'supportemail'    => 'Support E-mail address',
 7413:                    'adminemail'      => 'Default Server Admin E-mail address',
 7414:                    'errormail'       => 'Error reports to be e-mailed to',
 7415:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 7416:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 7417:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 7418:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 7419:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 7420:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 7421:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 7422:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 7423:                    'errorthreshold'  => 'Error/warning threshold for status e-mail',
 7424:                    'errorsysmail'    => 'Error threshold for e-mail to core group',
 7425:                    'errorweights'    => 'Weights used to compute error count',
 7426:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 7427:                  );
 7428:     my %short_titles = &Apache::lonlocal::texthash (
 7429:                            adminemail   => 'Admin E-mail address',
 7430:                            supportemail => 'Support E-mail',
 7431:                        );   
 7432:     return (\%titles,\%short_titles);
 7433: }
 7434: 
 7435: sub helpform_fields {
 7436:     my %titles =  &Apache::lonlocal::texthash (
 7437:                        'username'   => 'Name',
 7438:                        'user'       => 'Username/domain',
 7439:                        'phone'      => 'Phone',
 7440:                        'cc'         => 'Cc e-mail',
 7441:                        'course'     => 'Course Details',
 7442:                        'section'    => 'Sections',
 7443:                        'screenshot' => 'File upload',
 7444:     );
 7445:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 7446:     my %possoptions = (
 7447:                         username     => ['yes','no','req'],
 7448:                         phone        => ['yes','no','req'],
 7449:                         user         => ['yes','no'],
 7450:                         cc           => ['yes','no'],
 7451:                         course       => ['yes','no'],
 7452:                         section      => ['yes','no'],
 7453:                         screenshot   => ['yes','no'],
 7454:                       );
 7455:     my %fieldoptions = &Apache::lonlocal::texthash (
 7456:                          'yes'  => 'Optional',
 7457:                          'req'  => 'Required',
 7458:                          'no'   => "Not shown",
 7459:     );
 7460:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 7461: }
 7462: 
 7463: sub tool_titles {
 7464:     my %titles = &Apache::lonlocal::texthash (
 7465:                      aboutme    => 'Personal web page',
 7466:                      blog       => 'Blog',
 7467:                      webdav     => 'WebDAV',
 7468:                      portfolio  => 'Portfolio',
 7469:                      official   => 'Official courses (with institutional codes)',
 7470:                      unofficial => 'Unofficial courses',
 7471:                      community  => 'Communities',
 7472:                      textbook   => 'Textbook courses',
 7473:                      placement  => 'Placement tests',
 7474:                  );
 7475:     return %titles;
 7476: }
 7477: 
 7478: sub courserequest_titles {
 7479:     my %titles = &Apache::lonlocal::texthash (
 7480:                                    official   => 'Official',
 7481:                                    unofficial => 'Unofficial',
 7482:                                    community  => 'Communities',
 7483:                                    textbook   => 'Textbook',
 7484:                                    placement  => 'Placement tests',
 7485:                                    lti        => 'LTI Provider',
 7486:                                    norequest  => 'Not allowed',
 7487:                                    approval   => 'Approval by DC',
 7488:                                    validate   => 'With validation',
 7489:                                    autolimit  => 'Numerical limit',
 7490:                                    unlimited  => '(blank for unlimited)',
 7491:                  );
 7492:     return %titles;
 7493: }
 7494: 
 7495: sub authorrequest_titles {
 7496:     my %titles = &Apache::lonlocal::texthash (
 7497:                                    norequest  => 'Not allowed',
 7498:                                    approval   => 'Approval by Dom. Coord.',
 7499:                                    automatic  => 'Automatic approval',
 7500:                  );
 7501:     return %titles;
 7502: }
 7503: 
 7504: sub courserequest_conditions {
 7505:     my %conditions = &Apache::lonlocal::texthash (
 7506:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 7507:        validate   => '(Processing of request subject to institutional validation).',
 7508:                  );
 7509:     return %conditions;
 7510: }
 7511: 
 7512: 
 7513: sub print_usercreation {
 7514:     my ($position,$dom,$settings,$rowtotal) = @_;
 7515:     my $numinrow = 4;
 7516:     my $datatable;
 7517:     if ($position eq 'top') {
 7518:         $$rowtotal ++;
 7519:         my $rowcount = 0;
 7520:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 7521:         if (ref($rules) eq 'HASH') {
 7522:             if (keys(%{$rules}) > 0) {
 7523:                 $datatable .= &user_formats_row('username',$settings,$rules,
 7524:                                                 $ruleorder,$numinrow,$rowcount);
 7525:                 $$rowtotal ++;
 7526:                 $rowcount ++;
 7527:             }
 7528:         }
 7529:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 7530:         if (ref($idrules) eq 'HASH') {
 7531:             if (keys(%{$idrules}) > 0) {
 7532:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 7533:                                                 $idruleorder,$numinrow,$rowcount);
 7534:                 $$rowtotal ++;
 7535:                 $rowcount ++;
 7536:             }
 7537:         }
 7538:         if ($rowcount == 0) {
 7539:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 7540:             $$rowtotal ++;
 7541:             $rowcount ++;
 7542:         }
 7543:     } elsif ($position eq 'middle') {
 7544:         my @creators = ('author','course','requestcrs');
 7545:         my ($rules,$ruleorder) =
 7546:             &Apache::lonnet::inst_userrules($dom,'username');
 7547:         my %lt = &usercreation_types();
 7548:         my %checked;
 7549:         if (ref($settings) eq 'HASH') {
 7550:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 7551:                 foreach my $item (@creators) {
 7552:                     $checked{$item} = $settings->{'cancreate'}{$item};
 7553:                 }
 7554:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 7555:                 foreach my $item (@creators) {
 7556:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 7557:                         $checked{$item} = 'none';
 7558:                     }
 7559:                 }
 7560:             }
 7561:         }
 7562:         my $rownum = 0;
 7563:         foreach my $item (@creators) {
 7564:             $rownum ++;
 7565:             if ($checked{$item} eq '') {
 7566:                 $checked{$item} = 'any';
 7567:             }
 7568:             my $css_class;
 7569:             if ($rownum%2) {
 7570:                 $css_class = '';
 7571:             } else {
 7572:                 $css_class = ' class="LC_odd_row" ';
 7573:             }
 7574:             $datatable .= '<tr'.$css_class.'>'.
 7575:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 7576:                          '</span></td><td style="text-align: right">';
 7577:             my @options = ('any');
 7578:             if (ref($rules) eq 'HASH') {
 7579:                 if (keys(%{$rules}) > 0) {
 7580:                     push(@options,('official','unofficial'));
 7581:                 }
 7582:             }
 7583:             push(@options,'none');
 7584:             foreach my $option (@options) {
 7585:                 my $type = 'radio';
 7586:                 my $check = ' ';
 7587:                 if ($checked{$item} eq $option) {
 7588:                     $check = ' checked="checked" ';
 7589:                 } 
 7590:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7591:                               '<input type="'.$type.'" name="can_createuser_'.
 7592:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 7593:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 7594:             }
 7595:             $datatable .= '</td></tr>';
 7596:         }
 7597:     } else {
 7598:         my @contexts = ('author','course','domain');
 7599:         my @authtypes = ('int','krb4','krb5','loc','lti');
 7600:         my %checked;
 7601:         if (ref($settings) eq 'HASH') {
 7602:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 7603:                 foreach my $item (@contexts) {
 7604:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 7605:                         foreach my $auth (@authtypes) {
 7606:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 7607:                                 $checked{$item}{$auth} = ' checked="checked" ';
 7608:                             }
 7609:                         }
 7610:                     }
 7611:                 }
 7612:             }
 7613:         } else {
 7614:             foreach my $item (@contexts) {
 7615:                 foreach my $auth (@authtypes) {
 7616:                     $checked{$item}{$auth} = ' checked="checked" ';
 7617:                 }
 7618:             }
 7619:         }
 7620:         my %title = &context_names();
 7621:         my %authname = &authtype_names();
 7622:         my $rownum = 0;
 7623:         my $css_class; 
 7624:         foreach my $item (@contexts) {
 7625:             if ($rownum%2) {
 7626:                 $css_class = '';
 7627:             } else {
 7628:                 $css_class = ' class="LC_odd_row" ';
 7629:             }
 7630:             $datatable .=   '<tr'.$css_class.'>'.
 7631:                             '<td>'.$title{$item}.
 7632:                             '</td><td class="LC_left_item">'.
 7633:                             '<span class="LC_nobreak">';
 7634:             foreach my $auth (@authtypes) {
 7635:                 $datatable .= '<label>'. 
 7636:                               '<input type="checkbox" name="'.$item.'_auth" '.
 7637:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 7638:                               $authname{$auth}.'</label>&nbsp;';
 7639:             }
 7640:             $datatable .= '</span></td></tr>';
 7641:             $rownum ++;
 7642:         }
 7643:         $$rowtotal += $rownum;
 7644:     }
 7645:     return $datatable;
 7646: }
 7647: 
 7648: sub print_selfcreation {
 7649:     my ($position,$dom,$settings,$rowtotal) = @_;
 7650:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 7651:         $emaildomain,$datatable);
 7652:     if (ref($settings) eq 'HASH') {
 7653:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 7654:             $createsettings = $settings->{'cancreate'};
 7655:             if (ref($createsettings) eq 'HASH') {
 7656:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 7657:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 7658:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 7659:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 7660:                         @selfcreate = ('email','login','sso');
 7661:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 7662:                         @selfcreate = ($createsettings->{'selfcreate'});
 7663:                     }
 7664:                 }
 7665:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 7666:                     $processing = $createsettings->{'selfcreateprocessing'};
 7667:                 }
 7668:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 7669:                     $emailoptions = $createsettings->{'emailoptions'};
 7670:                 }
 7671:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 7672:                     $emailverified = $createsettings->{'emailverified'};
 7673:                 }
 7674:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 7675:                     $emaildomain = $createsettings->{'emaildomain'};
 7676:                 }
 7677:             }
 7678:         }
 7679:     }
 7680:     my %radiohash;
 7681:     my $numinrow = 4;
 7682:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 7683:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7684:     if ($position eq 'top') {
 7685:         my %choices = &Apache::lonlocal::texthash (
 7686:                                                       cancreate_login      => 'Institutional Login',
 7687:                                                       cancreate_sso        => 'Institutional Single Sign On',
 7688:                                                   );
 7689:         my @toggles = sort(keys(%choices));
 7690:         my %defaultchecked = (
 7691:                                'cancreate_login' => 'off',
 7692:                                'cancreate_sso'   => 'off',
 7693:                              );
 7694:         my ($onclick,$itemcount);
 7695:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7696:                                                      \%choices,$itemcount,$onclick);
 7697:         $$rowtotal += $itemcount;
 7698:         
 7699:         if (ref($usertypes) eq 'HASH') {
 7700:             if (keys(%{$usertypes}) > 0) {
 7701:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 7702:                                              $dom,$numinrow,$othertitle,
 7703:                                              'statustocreate',$rowtotal);
 7704:                 $$rowtotal ++;
 7705:             }
 7706:         }
 7707:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 7708:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7709:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 7710:         my $rem;
 7711:         my $numperrow = 2;
 7712:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 7713:         $datatable .= '<tr'.$css_class.'>'.
 7714:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 7715:                      '<td class="LC_left_item">'."\n".
 7716:                      '<table>'."\n";
 7717:         for (my $i=0; $i<@fields; $i++) {
 7718:             $rem = $i%($numperrow);
 7719:             if ($rem == 0) {
 7720:                 if ($i > 0) {
 7721:                     $datatable .= '</tr>';
 7722:                 }
 7723:                 $datatable .= '<tr>';
 7724:             }
 7725:             my $currval;
 7726:             if (ref($createsettings) eq 'HASH') {
 7727:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 7728:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 7729:                 }
 7730:             }
 7731:             $datatable .= '<td class="LC_left_item">'.
 7732:                           '<span class="LC_nobreak">'.
 7733:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 7734:                           'value="'.$currval.'" size="10" />&nbsp;'.
 7735:                           $fieldtitles{$fields[$i]}.'</span></td>';
 7736:         }
 7737:         my $colsleft = $numperrow - $rem;
 7738:         if ($colsleft > 1 ) {
 7739:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7740:                          '&nbsp;</td>';
 7741:         } elsif ($colsleft == 1) {
 7742:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7743:         }
 7744:         $datatable .= '</tr></table></td></tr>';
 7745:         $$rowtotal ++;
 7746:     } elsif ($position eq 'middle') {
 7747:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 7748:         my @posstypes;
 7749:         if (ref($types) eq 'ARRAY') {
 7750:             @posstypes = @{$types};
 7751:         }
 7752:         unless (grep(/^default$/,@posstypes)) {
 7753:             push(@posstypes,'default');
 7754:         }
 7755:         my %usertypeshash;
 7756:         if (ref($usertypes) eq 'HASH') {
 7757:             %usertypeshash = %{$usertypes};
 7758:         }
 7759:         $usertypeshash{'default'} = $othertitle;
 7760:         foreach my $status (@posstypes) {
 7761:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 7762:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 7763:             $$rowtotal ++;
 7764:         }
 7765:     } else {
 7766:         my %choices = &Apache::lonlocal::texthash (
 7767:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 7768:                                                   );
 7769:         my @toggles = sort(keys(%choices));
 7770:         my %defaultchecked = (
 7771:                                'cancreate_email' => 'off',
 7772:                              );
 7773:         my $customclass = 'LC_selfcreate_email';
 7774:         my $classprefix = 'LC_canmodify_emailusername_';
 7775:         my $optionsprefix = 'LC_options_emailusername_';
 7776:         my $display = 'none';
 7777:         my $rowstyle = 'display:none';
 7778:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 7779:             $display = 'block';
 7780:             $rowstyle = 'display:table-row';
 7781:         }
 7782:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 7783:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 7784:                                                      \%choices,$$rowtotal,$onclick);
 7785:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 7786:                                          $rowstyle);
 7787:         $$rowtotal ++;
 7788:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 7789:                                       $rowstyle);
 7790:         $$rowtotal ++;
 7791:         my (@ordered,@posstypes,%usertypeshash);
 7792:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7793:         my ($emailrules,$emailruleorder) =
 7794:             &Apache::lonnet::inst_userrules($dom,'email');
 7795:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7796:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7797:         if (ref($types) eq 'ARRAY') {
 7798:             @posstypes = @{$types};
 7799:         }
 7800:         if (@posstypes) {
 7801:             unless (grep(/^default$/,@posstypes)) {
 7802:                 push(@posstypes,'default');
 7803:             }
 7804:             if (ref($usertypes) eq 'HASH') {
 7805:                 %usertypeshash = %{$usertypes};
 7806:             }
 7807:             my $currassign;
 7808:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 7809:                 $currassign = {
 7810:                                   selfassign => $domdefaults{'inststatusguest'},
 7811:                               };
 7812:                 @ordered = @{$domdefaults{'inststatusguest'}};
 7813:             } else {
 7814:                 $currassign = { selfassign => [] };
 7815:             }
 7816:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 7817:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 7818:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 7819:                                          $numinrow,$othertitle,'selfassign',
 7820:                                          $rowtotal,$onclicktypes,$customclass,
 7821:                                          $rowstyle);
 7822:             $$rowtotal ++;
 7823:             $usertypeshash{'default'} = $othertitle;
 7824:             foreach my $status (@posstypes) {
 7825:                 my $css_class;
 7826:                 if ($$rowtotal%2) {
 7827:                     $css_class = 'LC_odd_row ';
 7828:                 }
 7829:                 $css_class .= $customclass;
 7830:                 my $rowid = $optionsprefix.$status;
 7831:                 my $hidden = 1;
 7832:                 my $currstyle = 'display:none';
 7833:                 if (grep(/^\Q$status\E$/,@ordered)) {
 7834:                     $currstyle = $rowstyle;
 7835:                     $hidden = 0; 
 7836:                 }
 7837:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7838:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 7839:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 7840:                 unless ($hidden) {
 7841:                     $$rowtotal ++;
 7842:                 }
 7843:             }
 7844:         } else {
 7845:             my $css_class;
 7846:             if ($$rowtotal%2) {
 7847:                 $css_class = 'LC_odd_row ';
 7848:             }
 7849:             $css_class .= $customclass;
 7850:             $usertypeshash{'default'} = $othertitle;
 7851:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 7852:                                          $emailrules,$emailruleorder,$settings,'default','',
 7853:                                          $othertitle,$css_class,$rowstyle,$intdom);
 7854:             $$rowtotal ++;
 7855:         }
 7856:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 7857:         $numinrow = 1;
 7858:         if (@posstypes) {
 7859:             foreach my $status (@posstypes) {
 7860:                 my $rowid = $classprefix.$status;
 7861:                 my $datarowstyle = 'display:none';
 7862:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 7863:                     $datarowstyle = $rowstyle; 
 7864:                 }
 7865:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 7866:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7867:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 7868:                 unless ($datarowstyle eq 'display:none') {
 7869:                     $$rowtotal ++;
 7870:                 }
 7871:             }
 7872:         } else {
 7873:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 7874:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 7875:                                                    $infotitles,'',$customclass,$rowstyle);
 7876:         }
 7877:     }
 7878:     return $datatable;
 7879: }
 7880: 
 7881: sub selfcreate_javascript {
 7882:     return <<"ENDSCRIPT";
 7883: 
 7884: <script type="text/javascript">
 7885: // <![CDATA[
 7886: 
 7887: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 7888:     var x = document.getElementsByClassName(target);
 7889:     var insttypes = 0;
 7890:     var insttypeRegExp = new RegExp(prefix);
 7891:     if ((x.length != undefined) && (x.length > 0)) {
 7892:         if (form.elements[radio].length != undefined) {
 7893:             for (var i=0; i<form.elements[radio].length; i++) {
 7894:                 if (form.elements[radio][i].checked) {
 7895:                     if (form.elements[radio][i].value == 1) {
 7896:                         for (var j=0; j<x.length; j++) {
 7897:                             if (x[j].id == 'undefined') {
 7898:                                 x[j].style.display = 'table-row';
 7899:                             } else if (insttypeRegExp.test(x[j].id)) {
 7900:                                 insttypes ++;
 7901:                             } else {
 7902:                                 x[j].style.display = 'table-row';
 7903:                             }
 7904:                         }
 7905:                     } else {
 7906:                         for (var j=0; j<x.length; j++) {
 7907:                             x[j].style.display = 'none';
 7908:                         }
 7909:                     }
 7910:                     break;
 7911:                 }
 7912:             }
 7913:             if (insttypes > 0) {
 7914:                 toggleDataRow(form,checkbox,target,altprefix);
 7915:                 toggleDataRow(form,checkbox,target,prefix,1);
 7916:             }
 7917:         }
 7918:     }
 7919:     return;
 7920: }
 7921: 
 7922: function toggleDataRow(form,checkbox,target,prefix,docount) {
 7923:     if (form.elements[checkbox].length != undefined) {
 7924:         var count = 0;
 7925:         if (docount) {
 7926:             for (var i=0; i<form.elements[checkbox].length; i++) {
 7927:                 if (form.elements[checkbox][i].checked) {
 7928:                     count ++;
 7929:                 }
 7930:             }
 7931:         }
 7932:         for (var i=0; i<form.elements[checkbox].length; i++) {
 7933:             var type = form.elements[checkbox][i].value;
 7934:             if (document.getElementById(prefix+type)) {
 7935:                 if (form.elements[checkbox][i].checked) {
 7936:                     document.getElementById(prefix+type).style.display = 'table-row';
 7937:                     if (count % 2 == 1) {
 7938:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 7939:                     } else {
 7940:                         document.getElementById(prefix+type).className = target;
 7941:                     }
 7942:                     count ++;
 7943:                 } else {
 7944:                     document.getElementById(prefix+type).style.display = 'none';
 7945:                 }
 7946:             }
 7947:         }
 7948:     }
 7949:     return;
 7950: }
 7951: 
 7952: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 7953:     var caller = radio+'_'+status;
 7954:     if (form.elements[caller].length != undefined) {
 7955:         for (var i=0; i<form.elements[caller].length; i++) {
 7956:             if (form.elements[caller][i].checked) {
 7957:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 7958:                     var curr = form.elements[caller][i].value;
 7959:                     if (prefix) {
 7960:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 7961:                     }
 7962:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 7963:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 7964:                     if (curr == 'custom') {
 7965:                         if (prefix) { 
 7966:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 7967:                         }
 7968:                     } else if (curr == 'inst') {
 7969:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 7970:                     } else if (curr == 'noninst') {
 7971:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 7972:                     }
 7973:                     break;
 7974:                 }
 7975:             }
 7976:         }
 7977:     }
 7978: }
 7979: 
 7980: // ]]>
 7981: </script>
 7982: 
 7983: ENDSCRIPT
 7984: }
 7985: 
 7986: sub noninst_users {
 7987:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 7988:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 7989:     my $class = 'LC_left_item';
 7990:     if ($css_class) {
 7991:         $css_class = ' class="'.$css_class.'"'; 
 7992:     }
 7993:     if ($rowid) {
 7994:         $rowid = ' id="'.$rowid.'"';
 7995:     }
 7996:     if ($rowstyle) {
 7997:         $rowstyle = ' style="'.$rowstyle.'"';
 7998:     }
 7999:     my ($output,$description);
 8000:     if ($type eq 'default') {
 8001:         $description = &mt('Requests for: [_1]',$typetitle);
 8002:     } else {
 8003:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 8004:     }
 8005:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 8006:               "<td>$description</td>\n".           
 8007:               '<td class="'.$class.'" colspan="2">'.
 8008:               '<table><tr>';
 8009:     my %headers = &Apache::lonlocal::texthash( 
 8010:               approve  => 'Processing',
 8011:               email    => 'E-mail',
 8012:               username => 'Username',
 8013:     );
 8014:     foreach my $item ('approve','email','username') {
 8015:         $output .= '<th>'.$headers{$item}.'</th>';
 8016:     }
 8017:     $output .= '</tr><tr>';
 8018:     foreach my $item ('approve','email','username') {
 8019:         $output .= '<td style="vertical-align: top">';
 8020:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 8021:         if ($item eq 'approve') {
 8022:             %choices = &Apache::lonlocal::texthash (
 8023:                                                      automatic => 'Automatically approved',
 8024:                                                      approval  => 'Queued for approval',
 8025:                                                    );
 8026:             @options = ('automatic','approval');
 8027:             $hashref = $processing;
 8028:             $defoption = 'automatic';
 8029:             $name = 'cancreate_emailprocess_'.$type;
 8030:         } elsif ($item eq 'email') {
 8031:             %choices = &Apache::lonlocal::texthash (
 8032:                                                      any     => 'Any e-mail',
 8033:                                                      inst    => 'Institutional only',
 8034:                                                      noninst => 'Non-institutional only',
 8035:                                                      custom  => 'Custom restrictions',
 8036:                                                    );
 8037:             @options = ('any','inst','noninst');
 8038:             my $showcustom;
 8039:             if (ref($emailrules) eq 'HASH') {
 8040:                 if (keys(%{$emailrules}) > 0) {
 8041:                     push(@options,'custom');
 8042:                     $showcustom = 'cancreate_emailrule';
 8043:                     if (ref($settings) eq 'HASH') {
 8044:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 8045:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 8046:                                 if (exists($emailrules->{$rule})) {
 8047:                                     $hascustom ++;
 8048:                                 }
 8049:                             }
 8050:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 8051:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 8052:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 8053:                                     if (exists($emailrules->{$rule})) {
 8054:                                         $hascustom ++;
 8055:                                     }
 8056:                                 }
 8057:                             }
 8058:                         }
 8059:                     }
 8060:                 }
 8061:             }
 8062:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 8063:                                                      "'cancreate_emaildomain','$type'".');"';
 8064:             $hashref = $emailoptions;
 8065:             $defoption = 'any';
 8066:             $name = 'cancreate_emailoptions_'.$type;
 8067:         } elsif ($item eq 'username') {
 8068:             %choices = &Apache::lonlocal::texthash (
 8069:                                                      all    => 'Same as e-mail',
 8070:                                                      first  => 'Omit @domain',
 8071:                                                      free   => 'Free to choose',
 8072:                                                    );
 8073:             @options = ('all','first','free');
 8074:             $hashref = $emailverified;
 8075:             $defoption = 'all';
 8076:             $name = 'cancreate_usernameoptions_'.$type;
 8077:         }
 8078:         foreach my $option (@options) {
 8079:             my $checked;
 8080:             if (ref($hashref) eq 'HASH') {
 8081:                 if ($type eq '') {
 8082:                     if (!exists($hashref->{'default'})) {
 8083:                         if ($option eq $defoption) {
 8084:                             $checked = ' checked="checked"';
 8085:                         }
 8086:                     } else {
 8087:                         if ($hashref->{'default'} eq $option) {
 8088:                             $checked = ' checked="checked"';
 8089:                         }
 8090:                     }
 8091:                 } else {
 8092:                     if (!exists($hashref->{$type})) {
 8093:                         if ($option eq $defoption) {
 8094:                             $checked = ' checked="checked"';
 8095:                         }
 8096:                     } else {
 8097:                         if ($hashref->{$type} eq $option) {
 8098:                             $checked = ' checked="checked"';
 8099:                         }
 8100:                     }
 8101:                 }
 8102:             } elsif (($item eq 'email') && ($hascustom)) {
 8103:                 if ($option eq 'custom') {
 8104:                     $checked = ' checked="checked"';
 8105:                 }
 8106:             } elsif ($option eq $defoption) {
 8107:                 $checked = ' checked="checked"';
 8108:             }
 8109:             $output .= '<span class="LC_nobreak"><label>'.
 8110:                        '<input type="radio" name="'.$name.'"'.
 8111:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 8112:                        $choices{$option}.'</label></span><br />';
 8113:             if ($item eq 'email') {
 8114:                 if ($option eq 'custom') {
 8115:                     my $id = 'cancreate_emailrule_'.$type;
 8116:                     my $display = 'none';
 8117:                     if ($checked) {
 8118:                         $display = 'inline';
 8119:                     }
 8120:                     my $numinrow = 2;
 8121:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 8122:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 8123:                                &user_formats_row('email',$settings,$emailrules,
 8124:                                                  $emailruleorder,$numinrow,'',$type);
 8125:                               '</table></fieldset>';
 8126:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 8127:                     my %text = &Apache::lonlocal::texthash (
 8128:                                                              inst    => 'must end:',
 8129:                                                              noninst => 'cannot end:',
 8130:                                                            );
 8131:                     my $value;
 8132:                     if (ref($emaildomain) eq 'HASH') {
 8133:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 8134:                             $value = $emaildomain->{$type}->{$option}; 
 8135:                         }
 8136:                     }
 8137:                     if ($value eq '') {
 8138:                         $value = '@'.$intdom;
 8139:                     }
 8140:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 8141:                     my $display = 'none';
 8142:                     if ($checked) {
 8143:                         $display = 'inline';
 8144:                     }
 8145:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 8146:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 8147:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 8148:                                '</div>';
 8149:                 }
 8150:             }
 8151:         }
 8152:         $output .= '</td>'."\n";
 8153:     }
 8154:     $output .= "</tr></table></td></tr>\n";
 8155:     return $output;
 8156: }
 8157: 
 8158: sub captcha_choice {
 8159:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 8160:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 8161:         $vertext,$currver);
 8162:     my %lt = &captcha_phrases();
 8163:     $keyentry = 'hidden';
 8164:     my $colspan=2;
 8165:     if ($context eq 'cancreate') {
 8166:         $rowname = &mt('CAPTCHA validation');
 8167:     } elsif ($context eq 'login') {
 8168:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 8169:     } elsif ($context eq 'passwords') {
 8170:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 8171:         $colspan=1;
 8172:     }
 8173:     if (ref($settings) eq 'HASH') {
 8174:         if ($settings->{'captcha'}) {
 8175:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 8176:         } else {
 8177:             $checked{'original'} = ' checked="checked"';
 8178:         }
 8179:         if ($settings->{'captcha'} eq 'recaptcha') {
 8180:             $pubtext = $lt{'pub'};
 8181:             $privtext = $lt{'priv'};
 8182:             $keyentry = 'text';
 8183:             $vertext = $lt{'ver'};
 8184:             $currver = $settings->{'recaptchaversion'};
 8185:             if ($currver ne '2') {
 8186:                 $currver = 1;
 8187:             }
 8188:         }
 8189:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 8190:             $currpub = $settings->{'recaptchakeys'}{'public'};
 8191:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 8192:         }
 8193:     } else {
 8194:         $checked{'original'} = ' checked="checked"';
 8195:     }
 8196:     my $css_class;
 8197:     if ($itemcount%2) {
 8198:         $css_class = 'LC_odd_row';
 8199:     }
 8200:     if ($customcss) {
 8201:         $css_class .= " $customcss";
 8202:     }
 8203:     $css_class =~ s/^\s+//;
 8204:     if ($css_class) {
 8205:         $css_class = ' class="'.$css_class.'"';
 8206:     }
 8207:     if ($rowstyle) {
 8208:         $css_class .= ' style="'.$rowstyle.'"';
 8209:     }
 8210:     my $output = '<tr'.$css_class.'>'.
 8211:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 8212:                  '<table><tr><td>'."\n";
 8213:     foreach my $option ('original','recaptcha','notused') {
 8214:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 8215:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 8216:                    $lt{$option}.'</label></span>';
 8217:         unless ($option eq 'notused') {
 8218:             $output .= ('&nbsp;'x2)."\n";
 8219:         }
 8220:     }
 8221: #
 8222: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 8223: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 8224: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 8225: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 8226: #
 8227:     $output .= '</td></tr>'."\n".
 8228:                '<tr><td class="LC_zero_height">'."\n".
 8229:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 8230:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 8231:                $currpub.'" size="40" /></span><br />'."\n".
 8232:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 8233:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 8234:                $currpriv.'" size="40" /></span><br />'.
 8235:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 8236:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 8237:                $currver.'" size="3" /></span><br />'.
 8238:                '</td></tr></table>'."\n".
 8239:                '</td></tr>';
 8240:     return $output;
 8241: }
 8242: 
 8243: sub user_formats_row {
 8244:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 8245:     my $output;
 8246:     my %text = (
 8247:                    'username' => 'new usernames',
 8248:                    'id'       => 'IDs',
 8249:                );
 8250:     unless ($type eq 'email') {
 8251:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 8252:         $output = '<tr '.$css_class.'>'.
 8253:                   '<td><span class="LC_nobreak">'.
 8254:                   &mt("Format rules to check for $text{$type}: ").
 8255:                   '</td><td class="LC_left_item" colspan="2"><table>';
 8256:     }
 8257:     my $rem;
 8258:     if (ref($ruleorder) eq 'ARRAY') {
 8259:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 8260:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 8261:                 my $rem = $i%($numinrow);
 8262:                 if ($rem == 0) {
 8263:                     if ($i > 0) {
 8264:                         $output .= '</tr>';
 8265:                     }
 8266:                     $output .= '<tr>';
 8267:                 }
 8268:                 my $check = ' ';
 8269:                 if (ref($settings) eq 'HASH') {
 8270:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 8271:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 8272:                             $check = ' checked="checked" ';
 8273:                         }
 8274:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 8275:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 8276:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 8277:                                 $check = ' checked="checked" ';
 8278:                             }
 8279:                         }
 8280:                     }
 8281:                 }
 8282:                 my $name = $type.'_rule';
 8283:                 if ($type eq 'email') {
 8284:                     $name .= '_'.$status;
 8285:                 }
 8286:                 $output .= '<td class="LC_left_item">'.
 8287:                            '<span class="LC_nobreak"><label>'.
 8288:                            '<input type="checkbox" name="'.$name.'" '.
 8289:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 8290:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 8291:             }
 8292:         }
 8293:         $rem = @{$ruleorder}%($numinrow);
 8294:     }
 8295:     my $colsleft;
 8296:     if ($rem) {
 8297:         $colsleft = $numinrow - $rem;
 8298:     }
 8299:     if ($colsleft > 1 ) {
 8300:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8301:                    '&nbsp;</td>';
 8302:     } elsif ($colsleft == 1) {
 8303:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 8304:     }
 8305:     $output .= '</tr></table>';
 8306:     unless ($type eq 'email') {
 8307:         $output .= '</td></tr>';
 8308:     }
 8309:     return $output;
 8310: }
 8311: 
 8312: sub usercreation_types {
 8313:     my %lt = &Apache::lonlocal::texthash (
 8314:                     author     => 'When adding a co-author',
 8315:                     course     => 'When adding a user to a course',
 8316:                     requestcrs => 'When requesting a course',
 8317:                     any        => 'Any',
 8318:                     official   => 'Institutional only ',
 8319:                     unofficial => 'Non-institutional only',
 8320:                     none       => 'None',
 8321:     );
 8322:     return %lt;
 8323: }
 8324: 
 8325: sub selfcreation_types {
 8326:     my %lt = &Apache::lonlocal::texthash (
 8327:                     selfcreate => 'User creates own account',
 8328:                     any        => 'Any',
 8329:                     official   => 'Institutional only ',
 8330:                     unofficial => 'Non-institutional only',
 8331:                     email      => 'E-mail address',
 8332:                     login      => 'Institutional Login',
 8333:                     sso        => 'SSO',
 8334:              );
 8335: }
 8336: 
 8337: sub authtype_names {
 8338:     my %lt = &Apache::lonlocal::texthash(
 8339:                       int    => 'Internal',
 8340:                       krb4   => 'Kerberos 4',
 8341:                       krb5   => 'Kerberos 5',
 8342:                       loc    => 'Local',
 8343:                       lti    => 'LTI',
 8344:                   );
 8345:     return %lt;
 8346: }
 8347: 
 8348: sub context_names {
 8349:     my %context_title = &Apache::lonlocal::texthash(
 8350:        author => 'Creating users when an Author',
 8351:        course => 'Creating users when in a course',
 8352:        domain => 'Creating users when a Domain Coordinator',
 8353:     );
 8354:     return %context_title;
 8355: }
 8356: 
 8357: sub print_usermodification {
 8358:     my ($position,$dom,$settings,$rowtotal) = @_;
 8359:     my $numinrow = 4;
 8360:     my ($context,$datatable,$rowcount);
 8361:     if ($position eq 'top') {
 8362:         $rowcount = 0;
 8363:         $context = 'author'; 
 8364:         foreach my $role ('ca','aa') {
 8365:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 8366:                                                    $numinrow,$rowcount);
 8367:             $$rowtotal ++;
 8368:             $rowcount ++;
 8369:         }
 8370:     } elsif ($position eq 'bottom') {
 8371:         $context = 'course';
 8372:         $rowcount = 0;
 8373:         foreach my $role ('st','ep','ta','in','cr') {
 8374:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 8375:                                                    $numinrow,$rowcount);
 8376:             $$rowtotal ++;
 8377:             $rowcount ++;
 8378:         }
 8379:     }
 8380:     return $datatable;
 8381: }
 8382: 
 8383: sub print_defaults {
 8384:     my ($position,$dom,$settings,$rowtotal) = @_;
 8385:     my $rownum = 0;
 8386:     my ($datatable,$css_class,$titles);
 8387:     unless ($position eq 'bottom') {
 8388:         $titles = &defaults_titles($dom);
 8389:     }
 8390:     if ($position eq 'top') {
 8391:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 8392:                      'datelocale_def','portal_def');
 8393:         my %defaults;
 8394:         if (ref($settings) eq 'HASH') {
 8395:             %defaults = %{$settings};
 8396:         } else {
 8397:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8398:             foreach my $item (@items) {
 8399:                 $defaults{$item} = $domdefaults{$item};
 8400:             }
 8401:         }
 8402:         foreach my $item (@items) {
 8403:             if ($rownum%2) {
 8404:                 $css_class = '';
 8405:             } else {
 8406:                 $css_class = ' class="LC_odd_row" ';
 8407:             }
 8408:             $datatable .= '<tr'.$css_class.'>'.
 8409:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 8410:                           '</span></td><td class="LC_right_item" colspan="3">';
 8411:             if ($item eq 'auth_def') {
 8412:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 8413:                 my %shortauth = (
 8414:                                  internal => 'int',
 8415:                                  krb4 => 'krb4',
 8416:                                  krb5 => 'krb5',
 8417:                                  localauth  => 'loc',
 8418:                                  lti => 'lti',
 8419:                                 );
 8420:                 my %authnames = &authtype_names();
 8421:                 foreach my $auth (@authtypes) {
 8422:                     my $checked = ' ';
 8423:                     if ($defaults{$item} eq $auth) {
 8424:                         $checked = ' checked="checked" ';
 8425:                     }
 8426:                     $datatable .= '<label><input type="radio" name="'.$item.
 8427:                                   '" value="'.$auth.'"'.$checked.'/>'.
 8428:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 8429:                 }
 8430:             } elsif ($item eq 'timezone_def') {
 8431:                 my $includeempty = 1;
 8432:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 8433:             } elsif ($item eq 'datelocale_def') {
 8434:                 my $includeempty = 1;
 8435:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 8436:             } elsif ($item eq 'lang_def') {
 8437:                 my $includeempty = 1;
 8438:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 8439:             } else {
 8440:                 my $size;
 8441:                 if ($item eq 'portal_def') {
 8442:                     $size = ' size="25"';
 8443:                 }
 8444:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 8445:                               $defaults{$item}.'"'.$size.' />';
 8446:             }
 8447:             $datatable .= '</td></tr>';
 8448:             $rownum ++;
 8449:         }
 8450:     } else {
 8451:         my %defaults;
 8452:         if (ref($settings) eq 'HASH') {
 8453:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 8454:                 my $maxnum = @{$settings->{'inststatusorder'}};
 8455:                 for (my $i=0; $i<$maxnum; $i++) {
 8456:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 8457:                     my $item = $settings->{'inststatusorder'}->[$i];
 8458:                     my $title = $settings->{'inststatustypes'}->{$item};
 8459:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 8460:                     $datatable .= '<tr'.$css_class.'>'.
 8461:                                   '<td><span class="LC_nobreak">'.
 8462:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 8463:                     for (my $k=0; $k<=$maxnum; $k++) {
 8464:                         my $vpos = $k+1;
 8465:                         my $selstr;
 8466:                         if ($k == $i) {
 8467:                             $selstr = ' selected="selected" ';
 8468:                         }
 8469:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8470:                     }
 8471:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 8472:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 8473:                                   &mt('delete').'</span></td>'.
 8474:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 8475:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 8476:                                   '</span></td></tr>';
 8477:                 }
 8478:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 8479:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 8480:                 $datatable .= '<tr '.$css_class.'>'.
 8481:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 8482:                 for (my $k=0; $k<=$maxnum; $k++) {
 8483:                     my $vpos = $k+1;
 8484:                     my $selstr;
 8485:                     if ($k == $maxnum) {
 8486:                         $selstr = ' selected="selected" ';
 8487:                     }
 8488:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 8489:                 }
 8490:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 8491:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 8492:                               '&nbsp;'.&mt('(new)').
 8493:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 8494:                               &mt('Name displayed:').
 8495:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 8496:                               '</tr>'."\n";
 8497:                 $rownum ++;
 8498:             }
 8499:         }
 8500:     }
 8501:     $$rowtotal += $rownum;
 8502:     return $datatable;
 8503: }
 8504: 
 8505: sub get_languages_hash {
 8506:     my %langchoices;
 8507:     foreach my $id (&Apache::loncommon::languageids()) {
 8508:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 8509:         if ($code ne '') {
 8510:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 8511:         }
 8512:     }
 8513:     return %langchoices;
 8514: }
 8515: 
 8516: sub defaults_titles {
 8517:     my ($dom) = @_;
 8518:     my %titles = &Apache::lonlocal::texthash (
 8519:                    'auth_def'      => 'Default authentication type',
 8520:                    'auth_arg_def'  => 'Default authentication argument',
 8521:                    'lang_def'      => 'Default language',
 8522:                    'timezone_def'  => 'Default timezone',
 8523:                    'datelocale_def' => 'Default locale for dates',
 8524:                    'portal_def'     => 'Portal/Default URL',
 8525:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 8526:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 8527:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 8528:                  );
 8529:     if ($dom) {
 8530:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 8531:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 8532:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 8533:         $protocol = 'http' if ($protocol ne 'https');
 8534:         if ($uint_dom) {
 8535:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 8536:                                          $uint_dom);
 8537:         }
 8538:     }
 8539:     return (\%titles);
 8540: }
 8541: 
 8542: sub print_scantron {
 8543:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 8544:     if ($position eq 'top') {
 8545:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 8546:     } else {
 8547:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 8548:     }
 8549: }
 8550: 
 8551: sub scantron_javascript {
 8552:     return <<"ENDSCRIPT";
 8553: 
 8554: <script type="text/javascript">
 8555: // <![CDATA[
 8556: 
 8557: function toggleScantron(form) {
 8558:     var csvfieldset = new Array();
 8559:     if (document.getElementById('scantroncsv_cols')) {
 8560:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 8561:     }
 8562:     if (document.getElementById('scantroncsv_options')) {
 8563:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 8564:     }
 8565:     if (csvfieldset.length) {
 8566:         if (document.getElementById('scantronconfcsv')) {
 8567:             var scantroncsv = document.getElementById('scantronconfcsv');
 8568:             if (scantroncsv.checked) {
 8569:                 for (var i=0; i<csvfieldset.length; i++) {
 8570:                     csvfieldset[i].style.display = 'block';
 8571:                 }
 8572:             } else {
 8573:                 for (var i=0; i<csvfieldset.length; i++) {
 8574:                     csvfieldset[i].style.display = 'none';
 8575:                 }
 8576:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 8577:                 if (csvselects.length) {
 8578:                     for (var j=0; j<csvselects.length; j++) {
 8579:                         csvselects[j].selectedIndex = 0;
 8580:                     }
 8581:                 }
 8582:             }
 8583:         }
 8584:     }
 8585:     return;
 8586: }
 8587: // ]]>
 8588: </script>
 8589: 
 8590: ENDSCRIPT
 8591: 
 8592: }
 8593: 
 8594: sub print_scantronformat {
 8595:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 8596:     my $itemcount = 1;
 8597:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 8598:         %confhash);
 8599:     my $switchserver = &check_switchserver($dom,$confname);
 8600:     my %lt = &Apache::lonlocal::texthash (
 8601:                 default => 'Default bubblesheet format file error',
 8602:                 custom  => 'Custom bubblesheet format file error',
 8603:              );
 8604:     my %scantronfiles = (
 8605:         default => 'default.tab',
 8606:         custom => 'custom.tab',
 8607:     );
 8608:     foreach my $key (keys(%scantronfiles)) {
 8609:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 8610:                               .$scantronfiles{$key};
 8611:     }
 8612:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 8613:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 8614:         if (!$switchserver) {
 8615:             my $servadm = $r->dir_config('lonAdmEMail');
 8616:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 8617:             if ($configuserok eq 'ok') {
 8618:                 if ($author_ok eq 'ok') {
 8619:                     my %legacyfile = (
 8620:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 8621:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 8622:                     );
 8623:                     my %md5chk;
 8624:                     foreach my $type (keys(%legacyfile)) {
 8625:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 8626:                         chomp($md5chk{$type});
 8627:                     }
 8628:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 8629:                         foreach my $type (keys(%legacyfile)) {
 8630:                             ($scantronurls{$type},my $error) =
 8631:                                 &legacy_scantronformat($r,$dom,$confname,
 8632:                                                  $type,$legacyfile{$type},
 8633:                                                  $scantronurls{$type},
 8634:                                                  $scantronfiles{$type});
 8635:                             if ($error ne '') {
 8636:                                 $error{$type} = $error;
 8637:                             }
 8638:                         }
 8639:                         if (keys(%error) == 0) {
 8640:                             $is_custom = 1;
 8641:                             $confhash{'scantron'}{'scantronformat'} =
 8642:                                 $scantronurls{'custom'};
 8643:                             my $putresult =
 8644:                                 &Apache::lonnet::put_dom('configuration',
 8645:                                                          \%confhash,$dom);
 8646:                             if ($putresult ne 'ok') {
 8647:                                 $error{'custom'} =
 8648:                                     '<span class="LC_error">'.
 8649:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8650:                             }
 8651:                         }
 8652:                     } else {
 8653:                         ($scantronurls{'default'},my $error) =
 8654:                             &legacy_scantronformat($r,$dom,$confname,
 8655:                                           'default',$legacyfile{'default'},
 8656:                                           $scantronurls{'default'},
 8657:                                           $scantronfiles{'default'});
 8658:                         if ($error eq '') {
 8659:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 8660:                             my $putresult =
 8661:                                 &Apache::lonnet::put_dom('configuration',
 8662:                                                          \%confhash,$dom);
 8663:                             if ($putresult ne 'ok') {
 8664:                                 $error{'default'} =
 8665:                                     '<span class="LC_error">'.
 8666:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 8667:                             }
 8668:                         } else {
 8669:                             $error{'default'} = $error;
 8670:                         }
 8671:                     }
 8672:                 }
 8673:             }
 8674:         } else {
 8675:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 8676:         }
 8677:     }
 8678:     if (ref($settings) eq 'HASH') {
 8679:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 8680:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 8681:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 8682:                 $scantronurl = '';
 8683:             } else {
 8684:                 $scantronurl = $settings->{'scantronformat'};
 8685:             }
 8686:             $is_custom = 1;
 8687:         } else {
 8688:             $scantronurl = $scantronurls{'default'};
 8689:         }
 8690:     } else {
 8691:         if ($is_custom) {
 8692:             $scantronurl = $scantronurls{'custom'};
 8693:         } else {
 8694:             $scantronurl = $scantronurls{'default'};
 8695:         }
 8696:     }
 8697:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8698:     $datatable .= '<tr'.$css_class.'>';
 8699:     if (!$is_custom) {
 8700:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 8701:                       '<span class="LC_nobreak">';
 8702:         if ($scantronurl) {
 8703:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 8704:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 8705:         } else {
 8706:             $datatable = &mt('File unavailable for display');
 8707:         }
 8708:         $datatable .= '</span></td>';
 8709:         if (keys(%error) == 0) { 
 8710:             $datatable .= '<td style="vertical-align: bottom">';
 8711:             if (!$switchserver) {
 8712:                 $datatable .= &mt('Upload:').'<br />';
 8713:             }
 8714:         } else {
 8715:             my $errorstr;
 8716:             foreach my $key (sort(keys(%error))) {
 8717:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8718:             }
 8719:             $datatable .= '<td>'.$errorstr;
 8720:         }
 8721:     } else {
 8722:         if (keys(%error) > 0) {
 8723:             my $errorstr;
 8724:             foreach my $key (sort(keys(%error))) {
 8725:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 8726:             } 
 8727:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 8728:         } elsif ($scantronurl) {
 8729:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 8730:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 8731:             $datatable .= '<td><span class="LC_nobreak">'.
 8732:                           $link.
 8733:                           '<label><input type="checkbox" name="scantronformat_del"'.
 8734:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 8735:                           '<td><span class="LC_nobreak">&nbsp;'.
 8736:                           &mt('Replace:').'</span><br />';
 8737:         }
 8738:     }
 8739:     if (keys(%error) == 0) {
 8740:         if ($switchserver) {
 8741:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 8742:         } else {
 8743:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8744:                          '<input type="file" name="scantronformat" /></span>';
 8745:         }
 8746:     }
 8747:     $datatable .= '</td></tr>';
 8748:     $$rowtotal ++;
 8749:     return $datatable;
 8750: }
 8751: 
 8752: sub legacy_scantronformat {
 8753:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 8754:     my ($url,$error);
 8755:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 8756:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 8757:         (my $result,$url) =
 8758:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 8759:                          '','',$newfile);
 8760:         if ($result ne 'ok') {
 8761:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 8762:         }
 8763:     }
 8764:     return ($url,$error);
 8765: }
 8766: 
 8767: sub print_scantronconfig {
 8768:     my ($dom,$settings,$rowtotal) = @_;
 8769:     my $itemcount = 2;
 8770:     my $is_checked = ' checked="checked"';
 8771:     my %optionson = (
 8772:                      hdr => ' checked="checked"',
 8773:                      pad => ' checked="checked"',
 8774:                      rem => ' checked="checked"',
 8775:                     );
 8776:     my %optionsoff = (
 8777:                       hdr => '',
 8778:                       pad => '',
 8779:                       rem => '',
 8780:                      );
 8781:     my $currcsvsty = 'none';
 8782:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 8783:     my @fields = &scantroncsv_fields();
 8784:     my %titles = &scantronconfig_titles();
 8785:     if (ref($settings) eq 'HASH') {
 8786:         if (ref($settings->{config}) eq 'HASH') {
 8787:             if ($settings->{config}->{dat}) {
 8788:                 $checked{'dat'} = $is_checked;
 8789:             }
 8790:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 8791:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 8792:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 8793:                     if (keys(%csvfields) > 0) {
 8794:                         $checked{'csv'} = $is_checked;
 8795:                         $currcsvsty = 'block';
 8796:                     }
 8797:                 }
 8798:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 8799:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 8800:                     foreach my $option (keys(%optionson)) {
 8801:                         unless ($csvoptions{$option}) {
 8802:                             $optionsoff{$option} = $optionson{$option};
 8803:                             $optionson{$option} = '';
 8804:                         }
 8805:                     }
 8806:                 }
 8807:             }
 8808:         } else {
 8809:             $checked{'dat'} = $is_checked;
 8810:         }
 8811:     } else {
 8812:         $checked{'dat'} = $is_checked;
 8813:     }
 8814:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 8815:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8816:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 8817:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 8818:     foreach my $item ('dat','csv') {
 8819:         my $id;
 8820:         if ($item eq 'csv') {
 8821:             $id = 'id="scantronconfcsv" ';
 8822:         }
 8823:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 8824:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 8825:         if ($item eq 'csv') {
 8826:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 8827:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 8828:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 8829:             foreach my $col (@fields) {
 8830:                 my $selnone;
 8831:                 if ($csvfields{$col} eq '') {
 8832:                     $selnone = ' selected="selected"';
 8833:                 }
 8834:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 8835:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 8836:                               '<option value=""'.$selnone.'></option>';
 8837:                 for (my $i=0; $i<20; $i++) {
 8838:                     my $shown = $i+1;
 8839:                     my $sel;
 8840:                     unless ($selnone) {
 8841:                         if (exists($csvfields{$col})) {
 8842:                             if ($csvfields{$col} == $i) {
 8843:                                 $sel = ' selected="selected"';
 8844:                             }
 8845:                         }
 8846:                     }
 8847:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 8848:                 }
 8849:                 $datatable .= '</select></td></tr>';
 8850:            }
 8851:            $datatable .= '</table></fieldset>'.
 8852:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 8853:                          '<legend>'.&mt('CSV Options').'</legend>';
 8854:            foreach my $option ('hdr','pad','rem') {
 8855:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 8856:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 8857:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 8858:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 8859:            }
 8860:            $datatable .= '</fieldset>';
 8861:            $itemcount ++;
 8862:         }
 8863:     }
 8864:     $datatable .= '</td></tr>';
 8865:     $$rowtotal ++;
 8866:     return $datatable;
 8867: }
 8868: 
 8869: sub scantronconfig_titles {
 8870:     return &Apache::lonlocal::texthash(
 8871:                                           dat => 'Standard format (.dat)',
 8872:                                           csv => 'Comma separated values (.csv)',
 8873:                                           hdr => 'Remove first line in file (contains column titles)',
 8874:                                           pad => 'Prepend 0s to PaperID',
 8875:                                           rem => 'Remove leading spaces (except Question Response columns)',
 8876:                                           CODE => 'CODE',
 8877:                                           ID   => 'Student ID',
 8878:                                           PaperID => 'Paper ID',
 8879:                                           FirstName => 'First Name',
 8880:                                           LastName => 'Last Name',
 8881:                                           FirstQuestion => 'First Question Response',
 8882:                                           Section => 'Section',
 8883:     );
 8884: }
 8885: 
 8886: sub scantroncsv_fields {
 8887:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 8888: }
 8889: 
 8890: sub print_coursecategories {
 8891:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 8892:     my $datatable;
 8893:     if ($position eq 'top') {
 8894:         my (%checked);
 8895:         my @catitems = ('unauth','auth');
 8896:         my @cattypes = ('std','domonly','codesrch','none');
 8897:         $checked{'unauth'} = 'std';
 8898:         $checked{'auth'} = 'std';
 8899:         if (ref($settings) eq 'HASH') {
 8900:             foreach my $type (@cattypes) {
 8901:                 if ($type eq $settings->{'unauth'}) {
 8902:                     $checked{'unauth'} = $type;
 8903:                 }
 8904:                 if ($type eq $settings->{'auth'}) {
 8905:                     $checked{'auth'} = $type;
 8906:                 }
 8907:             }
 8908:         }
 8909:         my %lt = &Apache::lonlocal::texthash (
 8910:                                                unauth   => 'Catalog type for unauthenticated users',
 8911:                                                auth     => 'Catalog type for authenticated users',
 8912:                                                none     => 'No catalog',
 8913:                                                std      => 'Standard catalog',
 8914:                                                domonly  => 'Domain-only catalog',
 8915:                                                codesrch => "Code search form",
 8916:                                              );
 8917:        my $itemcount = 0;
 8918:        foreach my $item (@catitems) {
 8919:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 8920:            $datatable .= '<tr '.$css_class.'>'.
 8921:                          '<td>'.$lt{$item}.'</td>'.
 8922:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 8923:            foreach my $type (@cattypes) {
 8924:                my $ischecked;
 8925:                if ($checked{$item} eq $type) {
 8926:                    $ischecked=' checked="checked"';
 8927:                }
 8928:                $datatable .= '<label>'.
 8929:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 8930:                              ' />'.$lt{$type}.'</label>&nbsp;';
 8931:            }
 8932:            $datatable .= '</span></td></tr>';
 8933:            $itemcount ++;
 8934:         }
 8935:         $$rowtotal += $itemcount;
 8936:     } elsif ($position eq 'middle') {
 8937:         my $toggle_cats_crs = ' ';
 8938:         my $toggle_cats_dom = ' checked="checked" ';
 8939:         my $can_cat_crs = ' ';
 8940:         my $can_cat_dom = ' checked="checked" ';
 8941:         my $toggle_catscomm_comm = ' ';
 8942:         my $toggle_catscomm_dom = ' checked="checked" ';
 8943:         my $can_catcomm_comm = ' ';
 8944:         my $can_catcomm_dom = ' checked="checked" ';
 8945:         my $toggle_catsplace_place = ' ';
 8946:         my $toggle_catsplace_dom = ' checked="checked" ';
 8947:         my $can_catplace_place = ' ';
 8948:         my $can_catplace_dom = ' checked="checked" ';
 8949: 
 8950:         if (ref($settings) eq 'HASH') {
 8951:             if ($settings->{'togglecats'} eq 'crs') {
 8952:                 $toggle_cats_crs = $toggle_cats_dom;
 8953:                 $toggle_cats_dom = ' ';
 8954:             }
 8955:             if ($settings->{'categorize'} eq 'crs') {
 8956:                 $can_cat_crs = $can_cat_dom;
 8957:                 $can_cat_dom = ' ';
 8958:             }
 8959:             if ($settings->{'togglecatscomm'} eq 'comm') {
 8960:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 8961:                 $toggle_catscomm_dom = ' ';
 8962:             }
 8963:             if ($settings->{'categorizecomm'} eq 'comm') {
 8964:                 $can_catcomm_comm = $can_catcomm_dom;
 8965:                 $can_catcomm_dom = ' ';
 8966:             }
 8967:             if ($settings->{'togglecatsplace'} eq 'place') {
 8968:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 8969:                 $toggle_catsplace_dom = ' ';
 8970:             }
 8971:             if ($settings->{'categorizeplace'} eq 'place') {
 8972:                 $can_catplace_place = $can_catplace_dom;
 8973:                 $can_catplace_dom = ' ';
 8974:             }
 8975:         }
 8976:         my %title = &Apache::lonlocal::texthash (
 8977:                      togglecats      => 'Show/Hide a course in catalog',
 8978:                      togglecatscomm  => 'Show/Hide a community in catalog',
 8979:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 8980:                      categorize      => 'Assign a category to a course',
 8981:                      categorizecomm  => 'Assign a category to a community',
 8982:                      categorizeplace => 'Assign a category to a placement test',
 8983:                     );
 8984:         my %level = &Apache::lonlocal::texthash (
 8985:                      dom   => 'Set in Domain',
 8986:                      crs   => 'Set in Course',
 8987:                      comm  => 'Set in Community',
 8988:                      place => 'Set in Placement Test',
 8989:                     );
 8990:         $datatable = '<tr class="LC_odd_row">'.
 8991:                   '<td>'.$title{'togglecats'}.'</td>'.
 8992:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 8993:                   '<input type="radio" name="togglecats"'.
 8994:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 8995:                   '<label><input type="radio" name="togglecats"'.
 8996:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 8997:                   '</tr><tr>'.
 8998:                   '<td>'.$title{'categorize'}.'</td>'.
 8999:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9000:                   '<label><input type="radio" name="categorize"'.
 9001:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9002:                   '<label><input type="radio" name="categorize"'.
 9003:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 9004:                   '</tr><tr class="LC_odd_row">'.
 9005:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 9006:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9007:                   '<input type="radio" name="togglecatscomm"'.
 9008:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9009:                   '<label><input type="radio" name="togglecatscomm"'.
 9010:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 9011:                   '</tr><tr>'.
 9012:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 9013:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9014:                   '<label><input type="radio" name="categorizecomm"'.
 9015:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9016:                   '<label><input type="radio" name="categorizecomm"'.
 9017:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 9018:                   '</tr><tr class="LC_odd_row">'.
 9019:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 9020:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9021:                   '<input type="radio" name="togglecatsplace"'.
 9022:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9023:                   '<label><input type="radio" name="togglecatscomm"'.
 9024:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 9025:                   '</tr><tr>'.
 9026:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 9027:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9028:                   '<label><input type="radio" name="categorizeplace"'.
 9029:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9030:                   '<label><input type="radio" name="categorizeplace"'.
 9031:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 9032:                   '</tr>';
 9033:         $$rowtotal += 6;
 9034:     } else {
 9035:         my $css_class;
 9036:         my $itemcount = 1;
 9037:         my $cathash; 
 9038:         if (ref($settings) eq 'HASH') {
 9039:             $cathash = $settings->{'cats'};
 9040:         }
 9041:         if (ref($cathash) eq 'HASH') {
 9042:             my (@cats,@trails,%allitems,%idx,@jsarray);
 9043:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 9044:                                                    \%allitems,\%idx,\@jsarray);
 9045:             my $maxdepth = scalar(@cats);
 9046:             my $colattrib = '';
 9047:             if ($maxdepth > 2) {
 9048:                 $colattrib = ' colspan="2" ';
 9049:             }
 9050:             my @path;
 9051:             if (@cats > 0) {
 9052:                 if (ref($cats[0]) eq 'ARRAY') {
 9053:                     my $numtop = @{$cats[0]};
 9054:                     my $maxnum = $numtop;
 9055:                     my %default_names = (
 9056:                           instcode    => &mt('Official courses'),
 9057:                           communities => &mt('Communities'),
 9058:                           placement   => &mt('Placement Tests'),
 9059:                     );
 9060: 
 9061:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 9062:                         ($cathash->{'instcode::0'} eq '') ||
 9063:                         (!grep(/^communities$/,@{$cats[0]})) || 
 9064:                         ($cathash->{'communities::0'} eq '') ||
 9065:                         (!grep(/^placement$/,@{$cats[0]})) ||
 9066:                         ($cathash->{'placement::0'} eq '')) {
 9067:                         $maxnum ++;
 9068:                     }
 9069:                     my $lastidx;
 9070:                     for (my $i=0; $i<$numtop; $i++) {
 9071:                         my $parent = $cats[0][$i];
 9072:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9073:                         my $item = &escape($parent).'::0';
 9074:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 9075:                         $lastidx = $idx{$item};
 9076:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 9077:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 9078:                         for (my $k=0; $k<=$maxnum; $k++) {
 9079:                             my $vpos = $k+1;
 9080:                             my $selstr;
 9081:                             if ($k == $i) {
 9082:                                 $selstr = ' selected="selected" ';
 9083:                             }
 9084:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9085:                         }
 9086:                         $datatable .= '</select></span></td><td>';
 9087:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 9088:                             $datatable .=  '<span class="LC_nobreak">'
 9089:                                            .$default_names{$parent}.'</span>';
 9090:                             if ($parent eq 'instcode') {
 9091:                                 $datatable .= '<br /><span class="LC_nobreak">('
 9092:                                               .&mt('with institutional codes')
 9093:                                               .')</span></td><td'.$colattrib.'>';
 9094:                             } else {
 9095:                                 $datatable .= '<table><tr><td>';
 9096:                             }
 9097:                             $datatable .= '<span class="LC_nobreak">'
 9098:                                           .'<label><input type="radio" name="'
 9099:                                           .$parent.'" value="1" checked="checked" />'
 9100:                                           .&mt('Display').'</label>';
 9101:                             if ($parent eq 'instcode') {
 9102:                                 $datatable .= '&nbsp;';
 9103:                             } else {
 9104:                                 $datatable .= '</span></td></tr><tr><td>'
 9105:                                               .'<span class="LC_nobreak">';
 9106:                             }
 9107:                             $datatable .= '<label><input type="radio" name="'
 9108:                                           .$parent.'" value="0" />'
 9109:                                           .&mt('Do not display').'</label></span>';
 9110:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 9111:                                 $datatable .= '</td></tr></table>';
 9112:                             }
 9113:                             $datatable .= '</td>';
 9114:                         } else {
 9115:                             $datatable .= $parent
 9116:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 9117:                                           .'<input type="checkbox" name="deletecategory" '
 9118:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 9119:                         }
 9120:                         my $depth = 1;
 9121:                         push(@path,$parent);
 9122:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 9123:                         pop(@path);
 9124:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 9125:                         $itemcount ++;
 9126:                     }
 9127:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9128:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 9129:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 9130:                     for (my $k=0; $k<=$maxnum; $k++) {
 9131:                         my $vpos = $k+1;
 9132:                         my $selstr;
 9133:                         if ($k == $numtop) {
 9134:                             $selstr = ' selected="selected" ';
 9135:                         }
 9136:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9137:                     }
 9138:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 9139:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 9140:                                   .'</tr>'."\n";
 9141:                     $itemcount ++;
 9142:                     foreach my $default ('instcode','communities','placement') {
 9143:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 9144:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9145:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 9146:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 9147:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 9148:                             for (my $k=0; $k<=$maxnum; $k++) {
 9149:                                 my $vpos = $k+1;
 9150:                                 my $selstr;
 9151:                                 if ($k == $maxnum) {
 9152:                                     $selstr = ' selected="selected" ';
 9153:                                 }
 9154:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9155:                             }
 9156:                             $datatable .= '</select></span></td>'.
 9157:                                           '<td><span class="LC_nobreak">'.
 9158:                                           $default_names{$default}.'</span>';
 9159:                             if ($default eq 'instcode') {
 9160:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 9161:                                               .&mt('with institutional codes').')</span>';
 9162:                             }
 9163:                             $datatable .= '</td>'
 9164:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 9165:                                           .&mt('Display').'</label>&nbsp;'
 9166:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 9167:                                           .&mt('Do not display').'</label></span></td></tr>';
 9168:                         }
 9169:                     }
 9170:                 }
 9171:             } else {
 9172:                 $datatable .= &initialize_categories($itemcount);
 9173:             }
 9174:         } else {
 9175:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 9176:                           .&initialize_categories($itemcount);
 9177:         }
 9178:         $$rowtotal += $itemcount;
 9179:     }
 9180:     return $datatable;
 9181: }
 9182: 
 9183: sub print_serverstatuses {
 9184:     my ($dom,$settings,$rowtotal) = @_;
 9185:     my $datatable;
 9186:     my @pages = &serverstatus_pages();
 9187:     my (%namedaccess,%machineaccess);
 9188:     foreach my $type (@pages) {
 9189:         $namedaccess{$type} = '';
 9190:         $machineaccess{$type}= '';
 9191:     }
 9192:     if (ref($settings) eq 'HASH') {
 9193:         foreach my $type (@pages) {
 9194:             if (exists($settings->{$type})) {
 9195:                 if (ref($settings->{$type}) eq 'HASH') {
 9196:                     foreach my $key (keys(%{$settings->{$type}})) {
 9197:                         if ($key eq 'namedusers') {
 9198:                             $namedaccess{$type} = $settings->{$type}->{$key};
 9199:                         } elsif ($key eq 'machines') {
 9200:                             $machineaccess{$type} = $settings->{$type}->{$key};
 9201:                         }
 9202:                     }
 9203:                 }
 9204:             }
 9205:         }
 9206:     }
 9207:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9208:     my $rownum = 0;
 9209:     my $css_class;
 9210:     foreach my $type (@pages) {
 9211:         $rownum ++;
 9212:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 9213:         $datatable .= '<tr'.$css_class.'>'.
 9214:                       '<td><span class="LC_nobreak">'.
 9215:                       $titles->{$type}.'</span></td>'.
 9216:                       '<td class="LC_left_item">'.
 9217:                       '<input type="text" name="'.$type.'_namedusers" '.
 9218:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 9219:                       '<td class="LC_right_item">'.
 9220:                       '<span class="LC_nobreak">'.
 9221:                       '<input type="text" name="'.$type.'_machines" '.
 9222:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 9223:                       '</span></td></tr>'."\n";
 9224:     }
 9225:     $$rowtotal += $rownum;
 9226:     return $datatable;
 9227: }
 9228: 
 9229: sub serverstatus_pages {
 9230:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 9231:             'checksums','clusterstatus','certstatus','metadata_keywords',
 9232:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 9233:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 9234: }
 9235: 
 9236: sub defaults_javascript {
 9237:     my ($settings) = @_;
 9238:     return unless (ref($settings) eq 'HASH');
 9239:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 9240:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 9241:         if ($maxnum eq '') {
 9242:             $maxnum = 0;
 9243:         }
 9244:         $maxnum ++;
 9245:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 9246:         return <<"ENDSCRIPT";
 9247: <script type="text/javascript">
 9248: // <![CDATA[
 9249: function reorderTypes(form,caller) {
 9250:     var changedVal;
 9251: $jstext 
 9252:     var newpos = 'addinststatus_pos';
 9253:     var current = new Array;
 9254:     var maxh = $maxnum;
 9255:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 9256:     var oldVal;
 9257:     if (caller == newpos) {
 9258:         changedVal = newitemVal;
 9259:     } else {
 9260:         var curritem = 'inststatus_pos_'+caller;
 9261:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 9262:         current[newitemVal] = newpos;
 9263:     }
 9264:     for (var i=0; i<inststatuses.length; i++) {
 9265:         if (inststatuses[i] != caller) {
 9266:             var elementName = 'inststatus_pos_'+inststatuses[i];
 9267:             if (form.elements[elementName]) {
 9268:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 9269:                 current[currVal] = elementName;
 9270:             }
 9271:         }
 9272:     }
 9273:     for (var j=0; j<maxh; j++) {
 9274:         if (current[j] == undefined) {
 9275:             oldVal = j;
 9276:         }
 9277:     }
 9278:     if (oldVal < changedVal) {
 9279:         for (var k=oldVal+1; k<=changedVal ; k++) {
 9280:            var elementName = current[k];
 9281:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 9282:         }
 9283:     } else {
 9284:         for (var k=changedVal; k<oldVal; k++) {
 9285:             var elementName = current[k];
 9286:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 9287:         }
 9288:     }
 9289:     return;
 9290: }
 9291: 
 9292: // ]]>
 9293: </script>
 9294: 
 9295: ENDSCRIPT
 9296:     }
 9297:     return;
 9298: }
 9299: 
 9300: sub passwords_javascript {
 9301:     my %intalert = &Apache::lonlocal::texthash (
 9302:         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.',
 9303:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
 9304:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
 9305:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
 9306:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
 9307:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
 9308:     );
 9309:     &js_escape(\%intalert);
 9310:     my $defmin = $Apache::lonnet::passwdmin;
 9311:     my $intauthjs = <<"ENDSCRIPT";
 9312: 
 9313: function warnIntAuth(field) {
 9314:     if (field.name == 'intauth_check') {
 9315:         if (field.value == '2') {
 9316:             alert('$intalert{authcheck}');
 9317:         }
 9318:     }
 9319:     if (field.name == 'intauth_cost') {
 9320:         field.value.replace(/\s/g,'');
 9321:         if (field.value != '') {
 9322:             var regexdigit=/^\\d+\$/;
 9323:             if (!regexdigit.test(field.value)) {
 9324:                 alert('$intalert{authcost}');
 9325:             }
 9326:         }
 9327:     }
 9328:     return;
 9329: }
 9330: 
 9331: function warnIntPass(field) {
 9332:     field.value.replace(/^\s+/,'');
 9333:     field.value.replace(/\s+\$/,'');
 9334:     var regexdigit=/^\\d+\$/;
 9335:     if (field.name == 'passwords_min') {
 9336:         if (field.value == '') {
 9337:             alert('$intalert{passmin}');
 9338:             field.value = '$defmin';
 9339:         } else {
 9340:             if (!regexdigit.test(field.value)) {
 9341:                 alert('$intalert{passmin}');
 9342:                 field.value = '$defmin';
 9343:             }
 9344:             var minval = parseInt(field.value,10);
 9345:             if (minval < $defmin) {
 9346:                 alert('$intalert{passmin}');
 9347:                 field.value = '$defmin';
 9348:             }
 9349:         }
 9350:     } else {
 9351:         if (field.value == '0') {
 9352:             field.value = '';
 9353:         }
 9354:         if (field.value != '') {
 9355:             if (field.name == 'passwords_expire') {
 9356:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
 9357:                 if (!regexpposnum.test(field.value)) {
 9358:                     alert('$intalert{passexp}');
 9359:                     field.value = '';
 9360:                 } else {
 9361:                     var expval = parseFloat(field.value);
 9362:                     if (expval == 0) {
 9363:                         alert('$intalert{passexp}');
 9364:                         field.value = '';
 9365:                     }
 9366:                 }
 9367:             } else {
 9368:                 if (!regexdigit.test(field.value)) {
 9369:                     if (field.name == 'passwords_max') {
 9370:                         alert('$intalert{passmax}');
 9371:                     } else {
 9372:                         if (field.name == 'passwords_numsaved') {
 9373:                             alert('$intalert{passnum}');
 9374:                         }
 9375:                     }
 9376:                 }
 9377:                 field.value = '';
 9378:             }
 9379:         }
 9380:     }
 9381:     return;
 9382: }
 9383: 
 9384: ENDSCRIPT
 9385:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
 9386: }
 9387: 
 9388: sub coursecategories_javascript {
 9389:     my ($settings) = @_;
 9390:     my ($output,$jstext,$cathash);
 9391:     if (ref($settings) eq 'HASH') {
 9392:         $cathash = $settings->{'cats'};
 9393:     }
 9394:     if (ref($cathash) eq 'HASH') {
 9395:         my (@cats,@jsarray,%idx);
 9396:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 9397:         if (@jsarray > 0) {
 9398:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 9399:             for (my $i=0; $i<@jsarray; $i++) {
 9400:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 9401:                     my $catstr = join('","',@{$jsarray[$i]});
 9402:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 9403:                 }
 9404:             }
 9405:         }
 9406:     } else {
 9407:         $jstext  = '    var categories = Array(1);'."\n".
 9408:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 9409:     }
 9410:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 9411:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 9412:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 9413:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 9414:     &js_escape(\$instcode_reserved);
 9415:     &js_escape(\$communities_reserved);
 9416:     &js_escape(\$placement_reserved);
 9417:     &js_escape(\$choose_again);
 9418:     $output = <<"ENDSCRIPT";
 9419: <script type="text/javascript">
 9420: // <![CDATA[
 9421: function reorderCats(form,parent,item,idx) {
 9422:     var changedVal;
 9423: $jstext
 9424:     var newpos = 'addcategory_pos';
 9425:     if (parent == '') {
 9426:         var has_instcode = 0;
 9427:         var maxtop = categories[idx].length;
 9428:         for (var j=0; j<maxtop; j++) {
 9429:             if (categories[idx][j] == 'instcode::0') {
 9430:                 has_instcode == 1;
 9431:             }
 9432:         }
 9433:         if (has_instcode == 0) {
 9434:             categories[idx][maxtop] = 'instcode_pos';
 9435:         }
 9436:     } else {
 9437:         newpos += '_'+parent;
 9438:     }
 9439:     var maxh = 1 + categories[idx].length;
 9440:     var current = new Array;
 9441:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 9442:     if (item == newpos) {
 9443:         changedVal = newitemVal;
 9444:     } else {
 9445:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 9446:         current[newitemVal] = newpos;
 9447:     }
 9448:     for (var i=0; i<categories[idx].length; i++) {
 9449:         var elementName = categories[idx][i];
 9450:         if (elementName != item) {
 9451:             if (form.elements[elementName]) {
 9452:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 9453:                 current[currVal] = elementName;
 9454:             }
 9455:         }
 9456:     }
 9457:     var oldVal;
 9458:     for (var j=0; j<maxh; j++) {
 9459:         if (current[j] == undefined) {
 9460:             oldVal = j;
 9461:         }
 9462:     }
 9463:     if (oldVal < changedVal) {
 9464:         for (var k=oldVal+1; k<=changedVal ; k++) {
 9465:            var elementName = current[k];
 9466:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 9467:         }
 9468:     } else {
 9469:         for (var k=changedVal; k<oldVal; k++) {
 9470:             var elementName = current[k];
 9471:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 9472:         }
 9473:     }
 9474:     return;
 9475: }
 9476: 
 9477: function categoryCheck(form) {
 9478:     if (form.elements['addcategory_name'].value == 'instcode') {
 9479:         alert('$instcode_reserved\\n$choose_again');
 9480:         return false;
 9481:     }
 9482:     if (form.elements['addcategory_name'].value == 'communities') {
 9483:         alert('$communities_reserved\\n$choose_again');
 9484:         return false;
 9485:     }
 9486:     if (form.elements['addcategory_name'].value == 'placement') {
 9487:         alert('$placement_reserved\\n$choose_again');
 9488:         return false;
 9489:     }
 9490:     return true;
 9491: }
 9492: 
 9493: // ]]>
 9494: </script>
 9495: 
 9496: ENDSCRIPT
 9497:     return $output;
 9498: }
 9499: 
 9500: sub initialize_categories {
 9501:     my ($itemcount) = @_;
 9502:     my ($datatable,$css_class,$chgstr);
 9503:     my %default_names = (
 9504:                       instcode    => 'Official courses (with institutional codes)',
 9505:                       communities => 'Communities',
 9506:                       placement   => 'Placement Tests',
 9507:                         );
 9508:     my %selnum = (
 9509:                    instcode    => '0',
 9510:                    communities => '1',
 9511:                    placement   => '2',
 9512:                  );
 9513:     my %selected;
 9514:     foreach my $default ('instcode','communities','placement') {
 9515:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9516:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
 9517:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
 9518:         $selected{$selnum{$default}} = ' selected="selected"';
 9519:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 9520:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
 9521:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
 9522:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
 9523:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
 9524:                      .'<option value="3">4</option></select>&nbsp;'
 9525:                      .$default_names{$default}
 9526:                      .'</span></td><td><span class="LC_nobreak">'
 9527:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 9528:                      .&mt('Display').'</label>&nbsp;<label>'
 9529:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 9530:                  .'</label></span></td></tr>';
 9531:         $itemcount ++;
 9532:     }
 9533:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9534:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 9535:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 9536:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 9537:                   .'<option value="0">1</option>'
 9538:                   .'<option value="1">2</option>'
 9539:                   .'<option value="2">3</option>'
 9540:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
 9541:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
 9542:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
 9543:                   .'</td></tr>';
 9544:     return $datatable;
 9545: }
 9546: 
 9547: sub build_category_rows {
 9548:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 9549:     my ($text,$name,$item,$chgstr);
 9550:     if (ref($cats) eq 'ARRAY') {
 9551:         my $maxdepth = scalar(@{$cats});
 9552:         if (ref($cats->[$depth]) eq 'HASH') {
 9553:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 9554:                 my $numchildren = @{$cats->[$depth]{$parent}};
 9555:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9556:                 $text .= '<td><table class="LC_data_table">';
 9557:                 my ($idxnum,$parent_name,$parent_item);
 9558:                 my $higher = $depth - 1;
 9559:                 if ($higher == 0) {
 9560:                     $parent_name = &escape($parent).'::'.$higher;
 9561:                 } else {
 9562:                     if (ref($path) eq 'ARRAY') {
 9563:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 9564:                     }
 9565:                 }
 9566:                 $parent_item = 'addcategory_pos_'.$parent_name;
 9567:                 for (my $j=0; $j<=$numchildren; $j++) {
 9568:                     if ($j < $numchildren) {
 9569:                         $name = $cats->[$depth]{$parent}[$j];
 9570:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 9571:                         $idxnum = $idx->{$item};
 9572:                     } else {
 9573:                         $name = $parent_name;
 9574:                         $item = $parent_item;
 9575:                     }
 9576:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 9577:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 9578:                     for (my $i=0; $i<=$numchildren; $i++) {
 9579:                         my $vpos = $i+1;
 9580:                         my $selstr;
 9581:                         if ($j == $i) {
 9582:                             $selstr = ' selected="selected" ';
 9583:                         }
 9584:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 9585:                     }
 9586:                     $text .= '</select>&nbsp;';
 9587:                     if ($j < $numchildren) {
 9588:                         my $deeper = $depth+1;
 9589:                         $text .= $name.'&nbsp;'
 9590:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 9591:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 9592:                         if(ref($path) eq 'ARRAY') {
 9593:                             push(@{$path},$name);
 9594:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 9595:                             pop(@{$path});
 9596:                         }
 9597:                     } else {
 9598:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
 9599:                         if ($j == $numchildren) {
 9600:                             $text .= $name;
 9601:                         } else {
 9602:                             $text .= $item;
 9603:                         }
 9604:                         $text .= '" value="" />';
 9605:                     }
 9606:                     $text .= '</td></tr>';
 9607:                 }
 9608:                 $text .= '</table></td>';
 9609:             } else {
 9610:                 my $higher = $depth-1;
 9611:                 if ($higher == 0) {
 9612:                     $name = &escape($parent).'::'.$higher;
 9613:                 } else {
 9614:                     if (ref($path) eq 'ARRAY') {
 9615:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 9616:                     }
 9617:                 }
 9618:                 my $colspan;
 9619:                 if ($parent ne 'instcode') {
 9620:                     $colspan = $maxdepth - $depth - 1;
 9621:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
 9622:                 }
 9623:             }
 9624:         }
 9625:     }
 9626:     return $text;
 9627: }
 9628: 
 9629: sub modifiable_userdata_row {
 9630:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 9631:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
 9632:     my ($role,$rolename,$statustype);
 9633:     $role = $item;
 9634:     if ($context eq 'cancreate') {
 9635:         if ($item =~ /^(emailusername)_(.+)$/) {
 9636:             $role = $1;
 9637:             $statustype = $2;
 9638:             if (ref($usertypes) eq 'HASH') {
 9639:                 if ($usertypes->{$statustype}) {
 9640:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 9641:                 } else {
 9642:                     $rolename = &mt('Data provided by user');
 9643:                 }
 9644:             }
 9645:         }
 9646:     } elsif ($context eq 'selfcreate') {
 9647:         if (ref($usertypes) eq 'HASH') {
 9648:             $rolename = $usertypes->{$role};
 9649:         } else {
 9650:             $rolename = $role;
 9651:         }
 9652:     } elsif ($context eq 'lti') {
 9653:         $rolename = &mt('Institutional data used (if available)');
 9654:     } elsif ($context eq 'privacy') {
 9655:         $rolename = $itemdesc;
 9656:     } else {
 9657:         if ($role eq 'cr') {
 9658:             $rolename = &mt('Custom role');
 9659:         } else {
 9660:             $rolename = &Apache::lonnet::plaintext($role);
 9661:         }
 9662:     }
 9663:     my (@fields,%fieldtitles);
 9664:     if (ref($fieldsref) eq 'ARRAY') {
 9665:         @fields = @{$fieldsref};
 9666:     } else {
 9667:         @fields = ('lastname','firstname','middlename','generation',
 9668:                    'permanentemail','id');
 9669:     }
 9670:     if ((ref($titlesref) eq 'HASH')) {
 9671:         %fieldtitles = %{$titlesref};
 9672:     } else {
 9673:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9674:     }
 9675:     my $output;
 9676:     my $css_class;
 9677:     if ($rowcount%2) {
 9678:         $css_class = 'LC_odd_row';
 9679:     }
 9680:     if ($customcss) {
 9681:         $css_class .= " $customcss";
 9682:     }
 9683:     $css_class =~ s/^\s+//;
 9684:     if ($css_class) {
 9685:         $css_class = ' class="'.$css_class.'"';
 9686:     }
 9687:     if ($rowstyle) {
 9688:         $css_class .= ' style="'.$rowstyle.'"';
 9689:     }
 9690:     if ($rowid) {
 9691:         $rowid = ' id="'.$rowid.'"';
 9692:     }
 9693:     $output = '<tr '.$css_class.$rowid.'>'.
 9694:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 9695:               '<td class="LC_left_item" colspan="2"><table>';
 9696:     my $rem;
 9697:     my %checks;
 9698:     my %current;
 9699:     if (ref($settings) eq 'HASH') {
 9700:         my $hashref;
 9701:         if ($context eq 'lti') {
 9702:             if (ref($settings) eq 'HASH') {
 9703:                 $hashref = $settings->{'instdata'};
 9704:             }
 9705:         } elsif ($context eq 'privacy') {
 9706:             my ($key,$inner) = split(/_/,$role);
 9707:             if (ref($settings) eq 'HASH') {
 9708:                 if (ref($settings->{$key}) eq 'HASH') {
 9709:                     $hashref = $settings->{$key}->{$inner};
 9710:                 }
 9711:             }
 9712:         } elsif (ref($settings->{$context}) eq 'HASH') {
 9713:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 9714:                 $hashref = $settings->{'lti_instdata'};
 9715:             }
 9716:             if ($role eq 'emailusername') {
 9717:                 if ($statustype) {
 9718:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 9719:                         $hashref = $settings->{$context}->{$role}->{$statustype};
 9720:                     }
 9721:                 }
 9722:             }
 9723:         }
 9724:         if (ref($hashref) eq 'HASH') { 
 9725:             foreach my $field (@fields) {
 9726:                 if ($hashref->{$field}) {
 9727:                     if ($role eq 'emailusername') {
 9728:                         $checks{$field} = $hashref->{$field};
 9729:                     } else {
 9730:                         $checks{$field} = ' checked="checked" ';
 9731:                     }
 9732:                 }
 9733:             }
 9734:         }
 9735:     }
 9736:  
 9737:     my $total = scalar(@fields);
 9738:     for (my $i=0; $i<$total; $i++) {
 9739:         $rem = $i%($numinrow);
 9740:         if ($rem == 0) {
 9741:             if ($i > 0) {
 9742:                 $output .= '</tr>';
 9743:             }
 9744:             $output .= '<tr>';
 9745:         }
 9746:         my $check = ' ';
 9747:         unless ($role eq 'emailusername') {
 9748:             if (exists($checks{$fields[$i]})) {
 9749:                 $check = $checks{$fields[$i]};
 9750:             } elsif ($context eq 'privacy') {
 9751:                 if ($role =~ /^priv_(domain|course)$/) {
 9752:                     if (ref($settings) ne 'HASH') {
 9753:                         $check = ' checked="checked" ';
 9754:                     }
 9755:                 } elsif ($role =~ /^priv_(author|community)$/) {
 9756:                     if (ref($settings) ne 'HASH') {
 9757:                         unless ($fields[$i] eq 'id') {
 9758:                             $check = ' checked="checked" ';
 9759:                         }
 9760:                     }
 9761:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
 9762:                     if (ref($settings) ne 'HASH') {
 9763:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
 9764:                             $check = ' checked="checked" ';
 9765:                         }
 9766:                     }
 9767:                 }
 9768:             } elsif ($context ne 'lti') {
 9769:                 if ($role eq 'st') {
 9770:                     if (ref($settings) ne 'HASH') {
 9771:                         $check = ' checked="checked" '; 
 9772:                     }
 9773:                 }
 9774:             }
 9775:         }
 9776:         $output .= '<td class="LC_left_item">'.
 9777:                    '<span class="LC_nobreak">';
 9778:         my $prefix = 'canmodify';
 9779:         if ($role eq 'emailusername') {
 9780:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 9781:                 $checks{$fields[$i]} = 'omit';
 9782:             }
 9783:             foreach my $option ('required','optional','omit') {
 9784:                 my $checked='';
 9785:                 if ($checks{$fields[$i]} eq $option) {
 9786:                     $checked='checked="checked" ';
 9787:                 }
 9788:                 $output .= '<label>'.
 9789:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 9790:                            &mt($option).'</label>'.('&nbsp;' x2);
 9791:             }
 9792:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 9793:         } else {
 9794:             if ($context eq 'lti') {
 9795:                 $prefix = 'lti';
 9796:             } elsif ($context eq 'privacy') {
 9797:                 $prefix = 'privacy';
 9798:             }
 9799:             $output .= '<label>'.
 9800:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
 9801:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 9802:                        '</label>';
 9803:         }
 9804:         $output .= '</span></td>';
 9805:     }
 9806:     $rem = $total%$numinrow;
 9807:     my $colsleft;
 9808:     if ($rem) {
 9809:         $colsleft = $numinrow - $rem;
 9810:     }
 9811:     if ($colsleft > 1) {
 9812:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9813:                    '&nbsp;</td>';
 9814:     } elsif ($colsleft == 1) {
 9815:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9816:     }
 9817:     $output .= '</tr></table></td></tr>';
 9818:     return $output;
 9819: }
 9820: 
 9821: sub insttypes_row {
 9822:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 9823:         $customcss,$rowstyle) = @_;
 9824:     my %lt = &Apache::lonlocal::texthash (
 9825:                       cansearch => 'Users allowed to search',
 9826:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 9827:                       lockablenames => 'User preference to lock name',
 9828:                       selfassign    => 'Self-reportable affiliations',
 9829:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 9830:              );
 9831:     my $showdom;
 9832:     if ($context eq 'cansearch') {
 9833:         $showdom = ' ('.$dom.')';
 9834:     }
 9835:     my $class = 'LC_left_item';
 9836:     if ($context eq 'statustocreate') {
 9837:         $class = 'LC_right_item';
 9838:     }
 9839:     my $css_class;
 9840:     if ($$rowtotal%2) {
 9841:         $css_class = 'LC_odd_row';
 9842:     }
 9843:     if ($customcss) {
 9844:         $css_class .= ' '.$customcss;
 9845:     }
 9846:     $css_class =~ s/^\s+//;
 9847:     if ($css_class) {
 9848:         $css_class = ' class="'.$css_class.'"';
 9849:     }
 9850:     if ($rowstyle) {
 9851:         $css_class .= ' style="'.$rowstyle.'"';
 9852:     }
 9853:     if ($onclick) {
 9854:         $onclick = 'onclick="'.$onclick.'" ';
 9855:     }
 9856:     my $output = '<tr'.$css_class.'>'.
 9857:                  '<td>'.$lt{$context}.$showdom.
 9858:                  '</td><td class="'.$class.'" colspan="2"><table>';
 9859:     my $rem;
 9860:     if (ref($types) eq 'ARRAY') {
 9861:         for (my $i=0; $i<@{$types}; $i++) {
 9862:             if (defined($usertypes->{$types->[$i]})) {
 9863:                 my $rem = $i%($numinrow);
 9864:                 if ($rem == 0) {
 9865:                     if ($i > 0) {
 9866:                         $output .= '</tr>';
 9867:                     }
 9868:                     $output .= '<tr>';
 9869:                 }
 9870:                 my $check = ' ';
 9871:                 if (ref($settings) eq 'HASH') {
 9872:                     if (ref($settings->{$context}) eq 'ARRAY') {
 9873:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 9874:                             $check = ' checked="checked" ';
 9875:                         }
 9876:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 9877:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 9878:                             $check = ' checked="checked" ';
 9879:                         }
 9880:                     } elsif ($context eq 'statustocreate') {
 9881:                         $check = ' checked="checked" ';
 9882:                     }
 9883:                 }
 9884:                 $output .= '<td class="LC_left_item">'.
 9885:                            '<span class="LC_nobreak"><label>'.
 9886:                            '<input type="checkbox" name="'.$context.'" '.
 9887:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
 9888:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 9889:             }
 9890:         }
 9891:         $rem = @{$types}%($numinrow);
 9892:     }
 9893:     my $colsleft = $numinrow - $rem;
 9894:     if ($context eq 'overrides') {
 9895:         if ($colsleft > 1) {
 9896:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9897:         } else {
 9898:             $output .= '<td class="LC_left_item">';
 9899:         }
 9900:         $output .= '&nbsp;';  
 9901:     } else {
 9902:         if ($rem == 0) {
 9903:             $output .= '<tr>';
 9904:         }
 9905:         if ($colsleft > 1) {
 9906:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 9907:         } else {
 9908:             $output .= '<td class="LC_left_item">';
 9909:         }
 9910:         my $defcheck = ' ';
 9911:         if (ref($settings) eq 'HASH') {  
 9912:             if (ref($settings->{$context}) eq 'ARRAY') {
 9913:                 if (grep(/^default$/,@{$settings->{$context}})) {
 9914:                     $defcheck = ' checked="checked" ';
 9915:                 }
 9916:             } elsif ($context eq 'statustocreate') {
 9917:                 $defcheck = ' checked="checked" ';
 9918:             }
 9919:         }
 9920:         $output .= '<span class="LC_nobreak"><label>'.
 9921:                    '<input type="checkbox" name="'.$context.'" '.
 9922:                    'value="default"'.$defcheck.$onclick.' />'.
 9923:                    $othertitle.'</label></span>';
 9924:     }
 9925:     $output .= '</td></tr></table></td></tr>';
 9926:     return $output;
 9927: }
 9928: 
 9929: sub sorted_searchtitles {
 9930:     my %searchtitles = &Apache::lonlocal::texthash(
 9931:                          'uname' => 'username',
 9932:                          'lastname' => 'last name',
 9933:                          'lastfirst' => 'last name, first name',
 9934:                      );
 9935:     my @titleorder = ('uname','lastname','lastfirst');
 9936:     return (\%searchtitles,\@titleorder);
 9937: }
 9938: 
 9939: sub sorted_searchtypes {
 9940:     my %srchtypes_desc = (
 9941:                            exact    => 'is exact match',
 9942:                            contains => 'contains ..',
 9943:                            begins   => 'begins with ..',
 9944:                          );
 9945:     my @srchtypeorder = ('exact','begins','contains');
 9946:     return (\%srchtypes_desc,\@srchtypeorder);
 9947: }
 9948: 
 9949: sub usertype_update_row {
 9950:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 9951:     my $datatable;
 9952:     my $numinrow = 4;
 9953:     foreach my $type (@{$types}) {
 9954:         if (defined($usertypes->{$type})) {
 9955:             $$rownums ++;
 9956:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 9957:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 9958:                           '</td><td class="LC_left_item"><table>';
 9959:             for (my $i=0; $i<@{$fields}; $i++) {
 9960:                 my $rem = $i%($numinrow);
 9961:                 if ($rem == 0) {
 9962:                     if ($i > 0) {
 9963:                         $datatable .= '</tr>';
 9964:                     }
 9965:                     $datatable .= '<tr>';
 9966:                 }
 9967:                 my $check = ' ';
 9968:                 if (ref($settings) eq 'HASH') {
 9969:                     if (ref($settings->{'fields'}) eq 'HASH') {
 9970:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 9971:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 9972:                                 $check = ' checked="checked" ';
 9973:                             }
 9974:                         }
 9975:                     }
 9976:                 }
 9977: 
 9978:                 if ($i == @{$fields}-1) {
 9979:                     my $colsleft = $numinrow - $rem;
 9980:                     if ($colsleft > 1) {
 9981:                         $datatable .= '<td colspan="'.$colsleft.'">';
 9982:                     } else {
 9983:                         $datatable .= '<td>';
 9984:                     }
 9985:                 } else {
 9986:                     $datatable .= '<td>';
 9987:                 }
 9988:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9989:                               '<input type="checkbox" name="updateable_'.$type.
 9990:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 9991:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 9992:             }
 9993:             $datatable .= '</tr></table></td></tr>';
 9994:         }
 9995:     }
 9996:     return $datatable;
 9997: }
 9998: 
 9999: sub modify_login {
10000:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10001:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
10002:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
10003:     %title = ( coursecatalog => 'Display course catalog',
10004:                adminmail => 'Display administrator E-mail address',
10005:                helpdesk  => 'Display "Contact Helpdesk" link',
10006:                newuser => 'Link for visitors to create a user account',
10007:                loginheader => 'Log-in box header');
10008:     @offon = ('off','on');
10009:     if (ref($domconfig{login}) eq 'HASH') {
10010:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
10011:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
10012:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
10013:             }
10014:         }
10015:     }
10016:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
10017:                                            \%domconfig,\%loginhash);
10018:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
10019:     foreach my $item (@toggles) {
10020:         $loginhash{login}{$item} = $env{'form.'.$item};
10021:     }
10022:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
10023:     if (ref($colchanges{'login'}) eq 'HASH') {  
10024:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
10025:                                          \%loginhash);
10026:     }
10027: 
10028:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10029:     my %domservers = &Apache::lonnet::get_servers($dom);
10030:     my @loginvia_attribs = ('serverpath','custompath','exempt');
10031:     if (keys(%servers) > 1) {
10032:         foreach my $lonhost (keys(%servers)) {
10033:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
10034:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
10035:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
10036:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
10037:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
10038:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10039:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10040:                         $changes{'loginvia'}{$lonhost} = 1;
10041:                     } else {
10042:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
10043:                         $changes{'loginvia'}{$lonhost} = 1;
10044:                     }
10045:                 } else {
10046:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10047:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10048:                         $changes{'loginvia'}{$lonhost} = 1;
10049:                     }
10050:                 }
10051:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
10052:                     foreach my $item (@loginvia_attribs) {
10053:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
10054:                     }
10055:                 } else {
10056:                     foreach my $item (@loginvia_attribs) {
10057:                         my $new = $env{'form.'.$lonhost.'_'.$item};
10058:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
10059:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
10060:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
10061:                                 $new = '/';
10062:                             }
10063:                         }
10064:                         if (($item eq 'custompath') && 
10065:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
10066:                             $new = '';
10067:                         }
10068:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
10069:                             $changes{'loginvia'}{$lonhost} = 1;
10070:                         }
10071:                         if ($item eq 'exempt') {
10072:                             $new = &check_exempt_addresses($new);
10073:                         }
10074:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
10075:                     }
10076:                 }
10077:             } else {
10078:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10079:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10080:                     $changes{'loginvia'}{$lonhost} = 1;
10081:                     foreach my $item (@loginvia_attribs) {
10082:                         my $new = $env{'form.'.$lonhost.'_'.$item};
10083:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
10084:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
10085:                                 $new = '/';
10086:                             }
10087:                         }
10088:                         if (($item eq 'custompath') && 
10089:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
10090:                             $new = '';
10091:                         }
10092:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
10093:                     }
10094:                 }
10095:             }
10096:         }
10097:     }
10098: 
10099:     my $servadm = $r->dir_config('lonAdmEMail');
10100:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
10101:     if (ref($domconfig{'login'}) eq 'HASH') {
10102:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
10103:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
10104:                 if ($lang eq 'nolang') {
10105:                     push(@currlangs,$lang);
10106:                 } elsif (defined($langchoices{$lang})) {
10107:                     push(@currlangs,$lang);
10108:                 } else {
10109:                     next;
10110:                 }
10111:             }
10112:         }
10113:     }
10114:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
10115:     if (@currlangs > 0) {
10116:         foreach my $lang (@currlangs) {
10117:             if (grep(/^\Q$lang\E$/,@delurls)) {
10118:                 $changes{'helpurl'}{$lang} = 1;
10119:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
10120:                 $changes{'helpurl'}{$lang} = 1;
10121:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
10122:                 push(@newlangs,$lang);
10123:             } else {
10124:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
10125:             }
10126:         }
10127:     }
10128:     unless (grep(/^nolang$/,@currlangs)) {
10129:         if ($env{'form.loginhelpurl_nolang.filename'}) {
10130:             $changes{'helpurl'}{'nolang'} = 1;
10131:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
10132:             push(@newlangs,'nolang');
10133:         }
10134:     }
10135:     if ($env{'form.loginhelpurl_add_lang'}) {
10136:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
10137:             ($env{'form.loginhelpurl_add_file.filename'})) {
10138:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
10139:             $addedfile = $env{'form.loginhelpurl_add_lang'};
10140:         }
10141:     }
10142:     if ((@newlangs > 0) || ($addedfile)) {
10143:         my $error;
10144:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10145:         if ($configuserok eq 'ok') {
10146:             if ($switchserver) {
10147:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
10148:             } elsif ($author_ok eq 'ok') {
10149:                 my @allnew = @newlangs;
10150:                 if ($addedfile ne '') {
10151:                     push(@allnew,$addedfile);
10152:                 }
10153:                 foreach my $lang (@allnew) {
10154:                     my $formelem = 'loginhelpurl_'.$lang;
10155:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
10156:                         $formelem = 'loginhelpurl_add_file';
10157:                     }
10158:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
10159:                                                                "help/$lang",'','',$newfile{$lang});
10160:                     if ($result eq 'ok') {
10161:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
10162:                         $changes{'helpurl'}{$lang} = 1;
10163:                     } else {
10164:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
10165:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
10166:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
10167:                             (!grep(/^\Q$lang\E$/,@delurls))) {
10168:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
10169:                         }
10170:                     }
10171:                 }
10172:             } else {
10173:                 $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);
10174:             }
10175:         } else {
10176:             $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);
10177:         }
10178:         if ($error) {
10179:             &Apache::lonnet::logthis($error);
10180:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10181:         }
10182:     }
10183: 
10184:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
10185:     if (ref($domconfig{'login'}) eq 'HASH') {
10186:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
10187:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
10188:                 if ($domservers{$lonhost}) {
10189:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
10190:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
10191:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
10192:                     }
10193:                 }
10194:             }
10195:         }
10196:     }
10197:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
10198:     foreach my $lonhost (sort(keys(%domservers))) {
10199:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
10200:             $changes{'headtag'}{$lonhost} = 1;
10201:         } else {
10202:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
10203:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
10204:             }
10205:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
10206:                 push(@newhosts,$lonhost);
10207:             } elsif ($currheadtagurls{$lonhost}) {
10208:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
10209:                 if ($currexempt{$lonhost}) {
10210:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
10211:                         $changes{'headtag'}{$lonhost} = 1;
10212:                     }
10213:                 } elsif ($possexempt{$lonhost}) {
10214:                     $changes{'headtag'}{$lonhost} = 1;
10215:                 }
10216:                 if ($possexempt{$lonhost}) {
10217:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
10218:                 }
10219:             }
10220:         }
10221:     }
10222:     if (@newhosts) {
10223:         my $error;
10224:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10225:         if ($configuserok eq 'ok') {
10226:             if ($switchserver) {
10227:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
10228:             } elsif ($author_ok eq 'ok') {
10229:                 foreach my $lonhost (@newhosts) {
10230:                     my $formelem = 'loginheadtag_'.$lonhost;
10231:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
10232:                                                                           "login/headtag/$lonhost",'','',
10233:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
10234:                     if ($result eq 'ok') {
10235:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
10236:                         $changes{'headtag'}{$lonhost} = 1;
10237:                         if ($possexempt{$lonhost}) {
10238:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
10239:                         }
10240:                     } else {
10241:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
10242:                                            $newheadtagurls{$lonhost},$result);
10243:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
10244:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
10245:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
10246:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
10247:                         }
10248:                     }
10249:                 }
10250:             } else {
10251:                 $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);
10252:             }
10253:         } else {
10254:             $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);
10255:         }
10256:         if ($error) {
10257:             &Apache::lonnet::logthis($error);
10258:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10259:         }
10260:     }
10261:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
10262: 
10263:     my $defaulthelpfile = '/adm/loginproblems.html';
10264:     my $defaulttext = &mt('Default in use');
10265: 
10266:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
10267:                                              $dom);
10268:     if ($putresult eq 'ok') {
10269:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
10270:         my %defaultchecked = (
10271:                     'coursecatalog' => 'on',
10272:                     'helpdesk'      => 'on',
10273:                     'adminmail'     => 'off',
10274:                     'newuser'       => 'off',
10275:         );
10276:         if (ref($domconfig{'login'}) eq 'HASH') {
10277:             foreach my $item (@toggles) {
10278:                 if ($defaultchecked{$item} eq 'on') { 
10279:                     if (($domconfig{'login'}{$item} eq '0') &&
10280:                         ($env{'form.'.$item} eq '1')) {
10281:                         $changes{$item} = 1;
10282:                     } elsif (($domconfig{'login'}{$item} eq '' ||
10283:                               $domconfig{'login'}{$item} eq '1') &&
10284:                              ($env{'form.'.$item} eq '0')) {
10285:                         $changes{$item} = 1;
10286:                     }
10287:                 } elsif ($defaultchecked{$item} eq 'off') {
10288:                     if (($domconfig{'login'}{$item} eq '1') &&
10289:                         ($env{'form.'.$item} eq '0')) {
10290:                         $changes{$item} = 1;
10291:                     } elsif (($domconfig{'login'}{$item} eq '' ||
10292:                               $domconfig{'login'}{$item} eq '0') &&
10293:                              ($env{'form.'.$item} eq '1')) {
10294:                         $changes{$item} = 1;
10295:                     }
10296:                 }
10297:             }
10298:         }
10299:         if (keys(%changes) > 0 || $colchgtext) {
10300:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10301:             if (ref($lastactref) eq 'HASH') {
10302:                 $lastactref->{'domainconfig'} = 1;
10303:             }
10304:             $resulttext = &mt('Changes made:').'<ul>';
10305:             foreach my $item (sort(keys(%changes))) {
10306:                 if ($item eq 'loginvia') {
10307:                     if (ref($changes{$item}) eq 'HASH') {
10308:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
10309:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
10310:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
10311:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
10312:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
10313:                                     $protocol = 'http' if ($protocol ne 'https');
10314:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
10315: 
10316:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
10317:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
10318:                                     } else {
10319:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
10320:                                     }
10321:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
10322:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
10323:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
10324:                                     }
10325:                                     $resulttext .= '</li>';
10326:                                 } else {
10327:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
10328:                                 }
10329:                             } else {
10330:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
10331:                             }
10332:                         }
10333:                         $resulttext .= '</ul></li>';
10334:                     }
10335:                 } elsif ($item eq 'helpurl') {
10336:                     if (ref($changes{$item}) eq 'HASH') {
10337:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
10338:                             if (grep(/^\Q$lang\E$/,@delurls)) {
10339:                                 my ($chg,$link);
10340:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
10341:                                 if ($lang eq 'nolang') {
10342:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
10343:                                 } else {
10344:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
10345:                                 }
10346:                                 $resulttext .= '<li>'.$chg.'</li>';
10347:                             } else {
10348:                                 my $chg;
10349:                                 if ($lang eq 'nolang') {
10350:                                     $chg = &mt('custom log-in help file for no preferred language');
10351:                                 } else {
10352:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
10353:                                 }
10354:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
10355:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
10356:                                                       '?inhibitmenu=yes',$chg,600,500).
10357:                                                '</li>';
10358:                             }
10359:                         }
10360:                     }
10361:                 } elsif ($item eq 'headtag') {
10362:                     if (ref($changes{$item}) eq 'HASH') {
10363:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
10364:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
10365:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
10366:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
10367:                                 $resulttext .= '<li><a href="'.
10368:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
10369:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
10370:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
10371:                                 if ($possexempt{$lonhost}) {
10372:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
10373:                                 } else {
10374:                                     $resulttext .= &mt('included for any client IP');
10375:                                 }
10376:                                 $resulttext .= '</li>';
10377:                             }
10378:                         }
10379:                     }
10380:                 } elsif ($item eq 'captcha') {
10381:                     if (ref($loginhash{'login'}) eq 'HASH') {
10382:                         my $chgtxt;
10383:                         if ($loginhash{'login'}{$item} eq 'notused') {
10384:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
10385:                         } else {
10386:                             my %captchas = &captcha_phrases();
10387:                             if ($captchas{$loginhash{'login'}{$item}}) {
10388:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
10389:                             } else {
10390:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
10391:                             }
10392:                         }
10393:                         $resulttext .= '<li>'.$chgtxt.'</li>';
10394:                     }
10395:                 } elsif ($item eq 'recaptchakeys') {
10396:                     if (ref($loginhash{'login'}) eq 'HASH') {
10397:                         my ($privkey,$pubkey);
10398:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
10399:                             $pubkey = $loginhash{'login'}{$item}{'public'};
10400:                             $privkey = $loginhash{'login'}{$item}{'private'};
10401:                         }
10402:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
10403:                         if (!$pubkey) {
10404:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
10405:                         } else {
10406:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
10407:                         }
10408:                         if (!$privkey) {
10409:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
10410:                         } else {
10411:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
10412:                         }
10413:                         $chgtxt .= '</ul>';
10414:                         $resulttext .= '<li>'.$chgtxt.'</li>';
10415:                     }
10416:                 } elsif ($item eq 'recaptchaversion') {
10417:                     if (ref($loginhash{'login'}) eq 'HASH') {
10418:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
10419:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
10420:                                            '</li>';
10421:                         }
10422:                     }
10423:                 } else {
10424:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
10425:                 }
10426:             }
10427:             $resulttext .= $colchgtext.'</ul>';
10428:         } else {
10429:             $resulttext = &mt('No changes made to log-in page settings');
10430:         }
10431:     } else {
10432:         $resulttext = '<span class="LC_error">'.
10433: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10434:     }
10435:     if ($errors) {
10436:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
10437:                        $errors.'</ul>';
10438:     }
10439:     return $resulttext;
10440: }
10441: 
10442: sub check_exempt_addresses {
10443:     my ($iplist) = @_;
10444:     $iplist =~ s/^\s+//;
10445:     $iplist =~ s/\s+$//;
10446:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
10447:     my (@okips,$new);
10448:     foreach my $ip (@poss_ips) {
10449:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
10450:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
10451:                 push(@okips,$ip);
10452:             }
10453:         }
10454:     }
10455:     if (@okips > 0) {
10456:         $new = join(',',@okips);
10457:     } else {
10458:         $new = '';
10459:     }
10460:     return $new;
10461: }
10462: 
10463: sub color_font_choices {
10464:     my %choices =
10465:         &Apache::lonlocal::texthash (
10466:             img => "Header",
10467:             bgs => "Background colors",
10468:             links => "Link colors",
10469:             images => "Images",
10470:             font => "Font color",
10471:             fontmenu => "Font menu",
10472:             pgbg => "Page",
10473:             tabbg => "Header",
10474:             sidebg => "Border",
10475:             link => "Link",
10476:             alink => "Active link",
10477:             vlink => "Visited link",
10478:         );
10479:     return %choices;
10480: }
10481: 
10482: sub modify_rolecolors {
10483:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
10484:     my ($resulttext,%rolehash);
10485:     $rolehash{'rolecolors'} = {};
10486:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
10487:         if ($domconfig{'rolecolors'} eq '') {
10488:             $domconfig{'rolecolors'} = {};
10489:         }
10490:     }
10491:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
10492:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
10493:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
10494:                                              $dom);
10495:     if ($putresult eq 'ok') {
10496:         if (keys(%changes) > 0) {
10497:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10498:             if (ref($lastactref) eq 'HASH') {
10499:                 $lastactref->{'domainconfig'} = 1;
10500:             }
10501:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
10502:                                              $rolehash{'rolecolors'});
10503:         } else {
10504:             $resulttext = &mt('No changes made to default color schemes');
10505:         }
10506:     } else {
10507:         $resulttext = '<span class="LC_error">'.
10508: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10509:     }
10510:     if ($errors) {
10511:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10512:                        $errors.'</ul>';
10513:     }
10514:     return $resulttext;
10515: }
10516: 
10517: sub modify_colors {
10518:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
10519:     my (%changes,%choices);
10520:     my @bgs;
10521:     my @links = ('link','alink','vlink');
10522:     my @logintext;
10523:     my @images;
10524:     my $servadm = $r->dir_config('lonAdmEMail');
10525:     my $errors;
10526:     my %defaults;
10527:     foreach my $role (@{$roles}) {
10528:         if ($role eq 'login') {
10529:             %choices = &login_choices();
10530:             @logintext = ('textcol','bgcol');
10531:         } else {
10532:             %choices = &color_font_choices();
10533:         }
10534:         if ($role eq 'login') {
10535:             @images = ('img','logo','domlogo','login');
10536:             @bgs = ('pgbg','mainbg','sidebg');
10537:         } else {
10538:             @images = ('img');
10539:             @bgs = ('pgbg','tabbg','sidebg');
10540:         }
10541:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
10542:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
10543:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
10544:         }
10545:         if ($role eq 'login') {
10546:             foreach my $item (@logintext) {
10547:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
10548:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
10549:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
10550:                 }
10551:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
10552:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
10553:                 }
10554:             }
10555:         } else {
10556:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
10557:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
10558:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
10559:             }
10560:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
10561:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
10562:             }
10563:         }
10564:         foreach my $item (@bgs) {
10565:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
10566:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
10567:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
10568:             }
10569:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
10570:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
10571:             }
10572:         }
10573:         foreach my $item (@links) {
10574:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
10575:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
10576:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
10577:             }
10578:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
10579:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
10580:             }
10581:         }
10582:         my ($configuserok,$author_ok,$switchserver) = 
10583:             &config_check($dom,$confname,$servadm);
10584:         my ($width,$height) = &thumb_dimensions();
10585:         if (ref($domconfig->{$role}) ne 'HASH') {
10586:             $domconfig->{$role} = {};
10587:         }
10588:         foreach my $img (@images) {
10589:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
10590:                 if (defined($env{'form.login_showlogo_'.$img})) {
10591:                     $confhash->{$role}{'showlogo'}{$img} = 1;
10592:                 } else { 
10593:                     $confhash->{$role}{'showlogo'}{$img} = 0;
10594:                 }
10595:             } 
10596: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
10597: 		 && !defined($domconfig->{$role}{$img})
10598: 		 && !$env{'form.'.$role.'_del_'.$img}
10599: 		 && $env{'form.'.$role.'_import_'.$img}) {
10600: 		# import the old configured image from the .tab setting
10601: 		# if they haven't provided a new one 
10602: 		$domconfig->{$role}{$img} = 
10603: 		    $env{'form.'.$role.'_import_'.$img};
10604: 	    }
10605:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
10606:                 my $error;
10607:                 if ($configuserok eq 'ok') {
10608:                     if ($switchserver) {
10609:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
10610:                     } else {
10611:                         if ($author_ok eq 'ok') {
10612:                             my ($result,$logourl) = 
10613:                                 &publishlogo($r,'upload',$role.'_'.$img,
10614:                                            $dom,$confname,$img,$width,$height);
10615:                             if ($result eq 'ok') {
10616:                                 $confhash->{$role}{$img} = $logourl;
10617:                                 $changes{$role}{'images'}{$img} = 1;
10618:                             } else {
10619:                                 $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);
10620:                             }
10621:                         } else {
10622:                             $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);
10623:                         }
10624:                     }
10625:                 } else {
10626:                     $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);
10627:                 }
10628:                 if ($error) {
10629:                     &Apache::lonnet::logthis($error);
10630:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10631:                 }
10632:             } elsif ($domconfig->{$role}{$img} ne '') {
10633:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
10634:                     my $error;
10635:                     if ($configuserok eq 'ok') {
10636: # is confname an author?
10637:                         if ($switchserver eq '') {
10638:                             if ($author_ok eq 'ok') {
10639:                                 my ($result,$logourl) = 
10640:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
10641:                                             $dom,$confname,$img,$width,$height);
10642:                                 if ($result eq 'ok') {
10643:                                     $confhash->{$role}{$img} = $logourl;
10644: 				    $changes{$role}{'images'}{$img} = 1;
10645:                                 }
10646:                             }
10647:                         }
10648:                     }
10649:                 }
10650:             }
10651:         }
10652:         if (ref($domconfig) eq 'HASH') {
10653:             if (ref($domconfig->{$role}) eq 'HASH') {
10654:                 foreach my $img (@images) {
10655:                     if ($domconfig->{$role}{$img} ne '') {
10656:                         if ($env{'form.'.$role.'_del_'.$img}) {
10657:                             $confhash->{$role}{$img} = '';
10658:                             $changes{$role}{'images'}{$img} = 1;
10659:                         } else {
10660:                             if ($confhash->{$role}{$img} eq '') {
10661:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
10662:                             }
10663:                         }
10664:                     } else {
10665:                         if ($env{'form.'.$role.'_del_'.$img}) {
10666:                             $confhash->{$role}{$img} = '';
10667:                             $changes{$role}{'images'}{$img} = 1;
10668:                         } 
10669:                     }
10670:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
10671:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
10672:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
10673:                                 $domconfig->{$role}{'showlogo'}{$img}) {
10674:                                 $changes{$role}{'showlogo'}{$img} = 1; 
10675:                             }
10676:                         } else {
10677:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
10678:                                 $changes{$role}{'showlogo'}{$img} = 1;
10679:                             }
10680:                         }
10681:                     }
10682:                 }
10683:                 if ($domconfig->{$role}{'font'} ne '') {
10684:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
10685:                         $changes{$role}{'font'} = 1;
10686:                     }
10687:                 } else {
10688:                     if ($confhash->{$role}{'font'}) {
10689:                         $changes{$role}{'font'} = 1;
10690:                     }
10691:                 }
10692:                 if ($role ne 'login') {
10693:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
10694:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
10695:                             $changes{$role}{'fontmenu'} = 1;
10696:                         }
10697:                     } else {
10698:                         if ($confhash->{$role}{'fontmenu'}) {
10699:                             $changes{$role}{'fontmenu'} = 1;
10700:                         }
10701:                     }
10702:                 }
10703:                 foreach my $item (@bgs) {
10704:                     if ($domconfig->{$role}{$item} ne '') {
10705:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10706:                             $changes{$role}{'bgs'}{$item} = 1;
10707:                         } 
10708:                     } else {
10709:                         if ($confhash->{$role}{$item}) {
10710:                             $changes{$role}{'bgs'}{$item} = 1;
10711:                         }
10712:                     }
10713:                 }
10714:                 foreach my $item (@links) {
10715:                     if ($domconfig->{$role}{$item} ne '') {
10716:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10717:                             $changes{$role}{'links'}{$item} = 1;
10718:                         }
10719:                     } else {
10720:                         if ($confhash->{$role}{$item}) {
10721:                             $changes{$role}{'links'}{$item} = 1;
10722:                         }
10723:                     }
10724:                 }
10725:                 foreach my $item (@logintext) {
10726:                     if ($domconfig->{$role}{$item} ne '') {
10727:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
10728:                             $changes{$role}{'logintext'}{$item} = 1;
10729:                         }
10730:                     } else {
10731:                         if ($confhash->{$role}{$item}) {
10732:                             $changes{$role}{'logintext'}{$item} = 1;
10733:                         }
10734:                     }
10735:                 }
10736:             } else {
10737:                 &default_change_checker($role,\@images,\@links,\@bgs,
10738:                                         \@logintext,$confhash,\%changes); 
10739:             }
10740:         } else {
10741:             &default_change_checker($role,\@images,\@links,\@bgs,
10742:                                     \@logintext,$confhash,\%changes); 
10743:         }
10744:     }
10745:     return ($errors,%changes);
10746: }
10747: 
10748: sub config_check {
10749:     my ($dom,$confname,$servadm) = @_;
10750:     my ($configuserok,$author_ok,$switchserver,%currroles);
10751:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
10752:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
10753:                                                    $confname,$servadm);
10754:     if ($configuserok eq 'ok') {
10755:         $switchserver = &check_switchserver($dom,$confname);
10756:         if ($switchserver eq '') {
10757:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
10758:         }
10759:     }
10760:     return ($configuserok,$author_ok,$switchserver);
10761: }
10762: 
10763: sub default_change_checker {
10764:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
10765:     foreach my $item (@{$links}) {
10766:         if ($confhash->{$role}{$item}) {
10767:             $changes->{$role}{'links'}{$item} = 1;
10768:         }
10769:     }
10770:     foreach my $item (@{$bgs}) {
10771:         if ($confhash->{$role}{$item}) {
10772:             $changes->{$role}{'bgs'}{$item} = 1;
10773:         }
10774:     }
10775:     foreach my $item (@{$logintext}) {
10776:         if ($confhash->{$role}{$item}) {
10777:             $changes->{$role}{'logintext'}{$item} = 1;
10778:         }
10779:     }
10780:     foreach my $img (@{$images}) {
10781:         if ($env{'form.'.$role.'_del_'.$img}) {
10782:             $confhash->{$role}{$img} = '';
10783:             $changes->{$role}{'images'}{$img} = 1;
10784:         }
10785:         if ($role eq 'login') {
10786:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
10787:                 $changes->{$role}{'showlogo'}{$img} = 1;
10788:             }
10789:         }
10790:     }
10791:     if ($confhash->{$role}{'font'}) {
10792:         $changes->{$role}{'font'} = 1;
10793:     }
10794: }
10795: 
10796: sub display_colorchgs {
10797:     my ($dom,$changes,$roles,$confhash) = @_;
10798:     my (%choices,$resulttext);
10799:     if (!grep(/^login$/,@{$roles})) {
10800:         $resulttext = &mt('Changes made:').'<br />';
10801:     }
10802:     foreach my $role (@{$roles}) {
10803:         if ($role eq 'login') {
10804:             %choices = &login_choices();
10805:         } else {
10806:             %choices = &color_font_choices();
10807:         }
10808:         if (ref($changes->{$role}) eq 'HASH') {
10809:             if ($role ne 'login') {
10810:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
10811:             }
10812:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
10813:                 if ($role ne 'login') {
10814:                     $resulttext .= '<ul>';
10815:                 }
10816:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
10817:                     if ($role ne 'login') {
10818:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
10819:                     }
10820:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
10821:                         if (($role eq 'login') && ($key eq 'showlogo')) {
10822:                             if ($confhash->{$role}{$key}{$item}) {
10823:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
10824:                             } else {
10825:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
10826:                             }
10827:                         } elsif ($confhash->{$role}{$item} eq '') {
10828:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
10829:                         } else {
10830:                             my $newitem = $confhash->{$role}{$item};
10831:                             if ($key eq 'images') {
10832:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
10833:                             }
10834:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
10835:                         }
10836:                     }
10837:                     if ($role ne 'login') {
10838:                         $resulttext .= '</ul></li>';
10839:                     }
10840:                 } else {
10841:                     if ($confhash->{$role}{$key} eq '') {
10842:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
10843:                     } else {
10844:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
10845:                     }
10846:                 }
10847:                 if ($role ne 'login') {
10848:                     $resulttext .= '</ul>';
10849:                 }
10850:             }
10851:         }
10852:     }
10853:     return $resulttext;
10854: }
10855: 
10856: sub thumb_dimensions {
10857:     return ('200','50');
10858: }
10859: 
10860: sub check_dimensions {
10861:     my ($inputfile) = @_;
10862:     my ($fullwidth,$fullheight);
10863:     if ($inputfile =~ m|^[/\w.\-]+$|) {
10864:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
10865:             my $imageinfo = <PIPE>;
10866:             if (!close(PIPE)) {
10867:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
10868:             }
10869:             chomp($imageinfo);
10870:             my ($fullsize) = 
10871:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
10872:             if ($fullsize) {
10873:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
10874:             }
10875:         }
10876:     }
10877:     return ($fullwidth,$fullheight);
10878: }
10879: 
10880: sub check_configuser {
10881:     my ($uhome,$dom,$confname,$servadm) = @_;
10882:     my ($configuserok,%currroles);
10883:     if ($uhome eq 'no_host') {
10884:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
10885:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
10886:         $configuserok = 
10887:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
10888:                              $configpass,'','','','','',undef,$servadm);
10889:     } else {
10890:         $configuserok = 'ok';
10891:         %currroles = 
10892:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
10893:     }
10894:     return ($configuserok,%currroles);
10895: }
10896: 
10897: sub check_authorstatus {
10898:     my ($dom,$confname,%currroles) = @_;
10899:     my $author_ok;
10900:     if (!$currroles{':'.$dom.':au'}) {
10901:         my $start = time;
10902:         my $end = 0;
10903:         $author_ok = 
10904:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
10905:                                         'au',$end,$start,'','','domconfig');
10906:     } else {
10907:         $author_ok = 'ok';
10908:     }
10909:     return $author_ok;
10910: }
10911: 
10912: sub publishlogo {
10913:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
10914:     my ($output,$fname,$logourl,$madethumb);
10915:     if ($action eq 'upload') {
10916:         $fname=$env{'form.'.$formname.'.filename'};
10917:         chop($env{'form.'.$formname});
10918:     } else {
10919:         ($fname) = ($formname =~ /([^\/]+)$/);
10920:     }
10921:     if ($savefileas ne '') {
10922:         $fname = $savefileas;
10923:     }
10924:     $fname=&Apache::lonnet::clean_filename($fname);
10925: # See if there is anything left
10926:     unless ($fname) { return ('error: no uploaded file'); }
10927:     $fname="$subdir/$fname";
10928:     my $docroot=$r->dir_config('lonDocRoot');
10929:     my $filepath="$docroot/priv";
10930:     my $relpath = "$dom/$confname";
10931:     my ($fnamepath,$file,$fetchthumb);
10932:     $file=$fname;
10933:     if ($fname=~m|/|) {
10934:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
10935:     }
10936:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
10937:     my $count;
10938:     for ($count=5;$count<=$#parts;$count++) {
10939:         $filepath.="/$parts[$count]";
10940:         if ((-e $filepath)!=1) {
10941:             mkdir($filepath,02770);
10942:         }
10943:     }
10944:     # Check for bad extension and disallow upload
10945:     if ($file=~/\.(\w+)$/ &&
10946:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
10947:         $output = 
10948:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
10949:     } elsif ($file=~/\.(\w+)$/ &&
10950:         !defined(&Apache::loncommon::fileembstyle($1))) {
10951:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
10952:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
10953:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
10954:     } elsif (-d "$filepath/$file") {
10955:         $output = &mt('Filename is a directory name - rename the file and re-upload');
10956:     } else {
10957:         my $source = $filepath.'/'.$file;
10958:         my $logfile;
10959:         if (!open($logfile,">>",$source.'.log')) {
10960:             return (&mt('No write permission to Authoring Space'));
10961:         }
10962:         print $logfile
10963: "\n================= Publish ".localtime()." ================\n".
10964: $env{'user.name'}.':'.$env{'user.domain'}."\n";
10965: # Save the file
10966:         if (!open(FH,">",$source)) {
10967:             &Apache::lonnet::logthis('Failed to create '.$source);
10968:             return (&mt('Failed to create file'));
10969:         }
10970:         if ($action eq 'upload') {
10971:             if (!print FH ($env{'form.'.$formname})) {
10972:                 &Apache::lonnet::logthis('Failed to write to '.$source);
10973:                 return (&mt('Failed to write file'));
10974:             }
10975:         } else {
10976:             my $original = &Apache::lonnet::filelocation('',$formname);
10977:             if(!copy($original,$source)) {
10978:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
10979:                 return (&mt('Failed to write file'));
10980:             }
10981:         }
10982:         close(FH);
10983:         chmod(0660, $source); # Permissions to rw-rw---.
10984: 
10985:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
10986:         my $copyfile=$targetdir.'/'.$file;
10987: 
10988:         my @parts=split(/\//,$targetdir);
10989:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
10990:         for (my $count=5;$count<=$#parts;$count++) {
10991:             $path.="/$parts[$count]";
10992:             if (!-e $path) {
10993:                 print $logfile "\nCreating directory ".$path;
10994:                 mkdir($path,02770);
10995:             }
10996:         }
10997:         my $versionresult;
10998:         if (-e $copyfile) {
10999:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
11000:         } else {
11001:             $versionresult = 'ok';
11002:         }
11003:         if ($versionresult eq 'ok') {
11004:             if (copy($source,$copyfile)) {
11005:                 print $logfile "\nCopied original source to ".$copyfile."\n";
11006:                 $output = 'ok';
11007:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
11008:                 push(@{$modified_urls},[$copyfile,$source]);
11009:                 my $metaoutput = 
11010:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
11011:                 unless ($registered_cleanup) {
11012:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
11013:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
11014:                     $registered_cleanup=1;
11015:                 }
11016:             } else {
11017:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
11018:                 $output = &mt('Failed to copy file to RES space').", $!";
11019:             }
11020:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
11021:                 my $inputfile = $filepath.'/'.$file;
11022:                 my $outfile = $filepath.'/'.'tn-'.$file;
11023:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
11024:                 if ($fullwidth ne '' && $fullheight ne '') { 
11025:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
11026:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
11027:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
11028:                         system({$args[0]} @args);
11029:                         chmod(0660, $filepath.'/tn-'.$file);
11030:                         if (-e $outfile) {
11031:                             my $copyfile=$targetdir.'/tn-'.$file;
11032:                             if (copy($outfile,$copyfile)) {
11033:                                 print $logfile "\nCopied source to ".$copyfile."\n";
11034:                                 my $thumb_metaoutput = 
11035:                                     &write_metadata($dom,$confname,$formname,
11036:                                                     $targetdir,'tn-'.$file,$logfile);
11037:                                 push(@{$modified_urls},[$copyfile,$outfile]);
11038:                                 unless ($registered_cleanup) {
11039:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
11040:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
11041:                                     $registered_cleanup=1;
11042:                                 }
11043:                                 $madethumb = 1;
11044:                             } else {
11045:                                 print $logfile "\nUnable to write ".$copyfile.
11046:                                                ':'.$!."\n";
11047:                             }
11048:                         }
11049:                     }
11050:                 }
11051:             }
11052:         } else {
11053:             $output = $versionresult;
11054:         }
11055:     }
11056:     return ($output,$logourl,$madethumb);
11057: }
11058: 
11059: sub logo_versioning {
11060:     my ($targetdir,$file,$logfile) = @_;
11061:     my $target = $targetdir.'/'.$file;
11062:     my ($maxversion,$fn,$extn,$output);
11063:     $maxversion = 0;
11064:     if ($file =~ /^(.+)\.(\w+)$/) {
11065:         $fn=$1;
11066:         $extn=$2;
11067:     }
11068:     opendir(DIR,$targetdir);
11069:     while (my $filename=readdir(DIR)) {
11070:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
11071:             $maxversion=($1>$maxversion)?$1:$maxversion;
11072:         }
11073:     }
11074:     $maxversion++;
11075:     print $logfile "\nCreating old version ".$maxversion."\n";
11076:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
11077:     if (copy($target,$copyfile)) {
11078:         print $logfile "Copied old target to ".$copyfile."\n";
11079:         $copyfile=$copyfile.'.meta';
11080:         if (copy($target.'.meta',$copyfile)) {
11081:             print $logfile "Copied old target metadata to ".$copyfile."\n";
11082:             $output = 'ok';
11083:         } else {
11084:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
11085:             $output = &mt('Failed to copy old meta').", $!, ";
11086:         }
11087:     } else {
11088:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
11089:         $output = &mt('Failed to copy old target').", $!, ";
11090:     }
11091:     return $output;
11092: }
11093: 
11094: sub write_metadata {
11095:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
11096:     my (%metadatafields,%metadatakeys,$output);
11097:     $metadatafields{'title'}=$formname;
11098:     $metadatafields{'creationdate'}=time;
11099:     $metadatafields{'lastrevisiondate'}=time;
11100:     $metadatafields{'copyright'}='public';
11101:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
11102:                                          $env{'user.domain'};
11103:     $metadatafields{'authorspace'}=$confname.':'.$dom;
11104:     $metadatafields{'domain'}=$dom;
11105:     {
11106:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
11107:         my $mfh;
11108:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
11109:             foreach (sort(keys(%metadatafields))) {
11110:                 unless ($_=~/\./) {
11111:                     my $unikey=$_;
11112:                     $unikey=~/^([A-Za-z]+)/;
11113:                     my $tag=$1;
11114:                     $tag=~tr/A-Z/a-z/;
11115:                     print $mfh "\n\<$tag";
11116:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
11117:                         my $value=$metadatafields{$unikey.'.'.$_};
11118:                         $value=~s/\"/\'\'/g;
11119:                         print $mfh ' '.$_.'="'.$value.'"';
11120:                     }
11121:                     print $mfh '>'.
11122:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
11123:                             .'</'.$tag.'>';
11124:                 }
11125:             }
11126:             $output = 'ok';
11127:             print $logfile "\nWrote metadata";
11128:             close($mfh);
11129:         } else {
11130:             print $logfile "\nFailed to open metadata file";
11131:             $output = &mt('Could not write metadata');
11132:         }
11133:     }
11134:     return $output;
11135: }
11136: 
11137: sub notifysubscribed {
11138:     foreach my $targetsource (@{$modified_urls}){
11139:         next unless (ref($targetsource) eq 'ARRAY');
11140:         my ($target,$source)=@{$targetsource};
11141:         if ($source ne '') {
11142:             if (open(my $logfh,">>",$source.'.log')) {
11143:                 print $logfh "\nCleanup phase: Notifications\n";
11144:                 my @subscribed=&subscribed_hosts($target);
11145:                 foreach my $subhost (@subscribed) {
11146:                     print $logfh "\nNotifying host ".$subhost.':';
11147:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
11148:                     print $logfh $reply;
11149:                 }
11150:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
11151:                 foreach my $subhost (@subscribedmeta) {
11152:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
11153:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
11154:                                                         $subhost);
11155:                     print $logfh $reply;
11156:                 }
11157:                 print $logfh "\n============ Done ============\n";
11158:                 close($logfh);
11159:             }
11160:         }
11161:     }
11162:     return OK;
11163: }
11164: 
11165: sub subscribed_hosts {
11166:     my ($target) = @_;
11167:     my @subscribed;
11168:     if (open(my $fh,"<","$target.subscription")) {
11169:         while (my $subline=<$fh>) {
11170:             if ($subline =~ /^($match_lonid):/) {
11171:                 my $host = $1;
11172:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
11173:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
11174:                         push(@subscribed,$host);
11175:                     }
11176:                 }
11177:             }
11178:         }
11179:     }
11180:     return @subscribed;
11181: }
11182: 
11183: sub check_switchserver {
11184:     my ($dom,$confname) = @_;
11185:     my ($allowed,$switchserver);
11186:     my $home = &Apache::lonnet::homeserver($confname,$dom);
11187:     if ($home eq 'no_host') {
11188:         $home = &Apache::lonnet::domain($dom,'primary');
11189:     }
11190:     my @ids=&Apache::lonnet::current_machine_ids();
11191:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
11192:     if (!$allowed) {
11193: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
11194:     }
11195:     return $switchserver;
11196: }
11197: 
11198: sub modify_quotas {
11199:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11200:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
11201:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
11202:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
11203:         $validationfieldsref);
11204:     if ($action eq 'quotas') {
11205:         $context = 'tools'; 
11206:     } else {
11207:         $context = $action;
11208:     }
11209:     if ($context eq 'requestcourses') {
11210:         @usertools = ('official','unofficial','community','textbook','placement','lti');
11211:         @options =('norequest','approval','validate','autolimit');
11212:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
11213:         %titles = &courserequest_titles();
11214:         $toolregexp = join('|',@usertools);
11215:         %conditions = &courserequest_conditions();
11216:         $confname = $dom.'-domainconfig';
11217:         my $servadm = $r->dir_config('lonAdmEMail');
11218:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11219:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
11220:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
11221:     } elsif ($context eq 'requestauthor') {
11222:         @usertools = ('author');
11223:         %titles = &authorrequest_titles();
11224:     } else {
11225:         @usertools = ('aboutme','blog','webdav','portfolio');
11226:         %titles = &tool_titles();
11227:     }
11228:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11229:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11230:     foreach my $key (keys(%env)) {
11231:         if ($context eq 'requestcourses') {
11232:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
11233:                 my $item = $1;
11234:                 my $type = $2;
11235:                 if ($type =~ /^limit_(.+)/) {
11236:                     $limithash{$item}{$1} = $env{$key};
11237:                 } else {
11238:                     $confhash{$item}{$type} = $env{$key};
11239:                 }
11240:             }
11241:         } elsif ($context eq 'requestauthor') {
11242:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
11243:                 $confhash{$1} = $env{$key};
11244:             }
11245:         } else {
11246:             if ($key =~ /^form\.quota_(.+)$/) {
11247:                 $confhash{'defaultquota'}{$1} = $env{$key};
11248:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
11249:                 $confhash{'authorquota'}{$1} = $env{$key};
11250:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
11251:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
11252:             }
11253:         }
11254:     }
11255:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
11256:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
11257:         @approvalnotify = sort(@approvalnotify);
11258:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
11259:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
11260:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
11261:         foreach my $type (@hasuniquecode) {
11262:             if (grep(/^\Q$type\E$/,@crstypes)) {
11263:                 $confhash{'uniquecode'}{$type} = 1;
11264:             }
11265:         }
11266:         my (%newbook,%allpos);
11267:         if ($context eq 'requestcourses') {
11268:             foreach my $type ('textbooks','templates') {
11269:                 @{$allpos{$type}} = (); 
11270:                 my $invalid;
11271:                 if ($type eq 'textbooks') {
11272:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
11273:                 } else {
11274:                     $invalid = &mt('Invalid LON-CAPA course for template');
11275:                 }
11276:                 if ($env{'form.'.$type.'_addbook'}) {
11277:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
11278:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
11279:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
11280:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
11281:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
11282:                         } else {
11283:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
11284:                             my $position = $env{'form.'.$type.'_addbook_pos'};
11285:                             $position =~ s/\D+//g;
11286:                             if ($position ne '') {
11287:                                 $allpos{$type}[$position] = $newbook{$type};
11288:                             }
11289:                         }
11290:                     } else {
11291:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
11292:                     }
11293:                 }
11294:             } 
11295:         }
11296:         if (ref($domconfig{$action}) eq 'HASH') {
11297:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
11298:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
11299:                     $changes{'notify'}{'approval'} = 1;
11300:                 }
11301:             } else {
11302:                 if ($confhash{'notify'}{'approval'}) {
11303:                     $changes{'notify'}{'approval'} = 1;
11304:                 }
11305:             }
11306:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
11307:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
11308:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
11309:                         unless ($confhash{'uniquecode'}{$crstype}) {
11310:                             $changes{'uniquecode'} = 1;
11311:                         }
11312:                     }
11313:                     unless ($changes{'uniquecode'}) {
11314:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
11315:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
11316:                                 $changes{'uniquecode'} = 1;
11317:                             }
11318:                         }
11319:                     }
11320:                } else {
11321:                    $changes{'uniquecode'} = 1;
11322:                }
11323:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
11324:                 $changes{'uniquecode'} = 1;
11325:             }
11326:             if ($context eq 'requestcourses') {
11327:                 foreach my $type ('textbooks','templates') {
11328:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
11329:                         my %deletions;
11330:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
11331:                         if (@todelete) {
11332:                             map { $deletions{$_} = 1; } @todelete;
11333:                         }
11334:                         my %imgdeletions;
11335:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
11336:                         if (@todeleteimages) {
11337:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
11338:                         }
11339:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
11340:                         for (my $i=0; $i<=$maxnum; $i++) {
11341:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
11342:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
11343:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
11344:                                 if ($deletions{$key}) {
11345:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
11346:                                         #FIXME need to obsolete item in RES space
11347:                                     }
11348:                                     next;
11349:                                 } else {
11350:                                     my $newpos = $env{'form.'.$itemid};
11351:                                     $newpos =~ s/\D+//g;
11352:                                     foreach my $item ('subject','title','publisher','author') {
11353:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
11354:                                                  ($type eq 'templates'));
11355:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
11356:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
11357:                                             $changes{$type}{$key} = 1;
11358:                                         }
11359:                                     }
11360:                                     $allpos{$type}[$newpos] = $key;
11361:                                 }
11362:                                 if ($imgdeletions{$key}) {
11363:                                     $changes{$type}{$key} = 1;
11364:                                     #FIXME need to obsolete item in RES space
11365:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
11366:                                     my ($cdom,$cnum) = split(/_/,$key);
11367:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
11368:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
11369:                                     } else {
11370:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
11371:                                                                                       $cdom,$cnum,$type,$configuserok,
11372:                                                                                       $switchserver,$author_ok);
11373:                                         if ($imgurl) {
11374:                                             $confhash{$type}{$key}{'image'} = $imgurl;
11375:                                             $changes{$type}{$key} = 1; 
11376:                                         }
11377:                                         if ($error) {
11378:                                             &Apache::lonnet::logthis($error);
11379:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11380:                                         }
11381:                                     }
11382:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
11383:                                     $confhash{$type}{$key}{'image'} = 
11384:                                         $domconfig{$action}{$type}{$key}{'image'};
11385:                                 }
11386:                             }
11387:                         }
11388:                     }
11389:                 }
11390:             }
11391:         } else {
11392:             if ($confhash{'notify'}{'approval'}) {
11393:                 $changes{'notify'}{'approval'} = 1;
11394:             }
11395:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
11396:                 $changes{'uniquecode'} = 1;
11397:             }
11398:         }
11399:         if ($context eq 'requestcourses') {
11400:             foreach my $type ('textbooks','templates') {
11401:                 if ($newbook{$type}) {
11402:                     $changes{$type}{$newbook{$type}} = 1;
11403:                     foreach my $item ('subject','title','publisher','author') {
11404:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
11405:                                  ($type eq 'template'));
11406:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
11407:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
11408:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
11409:                         }
11410:                     }
11411:                     if ($type eq 'textbooks') {
11412:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
11413:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
11414:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
11415:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
11416:                             } else {
11417:                                 my ($imageurl,$error) =
11418:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
11419:                                                             $configuserok,$switchserver,$author_ok);
11420:                                 if ($imageurl) {
11421:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
11422:                                 }
11423:                                 if ($error) {
11424:                                     &Apache::lonnet::logthis($error);
11425:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11426:                                 }
11427:                             }
11428:                         }
11429:                     }
11430:                 }
11431:                 if (@{$allpos{$type}} > 0) {
11432:                     my $idx = 0;
11433:                     foreach my $item (@{$allpos{$type}}) {
11434:                         if ($item ne '') {
11435:                             $confhash{$type}{$item}{'order'} = $idx;
11436:                             if (ref($domconfig{$action}) eq 'HASH') {
11437:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
11438:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
11439:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
11440:                                             $changes{$type}{$item} = 1;
11441:                                         }
11442:                                     }
11443:                                 }
11444:                             }
11445:                             $idx ++;
11446:                         }
11447:                     }
11448:                 }
11449:             }
11450:             if (ref($validationitemsref) eq 'ARRAY') {
11451:                 foreach my $item (@{$validationitemsref}) {
11452:                     if ($item eq 'fields') {
11453:                         my @changed;
11454:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
11455:                         if (@{$confhash{'validation'}{$item}} > 0) {
11456:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
11457:                         }
11458:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11459:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11460:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
11461:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
11462:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
11463:                                 } else {
11464:                                     @changed = @{$confhash{'validation'}{$item}};
11465:                                 }
11466:                             } else {
11467:                                 @changed = @{$confhash{'validation'}{$item}};
11468:                             }
11469:                         } else {
11470:                             @changed = @{$confhash{'validation'}{$item}};
11471:                         }
11472:                         if (@changed) {
11473:                             if ($confhash{'validation'}{$item}) {
11474:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
11475:                             } else {
11476:                                 $changes{'validation'}{$item} = &mt('None');
11477:                             }
11478:                         }
11479:                     } else {
11480:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
11481:                         if ($item eq 'markup') {
11482:                             if ($env{'form.requestcourses_validation_'.$item}) {
11483:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
11484:                             }
11485:                         }
11486:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11487:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11488:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
11489:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
11490:                                 }
11491:                             } else {
11492:                                 if ($confhash{'validation'}{$item} ne '') {
11493:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
11494:                                 }
11495:                             }
11496:                         } else {
11497:                             if ($confhash{'validation'}{$item} ne '') {
11498:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
11499:                             }
11500:                         }
11501:                     }
11502:                 }
11503:             }
11504:             if ($env{'form.validationdc'}) {
11505:                 my $newval = $env{'form.validationdc'};
11506:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
11507:                 if (exists($domcoords{$newval})) {
11508:                     $confhash{'validation'}{'dc'} = $newval;
11509:                 }
11510:             }
11511:             if (ref($confhash{'validation'}) eq 'HASH') {
11512:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11513:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11514:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
11515:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
11516:                                 if ($confhash{'validation'}{'dc'} eq '') {
11517:                                     $changes{'validation'}{'dc'} = &mt('None');
11518:                                 } else {
11519:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11520:                                 }
11521:                             }
11522:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
11523:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11524:                         }
11525:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
11526:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11527:                     }
11528:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
11529:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
11530:                 }
11531:             } else {
11532:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
11533:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
11534:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
11535:                             $changes{'validation'}{'dc'} = &mt('None');
11536:                         }
11537:                     }
11538:                 }
11539:             }
11540:         }
11541:     } else {
11542:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
11543:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
11544:     }
11545:     foreach my $item (@usertools) {
11546:         foreach my $type (@{$types},'default','_LC_adv') {
11547:             my $unset; 
11548:             if ($context eq 'requestcourses') {
11549:                 $unset = '0';
11550:                 if ($type eq '_LC_adv') {
11551:                     $unset = '';
11552:                 }
11553:                 if ($confhash{$item}{$type} eq 'autolimit') {
11554:                     $confhash{$item}{$type} .= '=';
11555:                     unless ($limithash{$item}{$type} =~ /\D/) {
11556:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
11557:                     }
11558:                 }
11559:             } elsif ($context eq 'requestauthor') {
11560:                 $unset = '0';
11561:                 if ($type eq '_LC_adv') {
11562:                     $unset = '';
11563:                 }
11564:             } else {
11565:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
11566:                     $confhash{$item}{$type} = 1;
11567:                 } else {
11568:                     $confhash{$item}{$type} = 0;
11569:                 }
11570:             }
11571:             if (ref($domconfig{$action}) eq 'HASH') {
11572:                 if ($action eq 'requestauthor') {
11573:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
11574:                         $changes{$type} = 1;
11575:                     }
11576:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
11577:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
11578:                         $changes{$item}{$type} = 1;
11579:                     }
11580:                 } else {
11581:                     if ($context eq 'requestcourses') {
11582:                         if ($confhash{$item}{$type} ne $unset) {
11583:                             $changes{$item}{$type} = 1;
11584:                         }
11585:                     } else {
11586:                         if (!$confhash{$item}{$type}) {
11587:                             $changes{$item}{$type} = 1;
11588:                         }
11589:                     }
11590:                 }
11591:             } else {
11592:                 if ($context eq 'requestcourses') {
11593:                     if ($confhash{$item}{$type} ne $unset) {
11594:                         $changes{$item}{$type} = 1;
11595:                     }
11596:                 } elsif ($context eq 'requestauthor') {
11597:                     if ($confhash{$type} ne $unset) {
11598:                         $changes{$type} = 1;
11599:                     }
11600:                 } else {
11601:                     if (!$confhash{$item}{$type}) {
11602:                         $changes{$item}{$type} = 1;
11603:                     }
11604:                 }
11605:             }
11606:         }
11607:     }
11608:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
11609:         if (ref($domconfig{'quotas'}) eq 'HASH') {
11610:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
11611:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
11612:                     if (exists($confhash{'defaultquota'}{$key})) {
11613:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
11614:                             $changes{'defaultquota'}{$key} = 1;
11615:                         }
11616:                     } else {
11617:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
11618:                     }
11619:                 }
11620:             } else {
11621:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
11622:                     if (exists($confhash{'defaultquota'}{$key})) {
11623:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
11624:                             $changes{'defaultquota'}{$key} = 1;
11625:                         }
11626:                     } else {
11627:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
11628:                     }
11629:                 }
11630:             }
11631:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
11632:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
11633:                     if (exists($confhash{'authorquota'}{$key})) {
11634:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
11635:                             $changes{'authorquota'}{$key} = 1;
11636:                         }
11637:                     } else {
11638:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
11639:                     }
11640:                 }
11641:             }
11642:         }
11643:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
11644:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
11645:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
11646:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
11647:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
11648:                             $changes{'defaultquota'}{$key} = 1;
11649:                         }
11650:                     } else {
11651:                         if (!exists($domconfig{'quotas'}{$key})) {
11652:                             $changes{'defaultquota'}{$key} = 1;
11653:                         }
11654:                     }
11655:                 } else {
11656:                     $changes{'defaultquota'}{$key} = 1;
11657:                 }
11658:             }
11659:         }
11660:         if (ref($confhash{'authorquota'}) eq 'HASH') {
11661:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
11662:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
11663:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
11664:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
11665:                             $changes{'authorquota'}{$key} = 1;
11666:                         }
11667:                     } else {
11668:                         $changes{'authorquota'}{$key} = 1;
11669:                     }
11670:                 } else {
11671:                     $changes{'authorquota'}{$key} = 1;
11672:                 }
11673:             }
11674:         }
11675:     }
11676: 
11677:     if ($context eq 'requestauthor') {
11678:         $domdefaults{'requestauthor'} = \%confhash;
11679:     } else {
11680:         foreach my $key (keys(%confhash)) {
11681:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
11682:                 $domdefaults{$key} = $confhash{$key};
11683:             }
11684:         }
11685:     }
11686: 
11687:     my %quotahash = (
11688:                       $action => { %confhash }
11689:                     );
11690:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
11691:                                              $dom);
11692:     if ($putresult eq 'ok') {
11693:         if (keys(%changes) > 0) {
11694:             my $cachetime = 24*60*60;
11695:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11696:             if (ref($lastactref) eq 'HASH') {
11697:                 $lastactref->{'domdefaults'} = 1;
11698:             }
11699:             $resulttext = &mt('Changes made:').'<ul>';
11700:             unless (($context eq 'requestcourses') ||
11701:                     ($context eq 'requestauthor')) {
11702:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
11703:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
11704:                     foreach my $type (@{$types},'default') {
11705:                         if (defined($changes{'defaultquota'}{$type})) {
11706:                             my $typetitle = $usertypes->{$type};
11707:                             if ($type eq 'default') {
11708:                                 $typetitle = $othertitle;
11709:                             }
11710:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
11711:                         }
11712:                     }
11713:                     $resulttext .= '</ul></li>';
11714:                 }
11715:                 if (ref($changes{'authorquota'}) eq 'HASH') {
11716:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
11717:                     foreach my $type (@{$types},'default') {
11718:                         if (defined($changes{'authorquota'}{$type})) {
11719:                             my $typetitle = $usertypes->{$type};
11720:                             if ($type eq 'default') {
11721:                                 $typetitle = $othertitle;
11722:                             }
11723:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
11724:                         }
11725:                     }
11726:                     $resulttext .= '</ul></li>';
11727:                 }
11728:             }
11729:             my %newenv;
11730:             foreach my $item (@usertools) {
11731:                 my (%haschgs,%inconf);
11732:                 if ($context eq 'requestauthor') {
11733:                     %haschgs = %changes;
11734:                     %inconf = %confhash;
11735:                 } else {
11736:                     if (ref($changes{$item}) eq 'HASH') {
11737:                         %haschgs = %{$changes{$item}};
11738:                     }
11739:                     if (ref($confhash{$item}) eq 'HASH') {
11740:                         %inconf = %{$confhash{$item}};
11741:                     }
11742:                 }
11743:                 if (keys(%haschgs) > 0) {
11744:                     my $newacc = 
11745:                         &Apache::lonnet::usertools_access($env{'user.name'},
11746:                                                           $env{'user.domain'},
11747:                                                           $item,'reload',$context);
11748:                     if (($context eq 'requestcourses') ||
11749:                         ($context eq 'requestauthor')) {
11750:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
11751:                             $newenv{'environment.canrequest.'.$item} = $newacc;
11752:                         }
11753:                     } else {
11754:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
11755:                             $newenv{'environment.availabletools.'.$item} = $newacc;
11756:                         }
11757:                     }
11758:                     unless ($context eq 'requestauthor') {
11759:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
11760:                     }
11761:                     foreach my $type (@{$types},'default','_LC_adv') {
11762:                         if ($haschgs{$type}) {
11763:                             my $typetitle = $usertypes->{$type};
11764:                             if ($type eq 'default') {
11765:                                 $typetitle = $othertitle;
11766:                             } elsif ($type eq '_LC_adv') {
11767:                                 $typetitle = 'LON-CAPA Advanced Users'; 
11768:                             }
11769:                             if ($inconf{$type}) {
11770:                                 if ($context eq 'requestcourses') {
11771:                                     my $cond;
11772:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
11773:                                         if ($1 eq '') {
11774:                                             $cond = &mt('(Automatic processing of any request).');
11775:                                         } else {
11776:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
11777:                                         }
11778:                                     } else { 
11779:                                         $cond = $conditions{$inconf{$type}};
11780:                                     }
11781:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
11782:                                 } elsif ($context eq 'requestauthor') {
11783:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
11784:                                                              $titles{$inconf{$type}},$typetitle);
11785: 
11786:                                 } else {
11787:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
11788:                                 }
11789:                             } else {
11790:                                 if ($type eq '_LC_adv') {
11791:                                     if ($inconf{$type} eq '0') {
11792:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11793:                                     } else { 
11794:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
11795:                                     }
11796:                                 } else {
11797:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
11798:                                 }
11799:                             }
11800:                         }
11801:                     }
11802:                     unless ($context eq 'requestauthor') {
11803:                         $resulttext .= '</ul></li>';
11804:                     }
11805:                 }
11806:             }
11807:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
11808:                 if (ref($changes{'notify'}) eq 'HASH') {
11809:                     if ($changes{'notify'}{'approval'}) {
11810:                         if (ref($confhash{'notify'}) eq 'HASH') {
11811:                             if ($confhash{'notify'}{'approval'}) {
11812:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
11813:                             } else {
11814:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
11815:                             }
11816:                         }
11817:                     }
11818:                 }
11819:             }
11820:             if ($action eq 'requestcourses') {
11821:                 my @offon = ('off','on');
11822:                 if ($changes{'uniquecode'}) {
11823:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
11824:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
11825:                         $resulttext .= '<li>'.
11826:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
11827:                                        '</li>';
11828:                     } else {
11829:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
11830:                                        '</li>';
11831:                     }
11832:                 }
11833:                 foreach my $type ('textbooks','templates') {
11834:                     if (ref($changes{$type}) eq 'HASH') {
11835:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
11836:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
11837:                             my %coursehash = &Apache::lonnet::coursedescription($key);
11838:                             my $coursetitle = $coursehash{'description'};
11839:                             my $position = $confhash{$type}{$key}{'order'} + 1;
11840:                             $resulttext .= '<li>';
11841:                             foreach my $item ('subject','title','publisher','author') {
11842:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
11843:                                          ($type eq 'templates'));
11844:                                 my $name = $item.':';
11845:                                 $name =~ s/^(\w)/\U$1/;
11846:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
11847:                             }
11848:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
11849:                             if ($type eq 'textbooks') {
11850:                                 if ($confhash{$type}{$key}{'image'}) {
11851:                                     $resulttext .= ' '.&mt('Image: [_1]',
11852:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
11853:                                                    ' alt="Textbook cover" />').'<br />';
11854:                                 }
11855:                             }
11856:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
11857:                         }
11858:                         $resulttext .= '</ul></li>';
11859:                     }
11860:                 }
11861:                 if (ref($changes{'validation'}) eq 'HASH') {
11862:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
11863:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
11864:                         foreach my $item (@{$validationitemsref}) {
11865:                             if (exists($changes{'validation'}{$item})) {
11866:                                 if ($item eq 'markup') {
11867:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11868:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
11869:                                 } else {
11870:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
11871:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
11872:                                 }
11873:                             }
11874:                         }
11875:                         if (exists($changes{'validation'}{'dc'})) {
11876:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
11877:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
11878:                         }
11879:                     }
11880:                 }
11881:             }
11882:             $resulttext .= '</ul>';
11883:             if (keys(%newenv)) {
11884:                 &Apache::lonnet::appenv(\%newenv);
11885:             }
11886:         } else {
11887:             if ($context eq 'requestcourses') {
11888:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
11889:             } elsif ($context eq 'requestauthor') {
11890:                 $resulttext = &mt('No changes made to rights to request author space.');
11891:             } else {
11892:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
11893:             }
11894:         }
11895:     } else {
11896:         $resulttext = '<span class="LC_error">'.
11897: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11898:     }
11899:     if ($errors) {
11900:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
11901:                        '<ul>'.$errors.'</ul></p>';
11902:     }
11903:     return $resulttext;
11904: }
11905: 
11906: sub process_textbook_image {
11907:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
11908:     my $filename = $env{'form.'.$caller.'.filename'};
11909:     my ($error,$url);
11910:     my ($width,$height) = (50,50);
11911:     if ($configuserok eq 'ok') {
11912:         if ($switchserver) {
11913:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
11914:                          $switchserver);
11915:         } elsif ($author_ok eq 'ok') {
11916:             my ($result,$imageurl) =
11917:                 &publishlogo($r,'upload',$caller,$dom,$confname,
11918:                              "$type/$cdom/$cnum/cover",$width,$height);
11919:             if ($result eq 'ok') {
11920:                 $url = $imageurl;
11921:             } else {
11922:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
11923:             }
11924:         } else {
11925:             $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);
11926:         }
11927:     } else {
11928:         $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);
11929:     }
11930:     return ($url,$error);
11931: }
11932: 
11933: sub modify_ltitools {
11934:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11935:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11936:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
11937:     my $confname = $dom.'-domainconfig';
11938:     my $servadm = $r->dir_config('lonAdmEMail');
11939:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11940:     my (%posslti,%possfield);
11941:     my @courseroles = ('cc','in','ta','ep','st');
11942:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
11943:     map { $posslti{$_} = 1; } @ltiroles;
11944:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
11945:     map { $possfield{$_} = 1; } @allfields;
11946:     my %lt = &ltitools_names(); 
11947:     if ($env{'form.ltitools_add'}) {
11948:         my $title = $env{'form.ltitools_add_title'};
11949:         $title =~ s/(`)/'/g;
11950:         ($newid,my $error) = &get_ltitools_id($dom,$title);
11951:         if ($newid) {
11952:             my $position = $env{'form.ltitools_add_pos'};
11953:             $position =~ s/\D+//g;
11954:             if ($position ne '') {
11955:                 $allpos[$position] = $newid;
11956:             }
11957:             $changes{$newid} = 1;
11958:             foreach my $item ('title','url','key','secret','lifetime') {
11959:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
11960:                 if ($item eq 'lifetime') {
11961:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
11962:                 }
11963:                 if ($env{'form.ltitools_add_'.$item}) {
11964:                     if (($item eq 'key') || ($item eq 'secret')) {
11965:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11966:                     } else {
11967:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
11968:                     }
11969:                 }
11970:             }
11971:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
11972:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
11973:             }
11974:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
11975:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
11976:             }
11977:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
11978:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
11979:             } else {
11980:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
11981:             }
11982:             foreach my $item ('width','height','linktext','explanation') {
11983:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
11984:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
11985:                 if (($item eq 'width') || ($item eq 'height')) {
11986:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
11987:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
11988:                     }
11989:                 } else {
11990:                     if ($env{'form.ltitools_add_'.$item} ne '') {
11991:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
11992:                     }
11993:                 }
11994:             }
11995:             if ($env{'form.ltitools_add_target'} eq 'window') {
11996:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11997:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
11998:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
11999:             } else {
12000:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
12001:             }
12002:             foreach my $item ('passback','roster') {
12003:                 if ($env{'form.ltitools_'.$item.'_add'}) {
12004:                     $confhash{$newid}{$item} = 1;
12005:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
12006:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
12007:                         $lifetime =~ s/^\s+|\s+$//g;
12008:                         if ($lifetime =~ /^\d+\.?\d*$/) {
12009:                             $confhash{$newid}{$item.'valid'} = $lifetime;
12010:                         }
12011:                     }
12012:                 }
12013:             }
12014:             if ($env{'form.ltitools_add_image.filename'} ne '') {
12015:                 my ($imageurl,$error) =
12016:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
12017:                                             $configuserok,$switchserver,$author_ok);
12018:                 if ($imageurl) {
12019:                     $confhash{$newid}{'image'} = $imageurl;
12020:                 }
12021:                 if ($error) {
12022:                     &Apache::lonnet::logthis($error);
12023:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12024:                 }
12025:             }
12026:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
12027:             foreach my $field (@fields) {
12028:                 if ($possfield{$field}) {
12029:                     if ($field eq 'roles') {
12030:                         foreach my $role (@courseroles) {
12031:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
12032:                             if (($choice ne '') && ($posslti{$choice})) {
12033:                                 $confhash{$newid}{'roles'}{$role} = $choice;
12034:                                 if ($role eq 'cc') {
12035:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
12036:                                 }
12037:                             }
12038:                         }
12039:                     } else {
12040:                         $confhash{$newid}{'fields'}{$field} = 1;
12041:                     }
12042:                 }
12043:             }
12044:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
12045:                 if ($confhash{$newid}{'fields'}{'user'}) {
12046:                     if ($env{'form.ltitools_userincdom_add'}) {
12047:                         $confhash{$newid}{'incdom'} = 1;
12048:                     }
12049:                 }
12050:             }
12051:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
12052:             foreach my $item (@courseconfig) {
12053:                 $confhash{$newid}{'crsconf'}{$item} = 1;
12054:             }
12055:             if ($env{'form.ltitools_add_custom'}) {
12056:                 my $name = $env{'form.ltitools_add_custom_name'};
12057:                 my $value = $env{'form.ltitools_add_custom_value'};
12058:                 $value =~ s/(`)/'/g;
12059:                 $name =~ s/(`)/'/g;
12060:                 $confhash{$newid}{'custom'}{$name} = $value;
12061:             }
12062:         } else {
12063:             my $error = &mt('Failed to acquire unique ID for new external tool');   
12064:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12065:         }
12066:     }
12067:     if (ref($domconfig{$action}) eq 'HASH') {
12068:         my %deletions;
12069:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
12070:         if (@todelete) {
12071:             map { $deletions{$_} = 1; } @todelete;
12072:         }
12073:         my %customadds;
12074:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
12075:         if (@newcustom) {
12076:             map { $customadds{$_} = 1; } @newcustom;
12077:         } 
12078:         my %imgdeletions;
12079:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
12080:         if (@todeleteimages) {
12081:             map { $imgdeletions{$_} = 1; } @todeleteimages;
12082:         }
12083:         my $maxnum = $env{'form.ltitools_maxnum'};
12084:         for (my $i=0; $i<=$maxnum; $i++) {
12085:             my $itemid = $env{'form.ltitools_id_'.$i};
12086:             $itemid =~ s/\D+//g;
12087:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12088:                 if ($deletions{$itemid}) {
12089:                     if ($domconfig{$action}{$itemid}{'image'}) {
12090:                         #FIXME need to obsolete item in RES space
12091:                     }
12092:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
12093:                     next;
12094:                 } else {
12095:                     my $newpos = $env{'form.ltitools_'.$itemid};
12096:                     $newpos =~ s/\D+//g;
12097:                     foreach my $item ('title','url','lifetime') {
12098:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
12099:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
12100:                             $changes{$itemid} = 1;
12101:                         }
12102:                     }
12103:                     foreach my $item ('key','secret') {
12104:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
12105:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
12106:                             $changes{$itemid} = 1;
12107:                         }
12108:                     }
12109:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
12110:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
12111:                     }
12112:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
12113:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
12114:                     }
12115:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
12116:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
12117:                     } else {
12118:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
12119:                     }
12120:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
12121:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
12122:                             $changes{$itemid} = 1;
12123:                         }
12124:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
12125:                         $changes{$itemid} = 1;
12126:                     }
12127:                     foreach my $size ('width','height') {
12128:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
12129:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
12130:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
12131:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
12132:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12133:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
12134:                                     $changes{$itemid} = 1;
12135:                                 }
12136:                             } else {
12137:                                 $changes{$itemid} = 1;
12138:                             }
12139:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12140:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
12141:                                 $changes{$itemid} = 1;
12142:                             }
12143:                         }
12144:                     }
12145:                     foreach my $item ('linktext','explanation') {
12146:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
12147:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
12148:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
12149:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
12150:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12151:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
12152:                                     $changes{$itemid} = 1;
12153:                                 }
12154:                             } else {
12155:                                 $changes{$itemid} = 1;
12156:                             }
12157:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12158:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
12159:                                 $changes{$itemid} = 1;
12160:                             }
12161:                         }
12162:                     }
12163:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
12164:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
12165:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
12166:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
12167:                     } else {
12168:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
12169:                     }
12170:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12171:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
12172:                             $changes{$itemid} = 1;
12173:                         }
12174:                     } else {
12175:                         $changes{$itemid} = 1;
12176:                     }
12177:                     foreach my $extra ('passback','roster') {
12178:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
12179:                             $confhash{$itemid}{$extra} = 1;
12180:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
12181:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
12182:                                 $lifetime =~ s/^\s+|\s+$//g;
12183:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
12184:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
12185:                                 }
12186:                             }
12187:                         }
12188:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
12189:                             $changes{$itemid} = 1;
12190:                         }
12191:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
12192:                             $changes{$itemid} = 1;
12193:                         }
12194:                     }
12195:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
12196:                     foreach my $item ('label','title','target','linktext','explanation','append') {
12197:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
12198:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
12199:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
12200:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
12201:                                     $changes{$itemid} = 1;
12202:                                 }
12203:                             } else {
12204:                                 $changes{$itemid} = 1;
12205:                             }
12206:                         }
12207:                     }
12208:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
12209:                     foreach my $field (@fields) {
12210:                         if ($possfield{$field}) {
12211:                             if ($field eq 'roles') {
12212:                                 foreach my $role (@courseroles) {
12213:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
12214:                                     if (($choice ne '') && ($posslti{$choice})) {
12215:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
12216:                                         if ($role eq 'cc') {
12217:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
12218:                                         }
12219:                                     }
12220:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
12221:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
12222:                                             $changes{$itemid} = 1;
12223:                                         }
12224:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
12225:                                         $changes{$itemid} = 1;
12226:                                     }
12227:                                 }
12228:                             } else {
12229:                                 $confhash{$itemid}{'fields'}{$field} = 1;
12230:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
12231:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
12232:                                         $changes{$itemid} = 1;
12233:                                     }
12234:                                 } else {
12235:                                     $changes{$itemid} = 1;
12236:                                 }
12237:                             }
12238:                         }
12239:                     }
12240:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
12241:                         if ($confhash{$itemid}{'fields'}{'user'}) {
12242:                             if ($env{'form.ltitools_userincdom_'.$i}) {
12243:                                 $confhash{$itemid}{'incdom'} = 1;
12244:                             }
12245:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
12246:                                 $changes{$itemid} = 1;
12247:                             }
12248:                         }
12249:                     }
12250:                     $allpos[$newpos] = $itemid;
12251:                 }
12252:                 if ($imgdeletions{$itemid}) {
12253:                     $changes{$itemid} = 1;
12254:                     #FIXME need to obsolete item in RES space
12255:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
12256:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
12257:                                                                  $itemid,$configuserok,$switchserver,
12258:                                                                  $author_ok);
12259:                     if ($imgurl) {
12260:                         $confhash{$itemid}{'image'} = $imgurl;
12261:                         $changes{$itemid} = 1;
12262:                     }
12263:                     if ($error) {
12264:                         &Apache::lonnet::logthis($error);
12265:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12266:                     }
12267:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
12268:                     $confhash{$itemid}{'image'} =
12269:                        $domconfig{$action}{$itemid}{'image'};
12270:                 }
12271:                 if ($customadds{$i}) {
12272:                     my $name = $env{'form.ltitools_custom_name_'.$i};
12273:                     $name =~ s/(`)/'/g;
12274:                     $name =~ s/^\s+//;
12275:                     $name =~ s/\s+$//;
12276:                     my $value = $env{'form.ltitools_custom_value_'.$i};
12277:                     $value =~ s/(`)/'/g;
12278:                     $value =~ s/^\s+//;
12279:                     $value =~ s/\s+$//;
12280:                     if ($name ne '') {
12281:                         $confhash{$itemid}{'custom'}{$name} = $value;
12282:                         $changes{$itemid} = 1;
12283:                     }
12284:                 }
12285:                 my %customdels;
12286:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
12287:                 if (@customdeletions) {
12288:                     $changes{$itemid} = 1;
12289:                 }
12290:                 map { $customdels{$_} = 1; } @customdeletions;
12291:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
12292:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
12293:                         unless ($customdels{$key}) {
12294:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
12295:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
12296:                             }
12297:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
12298:                                 $changes{$itemid} = 1;
12299:                             }
12300:                         }
12301:                     }
12302:                 }
12303:                 unless ($changes{$itemid}) {
12304:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
12305:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
12306:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
12307:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
12308:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
12309:                                         $changes{$itemid} = 1;
12310:                                         last;
12311:                                     }
12312:                                 }
12313:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
12314:                                 $changes{$itemid} = 1;
12315:                             }
12316:                         }
12317:                         last if ($changes{$itemid});
12318:                     }
12319:                 }
12320:             }
12321:         }
12322:     }
12323:     if (@allpos > 0) {
12324:         my $idx = 0;
12325:         foreach my $itemid (@allpos) {
12326:             if ($itemid ne '') {
12327:                 $confhash{$itemid}{'order'} = $idx;
12328:                 if (ref($domconfig{$action}) eq 'HASH') {
12329:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12330:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
12331:                             $changes{$itemid} = 1;
12332:                         }
12333:                     }
12334:                 }
12335:                 $idx ++;
12336:             }
12337:         }
12338:     }
12339:     my %ltitoolshash = (
12340:                           $action => { %confhash }
12341:                        );
12342:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
12343:                                              $dom);
12344:     if ($putresult eq 'ok') {
12345:         my %ltienchash = (
12346:                              $action => { %encconfig }
12347:                          );
12348:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
12349:         if (keys(%changes) > 0) {
12350:             my $cachetime = 24*60*60;
12351:             my %ltiall = %confhash;
12352:             foreach my $id (keys(%ltiall)) {
12353:                 if (ref($encconfig{$id}) eq 'HASH') {
12354:                     foreach my $item ('key','secret') {
12355:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
12356:                     }
12357:                 }
12358:             }
12359:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
12360:             if (ref($lastactref) eq 'HASH') {
12361:                 $lastactref->{'ltitools'} = 1;
12362:             }
12363:             $resulttext = &mt('Changes made:').'<ul>';
12364:             my %bynum;
12365:             foreach my $itemid (sort(keys(%changes))) {
12366:                 my $position = $confhash{$itemid}{'order'};
12367:                 $bynum{$position} = $itemid;
12368:             }
12369:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12370:                 my $itemid = $bynum{$pos}; 
12371:                 if (ref($confhash{$itemid}) ne 'HASH') {
12372:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12373:                 } else {
12374:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
12375:                     if ($confhash{$itemid}{'image'}) {
12376:                         $resulttext .= '&nbsp;'.
12377:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
12378:                                        ' alt="'.&mt('Tool Provider icon').'" />';
12379:                     }
12380:                     $resulttext .= '</li><ul>';
12381:                     my $position = $pos + 1;
12382:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12383:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
12384:                         if ($confhash{$itemid}{$item} ne '') {
12385:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
12386:                         }
12387:                     }
12388:                     if ($encconfig{$itemid}{'key'} ne '') {
12389:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
12390:                     }
12391:                     if ($encconfig{$itemid}{'secret'} ne '') {
12392:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
12393:                         my $num = length($encconfig{$itemid}{'secret'});
12394:                         $resulttext .= ('*'x$num).'</li>';
12395:                     }
12396:                     $resulttext .= '<li>'.&mt('Configurable in course:');
12397:                     my @possconfig = ('label','title','target','linktext','explanation','append');
12398:                     my $numconfig = 0; 
12399:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
12400:                         foreach my $item (@possconfig) {
12401:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
12402:                                 $numconfig ++;
12403:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
12404:                             }
12405:                         }
12406:                     }
12407:                     if (!$numconfig) {
12408:                         $resulttext .= &mt('None');
12409:                     }
12410:                     $resulttext .= '</li>';
12411:                     foreach my $item ('passback','roster') {
12412:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
12413:                         if ($confhash{$itemid}{$item}) {
12414:                             $resulttext .= &mt('Yes');
12415:                             if ($confhash{$itemid}{$item.'valid'}) {
12416:                                 if ($item eq 'passback') {
12417:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
12418:                                                            $confhash{$itemid}{$item.'valid'});
12419:                                 } else {
12420:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
12421:                                                            $confhash{$itemid}{$item.'valid'});
12422:                                 }
12423:                             }
12424:                         } else {
12425:                             $resulttext .= &mt('No');
12426:                         }
12427:                         $resulttext .= '</li>';
12428:                     }
12429:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
12430:                         my $displaylist;
12431:                         if ($confhash{$itemid}{'display'}{'target'}) {
12432:                             $displaylist = &mt('Display target').':&nbsp;'.
12433:                                            $confhash{$itemid}{'display'}{'target'}.',';
12434:                         }
12435:                         foreach my $size ('width','height') { 
12436:                             if ($confhash{$itemid}{'display'}{$size}) {
12437:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
12438:                                                 $confhash{$itemid}{'display'}{$size}.',';
12439:                             }
12440:                         }
12441:                         if ($displaylist) {
12442:                             $displaylist =~ s/,$//;
12443:                             $resulttext .= '<li>'.$displaylist.'</li>';
12444:                         }
12445:                         foreach my $item ('linktext','explanation') {
12446:                             if ($confhash{$itemid}{'display'}{$item}) {
12447:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
12448:                             }
12449:                         }
12450:                     }
12451:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
12452:                         my $fieldlist;
12453:                         foreach my $field (@allfields) {
12454:                             if ($confhash{$itemid}{'fields'}{$field}) {
12455:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
12456:                             }
12457:                         }
12458:                         if ($fieldlist) {
12459:                             $fieldlist =~ s/,$//;
12460:                             if ($confhash{$itemid}{'fields'}{'user'}) {
12461:                                 if ($confhash{$itemid}{'incdom'}) {
12462:                                     $fieldlist .= ' ('.&mt('username:domain').')';
12463:                                 } else {
12464:                                     $fieldlist .= ' ('.&mt('username').')';
12465:                                 }
12466:                             }
12467:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
12468:                         }
12469:                     }
12470:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
12471:                         my $rolemaps;
12472:                         foreach my $role (@courseroles) {
12473:                             if ($confhash{$itemid}{'roles'}{$role}) {
12474:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
12475:                                              $confhash{$itemid}{'roles'}{$role}.',';
12476:                             }
12477:                         }
12478:                         if ($rolemaps) {
12479:                             $rolemaps =~ s/,$//; 
12480:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
12481:                         }
12482:                     }
12483:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
12484:                         my $customlist;
12485:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
12486:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
12487:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
12488:                             } 
12489:                         }
12490:                         if ($customlist) {
12491:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
12492:                         }
12493:                     } 
12494:                     $resulttext .= '</ul></li>';
12495:                 }
12496:             }
12497:             $resulttext .= '</ul>';
12498:         } else {
12499:             $resulttext = &mt('No changes made.');
12500:         }
12501:     } else {
12502:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12503:     }
12504:     if ($errors) {
12505:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12506:                        $errors.'</ul>';
12507:     }
12508:     return $resulttext;
12509: }
12510: 
12511: sub process_ltitools_image {
12512:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
12513:     my $filename = $env{'form.'.$caller.'.filename'};
12514:     my ($error,$url);
12515:     my ($width,$height) = (21,21);
12516:     if ($configuserok eq 'ok') {
12517:         if ($switchserver) {
12518:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
12519:                          $switchserver);
12520:         } elsif ($author_ok eq 'ok') {
12521:             my ($result,$imageurl,$madethumb) =
12522:                 &publishlogo($r,'upload',$caller,$dom,$confname,
12523:                              "ltitools/$itemid/icon",$width,$height);
12524:             if ($result eq 'ok') {
12525:                 if ($madethumb) {
12526:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
12527:                     my $imagethumb = "$path/tn-".$imagefile;
12528:                     $url = $imagethumb;
12529:                 } else {
12530:                     $url = $imageurl;
12531:                 }
12532:             } else {
12533:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
12534:             }
12535:         } else {
12536:             $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);
12537:         }
12538:     } else {
12539:         $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);
12540:     }
12541:     return ($url,$error);
12542: }
12543: 
12544: sub get_ltitools_id {
12545:     my ($cdom,$title) = @_;
12546:     # get lock on ltitools db
12547:     my $lockhash = {
12548:                       lock => $env{'user.name'}.
12549:                               ':'.$env{'user.domain'},
12550:                    };
12551:     my $tries = 0;
12552:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
12553:     my ($id,$error);
12554:  
12555:     while (($gotlock ne 'ok') && ($tries<10)) {
12556:         $tries ++;
12557:         sleep (0.1);
12558:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
12559:     }
12560:     if ($gotlock eq 'ok') {
12561:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
12562:         if ($currids{'lock'}) {
12563:             delete($currids{'lock'});
12564:             if (keys(%currids)) {
12565:                 my @curr = sort { $a <=> $b } keys(%currids);
12566:                 if ($curr[-1] =~ /^\d+$/) {
12567:                     $id = 1 + $curr[-1];
12568:                 }
12569:             } else {
12570:                 $id = 1;
12571:             }
12572:             if ($id) {
12573:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
12574:                     $error = 'nostore';
12575:                 }
12576:             } else {
12577:                 $error = 'nonumber';
12578:             }
12579:         }
12580:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
12581:     } else {
12582:         $error = 'nolock';
12583:     }
12584:     return ($id,$error);
12585: }
12586: 
12587: sub modify_lti {
12588:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
12589:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12590:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
12591:     my (%posslti,%posslticrs,%posscrstype);
12592:     my @courseroles = ('cc','in','ta','ep','st');
12593:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
12594:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
12595:     my @coursetypes = ('official','unofficial','community','textbook','placement');
12596:     my %coursetypetitles = &Apache::lonlocal::texthash (
12597:                                official   => 'Official',
12598:                                unofficial => 'Unofficial',
12599:                                community  => 'Community',
12600:                                textbook   => 'Textbook',
12601:                                placement  => 'Placement Test',
12602:     );
12603:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12604:     my %lt = &lti_names();
12605:     map { $posslti{$_} = 1; } @ltiroles;
12606:     map { $posslticrs{$_} = 1; } @lticourseroles;
12607:     map { $posscrstype{$_} = 1; } @coursetypes;
12608: 
12609:     my %menutitles = &ltimenu_titles();
12610: 
12611:     my (@items,%deletions,%itemids);
12612:     if ($env{'form.lti_add'}) {
12613:         my $consumer = $env{'form.lti_consumer_add'};
12614:         $consumer =~ s/(`)/'/g;
12615:         ($newid,my $error) = &get_lti_id($dom,$consumer);
12616:         if ($newid) {
12617:             $itemids{'add'} = $newid;
12618:             push(@items,'add');
12619:             $changes{$newid} = 1;
12620:         } else {
12621:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
12622:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12623:         }
12624:     }
12625:     if (ref($domconfig{$action}) eq 'HASH') {
12626:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
12627:         if (@todelete) {
12628:             map { $deletions{$_} = 1; } @todelete;
12629:         }
12630:         my $maxnum = $env{'form.lti_maxnum'};
12631:         for (my $i=0; $i<=$maxnum; $i++) {
12632:             my $itemid = $env{'form.lti_id_'.$i};
12633:             $itemid =~ s/\D+//g;
12634:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12635:                 if ($deletions{$itemid}) {
12636:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
12637:                 } else {
12638:                    push(@items,$i);
12639:                    $itemids{$i} = $itemid;
12640:                 }
12641:             }
12642:         }
12643:     }
12644:     foreach my $idx (@items) {
12645:         my $itemid = $itemids{$idx};
12646:         next unless ($itemid);
12647:         my $position = $env{'form.lti_pos_'.$idx};
12648:         $position =~ s/\D+//g;
12649:         if ($position ne '') {
12650:             $allpos[$position] = $itemid;
12651:         }
12652:         foreach my $item ('consumer','key','secret','lifetime','requser') {
12653:             my $formitem = 'form.lti_'.$item.'_'.$idx;
12654:             $env{$formitem} =~ s/(`)/'/g;
12655:             if ($item eq 'lifetime') {
12656:                 $env{$formitem} =~ s/[^\d.]//g;
12657:             }
12658:             if ($env{$formitem} ne '') {
12659:                 if (($item eq 'key') || ($item eq 'secret')) {
12660:                     $encconfig{$itemid}{$item} = $env{$formitem};
12661:                 } else {
12662:                     $confhash{$itemid}{$item} = $env{$formitem};
12663:                     unless (($idx eq 'add') || ($changes{$itemid})) {
12664:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
12665:                             $changes{$itemid} = 1;
12666:                         }
12667:                     }
12668:                 }
12669:             }
12670:         }
12671:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
12672:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
12673:         }
12674:         if ($confhash{$itemid}{'requser'}) {
12675:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
12676:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
12677:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
12678:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
12679:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
12680:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
12681:                 $mapuser =~ s/(`)/'/g;
12682:                 $mapuser =~ s/^\s+|\s+$//g; 
12683:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
12684:             }
12685:             foreach my $ltirole (@lticourseroles) {
12686:                 my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
12687:                 if (grep(/^\Q$possrole\E$/,@courseroles)) {
12688:                     $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
12689:                 }
12690:             }
12691:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
12692:             my @makeuser;
12693:             foreach my $ltirole (sort(@possmakeuser)) {
12694:                 if ($posslti{$ltirole}) {
12695:                     push(@makeuser,$ltirole);
12696:                 }
12697:             }
12698:             $confhash{$itemid}{'makeuser'} = \@makeuser;
12699:             if (@makeuser) {
12700:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
12701:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
12702:                     $confhash{$itemid}{'lcauth'} = $lcauth;
12703:                     if ($lcauth ne 'internal') {
12704:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
12705:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
12706:                         $lcauthparm =~ s/`//g;
12707:                         if ($lcauthparm ne '') {
12708:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
12709:                         }
12710:                     }
12711:                 } else {
12712:                     $confhash{$itemid}{'lcauth'} = 'lti';
12713:                 }
12714:             }
12715:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
12716:             if (@possinstdata) {
12717:                 foreach my $field (@possinstdata) {
12718:                     if (exists($fieldtitles{$field})) {
12719:                         push(@{$confhash{$itemid}{'instdata'}});
12720:                     }
12721:                 }
12722:             }
12723:             if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
12724:                 ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
12725:                 $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
12726:             } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
12727:                 my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
12728:                 $mapcrs =~ s/(`)/'/g;
12729:                 $mapcrs =~ s/^\s+|\s+$//g;
12730:                 $confhash{$itemid}{'mapcrs'} = $mapcrs;
12731:             }
12732:             my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
12733:             my @crstypes;
12734:             foreach my $type (sort(@posstypes)) {
12735:                 if ($posscrstype{$type}) {
12736:                     push(@crstypes,$type);
12737:                 }
12738:             }
12739:             $confhash{$itemid}{'mapcrstype'} = \@crstypes;
12740:             if ($env{'form.lti_makecrs_'.$idx}) {
12741:                 $confhash{$itemid}{'makecrs'} = 1;
12742:             }
12743:             my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
12744:             my @selfenroll;
12745:             foreach my $type (sort(@possenroll)) {
12746:                 if ($posslticrs{$type}) {
12747:                     push(@selfenroll,$type);
12748:                 }
12749:             }
12750:             $confhash{$itemid}{'selfenroll'} = \@selfenroll;
12751:             if ($env{'form.lti_crssec_'.$idx}) {
12752:                 if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
12753:                     $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
12754:                 } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
12755:                     my $section = $env{'form.lti_customsection_'.$idx};
12756:                     $section =~ s/(`)/'/g;
12757:                     $section =~ s/^\s+|\s+$//g;
12758:                     if ($section ne '') {
12759:                         $confhash{$itemid}{'section'} = $section;
12760:                     }
12761:                 }
12762:             }
12763:             if ($env{'form.lti_callback_'.$idx}) {
12764:                 if ($env{'form.lti_callbackparam_'.$idx}) {
12765:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
12766:                     $callback =~ s/^\s+|\s+$//g;
12767:                     $confhash{$itemid}{'callback'} = $callback;
12768:                 }
12769:             }
12770:             foreach my $field ('passback','roster','topmenu','inlinemenu') {
12771:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
12772:                     $confhash{$itemid}{$field} = 1;
12773:                 }
12774:             }
12775:             if ($env{'form.lti_passback_'.$idx}) {
12776:                 if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
12777:                     $confhash{$itemid}{'passbackformat'} = '1.0';
12778:                 } else {
12779:                     $confhash{$itemid}{'passbackformat'} = '1.1';
12780:                 }
12781:             }
12782:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
12783:                 $confhash{$itemid}{lcmenu} = [];
12784:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
12785:                 foreach my $field (@possmenu) {
12786:                     if (exists($menutitles{$field})) {
12787:                         if ($field eq 'grades') {
12788:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
12789:                         }
12790:                         push(@{$confhash{$itemid}{lcmenu}},$field);
12791:                     }
12792:                 }
12793:             }
12794:             unless (($idx eq 'add') || ($changes{$itemid})) {
12795:                 foreach my $field ('mapuser','mapcrs','makecrs','section','passback','roster','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
12796:                     if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
12797:                         $changes{$itemid} = 1;
12798:                     }
12799:                 }
12800:                 unless ($changes{$itemid}) {
12801:                     if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
12802:                         if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
12803:                             $changes{$itemid} = 1;
12804:                         }
12805:                     }
12806:                 }
12807:                 foreach my $field ('makeuser','mapcrstype','selfenroll','instdata','lcmenu') {
12808:                     unless ($changes{$itemid}) {
12809:                         if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
12810:                             if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
12811:                                 my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
12812:                                                                                $confhash{$itemid}{$field});
12813:                                 if (@diffs) {
12814:                                     $changes{$itemid} = 1;
12815:                                 }
12816:                             } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
12817:                                 $changes{$itemid} = 1;
12818:                             }
12819:                         } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
12820:                             if (@{$confhash{$itemid}{$field}} > 0) {
12821:                                 $changes{$itemid} = 1;
12822:                             }
12823:                         }
12824:                     }
12825:                 }
12826:                 unless ($changes{$itemid}) {
12827:                     if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
12828:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12829:                             foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
12830:                                 if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
12831:                                     $confhash{$itemid}{'maproles'}{$ltirole}) {
12832:                                     $changes{$itemid} = 1;
12833:                                     last;
12834:                                 }
12835:                             }
12836:                             unless ($changes{$itemid}) {
12837:                                 foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
12838:                                     if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
12839:                                         $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
12840:                                         $changes{$itemid} = 1;
12841:                                         last;
12842:                                     }
12843:                                 }
12844:                             }
12845:                         } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
12846:                             $changes{$itemid} = 1;
12847:                         }
12848:                     } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12849:                         unless ($changes{$itemid}) {
12850:                             if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
12851:                                 $changes{$itemid} = 1;
12852:                             }
12853:                         }
12854:                     }
12855:                 }
12856:             }
12857:         }
12858:     }
12859:     if (@allpos > 0) {
12860:         my $idx = 0;
12861:         foreach my $itemid (@allpos) {
12862:             if ($itemid ne '') {
12863:                 $confhash{$itemid}{'order'} = $idx;
12864:                 if (ref($domconfig{$action}) eq 'HASH') {
12865:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12866:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
12867:                             $changes{$itemid} = 1;
12868:                         }
12869:                     }
12870:                 }
12871:                 $idx ++;
12872:             }
12873:         }
12874:     }
12875:     my %ltihash = (
12876:                           $action => { %confhash }
12877:                        );
12878:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
12879:                                              $dom);
12880:     if ($putresult eq 'ok') {
12881:         my %ltienchash = (
12882:                              $action => { %encconfig }
12883:                          );
12884:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
12885:         if (keys(%changes) > 0) {
12886:             my $cachetime = 24*60*60;
12887:             my %ltiall = %confhash;
12888:             foreach my $id (keys(%ltiall)) {
12889:                 if (ref($encconfig{$id}) eq 'HASH') {
12890:                     foreach my $item ('key','secret') {
12891:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
12892:                     }
12893:                 }
12894:             }
12895:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
12896:             if (ref($lastactref) eq 'HASH') {
12897:                 $lastactref->{'lti'} = 1;
12898:             }
12899:             $resulttext = &mt('Changes made:').'<ul>';
12900:             my %bynum;
12901:             foreach my $itemid (sort(keys(%changes))) {
12902:                 my $position = $confhash{$itemid}{'order'};
12903:                 $bynum{$position} = $itemid;
12904:             }
12905:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12906:                 my $itemid = $bynum{$pos};
12907:                 if (ref($confhash{$itemid}) ne 'HASH') {
12908:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12909:                 } else {
12910:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b></li><ul>';
12911:                     my $position = $pos + 1;
12912:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12913:                     foreach my $item ('version','lifetime') {
12914:                         if ($confhash{$itemid}{$item} ne '') {
12915:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
12916:                         }
12917:                     }
12918:                     if ($encconfig{$itemid}{'key'} ne '') {
12919:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
12920:                     }
12921:                     if ($encconfig{$itemid}{'secret'} ne '') {
12922:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
12923:                         my $num = length($encconfig{$itemid}{'secret'});
12924:                         $resulttext .= ('*'x$num).'</li>';
12925:                     }
12926:                     if ($confhash{$itemid}{'requser'}) {
12927:                         if ($confhash{$itemid}{'mapuser'}) {
12928:                             my $shownmapuser;
12929:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
12930:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
12931:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
12932:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
12933:                             } else {
12934:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
12935:                             }
12936:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
12937:                         }
12938:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
12939:                             my $rolemaps;
12940:                             foreach my $role (@ltiroles) {
12941:                                 if ($confhash{$itemid}{'maproles'}{$role}) {
12942:                                     $rolemaps .= ('&nbsp;'x2).$role.'='.
12943:                                                  &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
12944:                                                                             'Course').',';
12945:                                 }
12946:                             }
12947:                             if ($rolemaps) {
12948:                                 $rolemaps =~ s/,$//;
12949:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
12950:                             }
12951:                         }
12952:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
12953:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
12954:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
12955:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
12956:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
12957:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
12958:                                 } else {
12959:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
12960:                                                        $confhash{$itemid}{'lcauth'});
12961:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
12962:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
12963:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
12964:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
12965:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
12966:                                         }
12967:                                     } else {
12968:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
12969:                                     }
12970:                                 }
12971:                                 $resulttext .= '</li>';
12972:                             } else {
12973:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
12974:                             }
12975:                         }
12976:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
12977:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
12978:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
12979:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
12980:                             } else {
12981:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
12982:                             }
12983:                         }
12984:                         if ($confhash{$itemid}{'mapcrs'}) {
12985:                             $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
12986:                         }
12987:                         if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
12988:                             if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
12989:                                 $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
12990:                                                join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
12991:                                                '</li>';
12992:                             } else {
12993:                                 $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
12994:                             }
12995:                         }
12996:                         if ($confhash{$itemid}{'makecrs'}) {
12997:                             $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
12998:                         } else {
12999:                             $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
13000:                         }
13001:                         if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
13002:                             if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
13003:                                 $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
13004:                                                           join(', ',@{$confhash{$itemid}{'selfenroll'}})).
13005:                                                '</li>';
13006:                             } else {
13007:                                 $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
13008:                             }
13009:                         }
13010:                         if ($confhash{$itemid}{'section'}) {
13011:                             if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
13012:                                 $resulttext .= '<li>'.&mt('User section from standard field:').
13013:                                                      ' (course_section_sourcedid)'.'</li>';  
13014:                             } else {
13015:                                 $resulttext .= '<li>'.&mt('User section from:').' '.
13016:                                                       $confhash{$itemid}{'section'}.'</li>';
13017:                             }
13018:                         } else {
13019:                             $resulttext .= '<li>'.&mt('No section assignment').'</li>';
13020:                         }
13021:                         if ($confhash{$itemid}{'callback'}) {
13022:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
13023:                         } else {
13024:                             $resulttext .= '<li>'.&mt('No callback to logout LON-CAPA session when user logs out of Comsumer');
13025:                         }
13026:                         foreach my $item ('passback','roster','topmenu','inlinemenu') {
13027:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
13028:                             if ($confhash{$itemid}{$item}) {
13029:                                 $resulttext .= &mt('Yes');
13030:                                 if ($item eq 'passback') {
13031:                                     if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
13032:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
13033:                                     } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
13034:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
13035:                                     }
13036:                                 }
13037:                             } else {
13038:                                 $resulttext .= &mt('No');
13039:                             }
13040:                             $resulttext .= '</li>';
13041:                         }
13042:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
13043:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
13044:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
13045:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
13046:                             } else {
13047:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
13048:                             }
13049:                         }
13050:                     }
13051:                     $resulttext .= '</ul></li>';
13052:                 }
13053:             }
13054:             $resulttext .= '</ul>';
13055:         } else {
13056:             $resulttext = &mt('No changes made.');
13057:         }
13058:     } else {
13059:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13060:     }
13061:     if ($errors) {
13062:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13063:                        $errors.'</ul>';
13064:     }
13065:     return $resulttext;
13066: }
13067: 
13068: sub get_lti_id {
13069:     my ($domain,$consumer) = @_;
13070:     # get lock on lti db
13071:     my $lockhash = {
13072:                       lock => $env{'user.name'}.
13073:                               ':'.$env{'user.domain'},
13074:                    };
13075:     my $tries = 0;
13076:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
13077:     my ($id,$error);
13078: 
13079:     while (($gotlock ne 'ok') && ($tries<10)) {
13080:         $tries ++;
13081:         sleep (0.1);
13082:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
13083:     }
13084:     if ($gotlock eq 'ok') {
13085:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
13086:         if ($currids{'lock'}) {
13087:             delete($currids{'lock'});
13088:             if (keys(%currids)) {
13089:                 my @curr = sort { $a <=> $b } keys(%currids);
13090:                 if ($curr[-1] =~ /^\d+$/) {
13091:                     $id = 1 + $curr[-1];
13092:                 }
13093:             } else {
13094:                 $id = 1;
13095:             }
13096:             if ($id) {
13097:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
13098:                     $error = 'nostore';
13099:                 }
13100:             } else {
13101:                 $error = 'nonumber';
13102:             }
13103:         }
13104:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
13105:     } else {
13106:         $error = 'nolock';
13107:     }
13108:     return ($id,$error);
13109: }
13110: 
13111: sub modify_autoenroll {
13112:     my ($dom,$lastactref,%domconfig) = @_;
13113:     my ($resulttext,%changes);
13114:     my %currautoenroll;
13115:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
13116:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
13117:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
13118:         }
13119:     }
13120:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
13121:     my %title = ( run => 'Auto-enrollment active',
13122:                   sender => 'Sender for notification messages',
13123:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
13124:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
13125:     my @offon = ('off','on');
13126:     my $sender_uname = $env{'form.sender_uname'};
13127:     my $sender_domain = $env{'form.sender_domain'};
13128:     if ($sender_domain eq '') {
13129:         $sender_uname = '';
13130:     } elsif ($sender_uname eq '') {
13131:         $sender_domain = '';
13132:     }
13133:     my $coowners = $env{'form.autoassign_coowners'};
13134:     my $failsafe = $env{'form.autoenroll_failsafe'};
13135:     $failsafe =~ s{^\s+|\s+$}{}g;
13136:     if ($failsafe =~ /\D/) {
13137:         undef($failsafe);
13138:     }
13139:     my %autoenrollhash =  (
13140:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
13141:                                        'sender_uname' => $sender_uname,
13142:                                        'sender_domain' => $sender_domain,
13143:                                        'co-owners' => $coowners,
13144:                                        'autofailsafe' => $failsafe,
13145:                                 }
13146:                      );
13147:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
13148:                                              $dom);
13149:     if ($putresult eq 'ok') {
13150:         if (exists($currautoenroll{'run'})) {
13151:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
13152:                  $changes{'run'} = 1;
13153:              }
13154:         } elsif ($autorun) {
13155:             if ($env{'form.autoenroll_run'} ne '1') {
13156:                  $changes{'run'} = 1;
13157:             }
13158:         }
13159:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
13160:             $changes{'sender'} = 1;
13161:         }
13162:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
13163:             $changes{'sender'} = 1;
13164:         }
13165:         if ($currautoenroll{'co-owners'} ne '') {
13166:             if ($currautoenroll{'co-owners'} ne $coowners) {
13167:                 $changes{'coowners'} = 1;
13168:             }
13169:         } elsif ($coowners) {
13170:             $changes{'coowners'} = 1;
13171:         }
13172:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
13173:             $changes{'autofailsafe'} = 1;
13174:         }
13175:         if (keys(%changes) > 0) {
13176:             $resulttext = &mt('Changes made:').'<ul>';
13177:             if ($changes{'run'}) {
13178:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
13179:             }
13180:             if ($changes{'sender'}) {
13181:                 if ($sender_uname eq '' || $sender_domain eq '') {
13182:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
13183:                 } else {
13184:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
13185:                 }
13186:             }
13187:             if ($changes{'coowners'}) {
13188:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
13189:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
13190:                 if (ref($lastactref) eq 'HASH') {
13191:                     $lastactref->{'domainconfig'} = 1;
13192:                 }
13193:             }
13194:             if ($changes{'autofailsafe'}) {
13195:                 if ($failsafe ne '') {
13196:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
13197:                 } else {
13198:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
13199:                 }
13200:                 &Apache::lonnet::get_domain_defaults($dom,1);
13201:                 if (ref($lastactref) eq 'HASH') {
13202:                     $lastactref->{'domdefaults'} = 1;
13203:                 }
13204:             }
13205:             $resulttext .= '</ul>';
13206:         } else {
13207:             $resulttext = &mt('No changes made to auto-enrollment settings');
13208:         }
13209:     } else {
13210:         $resulttext = '<span class="LC_error">'.
13211: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13212:     }
13213:     return $resulttext;
13214: }
13215: 
13216: sub modify_autoupdate {
13217:     my ($dom,%domconfig) = @_;
13218:     my ($resulttext,%currautoupdate,%fields,%changes);
13219:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
13220:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
13221:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
13222:         }
13223:     }
13224:     my @offon = ('off','on');
13225:     my %title = &Apache::lonlocal::texthash (
13226:                    run => 'Auto-update:',
13227:                    classlists => 'Updates to user information in classlists?'
13228:                 );
13229:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13230:     my %fieldtitles = &Apache::lonlocal::texthash (
13231:                         id => 'Student/Employee ID',
13232:                         permanentemail => 'E-mail address',
13233:                         lastname => 'Last Name',
13234:                         firstname => 'First Name',
13235:                         middlename => 'Middle Name',
13236:                         generation => 'Generation',
13237:                       );
13238:     $othertitle = &mt('All users');
13239:     if (keys(%{$usertypes}) >  0) {
13240:         $othertitle = &mt('Other users');
13241:     }
13242:     foreach my $key (keys(%env)) {
13243:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
13244:             my ($usertype,$item) = ($1,$2);
13245:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
13246:                 if ($usertype eq 'default') {   
13247:                     push(@{$fields{$1}},$2);
13248:                 } elsif (ref($types) eq 'ARRAY') {
13249:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
13250:                         push(@{$fields{$1}},$2);
13251:                     }
13252:                 }
13253:             }
13254:         }
13255:     }
13256:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
13257:     @lockablenames = sort(@lockablenames);
13258:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
13259:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
13260:         if (@changed) {
13261:             $changes{'lockablenames'} = 1;
13262:         }
13263:     } else {
13264:         if (@lockablenames) {
13265:             $changes{'lockablenames'} = 1;
13266:         }
13267:     }
13268:     my %updatehash = (
13269:                       autoupdate => { run => $env{'form.autoupdate_run'},
13270:                                       classlists => $env{'form.classlists'},
13271:                                       fields => {%fields},
13272:                                       lockablenames => \@lockablenames,
13273:                                     }
13274:                      );
13275:     foreach my $key (keys(%currautoupdate)) {
13276:         if (($key eq 'run') || ($key eq 'classlists')) {
13277:             if (exists($updatehash{autoupdate}{$key})) {
13278:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
13279:                     $changes{$key} = 1;
13280:                 }
13281:             }
13282:         } elsif ($key eq 'fields') {
13283:             if (ref($currautoupdate{$key}) eq 'HASH') {
13284:                 foreach my $item (@{$types},'default') {
13285:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
13286:                         my $change = 0;
13287:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
13288:                             if (!exists($fields{$item})) {
13289:                                 $change = 1;
13290:                                 last;
13291:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
13292:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
13293:                                     $change = 1;
13294:                                     last;
13295:                                 }
13296:                             }
13297:                         }
13298:                         if ($change) {
13299:                             push(@{$changes{$key}},$item);
13300:                         }
13301:                     } 
13302:                 }
13303:             }
13304:         } elsif ($key eq 'lockablenames') {
13305:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
13306:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
13307:                 if (@changed) {
13308:                     $changes{'lockablenames'} = 1;
13309:                 }
13310:             } else {
13311:                 if (@lockablenames) {
13312:                     $changes{'lockablenames'} = 1;
13313:                 }
13314:             }
13315:         }
13316:     }
13317:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
13318:         if (@lockablenames) {
13319:             $changes{'lockablenames'} = 1;
13320:         }
13321:     }
13322:     foreach my $item (@{$types},'default') {
13323:         if (defined($fields{$item})) {
13324:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
13325:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
13326:                     my $change = 0;
13327:                     if (ref($fields{$item}) eq 'ARRAY') {
13328:                         foreach my $type (@{$fields{$item}}) {
13329:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
13330:                                 $change = 1;
13331:                                 last;
13332:                             }
13333:                         }
13334:                     }
13335:                     if ($change) {
13336:                         push(@{$changes{'fields'}},$item);
13337:                     }
13338:                 } else {
13339:                     push(@{$changes{'fields'}},$item);
13340:                 }
13341:             } else {
13342:                 push(@{$changes{'fields'}},$item);
13343:             }
13344:         }
13345:     }
13346:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
13347:                                              $dom);
13348:     if ($putresult eq 'ok') {
13349:         if (keys(%changes) > 0) {
13350:             $resulttext = &mt('Changes made:').'<ul>';
13351:             foreach my $key (sort(keys(%changes))) {
13352:                 if ($key eq 'lockablenames') {
13353:                     $resulttext .= '<li>';
13354:                     if (@lockablenames) {
13355:                         $usertypes->{'default'} = $othertitle;
13356:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
13357:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
13358:                     } else {
13359:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
13360:                     }
13361:                     $resulttext .= '</li>';
13362:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
13363:                     foreach my $item (@{$changes{$key}}) {
13364:                         my @newvalues;
13365:                         foreach my $type (@{$fields{$item}}) {
13366:                             push(@newvalues,$fieldtitles{$type});
13367:                         }
13368:                         my $newvaluestr;
13369:                         if (@newvalues > 0) {
13370:                             $newvaluestr = join(', ',@newvalues);
13371:                         } else {
13372:                             $newvaluestr = &mt('none');
13373:                         }
13374:                         if ($item eq 'default') {
13375:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
13376:                         } else {
13377:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
13378:                         }
13379:                     }
13380:                 } else {
13381:                     my $newvalue;
13382:                     if ($key eq 'run') {
13383:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
13384:                     } else {
13385:                         $newvalue = $offon[$env{'form.'.$key}];
13386:                     }
13387:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
13388:                 }
13389:             }
13390:             $resulttext .= '</ul>';
13391:         } else {
13392:             $resulttext = &mt('No changes made to autoupdates');
13393:         }
13394:     } else {
13395:         $resulttext = '<span class="LC_error">'.
13396: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13397:     }
13398:     return $resulttext;
13399: }
13400: 
13401: sub modify_autocreate {
13402:     my ($dom,%domconfig) = @_;
13403:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
13404:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
13405:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
13406:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
13407:         }
13408:     }
13409:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
13410:                  req => 'Auto-creation of validated requests for official courses',
13411:                  xmldc => 'Identity of course creator of courses from XML files',
13412:                );
13413:     my @types = ('xml','req');
13414:     foreach my $item (@types) {
13415:         $newvals{$item} = $env{'form.autocreate_'.$item};
13416:         $newvals{$item} =~ s/\D//g;
13417:         $newvals{$item} = 0 if ($newvals{$item} eq '');
13418:     }
13419:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
13420:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
13421:     unless (exists($domcoords{$newvals{'xmldc'}})) {
13422:         $newvals{'xmldc'} = '';
13423:     } 
13424:     %autocreatehash =  (
13425:                         autocreate => { xml => $newvals{'xml'},
13426:                                         req => $newvals{'req'},
13427:                                       }
13428:                        );
13429:     if ($newvals{'xmldc'} ne '') {
13430:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
13431:     }
13432:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
13433:                                              $dom);
13434:     if ($putresult eq 'ok') {
13435:         my @items = @types;
13436:         if ($newvals{'xml'}) {
13437:             push(@items,'xmldc');
13438:         }
13439:         foreach my $item (@items) {
13440:             if (exists($currautocreate{$item})) {
13441:                 if ($currautocreate{$item} ne $newvals{$item}) {
13442:                     $changes{$item} = 1;
13443:                 }
13444:             } elsif ($newvals{$item}) {
13445:                 $changes{$item} = 1;
13446:             }
13447:         }
13448:         if (keys(%changes) > 0) {
13449:             my @offon = ('off','on'); 
13450:             $resulttext = &mt('Changes made:').'<ul>';
13451:             foreach my $item (@types) {
13452:                 if ($changes{$item}) {
13453:                     my $newtxt = $offon[$newvals{$item}];
13454:                     $resulttext .= '<li>'.
13455:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
13456:                                        '<b>','</b>').
13457:                                    '</li>';
13458:                 }
13459:             }
13460:             if ($changes{'xmldc'}) {
13461:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
13462:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
13463:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
13464:             }
13465:             $resulttext .= '</ul>';
13466:         } else {
13467:             $resulttext = &mt('No changes made to auto-creation settings');
13468:         }
13469:     } else {
13470:         $resulttext = '<span class="LC_error">'.
13471:             &mt('An error occurred: [_1]',$putresult).'</span>';
13472:     }
13473:     return $resulttext;
13474: }
13475: 
13476: sub modify_directorysrch {
13477:     my ($dom,$lastactref,%domconfig) = @_;
13478:     my ($resulttext,%changes);
13479:     my %currdirsrch;
13480:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
13481:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
13482:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
13483:         }
13484:     }
13485:     my %title = ( available => 'Institutional directory search available',
13486:                   localonly => 'Other domains can search institution',
13487:                   lcavailable => 'LON-CAPA directory search available',
13488:                   lclocalonly => 'Other domains can search LON-CAPA domain',
13489:                   searchby => 'Search types',
13490:                   searchtypes => 'Search latitude');
13491:     my @offon = ('off','on');
13492:     my @otherdoms = ('Yes','No');
13493: 
13494:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
13495:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
13496:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
13497: 
13498:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13499:     if (keys(%{$usertypes}) == 0) {
13500:         @cansearch = ('default');
13501:     } else {
13502:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
13503:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
13504:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
13505:                     push(@{$changes{'cansearch'}},$type);
13506:                 }
13507:             }
13508:             foreach my $type (@cansearch) {
13509:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
13510:                     push(@{$changes{'cansearch'}},$type);
13511:                 }
13512:             }
13513:         } else {
13514:             push(@{$changes{'cansearch'}},@cansearch);
13515:         }
13516:     }
13517: 
13518:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
13519:         foreach my $by (@{$currdirsrch{'searchby'}}) {
13520:             if (!grep(/^\Q$by\E$/,@searchby)) {
13521:                 push(@{$changes{'searchby'}},$by);
13522:             }
13523:         }
13524:         foreach my $by (@searchby) {
13525:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
13526:                 push(@{$changes{'searchby'}},$by);
13527:             }
13528:         }
13529:     } else {
13530:         push(@{$changes{'searchby'}},@searchby);
13531:     }
13532: 
13533:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
13534:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
13535:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
13536:                 push(@{$changes{'searchtypes'}},$type);
13537:             }
13538:         }
13539:         foreach my $type (@searchtypes) {
13540:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
13541:                 push(@{$changes{'searchtypes'}},$type);
13542:             }
13543:         }
13544:     } else {
13545:         if (exists($currdirsrch{'searchtypes'})) {
13546:             foreach my $type (@searchtypes) {  
13547:                 if ($type ne $currdirsrch{'searchtypes'}) { 
13548:                     push(@{$changes{'searchtypes'}},$type);
13549:                 }
13550:             }
13551:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
13552:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
13553:             }   
13554:         } else {
13555:             push(@{$changes{'searchtypes'}},@searchtypes); 
13556:         }
13557:     }
13558: 
13559:     my %dirsrch_hash =  (
13560:             directorysrch => { available => $env{'form.dirsrch_available'},
13561:                                cansearch => \@cansearch,
13562:                                localonly => $env{'form.dirsrch_instlocalonly'},
13563:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
13564:                                lcavailable => $env{'form.dirsrch_domavailable'},
13565:                                searchby => \@searchby,
13566:                                searchtypes => \@searchtypes,
13567:                              }
13568:             );
13569:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
13570:                                              $dom);
13571:     if ($putresult eq 'ok') {
13572:         if (exists($currdirsrch{'available'})) {
13573:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
13574:                  $changes{'available'} = 1;
13575:              }
13576:         } else {
13577:             if ($env{'form.dirsrch_available'} eq '1') {
13578:                 $changes{'available'} = 1;
13579:             }
13580:         }
13581:         if (exists($currdirsrch{'lcavailable'})) {
13582:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
13583:                 $changes{'lcavailable'} = 1;
13584:             }
13585:         } else {
13586:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
13587:                 $changes{'lcavailable'} = 1;
13588:             }
13589:         }
13590:         if (exists($currdirsrch{'localonly'})) {
13591:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
13592:                 $changes{'localonly'} = 1;
13593:             }
13594:         } else {
13595:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
13596:                 $changes{'localonly'} = 1;
13597:             }
13598:         }
13599:         if (exists($currdirsrch{'lclocalonly'})) {
13600:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
13601:                 $changes{'lclocalonly'} = 1;
13602:             }
13603:         } else {
13604:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
13605:                 $changes{'lclocalonly'} = 1;
13606:             }
13607:         }
13608:         if (keys(%changes) > 0) {
13609:             $resulttext = &mt('Changes made:').'<ul>';
13610:             if ($changes{'available'}) {
13611:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
13612:             }
13613:             if ($changes{'lcavailable'}) {
13614:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
13615:             }
13616:             if ($changes{'localonly'}) {
13617:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
13618:             }
13619:             if ($changes{'lclocalonly'}) {
13620:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
13621:             }
13622:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
13623:                 my $chgtext;
13624:                 if (ref($usertypes) eq 'HASH') {
13625:                     if (keys(%{$usertypes}) > 0) {
13626:                         foreach my $type (@{$types}) {
13627:                             if (grep(/^\Q$type\E$/,@cansearch)) {
13628:                                 $chgtext .= $usertypes->{$type}.'; ';
13629:                             }
13630:                         }
13631:                         if (grep(/^default$/,@cansearch)) {
13632:                             $chgtext .= $othertitle;
13633:                         } else {
13634:                             $chgtext =~ s/\; $//;
13635:                         }
13636:                         $resulttext .=
13637:                             '<li>'.
13638:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
13639:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
13640:                             '</li>';
13641:                     }
13642:                 }
13643:             }
13644:             if (ref($changes{'searchby'}) eq 'ARRAY') {
13645:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
13646:                 my $chgtext;
13647:                 foreach my $type (@{$titleorder}) {
13648:                     if (grep(/^\Q$type\E$/,@searchby)) {
13649:                         if (defined($searchtitles->{$type})) {
13650:                             $chgtext .= $searchtitles->{$type}.'; ';
13651:                         }
13652:                     }
13653:                 }
13654:                 $chgtext =~ s/\; $//;
13655:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
13656:             }
13657:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
13658:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
13659:                 my $chgtext;
13660:                 foreach my $type (@{$srchtypeorder}) {
13661:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
13662:                         if (defined($srchtypes_desc->{$type})) {
13663:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
13664:                         }
13665:                     }
13666:                 }
13667:                 $chgtext =~ s/\; $//;
13668:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
13669:             }
13670:             $resulttext .= '</ul>';
13671:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
13672:             if (ref($lastactref) eq 'HASH') {
13673:                 $lastactref->{'directorysrch'} = 1;
13674:             }
13675:         } else {
13676:             $resulttext = &mt('No changes made to directory search settings');
13677:         }
13678:     } else {
13679:         $resulttext = '<span class="LC_error">'.
13680:                       &mt('An error occurred: [_1]',$putresult).'</span>';
13681:     }
13682:     return $resulttext;
13683: }
13684: 
13685: sub modify_contacts {
13686:     my ($dom,$lastactref,%domconfig) = @_;
13687:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
13688:     if (ref($domconfig{'contacts'}) eq 'HASH') {
13689:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
13690:             $currsetting{$key} = $domconfig{'contacts'}{$key};
13691:         }
13692:     }
13693:     my (%others,%to,%bcc,%includestr,%includeloc);
13694:     my @contacts = ('supportemail','adminemail');
13695:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
13696:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
13697:     my @toggles = ('reporterrors','reportupdates','reportstatus');
13698:     my @lonstatus = ('threshold','sysmail','weights','excluded');
13699:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
13700:     foreach my $type (@mailings) {
13701:         @{$newsetting{$type}} = 
13702:             &Apache::loncommon::get_env_multiple('form.'.$type);
13703:         foreach my $item (@contacts) {
13704:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
13705:                 $contacts_hash{contacts}{$type}{$item} = 1;
13706:             } else {
13707:                 $contacts_hash{contacts}{$type}{$item} = 0;
13708:             }
13709:         }
13710:         $others{$type} = $env{'form.'.$type.'_others'};
13711:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
13712:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13713:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
13714:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
13715:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
13716:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
13717:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
13718:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
13719:             }
13720:         }
13721:     }
13722:     foreach my $item (@contacts) {
13723:         $to{$item} = $env{'form.'.$item};
13724:         $contacts_hash{'contacts'}{$item} = $to{$item};
13725:     }
13726:     foreach my $item (@toggles) {
13727:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
13728:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
13729:         }
13730:     }
13731:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
13732:     foreach my $item (@lonstatus) {
13733:         if ($item eq 'excluded') {
13734:             my (%serverhomes,@excluded);
13735:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
13736:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
13737:             if (@possexcluded) {
13738:                 foreach my $id (sort(@possexcluded)) {
13739:                     if ($serverhomes{$id}) {
13740:                         push(@excluded,$id);
13741:                     }
13742:                 }
13743:             }
13744:             if (@excluded) {
13745:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
13746:             }
13747:         } elsif ($item eq 'weights') {
13748:             foreach my $type ('E','W','N') {
13749:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
13750:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
13751:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
13752:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
13753:                             $env{'form.error'.$item.'_'.$type};
13754:                     }
13755:                 }
13756:             }
13757:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
13758:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
13759:             if ($env{'form.error'.$item} =~ /^\d+$/) {
13760:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
13761:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
13762:                 }
13763:             }
13764:         }
13765:     }
13766:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
13767:         foreach my $field (@{$fields}) {
13768:             if (ref($possoptions->{$field}) eq 'ARRAY') {
13769:                 my $value = $env{'form.helpform_'.$field};
13770:                 $value =~ s/^\s+|\s+$//g;
13771:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
13772:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
13773:                     if ($field eq 'screenshot') {
13774:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
13775:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
13776:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
13777:                         }
13778:                     }
13779:                 }
13780:             }
13781:         }
13782:     }
13783:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13784:     my (@statuses,%usertypeshash,@overrides);
13785:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
13786:         @statuses = @{$types};
13787:         if (ref($usertypes) eq 'HASH') {
13788:             %usertypeshash = %{$usertypes};
13789:         }
13790:     }
13791:     if (@statuses) {
13792:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
13793:         foreach my $type (@possoverrides) {
13794:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
13795:                 push(@overrides,$type);
13796:             }
13797:         }
13798:         if (@overrides) {
13799:             foreach my $type (@overrides) {
13800:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
13801:                 foreach my $item (@contacts) {
13802:                     if (grep(/^\Q$item\E$/,@standard)) {
13803:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
13804:                         $newsetting{'override_'.$type}{$item} = 1;
13805:                     } else {
13806:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
13807:                         $newsetting{'override_'.$type}{$item} = 0;
13808:                     }
13809:                 }
13810:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
13811:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
13812:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
13813:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
13814:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
13815:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
13816:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
13817:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
13818:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
13819:                 }
13820:             }     
13821:         }
13822:     }
13823:     if (keys(%currsetting) > 0) {
13824:         foreach my $item (@contacts) {
13825:             if ($to{$item} ne $currsetting{$item}) {
13826:                 $changes{$item} = 1;
13827:             }
13828:         }
13829:         foreach my $type (@mailings) {
13830:             foreach my $item (@contacts) {
13831:                 if (ref($currsetting{$type}) eq 'HASH') {
13832:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
13833:                         push(@{$changes{$type}},$item);
13834:                     }
13835:                 } else {
13836:                     push(@{$changes{$type}},@{$newsetting{$type}});
13837:                 }
13838:             }
13839:             if ($others{$type} ne $currsetting{$type}{'others'}) {
13840:                 push(@{$changes{$type}},'others');
13841:             }
13842:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13843:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
13844:                     push(@{$changes{$type}},'bcc'); 
13845:                 }
13846:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
13847:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
13848:                     push(@{$changes{$type}},'include');
13849:                 }
13850:             }
13851:         }
13852:         if (ref($fields) eq 'ARRAY') {
13853:             if (ref($currsetting{'helpform'}) eq 'HASH') {
13854:                 foreach my $field (@{$fields}) {
13855:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
13856:                         push(@{$changes{'helpform'}},$field);
13857:                     }
13858:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13859:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
13860:                             push(@{$changes{'helpform'}},'maxsize');
13861:                         }
13862:                     }
13863:                 }
13864:             } else {
13865:                 foreach my $field (@{$fields}) {
13866:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
13867:                         push(@{$changes{'helpform'}},$field);
13868:                     }
13869:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
13870:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
13871:                             push(@{$changes{'helpform'}},'maxsize');
13872:                         }
13873:                     }
13874:                 }
13875:             }
13876:         }
13877:         if (@statuses) {
13878:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
13879:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
13880:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
13881:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
13882:                             foreach my $item (@contacts,'bcc','others','include') {
13883:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
13884:                                     push(@{$changes{'overrides'}},$key);
13885:                                     last;
13886:                                 }
13887:                             }
13888:                         } else {
13889:                             push(@{$changes{'overrides'}},$key);
13890:                         }
13891:                     }
13892:                 }
13893:                 foreach my $key (@overrides) {
13894:                     unless (exists($currsetting{'overrides'}{$key})) {
13895:                         push(@{$changes{'overrides'}},$key);
13896:                     }
13897:                 }
13898:             } else {
13899:                 foreach my $key (@overrides) {
13900:                     push(@{$changes{'overrides'}},$key); 
13901:                 }
13902:             }
13903:         }
13904:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
13905:             foreach my $key ('excluded','weights','threshold','sysmail') {
13906:                 if ($key eq 'excluded') {
13907:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13908:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
13909:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13910:                             (@{$currsetting{'lonstatus'}{$key}})) {
13911:                             my @diffs =
13912:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
13913:                                                                    $currsetting{'lonstatus'}{$key});
13914:                             if (@diffs) {
13915:                                 push(@{$changes{'lonstatus'}},$key);
13916:                             }
13917:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
13918:                             push(@{$changes{'lonstatus'}},$key);
13919:                         }
13920:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
13921:                              (@{$currsetting{'lonstatus'}{$key}})) {
13922:                         push(@{$changes{'lonstatus'}},$key);
13923:                     }
13924:                 } elsif ($key eq 'weights') {
13925:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
13926:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
13927:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13928:                             foreach my $type ('E','W','N','U') {
13929:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
13930:                                         $currsetting{'lonstatus'}{$key}{$type}) {
13931:                                     push(@{$changes{'lonstatus'}},$key);
13932:                                     last;
13933:                                 }
13934:                             }
13935:                         } else {
13936:                             foreach my $type ('E','W','N','U') {
13937:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
13938:                                     push(@{$changes{'lonstatus'}},$key);
13939:                                     last;
13940:                                 }
13941:                             }
13942:                         }
13943:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
13944:                         foreach my $type ('E','W','N','U') {
13945:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
13946:                                 push(@{$changes{'lonstatus'}},$key);
13947:                                 last;
13948:                             }
13949:                         }
13950:                     }
13951:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
13952:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13953:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13954:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
13955:                                 push(@{$changes{'lonstatus'}},$key);
13956:                             }
13957:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
13958:                             push(@{$changes{'lonstatus'}},$key);
13959:                         }
13960:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
13961:                         push(@{$changes{'lonstatus'}},$key);
13962:                     }
13963:                 }
13964:             }
13965:         } else {
13966:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
13967:                 foreach my $key ('excluded','weights','threshold','sysmail') {
13968:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
13969:                         push(@{$changes{'lonstatus'}},$key);
13970:                     }
13971:                 }
13972:             }
13973:         }
13974:     } else {
13975:         my %default;
13976:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
13977:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
13978:         $default{'errormail'} = 'adminemail';
13979:         $default{'packagesmail'} = 'adminemail';
13980:         $default{'helpdeskmail'} = 'supportemail';
13981:         $default{'otherdomsmail'} = 'supportemail';
13982:         $default{'lonstatusmail'} = 'adminemail';
13983:         $default{'requestsmail'} = 'adminemail';
13984:         $default{'updatesmail'} = 'adminemail';
13985:         $default{'hostipmail'} = 'adminemail';
13986:         foreach my $item (@contacts) {
13987:            if ($to{$item} ne $default{$item}) {
13988:                $changes{$item} = 1;
13989:            }
13990:         }
13991:         foreach my $type (@mailings) {
13992:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
13993:                 push(@{$changes{$type}},@{$newsetting{$type}});
13994:             }
13995:             if ($others{$type} ne '') {
13996:                 push(@{$changes{$type}},'others');
13997:             }
13998:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
13999:                 if ($bcc{$type} ne '') {
14000:                     push(@{$changes{$type}},'bcc');
14001:                 }
14002:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
14003:                     push(@{$changes{$type}},'include');
14004:                 }
14005:             }
14006:         }
14007:         if (ref($fields) eq 'ARRAY') {
14008:             foreach my $field (@{$fields}) {
14009:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
14010:                     push(@{$changes{'helpform'}},$field);
14011:                 }
14012:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
14013:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
14014:                         push(@{$changes{'helpform'}},'maxsize');
14015:                     }
14016:                 }
14017:             }
14018:         }
14019:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
14020:             foreach my $key ('excluded','weights','threshold','sysmail') {
14021:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
14022:                     push(@{$changes{'lonstatus'}},$key);
14023:                 }
14024:             }
14025:         }
14026:     }
14027:     foreach my $item (@toggles) {
14028:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
14029:             $changes{$item} = 1;
14030:         } elsif ((!$env{'form.'.$item}) &&
14031:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
14032:             $changes{$item} = 1;
14033:         }
14034:     }
14035:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
14036:                                              $dom);
14037:     if ($putresult eq 'ok') {
14038:         if (keys(%changes) > 0) {
14039:             &Apache::loncommon::devalidate_domconfig_cache($dom);
14040:             if (ref($lastactref) eq 'HASH') {
14041:                 $lastactref->{'domainconfig'} = 1;
14042:             }
14043:             my ($titles,$short_titles)  = &contact_titles();
14044:             $resulttext = &mt('Changes made:').'<ul>';
14045:             foreach my $item (@contacts) {
14046:                 if ($changes{$item}) {
14047:                     $resulttext .= '<li>'.$titles->{$item}.
14048:                                     &mt(' set to: ').
14049:                                     '<span class="LC_cusr_emph">'.
14050:                                     $to{$item}.'</span></li>';
14051:                 }
14052:             }
14053:             foreach my $type (@mailings) {
14054:                 if (ref($changes{$type}) eq 'ARRAY') {
14055:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
14056:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
14057:                     } else {
14058:                         $resulttext .= '<li>'.$titles->{$type}.': ';
14059:                     }
14060:                     my @text;
14061:                     foreach my $item (@{$newsetting{$type}}) {
14062:                         push(@text,$short_titles->{$item});
14063:                     }
14064:                     if ($others{$type} ne '') {
14065:                         push(@text,$others{$type});
14066:                     }
14067:                     if (@text) {
14068:                         $resulttext .= '<span class="LC_cusr_emph">'.
14069:                                        join(', ',@text).'</span>';
14070:                     }
14071:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
14072:                         if ($bcc{$type} ne '') {
14073:                             my $bcctext;
14074:                             if (@text) {
14075:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
14076:                             } else {
14077:                                 $bcctext = '(Bcc)';
14078:                             }
14079:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
14080:                         } elsif (!@text) {
14081:                             $resulttext .= &mt('No one');
14082:                         }   
14083:                         if ($includestr{$type} ne '') {
14084:                             if ($includeloc{$type} eq 'b') {
14085:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
14086:                             } elsif ($includeloc{$type} eq 's') {
14087:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
14088:                             }
14089:                         }
14090:                     } elsif (!@text) {
14091:                         $resulttext .= &mt('No recipients');
14092:                     }
14093:                     $resulttext .= '</li>';
14094:                 }
14095:             }
14096:             if (ref($changes{'overrides'}) eq 'ARRAY') {
14097:                 my @deletions;
14098:                 foreach my $type (@{$changes{'overrides'}}) {
14099:                     if ($usertypeshash{$type}) {
14100:                         if (grep(/^\Q$type\E/,@overrides)) {
14101:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
14102:                                                       $usertypeshash{$type}).'<ul><li>';
14103:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
14104:                                 my @text;
14105:                                 foreach my $item (@contacts) {
14106:                                     if ($newsetting{'override_'.$type}{$item}) { 
14107:                                         push(@text,$short_titles->{$item});
14108:                                     }
14109:                                 }
14110:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
14111:                                     push(@text,$newsetting{'override_'.$type}{'others'});
14112:                                 }
14113:   
14114:                                 if (@text) {
14115:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
14116:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
14117:                                 }
14118:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
14119:                                     my $bcctext;
14120:                                     if (@text) {
14121:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
14122:                                     } else {
14123:                                         $bcctext = '(Bcc)';
14124:                                     }
14125:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
14126:                                 } elsif (!@text) {
14127:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
14128:                                 }
14129:                                 $resulttext .= '</li>';
14130:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
14131:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
14132:                                     if ($loc eq 'b') {
14133:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
14134:                                     } elsif ($loc eq 's') {
14135:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
14136:                                     }
14137:                                 }
14138:                             }
14139:                             $resulttext .= '</li></ul></li>';
14140:                         } else {
14141:                             push(@deletions,$usertypeshash{$type});
14142:                         }
14143:                     }
14144:                 }
14145:                 if (@deletions) {
14146:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
14147:                                               join(', ',@deletions)).'</li>';
14148:                 }
14149:             }
14150:             my @offon = ('off','on');
14151:             my $corelink = &core_link_msu();
14152:             if ($changes{'reporterrors'}) {
14153:                 $resulttext .= '<li>'.
14154:                                &mt('E-mail error reports to [_1] set to "'.
14155:                                    $offon[$env{'form.reporterrors'}].'".',
14156:                                    $corelink).
14157:                                '</li>';
14158:             }
14159:             if ($changes{'reportupdates'}) {
14160:                 $resulttext .= '<li>'.
14161:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
14162:                                     $offon[$env{'form.reportupdates'}].'".',
14163:                                     $corelink).
14164:                                 '</li>';
14165:             }
14166:             if ($changes{'reportstatus'}) {
14167:                 $resulttext .= '<li>'.
14168:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
14169:                                     $offon[$env{'form.reportstatus'}].'".',
14170:                                     $corelink).
14171:                                 '</li>';
14172:             }
14173:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
14174:                 $resulttext .= '<li>'.
14175:                                &mt('Nightly status check e-mail settings').':<ul>';
14176:                 my (%defval,%use_def,%shown);
14177:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
14178:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
14179:                 $defval{'weights'} =
14180:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
14181:                 $defval{'excluded'} = &mt('None');
14182:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
14183:                     foreach my $item ('threshold','sysmail','weights','excluded') {
14184:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
14185:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
14186:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
14187:                             } elsif ($item eq 'weights') {
14188:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
14189:                                     foreach my $type ('E','W','N','U') {
14190:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
14191:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
14192:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
14193:                                         } else {
14194:                                             $shown{$item} .= $lonstatus_defs->{$type};
14195:                                         }
14196:                                         $shown{$item} .= ', ';
14197:                                     }
14198:                                     $shown{$item} =~ s/, $//;
14199:                                 } else {
14200:                                     $shown{$item} = $defval{$item};
14201:                                 }
14202:                             } elsif ($item eq 'excluded') {
14203:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
14204:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
14205:                                 } else {
14206:                                     $shown{$item} = $defval{$item};
14207:                                 }
14208:                             }
14209:                         } else {
14210:                             $shown{$item} = $defval{$item};
14211:                         }
14212:                     }
14213:                 } else {
14214:                     foreach my $item ('threshold','weights','excluded','sysmail') {
14215:                         $shown{$item} = $defval{$item};
14216:                     }
14217:                 }
14218:                 foreach my $item ('threshold','weights','excluded','sysmail') {
14219:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
14220:                                           $shown{$item}).'</li>';
14221:                 }
14222:                 $resulttext .= '</ul></li>';
14223:             }
14224:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
14225:                 my (@optional,@required,@unused,$maxsizechg);
14226:                 foreach my $field (@{$changes{'helpform'}}) {
14227:                     if ($field eq 'maxsize') {
14228:                         $maxsizechg = 1;
14229:                         next;
14230:                     }
14231:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
14232:                         push(@optional,$field);
14233:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
14234:                         push(@unused,$field);
14235:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
14236:                         push(@required,$field);
14237:                     }
14238:                 }
14239:                 if (@optional) {
14240:                     $resulttext .= '<li>'.
14241:                                    &mt('Help form fields changed to "Optional": [_1].',
14242:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
14243:                                    '</li>';
14244:                 }
14245:                 if (@required) {
14246:                     $resulttext .= '<li>'.
14247:                                    &mt('Help form fields changed to "Required": [_1].',
14248:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
14249:                                    '</li>';
14250:                 }
14251:                 if (@unused) {
14252:                     $resulttext .= '<li>'.
14253:                                    &mt('Help form fields changed to "Not shown": [_1].',
14254:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
14255:                                    '</li>';
14256:                 }
14257:                 if ($maxsizechg) {
14258:                     $resulttext .= '<li>'.
14259:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
14260:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
14261:                                    '</li>';
14262:                 }
14263:             }
14264:             $resulttext .= '</ul>';
14265:         } else {
14266:             $resulttext = &mt('No changes made to contacts and form settings');
14267:         }
14268:     } else {
14269:         $resulttext = '<span class="LC_error">'.
14270:             &mt('An error occurred: [_1].',$putresult).'</span>';
14271:     }
14272:     return $resulttext;
14273: }
14274: 
14275: sub modify_privacy {
14276:     my ($dom,%domconfig) = @_;
14277:     my ($resulttext,%current,%changes);
14278:     if (ref($domconfig{'privacy'}) eq 'HASH') {
14279:         %current = %{$domconfig{'privacy'}};
14280:     }
14281:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
14282:     my @items = ('domain','author','course','community');
14283:     my %names = &Apache::lonlocal::texthash (
14284:                    domain => 'Assigned domain role(s)',
14285:                    author => 'Assigned co-author role(s)',
14286:                    course => 'Assigned course role(s)',
14287:                    community => 'Assigned community role',
14288:                 );
14289:     my %roles = &Apache::lonlocal::texthash (
14290:                    domain => 'Domain role',
14291:                    author => 'Co-author role',
14292:                    course => 'Course role',
14293:                    community => 'Community role',
14294:                 );
14295:     my %titles = &Apache::lonlocal::texthash (
14296:                   approval => 'Approval for role in different domain',
14297:                   othdom   => 'User information available in other domain',
14298:                   priv     => 'Information viewable by privileged user in same domain',
14299:                   unpriv   => 'Information viewable by unprivileged user in same domain',
14300:                   instdom  => 'Other domain shares institution/provider',
14301:                   extdom   => 'Other domain has different institution/provider',
14302:                   none     => 'Not allowed',
14303:                   user     => 'User authorizes',
14304:                   domain   => 'Domain Coordinator authorizes',
14305:                   auto     => 'Unrestricted',
14306:     );
14307:     my %fieldnames = &Apache::lonlocal::texthash (
14308:                         id => 'Student/Employee ID',
14309:                         permanentemail => 'E-mail address',
14310:                         lastname => 'Last Name',
14311:                         firstname => 'First Name',
14312:                         middlename => 'Middle Name',
14313:                         generation => 'Generation',
14314:     );
14315:     my ($othertitle,$usertypes,$types) =
14316:         &Apache::loncommon::sorted_inst_types($dom);
14317:     my (%by_ip,%by_location,@intdoms,@instdoms);
14318:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14319: 
14320:     my %privacyhash = (
14321:                        'approval' => {
14322:                                        instdom => {},
14323:                                        extdom  => {},
14324:                                      },
14325:                        'othdom'   => {},
14326:                        'priv'     => {},
14327:                        'unpriv'   => {},
14328:                       );
14329:     foreach my $item (@items) {
14330:         if (@instdoms > 1) {
14331:             if ($env{'form.privacy_approval_instdom'.$item} =~ /^(none|user|domain|auto)$/) {
14332:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
14333:             }
14334:             if (ref($current{'approval'}) eq 'HASH') {
14335:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
14336:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
14337:                         $changes{'approval'} = 1;
14338:                     }
14339:                 }
14340:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
14341:                 $changes{'approval'} = 1;
14342:             }
14343:         }
14344:         if (keys(%by_location) > 0) {
14345:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
14346:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
14347:             }
14348:             if (ref($current{'approval'}) eq 'HASH') {
14349:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
14350:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
14351:                         $changes{'approval'} = 1;
14352:                     }
14353:                 }
14354:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
14355:                 $changes{'approval'} = 1;
14356:             }
14357:         }
14358:         foreach my $status ('priv','unpriv') {
14359:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
14360:             my @newvalues;
14361:             foreach my $field (@possibles) {
14362:                 if (grep(/^\Q$field\E$/,@fields)) {
14363:                     $privacyhash{$status}{$item}{$field} = 1;
14364:                     push(@newvalues,$field);
14365:                 }
14366:             }
14367:             @newvalues = sort(@newvalues);
14368:             if (ref($current{$status}) eq 'HASH') {
14369:                 if (ref($current{$status}{$item}) eq 'HASH') {
14370:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
14371:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
14372:                     if (@diffs > 0) {
14373:                         $changes{$status} = 1;
14374:                     }
14375:                 }
14376:             } else {
14377:                 my @stdfields;
14378:                 foreach my $field (@fields) {
14379:                     if ($field eq 'id') {
14380:                         next if ($status eq 'unpriv');
14381:                         next if (($status eq 'priv') && ($item eq 'community'));
14382:                     }
14383:                     push(@stdfields,$field);
14384:                 }
14385:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
14386:                 if (@diffs > 0) {
14387:                     $changes{$status} = 1;
14388:                 }
14389:             }
14390:         }
14391:     }
14392:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
14393:         my @statuses;
14394:         if (ref($types) eq 'ARRAY') {
14395:             @statuses = @{$types};
14396:         }
14397:         foreach my $type (@statuses,'default') {
14398:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
14399:             my @newvalues;
14400:             foreach my $field (sort(@possfields)) {
14401:                 if (grep(/^\Q$field\E$/,@fields)) {
14402:                     $privacyhash{'othdom'}{$type}{$field} = 1;
14403:                     push(@newvalues,$field);
14404:                 }
14405:             }
14406:             @newvalues = sort(@newvalues);
14407:             if (ref($current{'othdom'}) eq 'HASH') {
14408:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
14409:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
14410:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
14411:                     if (@diffs > 0) {
14412:                         $changes{'othdom'} = 1;
14413:                     }
14414:                 }
14415:             } else {
14416:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
14417:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
14418:                 if (@diffs > 0) {
14419:                     $changes{'othdom'} = 1;
14420:                 }
14421:             }
14422:         }
14423:     }
14424:     my %confighash = (
14425:                         privacy => \%privacyhash,
14426:                      );
14427:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
14428:     if ($putresult eq 'ok') {
14429:         if (keys(%changes) > 0) {
14430:             $resulttext = &mt('Changes made: ').'<ul>';
14431:             foreach my $key ('approval','othdom','priv','unpriv') {
14432:                 if ($changes{$key}) {
14433:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
14434:                     if ($key eq 'approval') {
14435:                         if (keys(%{$privacyhash{$key}{instdom}})) {
14436:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
14437:                             foreach my $item (@items) {
14438:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
14439:                             }
14440:                             $resulttext .= '</ul></li>';
14441:                         }
14442:                         if (keys(%{$privacyhash{$key}{extdom}})) {
14443:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
14444:                             foreach my $item (@items) {
14445:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
14446:                             }
14447:                             $resulttext .= '</ul></li>';
14448:                         }
14449:                     } elsif ($key eq 'othdom') {
14450:                         my @statuses;
14451:                         if (ref($types) eq 'ARRAY') {
14452:                             @statuses = @{$types};
14453:                         }
14454:                         if (ref($privacyhash{$key}) eq 'HASH') {
14455:                             foreach my $status (@statuses,'default') {
14456:                                 if ($status eq 'default') {
14457:                                     $resulttext .= '<li>'.$othertitle.': ';
14458:                                 } elsif (ref($usertypes) eq 'HASH') {
14459:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
14460:                                 } else {
14461:                                     next;
14462:                                 }
14463:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
14464:                                     if (keys(%{$privacyhash{$key}{$status}})) {
14465:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
14466:                                     } else {
14467:                                         $resulttext .= &mt('none');
14468:                                     }
14469:                                 }
14470:                                 $resulttext .= '</li>';
14471:                             }
14472:                         }
14473:                     } else {
14474:                         foreach my $item (@items) {
14475:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
14476:                                 $resulttext .= '<li>'.$names{$item}.': ';
14477:                                 if (keys(%{$privacyhash{$key}{$item}})) {
14478:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
14479:                                 } else {
14480:                                     $resulttext .= &mt('none');
14481:                                 }
14482:                                 $resulttext .= '</li>';
14483:                             }
14484:                         }
14485:                     }
14486:                     $resulttext .= '</ul></li>';
14487:                 }
14488:             }
14489:         } else {
14490:             $resulttext = &mt('No changes made to user information settings');
14491:         }
14492:     } else {
14493:         $resulttext = '<span class="LC_error">'.
14494:             &mt('An error occurred: [_1]',$putresult).'</span>';
14495:     }
14496:     return $resulttext;
14497: }
14498: 
14499: sub modify_passwords {
14500:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
14501:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
14502:         $updatedefaults,$updateconf);
14503:     my $customfn = 'resetpw.html';
14504:     if (ref($domconfig{'passwords'}) eq 'HASH') {
14505:         %current = %{$domconfig{'passwords'}};
14506:     }
14507:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14508:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14509:     if (ref($types) eq 'ARRAY') {
14510:         @oktypes = @{$types};
14511:     }
14512:     push(@oktypes,'default');
14513: 
14514:     my %titles = &Apache::lonlocal::texthash (
14515:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
14516:         intauth_check  => 'Check bcrypt cost if authenticated',
14517:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
14518:         permanent      => 'Permanent e-mail address',
14519:         critical       => 'Critical notification address',
14520:         notify         => 'Notification address',
14521:         min            => 'Minimum password length',
14522:         max            => 'Maximum password length',
14523:         chars          => 'Required characters',
14524:         expire         => 'Password expiration (days)',
14525:         numsaved       => 'Number of previous passwords to save',
14526:         reset          => 'Resetting Forgotten Password',
14527:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
14528:         rules          => 'Rules for LON-CAPA Passwords',
14529:         crsownerchg    => 'Course Owner Changing Student Passwords',
14530:         username       => 'Username',
14531:         email          => 'E-mail address',
14532:     );
14533: 
14534: #
14535: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
14536: #
14537:     my (%curr_defaults,%save_defaults);
14538:     if (ref($domconfig{'defaults'}) eq 'HASH') {
14539:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
14540:             if ($key =~ /^intauth_(cost|check|switch)$/) {
14541:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
14542:             } else {
14543:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
14544:             }
14545:         }
14546:     }
14547:     my %staticdefaults = (
14548:         'resetlink'      => 2,
14549:         'resetcase'      => \@oktypes,
14550:         'resetprelink'   => 'both',
14551:         'resetemail'     => ['critical','notify','permanent'],
14552:         'intauth_cost'   => 10,
14553:         'intauth_check'  => 0,
14554:         'intauth_switch' => 0,
14555:     );
14556:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
14557:     foreach my $type (@oktypes) {
14558:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
14559:     }
14560:     my $linklife = $env{'form.passwords_link'};
14561:     $linklife =~ s/^\s+|\s+$//g;
14562:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
14563:         $newvalues{'resetlink'} = $linklife;
14564:         if ($current{'resetlink'}) {
14565:             if ($current{'resetlink'} ne $linklife) {
14566:                 $changes{'reset'} = 1;
14567:             }
14568:         } elsif (!exists($domconfig{passwords})) {
14569:             if ($staticdefaults{'resetlink'} ne $linklife) {
14570:                 $changes{'reset'} = 1;
14571:             }
14572:         }
14573:     } elsif ($current{'resetlink'}) {
14574:         $changes{'reset'} = 1;
14575:     }
14576:     my @casesens;
14577:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
14578:     foreach my $case (sort(@posscase)) {
14579:         if (grep(/^\Q$case\E$/,@oktypes)) {
14580:             push(@casesens,$case);
14581:         }
14582:     }
14583:     $newvalues{'resetcase'} = \@casesens;
14584:     if (ref($current{'resetcase'}) eq 'ARRAY') {
14585:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
14586:         if (@diffs > 0) {
14587:             $changes{'reset'} = 1;
14588:         }
14589:     } elsif (!exists($domconfig{passwords})) {
14590:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
14591:         if (@diffs > 0) {
14592:             $changes{'reset'} = 1;
14593:         }
14594:     }
14595:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
14596:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
14597:         if (exists($current{'resetprelink'})) {
14598:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
14599:                 $changes{'reset'} = 1;
14600:             }
14601:         } elsif (!exists($domconfig{passwords})) {
14602:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
14603:                 $changes{'reset'} = 1;
14604:             }
14605:         }
14606:     } elsif ($current{'resetprelink'}) {
14607:         $changes{'reset'} = 1;
14608:     }
14609:     foreach my $type (@oktypes) {
14610:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
14611:         my @postlink;
14612:         foreach my $item (sort(@possplink)) {
14613:             if ($item =~ /^(email|username)$/) {
14614:                 push(@postlink,$item);
14615:             }
14616:         }
14617:         $newvalues{'resetpostlink'}{$type} = \@postlink;
14618:         unless ($changes{'reset'}) {
14619:             if (ref($current{'resetpostlink'}) eq 'HASH') {
14620:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
14621:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
14622:                     if (@diffs > 0) {
14623:                         $changes{'reset'} = 1;
14624:                     }
14625:                 } else {
14626:                     $changes{'reset'} = 1;
14627:                 }
14628:             } elsif (!exists($domconfig{passwords})) {
14629:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
14630:                 if (@diffs > 0) {
14631:                     $changes{'reset'} = 1;
14632:                 }
14633:             }
14634:         }
14635:     }
14636:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
14637:     my @resetemail;
14638:     foreach my $item (sort(@possemailsrc)) {
14639:         if ($item =~ /^(permanent|critical|notify)$/) {
14640:             push(@resetemail,$item);
14641:         }
14642:     }
14643:     $newvalues{'resetemail'} = \@resetemail;
14644:     unless ($changes{'reset'}) {
14645:         if (ref($current{'resetemail'}) eq 'ARRAY') {
14646:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
14647:             if (@diffs > 0) {
14648:                 $changes{'reset'} = 1;
14649:             }
14650:         } elsif (!exists($domconfig{passwords})) {
14651:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
14652:             if (@diffs > 0) {
14653:                 $changes{'reset'} = 1;
14654:             }
14655:         }
14656:     }
14657:     if ($env{'form.passwords_stdtext'} == 0) {
14658:         $newvalues{'resetremove'} = 1;
14659:         unless ($current{'resetremove'}) {
14660:             $changes{'reset'} = 1;
14661:         }
14662:     } elsif ($current{'resetremove'}) {
14663:         $changes{'reset'} = 1;
14664:     }
14665:     if ($env{'form.passwords_customfile.filename'} ne '') {
14666:         my $servadm = $r->dir_config('lonAdmEMail');
14667:         my ($configuserok,$author_ok,$switchserver) =
14668:             &config_check($dom,$confname,$servadm);
14669:         my $error;
14670:         if ($configuserok eq 'ok') {
14671:             if ($switchserver) {
14672:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
14673:             } else {
14674:                 if ($author_ok eq 'ok') {
14675:                     my ($result,$customurl) =
14676:                         &publishlogo($r,'upload','passwords_customfile',$dom,
14677:                                      $confname,'customtext/resetpw','','',$customfn);
14678:                     if ($result eq 'ok') {
14679:                         $newvalues{'resetcustom'} = $customurl;
14680:                         $changes{'reset'} = 1;
14681:                     } else {
14682:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
14683:                     }
14684:                 } else {
14685:                     $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);
14686:                 }
14687:             }
14688:         } else {
14689:             $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);
14690:         }
14691:         if ($error) {
14692:             &Apache::lonnet::logthis($error);
14693:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14694:         }
14695:     } elsif ($current{'resetcustom'}) {
14696:         if ($env{'form.passwords_custom_del'}) {
14697:             $changes{'reset'} = 1;
14698:         } else {
14699:             $newvalues{'resetcustom'} = $current{'resetcustom'};
14700:         }
14701:     }
14702:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
14703:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
14704:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
14705:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
14706:             $changes{'intauth'} = 1;
14707:         }
14708:     } else {
14709:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
14710:     }
14711:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
14712:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
14713:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
14714:             $changes{'intauth'} = 1;
14715:         }
14716:     } else {
14717:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
14718:     }
14719:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
14720:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
14721:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
14722:             $changes{'intauth'} = 1;
14723:         }
14724:     } else {
14725:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
14726:     }
14727:     foreach my $item ('cost','check','switch') {
14728:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
14729:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
14730:             $updatedefaults = 1;
14731:         }
14732:     }
14733:     foreach my $rule ('min','max','expire','numsaved') {
14734:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
14735:         my $ruleok;
14736:         if ($rule eq 'expire') {
14737:             if (($env{'form.passwords_'.$rule} =~ /^\d+(|\.\d*)$/) &&
14738:                 ($env{'form.passwords_'.$rule} ne '0')) {
14739:                 $ruleok = 1;
14740:             }
14741:         } elsif ($rule eq 'min') {
14742:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
14743:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
14744:                     $ruleok = 1;
14745:                 }
14746:             }
14747:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
14748:                  ($env{'form.passwords_'.$rule} ne '0')) {
14749:             $ruleok = 1;
14750:         }
14751:         if ($ruleok) {
14752:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
14753:             if (exists($current{$rule})) {
14754:                 if ($newvalues{$rule} ne $current{$rule}) {
14755:                     $changes{'rules'} = 1;
14756:                 }
14757:             } elsif ($rule eq 'min') {
14758:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
14759:                     $changes{'rules'} = 1;
14760:                 }
14761:             }
14762:         } elsif (exists($current{$rule})) {
14763:             $changes{'rules'} = 1;
14764:         }
14765:     }
14766:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
14767:     my @chars;
14768:     foreach my $item (sort(@posschars)) {
14769:         if ($item =~ /^(uc|lc|num|spec)$/) {
14770:             push(@chars,$item);
14771:         }
14772:     }
14773:     $newvalues{'chars'} = \@chars;
14774:     unless ($changes{'rules'}) {
14775:         if (ref($current{'chars'}) eq 'ARRAY') {
14776:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
14777:             if (@diffs > 0) {
14778:                 $changes{'rules'} = 1;
14779:             }
14780:         } else {
14781:             if (@chars > 0) {
14782:                 $changes{'rules'} = 1;
14783:             }
14784:         }
14785:     }
14786:     my %crsownerchg = (
14787:                         by => [],
14788:                         for => [],
14789:                       );
14790:     foreach my $item ('by','for') {
14791:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
14792:         foreach my $type (sort(@posstypes)) {
14793:             if (grep(/^\Q$type\E$/,@oktypes)) {
14794:                 push(@{$crsownerchg{$item}},$type);
14795:             }
14796:         }
14797:     }
14798:     $newvalues{'crsownerchg'} = \%crsownerchg;
14799:     if (ref($current{'crsownerchg'}) eq 'HASH') {
14800:         foreach my $item ('by','for') {
14801:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
14802:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
14803:                 if (@diffs > 0) {
14804:                     $changes{'crsownerchg'} = 1;
14805:                     last;
14806:                 }
14807:             }
14808:         }
14809:     } elsif (!exists($domconfig{passwords})) {
14810:         foreach my $item ('by','for') {
14811:             if (@{$crsownerchg{$item}} > 0) {
14812:                 $changes{'crsownerchg'} = 1;
14813:                 last;
14814:             }
14815:         }
14816:     }
14817: 
14818:     my %confighash = (
14819:                         defaults  => \%save_defaults,
14820:                         passwords => \%newvalues,
14821:                      );
14822:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
14823: 
14824:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
14825:     if ($putresult eq 'ok') {
14826:         if (keys(%changes) > 0) {
14827:             $resulttext = &mt('Changes made: ').'<ul>';
14828:             foreach my $key ('reset','intauth','rules','crsownerchg') {
14829:                 if ($changes{$key}) {
14830:                     unless ($key eq 'intauth') {
14831:                         $updateconf = 1;
14832:                     }
14833:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
14834:                     if ($key eq 'reset') {
14835:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
14836:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
14837:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
14838:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
14839:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />'.
14840:                                            &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchapub'}).'</br>'.
14841:                                            &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchapriv'}).'</li>';
14842:                         } else {
14843:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
14844:                         }
14845:                         if ($confighash{'passwords'}{'resetlink'}) {
14846:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
14847:                         } else {
14848:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
14849:                                                   &mt('Will default to 2 hours').'</li>';
14850:                         }
14851:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
14852:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
14853:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
14854:                             } else {
14855:                                 my $casesens;
14856:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
14857:                                     if ($type eq 'default') {
14858:                                         $casesens .= $othertitle.', ';
14859:                                     } elsif ($usertypes->{$type} ne '') {
14860:                                         $casesens .= $usertypes->{$type}.', ';
14861:                                     }
14862:                                 }
14863:                                 $casesens =~ s/\Q, \E$//;
14864:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
14865:                             }
14866:                         } else {
14867:                             $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>';
14868:                         }
14869:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
14870:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
14871:                         } else {
14872:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
14873:                         }
14874:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
14875:                             my $output;
14876:                             if (ref($types) eq 'ARRAY') {
14877:                                 foreach my $type (@{$types}) {
14878:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
14879:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
14880:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
14881:                                         } else {
14882:                                             $output .= $usertypes->{$type}.' -- '.
14883:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
14884:                                         }
14885:                                     }
14886:                                 }
14887:                             }
14888:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
14889:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
14890:                                     $output .= $othertitle.' -- '.&mt('none');
14891:                                 } else {
14892:                                     $output .= $othertitle.' -- '.
14893:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
14894:                                 }
14895:                             }
14896:                             if ($output) {
14897:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
14898:                             } else {
14899:                                 $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>';
14900:                             }
14901:                         } else {
14902:                             $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>';
14903:                         }
14904:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
14905:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
14906:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
14907:                             } else {
14908:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
14909:                             }
14910:                         } else {
14911:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA usedfor verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
14912:                         }
14913:                         if ($confighash{'passwords'}{'resetremove'}) {
14914:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
14915:                         } else {
14916:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
14917:                         }
14918:                         if ($confighash{'passwords'}{'resetcustom'}) {
14919:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
14920:                                                                             $titles{custom},600,500);
14921:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes [_1]',$customlink).'</li>';
14922:                         } else {
14923:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
14924:                         }
14925:                     } elsif ($key eq 'intauth') {
14926:                         foreach my $item ('cost','switch','check') {
14927:                             my $value = $save_defaults{$key.'_'.$item};
14928:                             if ($item eq 'switch') {
14929:                                 my %optiondesc = &Apache::lonlocal::texthash (
14930:                                                      0 => 'No',
14931:                                                      1 => 'Yes',
14932:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
14933:                                                  );
14934:                                 if ($value =~ /^(0|1|2)$/) {
14935:                                     $value = $optiondesc{$value};
14936:                                 } else {
14937:                                     $value = &mt('none -- defaults to No');
14938:                                 }
14939:                             } elsif ($item eq 'check') {
14940:                                 my %optiondesc = &Apache::lonlocal::texthash (
14941:                                                      0 => 'No',
14942:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
14943:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
14944:                                                  );
14945:                                 if ($value =~ /^(0|1|2)$/) {
14946:                                     $value = $optiondesc{$value};
14947:                                 } else {
14948:                                     $value = &mt('none -- defaults to No');
14949:                                 }
14950:                             }
14951:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
14952:                         }
14953:                     } elsif ($key eq 'rules') {
14954:                         foreach my $rule ('min','max','expire','numsaved') {
14955:                             if ($confighash{'passwords'}{$rule} eq '') {
14956:                                 if ($rule eq 'min') {
14957:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
14958:                                                    ' '.&mt('Default of [_1] will be used',
14959:                                                            $Apache::lonnet::passwdmin).'</li>';
14960:                                 } else {
14961:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
14962:                                 }
14963:                             } else {
14964:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
14965:                             }
14966:                         }
14967:                     } elsif ($key eq 'crsownerchg') {
14968:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
14969:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
14970:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
14971:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
14972:                             } else {
14973:                                 my %crsownerstr;
14974:                                 foreach my $item ('by','for') {
14975:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
14976:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
14977:                                             if ($type eq 'default') {
14978:                                                 $crsownerstr{$item} .= $othertitle.', ';
14979:                                             } elsif ($usertypes->{$type} ne '') {
14980:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
14981:                                             }
14982:                                         }
14983:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
14984:                                     }
14985:                                 }
14986:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
14987:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
14988:                             }
14989:                         } else {
14990:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
14991:                         }
14992:                     }
14993:                     $resulttext .= '</ul></li>';
14994:                 }
14995:             }
14996:             $resulttext .= '</ul>';
14997:         } else {
14998:             $resulttext = &mt('No changes made to password settings');
14999:         }
15000:         my $cachetime = 24*60*60;
15001:         if ($updatedefaults) {
15002:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15003:             if (ref($lastactref) eq 'HASH') {
15004:                 $lastactref->{'domdefaults'} = 1;
15005:             }
15006:         }
15007:         if ($updateconf) {
15008:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
15009:             if (ref($lastactref) eq 'HASH') {
15010:                 $lastactref->{'passwdconf'} = 1;
15011:             }
15012:         }
15013:     } else {
15014:         $resulttext = '<span class="LC_error">'.
15015:             &mt('An error occurred: [_1]',$putresult).'</span>';
15016:     }
15017:     if ($errors) {
15018:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
15019:                        $errors.'</ul></p>';
15020:     }
15021:     return $resulttext;
15022: }
15023: 
15024: sub modify_usercreation {
15025:     my ($dom,%domconfig) = @_;
15026:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
15027:     my $warningmsg;
15028:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
15029:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
15030:             if ($key eq 'cancreate') {
15031:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
15032:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
15033:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
15034:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
15035:                         } else {
15036:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
15037:                         }
15038:                     }
15039:                 }
15040:             } elsif ($key eq 'email_rule') {
15041:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
15042:             } else {
15043:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
15044:             }
15045:         }
15046:     }
15047:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
15048:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
15049:     my @contexts = ('author','course','requestcrs');
15050:     foreach my $item(@contexts) {
15051:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
15052:     }
15053:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
15054:         foreach my $item (@contexts) {
15055:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
15056:                 push(@{$changes{'cancreate'}},$item);
15057:             }
15058:         }
15059:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
15060:         foreach my $item (@contexts) {
15061:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
15062:                 if ($cancreate{$item} ne 'any') {
15063:                     push(@{$changes{'cancreate'}},$item);
15064:                 }
15065:             } else {
15066:                 if ($cancreate{$item} ne 'none') {
15067:                     push(@{$changes{'cancreate'}},$item);
15068:                 }
15069:             }
15070:         }
15071:     } else {
15072:         foreach my $item (@contexts)  {
15073:             push(@{$changes{'cancreate'}},$item);
15074:         }
15075:     }
15076: 
15077:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
15078:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
15079:             if (!grep(/^\Q$type\E$/,@username_rule)) {
15080:                 push(@{$changes{'username_rule'}},$type);
15081:             }
15082:         }
15083:         foreach my $type (@username_rule) {
15084:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
15085:                 push(@{$changes{'username_rule'}},$type);
15086:             }
15087:         }
15088:     } else {
15089:         push(@{$changes{'username_rule'}},@username_rule);
15090:     }
15091: 
15092:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
15093:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
15094:             if (!grep(/^\Q$type\E$/,@id_rule)) {
15095:                 push(@{$changes{'id_rule'}},$type);
15096:             }
15097:         }
15098:         foreach my $type (@id_rule) {
15099:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
15100:                 push(@{$changes{'id_rule'}},$type);
15101:             }
15102:         }
15103:     } else {
15104:         push(@{$changes{'id_rule'}},@id_rule);
15105:     }
15106: 
15107:     my @authen_contexts = ('author','course','domain');
15108:     my @authtypes = ('int','krb4','krb5','loc','lti');
15109:     my %authhash;
15110:     foreach my $item (@authen_contexts) {
15111:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
15112:         foreach my $auth (@authtypes) {
15113:             if (grep(/^\Q$auth\E$/,@authallowed)) {
15114:                 $authhash{$item}{$auth} = 1;
15115:             } else {
15116:                 $authhash{$item}{$auth} = 0;
15117:             }
15118:         }
15119:     }
15120:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
15121:         foreach my $item (@authen_contexts) {
15122:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
15123:                 foreach my $auth (@authtypes) {
15124:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
15125:                         push(@{$changes{'authtypes'}},$item);
15126:                         last;
15127:                     }
15128:                 }
15129:             }
15130:         }
15131:     } else {
15132:         foreach my $item (@authen_contexts) {
15133:             push(@{$changes{'authtypes'}},$item);
15134:         }
15135:     }
15136: 
15137:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
15138:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
15139:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
15140:     $save_usercreate{'id_rule'} = \@id_rule;
15141:     $save_usercreate{'username_rule'} = \@username_rule,
15142:     $save_usercreate{'authtypes'} = \%authhash;
15143: 
15144:     my %usercreation_hash =  (
15145:         usercreation     => \%save_usercreate,
15146:     );
15147: 
15148:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
15149:                                              $dom);
15150: 
15151:     if ($putresult eq 'ok') {
15152:         if (keys(%changes) > 0) {
15153:             $resulttext = &mt('Changes made:').'<ul>';
15154:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
15155:                 my %lt = &usercreation_types();
15156:                 foreach my $type (@{$changes{'cancreate'}}) {
15157:                     my $chgtext = $lt{$type}.', ';
15158:                     if ($cancreate{$type} eq 'none') {
15159:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
15160:                     } elsif ($cancreate{$type} eq 'any') {
15161:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
15162:                     } elsif ($cancreate{$type} eq 'official') {
15163:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
15164:                     } elsif ($cancreate{$type} eq 'unofficial') {
15165:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
15166:                     }
15167:                     $resulttext .= '<li>'.$chgtext.'</li>';
15168:                 }
15169:             }
15170:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
15171:                 my ($rules,$ruleorder) = 
15172:                     &Apache::lonnet::inst_userrules($dom,'username');
15173:                 my $chgtext = '<ul>';
15174:                 foreach my $type (@username_rule) {
15175:                     if (ref($rules->{$type}) eq 'HASH') {
15176:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
15177:                     }
15178:                 }
15179:                 $chgtext .= '</ul>';
15180:                 if (@username_rule > 0) {
15181:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
15182:                 } else {
15183:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
15184:                 }
15185:             }
15186:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
15187:                 my ($idrules,$idruleorder) = 
15188:                     &Apache::lonnet::inst_userrules($dom,'id');
15189:                 my $chgtext = '<ul>';
15190:                 foreach my $type (@id_rule) {
15191:                     if (ref($idrules->{$type}) eq 'HASH') {
15192:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
15193:                     }
15194:                 }
15195:                 $chgtext .= '</ul>';
15196:                 if (@id_rule > 0) {
15197:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
15198:                 } else {
15199:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
15200:                 }
15201:             }
15202:             my %authname = &authtype_names();
15203:             my %context_title = &context_names();
15204:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
15205:                 my $chgtext = '<ul>';
15206:                 foreach my $type (@{$changes{'authtypes'}}) {
15207:                     my @allowed;
15208:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
15209:                     foreach my $auth (@authtypes) {
15210:                         if ($authhash{$type}{$auth}) {
15211:                             push(@allowed,$authname{$auth});
15212:                         }
15213:                     }
15214:                     if (@allowed > 0) {
15215:                         $chgtext .= join(', ',@allowed).'</li>';
15216:                     } else {
15217:                         $chgtext .= &mt('none').'</li>';
15218:                     }
15219:                 }
15220:                 $chgtext .= '</ul>';
15221:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
15222:                 $resulttext .= '</li>';
15223:             }
15224:             $resulttext .= '</ul>';
15225:         } else {
15226:             $resulttext = &mt('No changes made to user creation settings');
15227:         }
15228:     } else {
15229:         $resulttext = '<span class="LC_error">'.
15230:             &mt('An error occurred: [_1]',$putresult).'</span>';
15231:     }
15232:     if ($warningmsg ne '') {
15233:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
15234:     }
15235:     return $resulttext;
15236: }
15237: 
15238: sub modify_selfcreation {
15239:     my ($dom,$lastactref,%domconfig) = @_;
15240:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
15241:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
15242:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15243:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
15244:     if (ref($typesref) eq 'ARRAY') {
15245:         @types = @{$typesref};
15246:     }
15247:     if (ref($usertypesref) eq 'HASH') {
15248:         %usertypes = %{$usertypesref};
15249:     }
15250:     $usertypes{'default'} = $othertitle;
15251: #
15252: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
15253: #
15254:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
15255:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
15256:             if ($key eq 'cancreate') {
15257:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
15258:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
15259:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
15260:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
15261:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
15262:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
15263:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
15264:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
15265:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
15266:                         } else {
15267:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
15268:                         }
15269:                     }
15270:                 }
15271:             } elsif ($key eq 'email_rule') {
15272:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
15273:             } else {
15274:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
15275:             }
15276:         }
15277:     }
15278: #
15279: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
15280: #
15281:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
15282:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
15283:             if ($key eq 'selfcreate') {
15284:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
15285:             } else {
15286:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
15287:             }
15288:         }
15289:     }
15290: #
15291: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
15292: #
15293:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
15294:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
15295:             if ($key eq 'inststatusguest') {
15296:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
15297:             } else {
15298:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
15299:             }
15300:         }
15301:     }
15302: 
15303:     my @contexts = ('selfcreate');
15304:     @{$cancreate{'selfcreate'}} = ();
15305:     %{$cancreate{'emailusername'}} = ();
15306:     if (@types) {
15307:         @{$cancreate{'statustocreate'}} = ();
15308:     }
15309:     %{$cancreate{'selfcreateprocessing'}} = ();
15310:     %{$cancreate{'shibenv'}} = ();
15311:     %{$cancreate{'emailverified'}} = ();
15312:     %{$cancreate{'emailoptions'}} = ();
15313:     %{$cancreate{'emaildomain'}} = ();
15314:     my %selfcreatetypes = (
15315:                              sso   => 'users authenticated by institutional single sign on',
15316:                              login => 'users authenticated by institutional log-in',
15317:                              email => 'users verified by e-mail',
15318:                           );
15319: #
15320: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
15321: # is permitted.
15322: #
15323:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
15324: 
15325:     my (@statuses,%email_rule);
15326:     foreach my $item ('login','sso','email') {
15327:         if ($item eq 'email') {
15328:             if ($env{'form.cancreate_email'}) {
15329:                 if (@types) {
15330:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
15331:                     foreach my $status (@poss_statuses) {
15332:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
15333:                             push(@statuses,$status);
15334:                         }
15335:                     }
15336:                     $save_inststatus{'inststatusguest'} = \@statuses;
15337:                 } else {
15338:                     push(@statuses,'default');
15339:                 }
15340:                 if (@statuses) {
15341:                     my %curr_rule;
15342:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
15343:                         foreach my $type (@statuses) {
15344:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
15345:                         }
15346:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
15347:                         foreach my $type (@statuses) {
15348:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
15349:                         }
15350:                     }
15351:                     push(@{$cancreate{'selfcreate'}},'email');
15352:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
15353:                     my %curremaildom;
15354:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
15355:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
15356:                     }
15357:                     foreach my $type (@statuses) {
15358:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
15359:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
15360:                         }
15361:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
15362:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
15363:                         }
15364:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
15365: #
15366: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
15367: #
15368:                             my $chosen = $1;
15369:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
15370:                                 my $emaildom;
15371:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
15372:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
15373:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
15374:                                     if (ref($curremaildom{$type}) eq 'HASH') {
15375:                                         if (exists($curremaildom{$type}{$chosen})) {
15376:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
15377:                                                 push(@{$changes{'cancreate'}},'emaildomain');
15378:                                             }
15379:                                         } elsif ($emaildom ne '') {
15380:                                             push(@{$changes{'cancreate'}},'emaildomain');
15381:                                         }
15382:                                     } elsif ($emaildom ne '') {
15383:                                         push(@{$changes{'cancreate'}},'emaildomain');
15384:                                     } 
15385:                                 }
15386:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
15387:                             } elsif ($chosen eq 'custom') {
15388:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
15389:                                 $email_rule{$type} = [];
15390:                                 if (ref($emailrules) eq 'HASH') {
15391:                                     foreach my $rule (@possemail_rules) {
15392:                                         if (exists($emailrules->{$rule})) {
15393:                                             push(@{$email_rule{$type}},$rule);
15394:                                         }
15395:                                     }
15396:                                 }
15397:                                 if (@{$email_rule{$type}}) {
15398:                                     $cancreate{'emailoptions'}{$type} = 'custom';
15399:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
15400:                                         if (@{$curr_rule{$type}} > 0) {
15401:                                             foreach my $rule (@{$curr_rule{$type}}) {
15402:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
15403:                                                     push(@{$changes{'email_rule'}},$type);
15404:                                                 }
15405:                                             }
15406:                                         }
15407:                                         foreach my $type (@{$email_rule{$type}}) {
15408:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
15409:                                                 push(@{$changes{'email_rule'}},$type);
15410:                                             }
15411:                                         }
15412:                                     } else {
15413:                                         push(@{$changes{'email_rule'}},$type);
15414:                                     }
15415:                                 }
15416:                             } else {
15417:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
15418:                             }
15419:                         }
15420:                     }
15421:                     if (@types) {
15422:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
15423:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
15424:                             if (@changed) {
15425:                                 push(@{$changes{'inststatus'}},'inststatusguest');
15426:                             }
15427:                         } else {
15428:                             push(@{$changes{'inststatus'}},'inststatusguest');
15429:                         }
15430:                     }
15431:                 } else {
15432:                     delete($env{'form.cancreate_email'});
15433:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
15434:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
15435:                             push(@{$changes{'inststatus'}},'inststatusguest');
15436:                         }
15437:                     }
15438:                 }
15439:             } else {
15440:                 $save_inststatus{'inststatusguest'} = [];
15441:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
15442:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
15443:                         push(@{$changes{'inststatus'}},'inststatusguest');
15444:                     }
15445:                 }
15446:             }
15447:         } else {
15448:             if ($env{'form.cancreate_'.$item}) {
15449:                 push(@{$cancreate{'selfcreate'}},$item);
15450:             }
15451:         }
15452:     }
15453:     my (%userinfo,%savecaptcha);
15454:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
15455: #
15456: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
15457: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
15458: #
15459: 
15460:     if ($env{'form.cancreate_email'}) {
15461:         push(@contexts,'emailusername');
15462:         if (@statuses) {
15463:             foreach my $type (@statuses) {
15464:                 if (ref($infofields) eq 'ARRAY') {
15465:                     foreach my $field (@{$infofields}) {
15466:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
15467:                             $cancreate{'emailusername'}{$type}{$field} = $1;
15468:                         }
15469:                     }
15470:                 }
15471:             }
15472:         }
15473: #
15474: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
15475: # queued requests for self-creation of account verified by e-mail.
15476: #
15477: 
15478:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
15479:         @approvalnotify = sort(@approvalnotify);
15480:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
15481:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
15482:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
15483:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
15484:                     push(@{$changes{'cancreate'}},'notify');
15485:                 }
15486:             } else {
15487:                 if ($cancreate{'notify'}{'approval'}) {
15488:                     push(@{$changes{'cancreate'}},'notify');
15489:                 }
15490:             }
15491:         } elsif ($cancreate{'notify'}{'approval'}) {
15492:             push(@{$changes{'cancreate'}},'notify');
15493:         }
15494: 
15495:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
15496:     }
15497: #  
15498: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
15499: # institutional log-in.
15500: #
15501:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
15502:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
15503:                ($domdefaults{'auth_def'} eq 'localauth'))) {
15504:             $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.').' '.
15505:                           &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.');
15506:         }
15507:     }
15508:     my @fields = ('lastname','firstname','middlename','generation',
15509:                   'permanentemail','id');
15510:     my @shibfields = (@fields,'inststatus');
15511:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15512: #
15513: # Where usernames may created for institutional log-in and/or institutional single sign on:
15514: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
15515: # may self-create accounts 
15516: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
15517: # which the user may supply, if institutional data is unavailable.
15518: #
15519:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
15520:         if (@types) {
15521:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
15522:             push(@contexts,'statustocreate');
15523:             foreach my $type (@types) {
15524:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
15525:                 foreach my $field (@fields) {
15526:                     if (grep(/^\Q$field\E$/,@modifiable)) {
15527:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
15528:                     } else {
15529:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
15530:                     }
15531:                 }
15532:             }
15533:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
15534:                 foreach my $type (@types) {
15535:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
15536:                         foreach my $field (@fields) {
15537:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
15538:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
15539:                                 push(@{$changes{'selfcreate'}},$type);
15540:                                 last;
15541:                             }
15542:                         }
15543:                     }
15544:                 }
15545:             } else {
15546:                 foreach my $type (@types) {
15547:                     push(@{$changes{'selfcreate'}},$type);
15548:                 }
15549:             }
15550:         }
15551:         foreach my $field (@shibfields) {
15552:             if ($env{'form.shibenv_'.$field} ne '') {
15553:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
15554:             }
15555:         }
15556:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
15557:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
15558:                 foreach my $field (@shibfields) {
15559:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
15560:                         push(@{$changes{'cancreate'}},'shibenv');
15561:                     }
15562:                 }
15563:             } else {
15564:                 foreach my $field (@shibfields) {
15565:                     if ($env{'form.shibenv_'.$field}) {
15566:                         push(@{$changes{'cancreate'}},'shibenv');
15567:                         last;
15568:                     }
15569:                 }
15570:             }
15571:         }
15572:     }
15573:     foreach my $item (@contexts) {
15574:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
15575:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
15576:                 if (ref($cancreate{$item}) eq 'ARRAY') {
15577:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
15578:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15579:                             push(@{$changes{'cancreate'}},$item);
15580:                         }
15581:                     }
15582:                 }
15583:             }
15584:             if (ref($cancreate{$item}) eq 'ARRAY') {
15585:                 foreach my $type (@{$cancreate{$item}}) {
15586:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
15587:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15588:                             push(@{$changes{'cancreate'}},$item);
15589:                         }
15590:                     }
15591:                 }
15592:             }
15593:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
15594:             if (ref($cancreate{$item}) eq 'HASH') {
15595:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
15596:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
15597:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
15598:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
15599:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15600:                                     push(@{$changes{'cancreate'}},$item);
15601:                                 }
15602:                             }
15603:                         }
15604:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
15605:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
15606:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15607:                                 push(@{$changes{'cancreate'}},$item);
15608:                             }
15609:                         }
15610:                     }
15611:                 }
15612:                 foreach my $type (keys(%{$cancreate{$item}})) {
15613:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
15614:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
15615:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
15616:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
15617:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15618:                                         push(@{$changes{'cancreate'}},$item);
15619:                                     }
15620:                                 }
15621:                             } else {
15622:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15623:                                     push(@{$changes{'cancreate'}},$item);
15624:                                 }
15625:                             }
15626:                         }
15627:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
15628:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
15629:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15630:                                 push(@{$changes{'cancreate'}},$item);
15631:                             }
15632:                         }
15633:                     }
15634:                 }
15635:             }
15636:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
15637:             if (ref($cancreate{$item}) eq 'ARRAY') {
15638:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
15639:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15640:                         push(@{$changes{'cancreate'}},$item);
15641:                     }
15642:                 }
15643:             }
15644:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
15645:             if (ref($cancreate{$item}) eq 'HASH') {
15646:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15647:                     push(@{$changes{'cancreate'}},$item);
15648:                 }
15649:             }
15650:         } elsif ($item eq 'emailusername') {
15651:             if (ref($cancreate{$item}) eq 'HASH') {
15652:                 foreach my $type (keys(%{$cancreate{$item}})) {
15653:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
15654:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
15655:                             if ($cancreate{$item}{$type}{$field}) {
15656:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
15657:                                     push(@{$changes{'cancreate'}},$item);
15658:                                 }
15659:                                 last;
15660:                             }
15661:                         }
15662:                     }
15663:                 }
15664:             }
15665:         }
15666:     }
15667: #
15668: # Populate %save_usercreate hash with updates to self-creation configuration.
15669: #
15670:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
15671:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
15672:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
15673:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
15674:     if (ref($cancreate{'notify'}) eq 'HASH') {
15675:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
15676:     }
15677:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
15678:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
15679:     }
15680:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
15681:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
15682:     }
15683:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
15684:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
15685:     }
15686:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
15687:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
15688:     }
15689:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
15690:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
15691:     }
15692:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
15693:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
15694:     }
15695:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
15696:     $save_usercreate{'email_rule'} = \%email_rule;
15697: 
15698:     my %userconfig_hash = (
15699:             usercreation     => \%save_usercreate,
15700:             usermodification => \%save_usermodify,
15701:             inststatus       => \%save_inststatus,
15702:     );
15703: 
15704:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
15705:                                              $dom);
15706: #
15707: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
15708: #
15709:     if ($putresult eq 'ok') {
15710:         if (keys(%changes) > 0) {
15711:             $resulttext = &mt('Changes made:').'<ul>';
15712:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
15713:                 my %lt = &selfcreation_types();
15714:                 foreach my $type (@{$changes{'cancreate'}}) {
15715:                     my $chgtext = '';
15716:                     if ($type eq 'selfcreate') {
15717:                         if (@{$cancreate{$type}} == 0) {
15718:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
15719:                         } else {
15720:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
15721:                                         '<ul>';
15722:                             foreach my $case (@{$cancreate{$type}}) {
15723:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
15724:                             }
15725:                             $chgtext .= '</ul>';
15726:                             if (ref($cancreate{$type}) eq 'ARRAY') {
15727:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
15728:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
15729:                                         if (@{$cancreate{'statustocreate'}} == 0) {
15730:                                             $chgtext .= '<span class="LC_warning">'.
15731:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
15732:                                                         '</span><br />';
15733:                                         }
15734:                                     }
15735:                                 }
15736:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
15737:                                     if (!@statuses) {
15738:                                         $chgtext .= '<span class="LC_warning">'.
15739:                                                     &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.").
15740:                                                     '</span><br />';
15741: 
15742:                                     }
15743:                                 }
15744:                             }
15745:                         }
15746:                     } elsif ($type eq 'shibenv') {
15747:                         if (keys(%{$cancreate{$type}}) == 0) {
15748:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
15749:                         } else {
15750:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
15751:                                         '<ul>';
15752:                             foreach my $field (@shibfields) {
15753:                                 next if ($cancreate{$type}{$field} eq '');
15754:                                 if ($field eq 'inststatus') {
15755:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
15756:                                 } else {
15757:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
15758:                                 }
15759:                             }
15760:                             $chgtext .= '</ul>';
15761:                         }
15762:                     } elsif ($type eq 'statustocreate') {
15763:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
15764:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
15765:                             if (@{$cancreate{'selfcreate'}} > 0) {
15766:                                 if (@{$cancreate{'statustocreate'}} == 0) {
15767:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
15768:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
15769:                                         $chgtext .= '<br />'.
15770:                                                     '<span class="LC_warning">'.
15771:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
15772:                                                     '</span>';
15773:                                     }
15774:                                 } elsif (keys(%usertypes) > 0) {
15775:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
15776:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
15777:                                     } else {
15778:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
15779:                                     }
15780:                                     $chgtext .= '<ul>';
15781:                                     foreach my $case (@{$cancreate{$type}}) {
15782:                                         if ($case eq 'default') {
15783:                                             $chgtext .= '<li>'.$othertitle.'</li>';
15784:                                         } else {
15785:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
15786:                                         }
15787:                                     }
15788:                                     $chgtext .= '</ul>';
15789:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
15790:                                         $chgtext .= '<span class="LC_warning">'.
15791:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
15792:                                                     '</span>';
15793:                                     }
15794:                                 }
15795:                             } else {
15796:                                 if (@{$cancreate{$type}} == 0) {
15797:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
15798:                                 } else {
15799:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
15800:                                 }
15801:                             }
15802:                             $chgtext .= '<br />';
15803:                         }
15804:                     } elsif ($type eq 'selfcreateprocessing') {
15805:                         my %choices = &Apache::lonlocal::texthash (
15806:                                                                     automatic => 'Automatic approval',
15807:                                                                     approval  => 'Queued for approval',
15808:                                                                   );
15809:                         if (@types) {
15810:                             if (@statuses) {
15811:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
15812:                                             '<ul>';
15813:                                 foreach my $status (@statuses) {
15814:                                     if ($status eq 'default') {
15815:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
15816:                                     } else {
15817:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
15818:                                     }
15819:                                 }
15820:                                 $chgtext .= '</ul>';
15821:                             }
15822:                         } else {
15823:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
15824:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
15825:                         }
15826:                     } elsif ($type eq 'emailverified') {
15827:                         my %options = &Apache::lonlocal::texthash (
15828:                                                                     all   => 'Same as e-mail',
15829:                                                                     first => 'Omit @domain',
15830:                                                                     free  => 'Free to choose',
15831:                                                                   );
15832:                         if (@types) {
15833:                             if (@statuses) {
15834:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
15835:                                             '<ul>';
15836:                                 foreach my $status (@statuses) {
15837:                                     if ($status eq 'default') {
15838:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
15839:                                     } else {
15840:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
15841:                                     }
15842:                                 }
15843:                                 $chgtext .= '</ul>';
15844:                             }
15845:                         } else {
15846:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
15847:                                             $options{$cancreate{'emailverified'}{'default'}});
15848:                         }
15849:                     } elsif ($type eq 'emailoptions') {
15850:                         my %options = &Apache::lonlocal::texthash (
15851:                                                                     any     => 'Any e-mail',
15852:                                                                     inst    => 'Institutional only',
15853:                                                                     noninst => 'Non-institutional only',
15854:                                                                     custom  => 'Custom restrictions',
15855:                                                                   );
15856:                         if (@types) {
15857:                             if (@statuses) {
15858:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
15859:                                             '<ul>';
15860:                                 foreach my $status (@statuses) {
15861:                                     if ($type eq 'default') {
15862:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
15863:                                     } else {
15864:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
15865:                                     }
15866:                                 }
15867:                                 $chgtext .= '</ul>';
15868:                             }
15869:                         } else {
15870:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
15871:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
15872:                             } else {
15873:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
15874:                                                 $options{$cancreate{'emailoptions'}{'default'}});
15875:                             }
15876:                         }
15877:                     } elsif ($type eq 'emaildomain') {
15878:                         my $output;
15879:                         if (@statuses) {
15880:                             foreach my $type (@statuses) {
15881:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
15882:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
15883:                                         if ($type eq 'default') {
15884:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
15885:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
15886:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
15887:                                             } else {
15888:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
15889:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
15890:                                             }
15891:                                         } else {
15892:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
15893:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
15894:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
15895:                                             } else {
15896:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
15897:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
15898:                                             }
15899:                                         }
15900:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
15901:                                         if ($type eq 'default') {
15902:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
15903:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
15904:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
15905:                                             } else {
15906:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
15907:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
15908:                                             }
15909:                                         } else {
15910:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
15911:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
15912:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
15913:                                             } else {
15914:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
15915:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
15916:                                             }
15917:                                         }
15918:                                     }
15919:                                 }
15920:                             }
15921:                         }
15922:                         if ($output ne '') {
15923:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
15924:                                         '<ul>'.$output.'</ul>';
15925:                         }
15926:                     } elsif ($type eq 'captcha') {
15927:                         if ($savecaptcha{$type} eq 'notused') {
15928:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
15929:                         } else {
15930:                             my %captchas = &captcha_phrases();
15931:                             if ($captchas{$savecaptcha{$type}}) {
15932:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
15933:                             } else {
15934:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
15935:                             }
15936:                         }
15937:                     } elsif ($type eq 'recaptchakeys') {
15938:                         my ($privkey,$pubkey);
15939:                         if (ref($savecaptcha{$type}) eq 'HASH') {
15940:                             $pubkey = $savecaptcha{$type}{'public'};
15941:                             $privkey = $savecaptcha{$type}{'private'};
15942:                         }
15943:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
15944:                         if (!$pubkey) {
15945:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
15946:                         } else {
15947:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
15948:                         }
15949:                         if (!$privkey) {
15950:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
15951:                         } else {
15952:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
15953:                         }
15954:                         $chgtext .= '</ul>';
15955:                     } elsif ($type eq 'recaptchaversion') {
15956:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
15957:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
15958:                         }
15959:                     } elsif ($type eq 'emailusername') {
15960:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
15961:                             if (@statuses) {
15962:                                 foreach my $type (@statuses) {
15963:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
15964:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
15965:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
15966:                                                     '<ul>';
15967:                                             foreach my $field (@{$infofields}) {
15968:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
15969:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
15970:                                                 }
15971:                                             }
15972:                                             $chgtext .= '</ul>';
15973:                                         } else {
15974:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
15975:                                         }
15976:                                     } else {
15977:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
15978:                                     }
15979:                                 }
15980:                             }
15981:                         }
15982:                     } elsif ($type eq 'notify') {
15983:                         my $numapprove = 0;
15984:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
15985:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
15986:                                 if ($cancreate{'notify'}{'approval'}) {
15987:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
15988:                                     $numapprove ++;
15989:                                 }
15990:                             }
15991:                         }
15992:                         unless ($numapprove) {
15993:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
15994:                         }
15995:                     }
15996:                     if ($chgtext) {
15997:                         $resulttext .= '<li>'.$chgtext.'</li>';
15998:                     }
15999:                 }
16000:             }
16001:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
16002:                 my ($emailrules,$emailruleorder) =
16003:                     &Apache::lonnet::inst_userrules($dom,'email');
16004:                 foreach my $type (@{$changes{'email_rule'}}) {
16005:                     if (ref($email_rule{$type}) eq 'ARRAY') {
16006:                         my $chgtext = '<ul>';
16007:                         foreach my $rule (@{$email_rule{$type}}) {
16008:                             if (ref($emailrules->{$rule}) eq 'HASH') {
16009:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
16010:                             }
16011:                         }
16012:                         $chgtext .= '</ul>';
16013:                         my $typename;
16014:                         if (@types) {
16015:                             if ($type eq 'default') {
16016:                                 $typename = $othertitle;
16017:                             } else {
16018:                                 $typename = $usertypes{$type};
16019:                             } 
16020:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
16021:                         }
16022:                         if (@{$email_rule{$type}} > 0) {
16023:                             $resulttext .= '<li>'.
16024:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
16025:                                                $usertypes{$type}).
16026:                                            $chgtext.
16027:                                            '</li>';
16028:                         } else {
16029:                             $resulttext .= '<li>'.
16030:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
16031:                                            '</li>'.
16032:                                            &mt('(Affiliation: [_1])',$typename);
16033:                         }
16034:                     }
16035:                 }
16036:             }
16037:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
16038:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
16039:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
16040:                         my $chgtext = '<ul>';
16041:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
16042:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
16043:                         }
16044:                         $chgtext .= '</ul>';
16045:                         $resulttext .= '<li>'.
16046:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
16047:                                           $chgtext.
16048:                                        '</li>';
16049:                     } else {
16050:                         $resulttext .= '<li>'.
16051:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
16052:                                        '</li>';
16053:                     }
16054:                 }
16055:             }
16056:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
16057:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
16058:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
16059:                 foreach my $type (@{$changes{'selfcreate'}}) {
16060:                     my $typename = $type;
16061:                     if (keys(%usertypes) > 0) {
16062:                         if ($usertypes{$type} ne '') {
16063:                             $typename = $usertypes{$type};
16064:                         }
16065:                     }
16066:                     my @modifiable;
16067:                     $resulttext .= '<li>'.
16068:                                     &mt('Self-creation of account by users with status: [_1]',
16069:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
16070:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
16071:                     foreach my $field (@fields) {
16072:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
16073:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
16074:                         }
16075:                     }
16076:                     if (@modifiable > 0) {
16077:                         $resulttext .= join(', ',@modifiable);
16078:                     } else {
16079:                         $resulttext .= &mt('none');
16080:                     }
16081:                     $resulttext .= '</li>';
16082:                 }
16083:                 $resulttext .= '</ul></li>';
16084:             }
16085:             $resulttext .= '</ul>';
16086:             my $cachetime = 24*60*60;
16087:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
16088:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16089:             if (ref($lastactref) eq 'HASH') {
16090:                 $lastactref->{'domdefaults'} = 1;
16091:             }
16092:         } else {
16093:             $resulttext = &mt('No changes made to self-creation settings');
16094:         }
16095:     } else {
16096:         $resulttext = '<span class="LC_error">'.
16097:             &mt('An error occurred: [_1]',$putresult).'</span>';
16098:     }
16099:     if ($warningmsg ne '') {
16100:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
16101:     }
16102:     return $resulttext;
16103: }
16104: 
16105: sub process_captcha {
16106:     my ($container,$changes,$newsettings,$current) = @_;
16107:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
16108:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
16109:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
16110:         $newsettings->{'captcha'} = 'original';
16111:     }
16112:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
16113:         if ($container eq 'cancreate') {
16114:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
16115:                 push(@{$changes->{'cancreate'}},'captcha');
16116:             } elsif (!defined($changes->{'cancreate'})) {
16117:                 $changes->{'cancreate'} = ['captcha'];
16118:             }
16119:         } else {
16120:             $changes->{'captcha'} = 1;
16121:         }
16122:     }
16123:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
16124:     if ($newsettings->{'captcha'} eq 'recaptcha') {
16125:         $newpub = $env{'form.'.$container.'_recaptchapub'};
16126:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
16127:         $newpub =~ s/[^\w\-]//g;
16128:         $newpriv =~ s/[^\w\-]//g;
16129:         $newsettings->{'recaptchakeys'} = {
16130:                                              public  => $newpub,
16131:                                              private => $newpriv,
16132:                                           };
16133:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
16134:         $newversion =~ s/\D//g;
16135:         if ($newversion ne '2') {
16136:             $newversion = 1;
16137:         }
16138:         $newsettings->{'recaptchaversion'} = $newversion;
16139:     }
16140:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
16141:         $currpub = $current->{'recaptchakeys'}{'public'};
16142:         $currpriv = $current->{'recaptchakeys'}{'private'};
16143:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
16144:             $newsettings->{'recaptchakeys'} = {
16145:                                                  public  => '',
16146:                                                  private => '',
16147:                                               }
16148:         }
16149:     }
16150:     if ($current->{'captcha'} eq 'recaptcha') {
16151:         $currversion = $current->{'recaptchaversion'};
16152:         if ($currversion ne '2') {
16153:             $currversion = 1;
16154:         }
16155:     }
16156:     if ($currversion ne $newversion) {
16157:         if ($container eq 'cancreate') {
16158:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
16159:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
16160:             } elsif (!defined($changes->{'cancreate'})) {
16161:                 $changes->{'cancreate'} = ['recaptchaversion'];
16162:             }
16163:         } else {
16164:             $changes->{'recaptchaversion'} = 1;
16165:         }
16166:     }
16167:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
16168:         if ($container eq 'cancreate') {
16169:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
16170:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
16171:             } elsif (!defined($changes->{'cancreate'})) {
16172:                 $changes->{'cancreate'} = ['recaptchakeys'];
16173:             }
16174:         } else {
16175:             $changes->{'recaptchakeys'} = 1;
16176:         }
16177:     }
16178:     return;
16179: }
16180: 
16181: sub modify_usermodification {
16182:     my ($dom,%domconfig) = @_;
16183:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
16184:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
16185:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
16186:             if ($key eq 'selfcreate') {
16187:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
16188:             } else {  
16189:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
16190:             }
16191:         }
16192:     }
16193:     my @contexts = ('author','course');
16194:     my %context_title = (
16195:                            author => 'In author context',
16196:                            course => 'In course context',
16197:                         );
16198:     my @fields = ('lastname','firstname','middlename','generation',
16199:                   'permanentemail','id');
16200:     my %roles = (
16201:                   author => ['ca','aa'],
16202:                   course => ['st','ep','ta','in','cr'],
16203:                 );
16204:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
16205:     foreach my $context (@contexts) {
16206:         foreach my $role (@{$roles{$context}}) {
16207:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
16208:             foreach my $item (@fields) {
16209:                 if (grep(/^\Q$item\E$/,@modifiable)) {
16210:                     $modifyhash{$context}{$role}{$item} = 1;
16211:                 } else {
16212:                     $modifyhash{$context}{$role}{$item} = 0;
16213:                 }
16214:             }
16215:         }
16216:         if (ref($curr_usermodification{$context}) eq 'HASH') {
16217:             foreach my $role (@{$roles{$context}}) {
16218:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
16219:                     foreach my $field (@fields) {
16220:                         if ($modifyhash{$context}{$role}{$field} ne 
16221:                                 $curr_usermodification{$context}{$role}{$field}) {
16222:                             push(@{$changes{$context}},$role);
16223:                             last;
16224:                         }
16225:                     }
16226:                 }
16227:             }
16228:         } else {
16229:             foreach my $context (@contexts) {
16230:                 foreach my $role (@{$roles{$context}}) {
16231:                     push(@{$changes{$context}},$role);
16232:                 }
16233:             }
16234:         }
16235:     }
16236:     my %usermodification_hash =  (
16237:                                    usermodification => \%modifyhash,
16238:                                  );
16239:     my $putresult = &Apache::lonnet::put_dom('configuration',
16240:                                              \%usermodification_hash,$dom);
16241:     if ($putresult eq 'ok') {
16242:         if (keys(%changes) > 0) {
16243:             $resulttext = &mt('Changes made: ').'<ul>';
16244:             foreach my $context (@contexts) {
16245:                 if (ref($changes{$context}) eq 'ARRAY') {
16246:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
16247:                     if (ref($changes{$context}) eq 'ARRAY') {
16248:                         foreach my $role (@{$changes{$context}}) {
16249:                             my $rolename;
16250:                             if ($role eq 'cr') {
16251:                                 $rolename = &mt('Custom');
16252:                             } else {
16253:                                 $rolename = &Apache::lonnet::plaintext($role);
16254:                             }
16255:                             my @modifiable;
16256:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
16257:                             foreach my $field (@fields) {
16258:                                 if ($modifyhash{$context}{$role}{$field}) {
16259:                                     push(@modifiable,$fieldtitles{$field});
16260:                                 }
16261:                             }
16262:                             if (@modifiable > 0) {
16263:                                 $resulttext .= join(', ',@modifiable);
16264:                             } else {
16265:                                 $resulttext .= &mt('none'); 
16266:                             }
16267:                             $resulttext .= '</li>';
16268:                         }
16269:                         $resulttext .= '</ul></li>';
16270:                     }
16271:                 }
16272:             }
16273:             $resulttext .= '</ul>';
16274:         } else {
16275:             $resulttext = &mt('No changes made to user modification settings');
16276:         }
16277:     } else {
16278:         $resulttext = '<span class="LC_error">'.
16279:             &mt('An error occurred: [_1]',$putresult).'</span>';
16280:     }
16281:     return $resulttext;
16282: }
16283: 
16284: sub modify_defaults {
16285:     my ($dom,$lastactref,%domconfig) = @_;
16286:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
16287:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16288:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
16289:                  'portal_def');
16290:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
16291:     foreach my $item (@items) {
16292:         $newvalues{$item} = $env{'form.'.$item};
16293:         if ($item eq 'auth_def') {
16294:             if ($newvalues{$item} ne '') {
16295:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
16296:                     push(@errors,$item);
16297:                 }
16298:             }
16299:         } elsif ($item eq 'lang_def') {
16300:             if ($newvalues{$item} ne '') {
16301:                 if ($newvalues{$item} =~ /^(\w+)/) {
16302:                     my $langcode = $1;
16303:                     if ($langcode ne 'x_chef') {
16304:                         if (code2language($langcode) eq '') {
16305:                             push(@errors,$item);
16306:                         }
16307:                     }
16308:                 } else {
16309:                     push(@errors,$item);
16310:                 }
16311:             }
16312:         } elsif ($item eq 'timezone_def') {
16313:             if ($newvalues{$item} ne '') {
16314:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
16315:                     push(@errors,$item);   
16316:                 }
16317:             }
16318:         } elsif ($item eq 'datelocale_def') {
16319:             if ($newvalues{$item} ne '') {
16320:                 my @datelocale_ids = DateTime::Locale->ids();
16321:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
16322:                     push(@errors,$item);
16323:                 }
16324:             }
16325:         } elsif ($item eq 'portal_def') {
16326:             if ($newvalues{$item} ne '') {
16327:                 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])\/?$/) {
16328:                     push(@errors,$item);
16329:                 }
16330:             }
16331:         }
16332:         if (grep(/^\Q$item\E$/,@errors)) {
16333:             $newvalues{$item} = $domdefaults{$item};
16334:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
16335:             $changes{$item} = 1;
16336:         }
16337:         $domdefaults{$item} = $newvalues{$item};
16338:     }
16339:     my %staticdefaults = (
16340:                            'intauth_cost'   => 10,
16341:                            'intauth_check'  => 0,
16342:                            'intauth_switch' => 0,
16343:                          );
16344:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
16345:         if (exists($domdefaults{$item})) {
16346:             $newvalues{$item} = $domdefaults{$item};
16347:         } else {
16348:             $newvalues{$item} = $staticdefaults{$item};
16349:         }
16350:     }
16351:     my %defaults_hash = (
16352:                          defaults => \%newvalues,
16353:                         );
16354:     my $title = &defaults_titles();
16355: 
16356:     my $currinststatus;
16357:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
16358:         $currinststatus = $domconfig{'inststatus'};
16359:     } else {
16360:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16361:         $currinststatus = {
16362:                              inststatustypes => $usertypes,
16363:                              inststatusorder => $types,
16364:                              inststatusguest => [],
16365:                           };
16366:     }
16367:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
16368:     my @allpos;
16369:     my %alltypes;
16370:     my @inststatusguest;
16371:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
16372:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
16373:             unless (grep(/^\Q$type\E$/,@todelete)) {
16374:                 push(@inststatusguest,$type);
16375:             }
16376:         }
16377:     }
16378:     my ($currtitles,$currorder);
16379:     if (ref($currinststatus) eq 'HASH') {
16380:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
16381:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
16382:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
16383:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
16384:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
16385:                     }
16386:                 }
16387:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
16388:                     my $position = $env{'form.inststatus_pos_'.$type};
16389:                     $position =~ s/\D+//g;
16390:                     $allpos[$position] = $type;
16391:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
16392:                     $alltypes{$type} =~ s/`//g;
16393:                 }
16394:             }
16395:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
16396:             $currtitles =~ s/,$//;
16397:         }
16398:     }
16399:     if ($env{'form.addinststatus'}) {
16400:         my $newtype = $env{'form.addinststatus'};
16401:         $newtype =~ s/\W//g;
16402:         unless (exists($alltypes{$newtype})) {
16403:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
16404:             $alltypes{$newtype} =~ s/`//g; 
16405:             my $position = $env{'form.addinststatus_pos'};
16406:             $position =~ s/\D+//g;
16407:             if ($position ne '') {
16408:                 $allpos[$position] = $newtype;
16409:             }
16410:         }
16411:     }
16412:     my @orderedstatus;
16413:     foreach my $type (@allpos) {
16414:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
16415:             push(@orderedstatus,$type);
16416:         }
16417:     }
16418:     foreach my $type (keys(%alltypes)) {
16419:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
16420:             delete($alltypes{$type});
16421:         }
16422:     }
16423:     $defaults_hash{'inststatus'} = {
16424:                                      inststatustypes => \%alltypes,
16425:                                      inststatusorder => \@orderedstatus,
16426:                                      inststatusguest => \@inststatusguest,
16427:                                    };
16428:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
16429:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
16430:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
16431:         }
16432:     }
16433:     if ($currorder ne join(',',@orderedstatus)) {
16434:         $changes{'inststatus'}{'inststatusorder'} = 1;
16435:     }
16436:     my $newtitles;
16437:     foreach my $item (@orderedstatus) {
16438:         $newtitles .= $alltypes{$item}.',';
16439:     }
16440:     $newtitles =~ s/,$//;
16441:     if ($currtitles ne $newtitles) {
16442:         $changes{'inststatus'}{'inststatustypes'} = 1;
16443:     }
16444:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
16445:                                              $dom);
16446:     if ($putresult eq 'ok') {
16447:         if (keys(%changes) > 0) {
16448:             $resulttext = &mt('Changes made:').'<ul>';
16449:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
16450:             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";
16451:             foreach my $item (sort(keys(%changes))) {
16452:                 if ($item eq 'inststatus') {
16453:                     if (ref($changes{'inststatus'}) eq 'HASH') {
16454:                         if (@orderedstatus) {
16455:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
16456:                             foreach my $type (@orderedstatus) { 
16457:                                 $resulttext .= $alltypes{$type}.', ';
16458:                             }
16459:                             $resulttext =~ s/, $//;
16460:                             $resulttext .= '</li>';
16461:                         } else {
16462:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
16463:                         }
16464:                     }
16465:                 } else {
16466:                     my $value = $env{'form.'.$item};
16467:                     if ($value eq '') {
16468:                         $value = &mt('none');
16469:                     } elsif ($item eq 'auth_def') {
16470:                         my %authnames = &authtype_names();
16471:                         my %shortauth = (
16472:                                           internal   => 'int',
16473:                                           krb4       => 'krb4',
16474:                                           krb5       => 'krb5',
16475:                                           localauth  => 'loc',
16476:                                           lti        => 'lti',
16477:                         );
16478:                         $value = $authnames{$shortauth{$value}};
16479:                     }
16480:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
16481:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
16482:                 }
16483:             }
16484:             $resulttext .= '</ul>';
16485:             $mailmsgtext .= "\n";
16486:             my $cachetime = 24*60*60;
16487:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16488:             if (ref($lastactref) eq 'HASH') {
16489:                 $lastactref->{'domdefaults'} = 1;
16490:             }
16491:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
16492:                 my $notify = 1;
16493:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
16494:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
16495:                         $notify = 0;
16496:                     }
16497:                 }
16498:                 if ($notify) {
16499:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
16500:                                                "LON-CAPA Domain Settings Change - $dom",
16501:                                                $mailmsgtext);
16502:                 }
16503:             }
16504:         } else {
16505:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
16506:         }
16507:     } else {
16508:         $resulttext = '<span class="LC_error">'.
16509:             &mt('An error occurred: [_1]',$putresult).'</span>';
16510:     }
16511:     if (@errors > 0) {
16512:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
16513:         foreach my $item (@errors) {
16514:             $resulttext .= ' "'.$title->{$item}.'",';
16515:         }
16516:         $resulttext =~ s/,$//;
16517:     }
16518:     return $resulttext;
16519: }
16520: 
16521: sub modify_scantron {
16522:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
16523:     my ($resulttext,%confhash,%changes,$errors);
16524:     my $custom = 'custom.tab';
16525:     my $default = 'default.tab';
16526:     my $servadm = $r->dir_config('lonAdmEMail');
16527:     my ($configuserok,$author_ok,$switchserver) =
16528:         &config_check($dom,$confname,$servadm);
16529:     if ($env{'form.scantronformat.filename'} ne '') {
16530:         my $error;
16531:         if ($configuserok eq 'ok') {
16532:             if ($switchserver) {
16533:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
16534:             } else {
16535:                 if ($author_ok eq 'ok') {
16536:                     my ($result,$scantronurl) =
16537:                         &publishlogo($r,'upload','scantronformat',$dom,
16538:                                      $confname,'scantron','','',$custom);
16539:                     if ($result eq 'ok') {
16540:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
16541:                         $changes{'scantronformat'} = 1;
16542:                     } else {
16543:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
16544:                     }
16545:                 } else {
16546:                     $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);
16547:                 }
16548:             }
16549:         } else {
16550:             $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);
16551:         }
16552:         if ($error) {
16553:             &Apache::lonnet::logthis($error);
16554:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
16555:         }
16556:     }
16557:     if (ref($domconfig{'scantron'}) eq 'HASH') {
16558:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
16559:             if ($env{'form.scantronformat_del'}) {
16560:                 $confhash{'scantron'}{'scantronformat'} = '';
16561:                 $changes{'scantronformat'} = 1;
16562:             } else {
16563:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
16564:             }
16565:         }
16566:     }
16567:     my @options = ('hdr','pad','rem');
16568:     my @fields = &scantroncsv_fields();
16569:     my %titles = &scantronconfig_titles();
16570:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
16571:     my ($newdat,$currdat,%newcol,%currcol);
16572:     if (grep(/^dat$/,@formats)) {
16573:         $confhash{'scantron'}{config}{dat} = 1;
16574:         $newdat = 1;
16575:     } else {
16576:         $newdat = 0;
16577:     }
16578:     if (grep(/^csv$/,@formats)) {
16579:         my %bynum;
16580:         foreach my $field (@fields) {
16581:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
16582:                 my $posscol = $1;
16583:                 if (($posscol < 20) && (!$bynum{$posscol})) {
16584:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
16585:                     $bynum{$posscol} = $field;
16586:                     $newcol{$field} = $posscol;
16587:                 }
16588:             }
16589:         }
16590:         if (keys(%newcol)) {
16591:             foreach my $option (@options) {
16592:                 if ($env{'form.scantroncsv_'.$option}) {
16593:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
16594:                 }
16595:             }
16596:         }
16597:     }
16598:     $currdat = 1;
16599:     if (ref($domconfig{'scantron'}) eq 'HASH') {
16600:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
16601:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
16602:                 $currdat = 0;
16603:             }
16604:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
16605:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
16606:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
16607:                 }
16608:             }
16609:         }
16610:     }
16611:     if ($currdat != $newdat) {
16612:         $changes{'config'} = 1;
16613:     } else {
16614:         foreach my $field (@fields) {
16615:             if ($currcol{$field} ne '') {
16616:                 if ($currcol{$field} ne $newcol{$field}) {
16617:                     $changes{'config'} = 1;
16618:                     last;
16619:                 }
16620:             } elsif ($newcol{$field} ne '') {
16621:                 $changes{'config'} = 1;
16622:                 last;
16623:             }
16624:         }
16625:     }
16626:     if (keys(%confhash) > 0) {
16627:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
16628:                                                  $dom);
16629:         if ($putresult eq 'ok') {
16630:             if (keys(%changes) > 0) {
16631:                 if (ref($confhash{'scantron'}) eq 'HASH') {
16632:                     $resulttext = &mt('Changes made:').'<ul>';
16633:                     if ($changes{'scantronformat'}) {
16634:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
16635:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
16636:                         } else {
16637:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
16638:                         }
16639:                     }
16640:                     if ($changes{'config'}) {
16641:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
16642:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
16643:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
16644:                             }
16645:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
16646:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
16647:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
16648:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
16649:                                         foreach my $field (@fields) {
16650:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
16651:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
16652:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
16653:                                             }
16654:                                         }
16655:                                         $resulttext .= '</ul></li>';
16656:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
16657:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
16658:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
16659:                                                 foreach my $option (@options) {
16660:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
16661:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
16662:                                                     }
16663:                                                 }
16664:                                                 $resulttext .= '</ul></li>';
16665:                                             }
16666:                                         }
16667:                                     }
16668:                                 }
16669:                             }
16670:                         } else {
16671:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
16672:                         }
16673:                     }
16674:                     $resulttext .= '</ul>';
16675:                 } else {
16676:                     $resulttext = &mt('Changes made to bubblesheet format file.');
16677:                 }
16678:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
16679:                 if (ref($lastactref) eq 'HASH') {
16680:                     $lastactref->{'domainconfig'} = 1;
16681:                 }
16682:             } else {
16683:                 $resulttext = &mt('No changes made to bubblesheet format settings');
16684:             }
16685:         } else {
16686:             $resulttext = '<span class="LC_error">'.
16687:                 &mt('An error occurred: [_1]',$putresult).'</span>';
16688:         }
16689:     } else {
16690:         $resulttext = &mt('No changes made to bubblesheet format file'); 
16691:     }
16692:     if ($errors) {
16693:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
16694:                        $errors.'</ul></p>';
16695:     }
16696:     return $resulttext;
16697: }
16698: 
16699: sub modify_coursecategories {
16700:     my ($dom,$lastactref,%domconfig) = @_;
16701:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
16702:         $cathash);
16703:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
16704:     my @catitems = ('unauth','auth');
16705:     my @cattypes = ('std','domonly','codesrch','none');
16706:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
16707:         $cathash = $domconfig{'coursecategories'}{'cats'};
16708:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
16709:             $changes{'togglecats'} = 1;
16710:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
16711:         }
16712:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
16713:             $changes{'categorize'} = 1;
16714:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
16715:         }
16716:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
16717:             $changes{'togglecatscomm'} = 1;
16718:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
16719:         }
16720:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
16721:             $changes{'categorizecomm'} = 1;
16722:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
16723: 
16724:         }
16725:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
16726:             $changes{'togglecatsplace'} = 1;
16727:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
16728:         }
16729:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
16730:             $changes{'categorizeplace'} = 1;
16731:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
16732:         }
16733:         foreach my $item (@catitems) {
16734:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
16735:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
16736:                     $changes{$item} = 1;
16737:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
16738:                 }
16739:             }
16740:         }
16741:     } else {
16742:         $changes{'togglecats'} = 1;
16743:         $changes{'categorize'} = 1;
16744:         $changes{'togglecatscomm'} = 1;
16745:         $changes{'categorizecomm'} = 1;
16746:         $changes{'togglecatsplace'} = 1;
16747:         $changes{'categorizeplace'} = 1;
16748:         $domconfig{'coursecategories'} = {
16749:                                              togglecats => $env{'form.togglecats'},
16750:                                              categorize => $env{'form.categorize'},
16751:                                              togglecatscomm => $env{'form.togglecatscomm'},
16752:                                              categorizecomm => $env{'form.categorizecomm'},
16753:                                              togglecatsplace => $env{'form.togglecatsplace'},
16754:                                              categorizeplace => $env{'form.categorizeplace'},
16755:                                          };
16756:         foreach my $item (@catitems) {
16757:             if ($env{'form.coursecat_'.$item} ne 'std') {
16758:                 $changes{$item} = 1;
16759:             }
16760:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
16761:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
16762:             }
16763:         }
16764:     }
16765:     if (ref($cathash) eq 'HASH') {
16766:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
16767:             push (@deletecategory,'instcode::0');
16768:         }
16769:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
16770:             push(@deletecategory,'communities::0');
16771:         }
16772:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
16773:             push(@deletecategory,'placement::0');
16774:         }
16775:     }
16776:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
16777:     if (ref($cathash) eq 'HASH') {
16778:         if (@deletecategory > 0) {
16779:             #FIXME Need to remove category from all courses using a deleted category 
16780:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
16781:             foreach my $item (@deletecategory) {
16782:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
16783:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
16784:                     $deletions{$item} = 1;
16785:                     &recurse_cat_deletes($item,$cathash,\%deletions);
16786:                 }
16787:             }
16788:         }
16789:         foreach my $item (keys(%{$cathash})) {
16790:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
16791:             if ($cathash->{$item} ne $env{'form.'.$item}) {
16792:                 $reorderings{$item} = 1;
16793:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
16794:             }
16795:             if ($env{'form.addcategory_name_'.$item} ne '') {
16796:                 my $newcat = $env{'form.addcategory_name_'.$item};
16797:                 my $newdepth = $depth+1;
16798:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
16799:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
16800:                 $adds{$newitem} = 1; 
16801:             }
16802:             if ($env{'form.subcat_'.$item} ne '') {
16803:                 my $newcat = $env{'form.subcat_'.$item};
16804:                 my $newdepth = $depth+1;
16805:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
16806:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
16807:                 $adds{$newitem} = 1;
16808:             }
16809:         }
16810:     }
16811:     if ($env{'form.instcode'} eq '1') {
16812:         if (ref($cathash) eq 'HASH') {
16813:             my $newitem = 'instcode::0';
16814:             if ($cathash->{$newitem} eq '') {  
16815:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
16816:                 $adds{$newitem} = 1;
16817:             }
16818:         } else {
16819:             my $newitem = 'instcode::0';
16820:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
16821:             $adds{$newitem} = 1;
16822:         }
16823:     }
16824:     if ($env{'form.communities'} eq '1') {
16825:         if (ref($cathash) eq 'HASH') {
16826:             my $newitem = 'communities::0';
16827:             if ($cathash->{$newitem} eq '') {
16828:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
16829:                 $adds{$newitem} = 1;
16830:             }
16831:         } else {
16832:             my $newitem = 'communities::0';
16833:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
16834:             $adds{$newitem} = 1;
16835:         }
16836:     }
16837:     if ($env{'form.placement'} eq '1') {
16838:         if (ref($cathash) eq 'HASH') {
16839:             my $newitem = 'placement::0';
16840:             if ($cathash->{$newitem} eq '') {
16841:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
16842:                 $adds{$newitem} = 1;
16843:             }
16844:         } else {
16845:             my $newitem = 'placement::0';
16846:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
16847:             $adds{$newitem} = 1;
16848:         }
16849:     }
16850:     if ($env{'form.addcategory_name'} ne '') {
16851:         if (($env{'form.addcategory_name'} ne 'instcode') &&
16852:             ($env{'form.addcategory_name'} ne 'communities') &&
16853:             ($env{'form.addcategory_name'} ne 'placement')) {
16854:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
16855:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
16856:             $adds{$newitem} = 1;
16857:         }
16858:     }
16859:     my $putresult;
16860:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
16861:         if (keys(%deletions) > 0) {
16862:             foreach my $key (keys(%deletions)) {
16863:                 if ($predelallitems{$key} ne '') {
16864:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
16865:                 }
16866:             }
16867:         }
16868:         my (@chkcats,@chktrails,%chkallitems);
16869:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
16870:         if (ref($chkcats[0]) eq 'ARRAY') {
16871:             my $depth = 0;
16872:             my $chg = 0;
16873:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
16874:                 my $name = $chkcats[0][$i];
16875:                 my $item;
16876:                 if ($name eq '') {
16877:                     $chg ++;
16878:                 } else {
16879:                     $item = &escape($name).'::0';
16880:                     if ($chg) {
16881:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
16882:                     }
16883:                     $depth ++; 
16884:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
16885:                     $depth --;
16886:                 }
16887:             }
16888:         }
16889:     }
16890:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
16891:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
16892:         if ($putresult eq 'ok') {
16893:             my %title = (
16894:                          togglecats     => 'Show/Hide a course in catalog',
16895:                          categorize     => 'Assign a category to a course',
16896:                          togglecatscomm => 'Show/Hide a community in catalog',
16897:                          categorizecomm => 'Assign a category to a community',
16898:                         );
16899:             my %level = (
16900:                          dom  => 'set in Domain ("Modify Course/Community")',
16901:                          crs  => 'set in Course ("Course Configuration")',
16902:                          comm => 'set in Community ("Community Configuration")',
16903:                          none     => 'No catalog',
16904:                          std      => 'Standard catalog',
16905:                          domonly  => 'Domain-only catalog',
16906:                          codesrch => 'Code search form',
16907:                         );
16908:             $resulttext = &mt('Changes made:').'<ul>';
16909:             if ($changes{'togglecats'}) {
16910:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
16911:             }
16912:             if ($changes{'categorize'}) {
16913:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
16914:             }
16915:             if ($changes{'togglecatscomm'}) {
16916:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
16917:             }
16918:             if ($changes{'categorizecomm'}) {
16919:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
16920:             }
16921:             if ($changes{'unauth'}) {
16922:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
16923:             }
16924:             if ($changes{'auth'}) {
16925:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
16926:             }
16927:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
16928:                 my $cathash;
16929:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
16930:                     $cathash = $domconfig{'coursecategories'}{'cats'};
16931:                 } else {
16932:                     $cathash = {};
16933:                 } 
16934:                 my (@cats,@trails,%allitems);
16935:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
16936:                 if (keys(%deletions) > 0) {
16937:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
16938:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
16939:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
16940:                     }
16941:                     $resulttext .= '</ul></li>';
16942:                 }
16943:                 if (keys(%reorderings) > 0) {
16944:                     my %sort_by_trail;
16945:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
16946:                     foreach my $key (keys(%reorderings)) {
16947:                         if ($allitems{$key} ne '') {
16948:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
16949:                         }
16950:                     }
16951:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
16952:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
16953:                     }
16954:                     $resulttext .= '</ul></li>';
16955:                 }
16956:                 if (keys(%adds) > 0) {
16957:                     my %sort_by_trail;
16958:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
16959:                     foreach my $key (keys(%adds)) {
16960:                         if ($allitems{$key} ne '') {
16961:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
16962:                         }
16963:                     }
16964:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
16965:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
16966:                     }
16967:                     $resulttext .= '</ul></li>';
16968:                 }
16969:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
16970:                 if (ref($lastactref) eq 'HASH') {
16971:                     $lastactref->{'cats'} = 1;
16972:                 }
16973:             }
16974:             $resulttext .= '</ul>';
16975:             if ($changes{'unauth'} || $changes{'auth'}) {
16976:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
16977:                 if ($changes{'auth'}) {
16978:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
16979:                 }
16980:                 if ($changes{'unauth'}) {
16981:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
16982:                 }
16983:                 my $cachetime = 24*60*60;
16984:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16985:                 if (ref($lastactref) eq 'HASH') {
16986:                     $lastactref->{'domdefaults'} = 1;
16987:                 }
16988:             }
16989:         } else {
16990:             $resulttext = '<span class="LC_error">'.
16991:                           &mt('An error occurred: [_1]',$putresult).'</span>';
16992:         }
16993:     } else {
16994:         $resulttext = &mt('No changes made to course and community categories');
16995:     }
16996:     return $resulttext;
16997: }
16998: 
16999: sub modify_serverstatuses {
17000:     my ($dom,%domconfig) = @_;
17001:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
17002:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
17003:         %currserverstatus = %{$domconfig{'serverstatuses'}};
17004:     }
17005:     my @pages = &serverstatus_pages();
17006:     foreach my $type (@pages) {
17007:         $newserverstatus{$type}{'namedusers'} = '';
17008:         $newserverstatus{$type}{'machines'} = '';
17009:         if (defined($env{'form.'.$type.'_namedusers'})) {
17010:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
17011:             my @okusers;
17012:             foreach my $user (@users) {
17013:                 my ($uname,$udom) = split(/:/,$user);
17014:                 if (($udom =~ /^$match_domain$/) &&   
17015:                     (&Apache::lonnet::domain($udom)) &&
17016:                     ($uname =~ /^$match_username$/)) {
17017:                     if (!grep(/^\Q$user\E/,@okusers)) {
17018:                         push(@okusers,$user);
17019:                     }
17020:                 }
17021:             }
17022:             if (@okusers > 0) {
17023:                  @okusers = sort(@okusers);
17024:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
17025:             }
17026:         }
17027:         if (defined($env{'form.'.$type.'_machines'})) {
17028:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
17029:             my @okmachines;
17030:             foreach my $ip (@machines) {
17031:                 my @parts = split(/\./,$ip);
17032:                 next if (@parts < 4);
17033:                 my $badip = 0;
17034:                 for (my $i=0; $i<4; $i++) {
17035:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
17036:                         $badip = 1;
17037:                         last;
17038:                     }
17039:                 }
17040:                 if (!$badip) {
17041:                     push(@okmachines,$ip);     
17042:                 }
17043:             }
17044:             @okmachines = sort(@okmachines);
17045:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
17046:         }
17047:     }
17048:     my %serverstatushash =  (
17049:                                 serverstatuses => \%newserverstatus,
17050:                             );
17051:     foreach my $type (@pages) {
17052:         foreach my $setting ('namedusers','machines') {
17053:             my (@current,@new);
17054:             if (ref($currserverstatus{$type}) eq 'HASH') {
17055:                 if ($currserverstatus{$type}{$setting} ne '') { 
17056:                     @current = split(/,/,$currserverstatus{$type}{$setting});
17057:                 }
17058:             }
17059:             if ($newserverstatus{$type}{$setting} ne '') {
17060:                 @new = split(/,/,$newserverstatus{$type}{$setting});
17061:             }
17062:             if (@current > 0) {
17063:                 if (@new > 0) {
17064:                     foreach my $item (@current) {
17065:                         if (!grep(/^\Q$item\E$/,@new)) {
17066:                             $changes{$type}{$setting} = 1;
17067:                             last;
17068:                         }
17069:                     }
17070:                     foreach my $item (@new) {
17071:                         if (!grep(/^\Q$item\E$/,@current)) {
17072:                             $changes{$type}{$setting} = 1;
17073:                             last;
17074:                         }
17075:                     }
17076:                 } else {
17077:                     $changes{$type}{$setting} = 1;
17078:                 }
17079:             } elsif (@new > 0) {
17080:                 $changes{$type}{$setting} = 1;
17081:             }
17082:         }
17083:     }
17084:     if (keys(%changes) > 0) {
17085:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
17086:         my $putresult = &Apache::lonnet::put_dom('configuration',
17087:                                                  \%serverstatushash,$dom);
17088:         if ($putresult eq 'ok') {
17089:             $resulttext .= &mt('Changes made:').'<ul>';
17090:             foreach my $type (@pages) {
17091:                 if (ref($changes{$type}) eq 'HASH') {
17092:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
17093:                     if ($changes{$type}{'namedusers'}) {
17094:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
17095:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
17096:                         } else {
17097:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
17098:                         }
17099:                     }
17100:                     if ($changes{$type}{'machines'}) {
17101:                         if ($newserverstatus{$type}{'machines'} eq '') {
17102:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
17103:                         } else {
17104:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
17105:                         }
17106: 
17107:                     }
17108:                     $resulttext .= '</ul></li>';
17109:                 }
17110:             }
17111:             $resulttext .= '</ul>';
17112:         } else {
17113:             $resulttext = '<span class="LC_error">'.
17114:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
17115: 
17116:         }
17117:     } else {
17118:         $resulttext = &mt('No changes made to access to server status pages');
17119:     }
17120:     return $resulttext;
17121: }
17122: 
17123: sub modify_helpsettings {
17124:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
17125:     my ($resulttext,$errors,%changes,%helphash);
17126:     my %defaultchecked = ('submitbugs' => 'on');
17127:     my @offon = ('off','on');
17128:     my @toggles = ('submitbugs');
17129:     my %current = ('submitbugs' => '',
17130:                    'adhoc'      => {},
17131:                   );
17132:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
17133:         %current = %{$domconfig{'helpsettings'}};
17134:     }
17135:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17136:     foreach my $item (@toggles) {
17137:         if ($defaultchecked{$item} eq 'on') { 
17138:             if ($current{$item} eq '') {
17139:                 if ($env{'form.'.$item} eq '0') {
17140:                     $changes{$item} = 1;
17141:                 }
17142:             } elsif ($current{$item} ne $env{'form.'.$item}) {
17143:                 $changes{$item} = 1;
17144:             }
17145:         } elsif ($defaultchecked{$item} eq 'off') {
17146:             if ($current{$item} eq '') {
17147:                 if ($env{'form.'.$item} eq '1') {
17148:                     $changes{$item} = 1;
17149:                 }
17150:             } elsif ($current{$item} ne $env{'form.'.$item}) {
17151:                 $changes{$item} = 1;
17152:             }
17153:         }
17154:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
17155:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
17156:         }
17157:     }
17158:     my $maxnum = $env{'form.helproles_maxnum'};
17159:     my $confname = $dom.'-domainconfig';
17160:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
17161:     my (@allpos,%newsettings,%changedprivs,$newrole);
17162:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17163:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
17164:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
17165:     my %lt = &Apache::lonlocal::texthash(
17166:                     s      => 'system',
17167:                     d      => 'domain',
17168:                     order  => 'Display order',
17169:                     access => 'Role usage',
17170:                     all    => 'All with domain helpdesk or helpdesk assistant role',
17171:                     dh     => 'All with domain helpdesk role',
17172:                     da     => 'All with domain helpdesk assistant role',
17173:                     none   => 'None',
17174:                     status => 'Determined based on institutional status',
17175:                     inc    => 'Include all, but exclude specific personnel',
17176:                     exc    => 'Exclude all, but include specific personnel',
17177:     );
17178:     for (my $num=0; $num<=$maxnum; $num++) {
17179:         my ($prefix,$identifier,$rolename,%curr);
17180:         if ($num == $maxnum) {
17181:             next unless ($env{'form.newcusthelp'} == $maxnum);
17182:             $identifier = 'custhelp'.$num;
17183:             $prefix = 'helproles_'.$num;
17184:             $rolename = $env{'form.custhelpname'.$num};
17185:             $rolename=~s/[^A-Za-z0-9]//gs;
17186:             next if ($rolename eq '');
17187:             next if (exists($existing{'rolesdef_'.$rolename}));
17188:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
17189:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
17190:                                                      $newprivs{'c'},$confname,$dom);
17191:             if ($result ne 'ok') {
17192:                 $errors .= '<li><span class="LC_error">'.
17193:                            &mt('An error occurred storing the new custom role: [_1]',
17194:                            $result).'</span></li>';
17195:                 next;
17196:             } else {
17197:                 $changedprivs{$rolename} = \%newprivs;
17198:                 $newrole = $rolename;
17199:             }
17200:         } else {
17201:             $prefix = 'helproles_'.$num;
17202:             $rolename = $env{'form.'.$prefix};
17203:             next if ($rolename eq '');
17204:             next unless (exists($existing{'rolesdef_'.$rolename}));
17205:             $identifier = 'custhelp'.$num;
17206:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
17207:             my %currprivs;
17208:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
17209:                 split(/\_/,$existing{'rolesdef_'.$rolename});
17210:             foreach my $level ('c','d','s') {
17211:                 if ($newprivs{$level} ne $currprivs{$level}) {
17212:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
17213:                                                              $newprivs{'c'},$confname,$dom);
17214:                     if ($result ne 'ok') {
17215:                         $errors .= '<li><span class="LC_error">'.
17216:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
17217:                                        $rolename,$result).'</span></li>';
17218:                     } else {
17219:                         $changedprivs{$rolename} = \%newprivs;
17220:                     }
17221:                     last;
17222:                 }
17223:             }
17224:             if (ref($current{'adhoc'}) eq 'HASH') {
17225:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
17226:                     %curr = %{$current{'adhoc'}{$rolename}};
17227:                 }
17228:             }
17229:         }
17230:         my $newpos = $env{'form.'.$prefix.'_pos'};
17231:         $newpos =~ s/\D+//g;
17232:         $allpos[$newpos] = $rolename;
17233:         my $newdesc = $env{'form.'.$prefix.'_desc'};
17234:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
17235:         if ($curr{'desc'}) {
17236:             if ($curr{'desc'} ne $newdesc) {
17237:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
17238:                 $newsettings{$rolename}{'desc'} = $newdesc;
17239:             }
17240:         } elsif ($newdesc ne '') {
17241:             $changes{'customrole'}{$rolename}{'desc'} = 1;
17242:             $newsettings{$rolename}{'desc'} = $newdesc;
17243:         }
17244:         my $access = $env{'form.'.$prefix.'_access'};
17245:         if (grep(/^\Q$access\E$/,@accesstypes)) {
17246:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
17247:             if ($access eq 'status') {
17248:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
17249:                 if (scalar(@statuses) == 0) {
17250:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
17251:                 } else {
17252:                     my (@shownstatus,$numtypes);
17253:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
17254:                     if (ref($types) eq 'ARRAY') {
17255:                         $numtypes = scalar(@{$types});
17256:                         foreach my $type (sort(@statuses)) {
17257:                             if ($type eq 'default') {
17258:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
17259:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
17260:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
17261:                                 push(@shownstatus,$usertypes->{$type});
17262:                             }
17263:                         }
17264:                     }
17265:                     if (grep(/^default$/,@statuses)) {
17266:                         push(@shownstatus,$othertitle);
17267:                     }
17268:                     if (scalar(@shownstatus) == 1+$numtypes) {
17269:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
17270:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
17271:                     } else {
17272:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
17273:                         if (ref($curr{'status'}) eq 'ARRAY') {
17274:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
17275:                             if (@diffs) {
17276:                                 $changes{'customrole'}{$rolename}{$access} = 1;
17277:                             }
17278:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
17279:                             $changes{'customrole'}{$rolename}{$access} = 1;
17280:                         }
17281:                     }
17282:                 }
17283:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
17284:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
17285:                 my @newspecstaff;
17286:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
17287:                 foreach my $person (sort(@personnel)) {
17288:                     if ($domhelpdesk{$person}) {
17289:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
17290:                     }
17291:                 }
17292:                 if (ref($curr{$access}) eq 'ARRAY') {
17293:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
17294:                     if (@diffs) {
17295:                         $changes{'customrole'}{$rolename}{$access} = 1;
17296:                     }
17297:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
17298:                     $changes{'customrole'}{$rolename}{$access} = 1;
17299:                 }
17300:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
17301:                     my ($uname,$udom) = split(/:/,$person);
17302:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
17303:                 }
17304:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
17305:             }
17306:         } else {
17307:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
17308:         }
17309:         unless ($curr{'access'} eq $access) {
17310:             $changes{'customrole'}{$rolename}{'access'} = 1;
17311:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
17312:         }
17313:     }
17314:     if (@allpos > 0) {
17315:         my $idx = 0;
17316:         foreach my $rolename (@allpos) {
17317:             if ($rolename ne '') {
17318:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
17319:                 if (ref($current{'adhoc'}) eq 'HASH') {
17320:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
17321:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
17322:                             $changes{'customrole'}{$rolename}{'order'} = 1;
17323:                             $newsettings{$rolename}{'order'} = $idx+1;
17324:                         }
17325:                     }
17326:                 }
17327:                 $idx ++;
17328:             }
17329:         }
17330:     }
17331:     my $putresult;
17332:     if (keys(%changes) > 0) {
17333:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
17334:         if ($putresult eq 'ok') {
17335:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
17336:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
17337:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
17338:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
17339:                 }
17340:             }
17341:             my $cachetime = 24*60*60;
17342:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17343:             if (ref($lastactref) eq 'HASH') {
17344:                 $lastactref->{'domdefaults'} = 1;
17345:             }
17346:         } else {
17347:             $errors .= '<li><span class="LC_error">'.
17348:                        &mt('An error occurred storing the settings: [_1]',
17349:                            $putresult).'</span></li>';
17350:         }
17351:     }
17352:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
17353:         $resulttext = &mt('Changes made:').'<ul>';
17354:         my (%shownprivs,@levelorder);
17355:         @levelorder = ('c','d','s');
17356:         if ((keys(%changes)) && ($putresult eq 'ok')) {
17357:             foreach my $item (sort(keys(%changes))) {
17358:                 if ($item eq 'submitbugs') {
17359:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
17360:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
17361:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
17362:                 } elsif ($item eq 'customrole') {
17363:                     if (ref($changes{'customrole'}) eq 'HASH') {
17364:                         my @keyorder = ('order','desc','access','status','exc','inc');
17365:                         my %keytext = &Apache::lonlocal::texthash(
17366:                                                                    order  => 'Order',
17367:                                                                    desc   => 'Role description',
17368:                                                                    access => 'Role usage',
17369:                                                                    status => 'Allowed institutional types',
17370:                                                                    exc    => 'Allowed personnel',
17371:                                                                    inc    => 'Disallowed personnel',
17372:                         );
17373:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
17374:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
17375:                                 if ($role eq $newrole) {
17376:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
17377:                                                               $role).'<ul>';
17378:                                 } else {
17379:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
17380:                                                               $role).'<ul>';
17381:                                 }
17382:                                 foreach my $key (@keyorder) {
17383:                                     if ($changes{'customrole'}{$role}{$key}) {
17384:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
17385:                                                                   $keytext{$key},$newsettings{$role}{$key}).
17386:                                                        '</li>';
17387:                                     }
17388:                                 }
17389:                                 if (ref($changedprivs{$role}) eq 'HASH') {
17390:                                     $shownprivs{$role} = 1;
17391:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
17392:                                     foreach my $level (@levelorder) {
17393:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
17394:                                             next if ($item eq '');
17395:                                             my ($priv) = split(/\&/,$item,2);
17396:                                             if (&Apache::lonnet::plaintext($priv)) {
17397:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
17398:                                                 unless ($level eq 'c') {
17399:                                                     $resulttext .= ' ('.$lt{$level}.')';
17400:                                                 }
17401:                                                 $resulttext .= '</li>';
17402:                                             }
17403:                                         }
17404:                                     }
17405:                                     $resulttext .= '</ul>';
17406:                                 }
17407:                                 $resulttext .= '</ul></li>';
17408:                             }
17409:                         }
17410:                     }
17411:                 }
17412:             }
17413:         }
17414:         if (keys(%changedprivs)) {
17415:             foreach my $role (sort(keys(%changedprivs))) {
17416:                 unless ($shownprivs{$role}) {
17417:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
17418:                                               $role).'<ul>'.
17419:                                    '<li>'.&mt('Privileges set to :').'<ul>';
17420:                     foreach my $level (@levelorder) {
17421:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
17422:                             next if ($item eq '');
17423:                             my ($priv) = split(/\&/,$item,2);
17424:                             if (&Apache::lonnet::plaintext($priv)) {
17425:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
17426:                                 unless ($level eq 'c') {
17427:                                     $resulttext .= ' ('.$lt{$level}.')';
17428:                                 }
17429:                                 $resulttext .= '</li>';
17430:                             }
17431:                         }
17432:                     }
17433:                     $resulttext .= '</ul></li></ul></li>';
17434:                 }
17435:             }
17436:         }
17437:         $resulttext .= '</ul>';
17438:     } else {
17439:         $resulttext = &mt('No changes made to help settings');
17440:     }
17441:     if ($errors) {
17442:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
17443:                                     $errors.'</ul>';
17444:     }
17445:     return $resulttext;
17446: }
17447: 
17448: sub modify_coursedefaults {
17449:     my ($dom,$lastactref,%domconfig) = @_;
17450:     my ($resulttext,$errors,%changes,%defaultshash);
17451:     my %defaultchecked = (
17452:                            'canuse_pdfforms' => 'off',
17453:                            'uselcmath'       => 'on',
17454:                            'usejsme'         => 'on'
17455:                          );
17456:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
17457:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
17458:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
17459:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
17460:                    'mysqltables_textbook','mysqltables_placement');
17461:     my @types = ('official','unofficial','community','textbook','placement');
17462:     my %staticdefaults = (
17463:                            anonsurvey_threshold => 10,
17464:                            uploadquota          => 500,
17465:                            postsubmit           => 60,
17466:                            mysqltables          => 172800,
17467:                          );
17468:     my %texoptions = (
17469:                         MathJax  => 'MathJax',
17470:                         mimetex  => &mt('Convert to Images'),
17471:                         tth      => &mt('TeX to HTML'),
17472:                      );
17473:     $defaultshash{'coursedefaults'} = {};
17474: 
17475:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
17476:         if ($domconfig{'coursedefaults'} eq '') {
17477:             $domconfig{'coursedefaults'} = {};
17478:         }
17479:     }
17480: 
17481:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
17482:         foreach my $item (@toggles) {
17483:             if ($defaultchecked{$item} eq 'on') {
17484:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
17485:                     ($env{'form.'.$item} eq '0')) {
17486:                     $changes{$item} = 1;
17487:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
17488:                     $changes{$item} = 1;
17489:                 }
17490:             } elsif ($defaultchecked{$item} eq 'off') {
17491:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
17492:                     ($env{'form.'.$item} eq '1')) {
17493:                     $changes{$item} = 1;
17494:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
17495:                     $changes{$item} = 1;
17496:                 }
17497:             }
17498:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
17499:         }
17500:         foreach my $item (@numbers) {
17501:             my ($currdef,$newdef);
17502:             $newdef = $env{'form.'.$item};
17503:             if ($item eq 'anonsurvey_threshold') {
17504:                 $currdef = $domconfig{'coursedefaults'}{$item};
17505:                 $newdef =~ s/\D//g;
17506:                 if ($newdef eq '' || $newdef < 1) {
17507:                     $newdef = 1;
17508:                 }
17509:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
17510:             } else {
17511:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
17512:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
17513:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
17514:                 }
17515:                 $newdef =~ s/[^\w.\-]//g;
17516:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
17517:             }
17518:             if ($currdef ne $newdef) {
17519:                 if ($item eq 'anonsurvey_threshold') {
17520:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
17521:                         $changes{$item} = 1;
17522:                     }
17523:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
17524:                     my $setting = $1;
17525:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
17526:                         $changes{$setting} = 1;
17527:                     }
17528:                 }
17529:             }
17530:         }
17531:         my $texengine;
17532:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
17533:             $texengine = $env{'form.texengine'};
17534:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
17535:             if ($currdef eq '') {
17536:                 unless ($texengine eq $Apache::lonnet::deftex) {
17537:                     $changes{'texengine'} = 1;
17538:                 }
17539:             } elsif ($currdef ne $texengine) {
17540:                 $changes{'texengine'} = 1;
17541:             }
17542:         }
17543:         if ($texengine ne '') {
17544:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
17545:         }
17546:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
17547:         my @currclonecode;
17548:         if (ref($currclone) eq 'HASH') {
17549:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
17550:                 @currclonecode = @{$currclone->{'instcode'}};
17551:             }
17552:         }
17553:         my $newclone;
17554:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
17555:             $newclone = $env{'form.canclone'};
17556:         }
17557:         if ($newclone eq 'instcode') {
17558:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
17559:             my (%codedefaults,@code_order,@clonecode);
17560:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
17561:                                                     \@code_order);
17562:             foreach my $item (@code_order) {
17563:                 if (grep(/^\Q$item\E$/,@newcodes)) {
17564:                     push(@clonecode,$item);
17565:                 }
17566:             }
17567:             if (@clonecode) {
17568:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
17569:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
17570:                 if (@diffs) {
17571:                     $changes{'canclone'} = 1;
17572:                 }
17573:             } else {
17574:                 $newclone eq '';
17575:             }
17576:         } elsif ($newclone ne '') {
17577:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
17578:         }
17579:         if ($newclone ne $currclone) {
17580:             $changes{'canclone'} = 1;
17581:         }
17582:         my %credits;
17583:         foreach my $type (@types) {
17584:             unless ($type eq 'community') {
17585:                 $credits{$type} = $env{'form.'.$type.'_credits'};
17586:                 $credits{$type} =~ s/[^\d.]+//g;
17587:             }
17588:         }
17589:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
17590:             ($env{'form.coursecredits'} eq '1')) {
17591:             $changes{'coursecredits'} = 1;
17592:             foreach my $type (keys(%credits)) {
17593:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
17594:             }
17595:         } else {
17596:             if ($env{'form.coursecredits'} eq '1') {
17597:                 foreach my $type (@types) {
17598:                     unless ($type eq 'community') {
17599:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
17600:                             $changes{'coursecredits'} = 1;
17601:                         }
17602:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
17603:                     }
17604:                 }
17605:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
17606:                 foreach my $type (@types) {
17607:                     unless ($type eq 'community') {
17608:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
17609:                             $changes{'coursecredits'} = 1;
17610:                             last;
17611:                         }
17612:                     }
17613:                 }
17614:             }
17615:         }
17616:         if ($env{'form.postsubmit'} eq '1') {
17617:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
17618:             my %currtimeout;
17619:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
17620:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
17621:                     $changes{'postsubmit'} = 1;
17622:                 }
17623:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
17624:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
17625:                 }
17626:             } else {
17627:                 $changes{'postsubmit'} = 1;
17628:             }
17629:             foreach my $type (@types) {
17630:                 my $timeout = $env{'form.'.$type.'_timeout'};
17631:                 $timeout =~ s/\D//g;
17632:                 if ($timeout == $staticdefaults{'postsubmit'}) {
17633:                     $timeout = '';
17634:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
17635:                     $timeout = '0';
17636:                 }
17637:                 unless ($timeout eq '') {
17638:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
17639:                 }
17640:                 if (exists($currtimeout{$type})) {
17641:                     if ($timeout ne $currtimeout{$type}) {
17642:                         $changes{'postsubmit'} = 1;
17643:                     }
17644:                 } elsif ($timeout ne '') {
17645:                     $changes{'postsubmit'} = 1;
17646:                 }
17647:             }
17648:         } else {
17649:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
17650:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
17651:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
17652:                     $changes{'postsubmit'} = 1;
17653:                 }
17654:             } else {
17655:                 $changes{'postsubmit'} = 1;
17656:             }
17657:         }
17658:     }
17659:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
17660:                                              $dom);
17661:     if ($putresult eq 'ok') {
17662:         if (keys(%changes) > 0) {
17663:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17664:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
17665:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
17666:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
17667:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
17668:                     if ($changes{$item}) {
17669:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
17670:                     }
17671:                 }
17672:                 if ($changes{'coursecredits'}) {
17673:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
17674:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
17675:                             $domdefaults{$type.'credits'} =
17676:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
17677:                         }
17678:                     }
17679:                 }
17680:                 if ($changes{'postsubmit'}) {
17681:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
17682:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
17683:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
17684:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
17685:                                 $domdefaults{$type.'postsubtimeout'} =
17686:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
17687:                             }
17688:                         }
17689:                     }
17690:                 }
17691:                 if ($changes{'uploadquota'}) {
17692:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
17693:                         foreach my $type (@types) {
17694:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
17695:                         }
17696:                     }
17697:                 }
17698:                 if ($changes{'canclone'}) {
17699:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
17700:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
17701:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
17702:                             if (@clonecodes) {
17703:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
17704:                             }
17705:                         }
17706:                     } else {
17707:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
17708:                     }
17709:                 }
17710:                 my $cachetime = 24*60*60;
17711:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17712:                 if (ref($lastactref) eq 'HASH') {
17713:                     $lastactref->{'domdefaults'} = 1;
17714:                 }
17715:             }
17716:             $resulttext = &mt('Changes made:').'<ul>';
17717:             foreach my $item (sort(keys(%changes))) {
17718:                 if ($item eq 'canuse_pdfforms') {
17719:                     if ($env{'form.'.$item} eq '1') {
17720:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
17721:                     } else {
17722:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
17723:                     }
17724:                 } elsif ($item eq 'uselcmath') {
17725:                     if ($env{'form.'.$item} eq '1') {
17726:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
17727:                     } else {
17728:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
17729:                     }
17730:                 } elsif ($item eq 'usejsme') {
17731:                     if ($env{'form.'.$item} eq '1') {
17732:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
17733:                     } else {
17734:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
17735:                     }
17736:                 } elsif ($item eq 'texengine') {
17737:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
17738:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
17739:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
17740:                     }
17741:                 } elsif ($item eq 'anonsurvey_threshold') {
17742:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
17743:                 } elsif ($item eq 'uploadquota') {
17744:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
17745:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
17746:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
17747:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
17748:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
17749:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
17750:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
17751:                                        '</ul>'.
17752:                                        '</li>';
17753:                     } else {
17754:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
17755:                     }
17756:                 } elsif ($item eq 'mysqltables') {
17757:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
17758:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
17759:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
17760:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
17761:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
17762:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
17763:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
17764:                                        '</ul>'.
17765:                                        '</li>';
17766:                     } else {
17767:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
17768:                     }
17769:                 } elsif ($item eq 'postsubmit') {
17770:                     if ($domdefaults{'postsubmit'} eq 'off') {
17771:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
17772:                     } else {
17773:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
17774:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
17775:                             $resulttext .= &mt('durations:').'<ul>';
17776:                             foreach my $type (@types) {
17777:                                 $resulttext .= '<li>';
17778:                                 my $timeout;
17779:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
17780:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
17781:                                 }
17782:                                 my $display;
17783:                                 if ($timeout eq '0') {
17784:                                     $display = &mt('unlimited');
17785:                                 } elsif ($timeout eq '') {
17786:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
17787:                                 } else {
17788:                                     $display = &mt('[quant,_1,second]',$timeout);
17789:                                 }
17790:                                 if ($type eq 'community') {
17791:                                     $resulttext .= &mt('Communities');
17792:                                 } elsif ($type eq 'official') {
17793:                                     $resulttext .= &mt('Official courses');
17794:                                 } elsif ($type eq 'unofficial') {
17795:                                     $resulttext .= &mt('Unofficial courses');
17796:                                 } elsif ($type eq 'textbook') {
17797:                                     $resulttext .= &mt('Textbook courses');
17798:                                 } elsif ($type eq 'placement') {
17799:                                     $resulttext .= &mt('Placement tests');
17800:                                 }
17801:                                 $resulttext .= ' -- '.$display.'</li>';
17802:                             }
17803:                             $resulttext .= '</ul>';
17804:                         }
17805:                         $resulttext .= '</li>';
17806:                     }
17807:                 } elsif ($item eq 'coursecredits') {
17808:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
17809:                         if (($domdefaults{'officialcredits'} eq '') &&
17810:                             ($domdefaults{'unofficialcredits'} eq '') &&
17811:                             ($domdefaults{'textbookcredits'} eq '')) {
17812:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
17813:                         } else {
17814:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
17815:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
17816:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
17817:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
17818:                                            '</ul>'.
17819:                                            '</li>';
17820:                         }
17821:                     } else {
17822:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
17823:                     }
17824:                 } elsif ($item eq 'canclone') {
17825:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
17826:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
17827:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
17828:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
17829:                         }
17830:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
17831:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
17832:                     } else {
17833:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
17834:                     }
17835:                 }
17836:             }
17837:             $resulttext .= '</ul>';
17838:         } else {
17839:             $resulttext = &mt('No changes made to course defaults');
17840:         }
17841:     } else {
17842:         $resulttext = '<span class="LC_error">'.
17843:             &mt('An error occurred: [_1]',$putresult).'</span>';
17844:     }
17845:     return $resulttext;
17846: }
17847: 
17848: sub modify_selfenrollment {
17849:     my ($dom,$lastactref,%domconfig) = @_;
17850:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
17851:     my @types = ('official','unofficial','community','textbook','placement');
17852:     my %titles = &tool_titles();
17853:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
17854:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
17855:     $ordered{'default'} = ['types','registered','approval','limit'];
17856: 
17857:     my (%roles,%shown,%toplevel);
17858:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
17859: 
17860:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
17861:         if ($domconfig{'selfenrollment'} eq '') {
17862:             $domconfig{'selfenrollment'} = {};
17863:         }
17864:     }
17865:     %toplevel = (
17866:                   admin      => 'Configuration Rights',
17867:                   default    => 'Default settings',
17868:                   validation => 'Validation of self-enrollment requests',
17869:                 );
17870:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
17871: 
17872:     if (ref($ordered{'admin'}) eq 'ARRAY') {
17873:         foreach my $item (@{$ordered{'admin'}}) {
17874:             foreach my $type (@types) {
17875:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
17876:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
17877:                 } else {
17878:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
17879:                 }
17880:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
17881:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
17882:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
17883:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
17884:                             push(@{$changes{'admin'}{$type}},$item);
17885:                         }
17886:                     } else {
17887:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
17888:                             push(@{$changes{'admin'}{$type}},$item);
17889:                         }
17890:                     }
17891:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
17892:                     push(@{$changes{'admin'}{$type}},$item);
17893:                 }
17894:             }
17895:         }
17896:     }
17897: 
17898:     foreach my $item (@{$ordered{'default'}}) {
17899:         foreach my $type (@types) {
17900:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
17901:             if ($item eq 'types') {
17902:                 unless (($value eq 'all') || ($value eq 'dom')) {
17903:                     $value = '';
17904:                 }
17905:             } elsif ($item eq 'registered') {
17906:                 unless ($value eq '1') {
17907:                     $value = 0;
17908:                 }
17909:             } elsif ($item eq 'approval') {
17910:                 unless ($value =~ /^[012]$/) {
17911:                     $value = 0;
17912:                 }
17913:             } else {
17914:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
17915:                     $value = 'none';
17916:                 }
17917:             }
17918:             $selfenrollhash{'default'}{$type}{$item} = $value;
17919:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
17920:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
17921:                     if ($selfenrollhash{'default'}{$type}{$item} ne
17922:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
17923:                          push(@{$changes{'default'}{$type}},$item);
17924:                     }
17925:                 } else {
17926:                     push(@{$changes{'default'}{$type}},$item);
17927:                 }
17928:             } else {
17929:                 push(@{$changes{'default'}{$type}},$item);
17930:             }
17931:             if ($item eq 'limit') {
17932:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
17933:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
17934:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
17935:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
17936:                     }
17937:                 } else {
17938:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
17939:                 }
17940:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
17941:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
17942:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
17943:                          push(@{$changes{'default'}{$type}},'cap');
17944:                     }
17945:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
17946:                     push(@{$changes{'default'}{$type}},'cap');
17947:                 }
17948:             }
17949:         }
17950:     }
17951: 
17952:     foreach my $item (@{$itemsref}) {
17953:         if ($item eq 'fields') {
17954:             my @changed;
17955:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
17956:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
17957:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
17958:             }
17959:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
17960:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
17961:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
17962:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
17963:                 } else {
17964:                     @changed = @{$selfenrollhash{'validation'}{$item}};
17965:                 }
17966:             } else {
17967:                 @changed = @{$selfenrollhash{'validation'}{$item}};
17968:             }
17969:             if (@changed) {
17970:                 if ($selfenrollhash{'validation'}{$item}) { 
17971:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
17972:                 } else {
17973:                     $changes{'validation'}{$item} = &mt('None');
17974:                 }
17975:             }
17976:         } else {
17977:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
17978:             if ($item eq 'markup') {
17979:                if ($env{'form.selfenroll_validation_'.$item}) {
17980:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
17981:                }
17982:             }
17983:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
17984:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
17985:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
17986:                 }
17987:             }
17988:         }
17989:     }
17990: 
17991:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
17992:                                              $dom);
17993:     if ($putresult eq 'ok') {
17994:         if (keys(%changes) > 0) {
17995:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17996:             $resulttext = &mt('Changes made:').'<ul>';
17997:             foreach my $key ('admin','default','validation') {
17998:                 if (ref($changes{$key}) eq 'HASH') {
17999:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
18000:                     if ($key eq 'validation') {
18001:                         foreach my $item (@{$itemsref}) {
18002:                             if (exists($changes{$key}{$item})) {
18003:                                 if ($item eq 'markup') {
18004:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
18005:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
18006:                                 } else {  
18007:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
18008:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
18009:                                 }
18010:                             }
18011:                         }
18012:                     } else {
18013:                         foreach my $type (@types) {
18014:                             if ($type eq 'community') {
18015:                                 $roles{'1'} = &mt('Community personnel');
18016:                             } else {
18017:                                 $roles{'1'} = &mt('Course personnel');
18018:                             }
18019:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
18020:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
18021:                                     if ($key eq 'admin') {
18022:                                         my @mgrdc = ();
18023:                                         if (ref($ordered{$key}) eq 'ARRAY') {
18024:                                             foreach my $item (@{$ordered{'admin'}}) {
18025:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
18026:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
18027:                                                         push(@mgrdc,$item);
18028:                                                     }
18029:                                                 }
18030:                                             }
18031:                                             if (@mgrdc) {
18032:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
18033:                                             } else {
18034:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
18035:                                             }
18036:                                         }
18037:                                     } else {
18038:                                         if (ref($ordered{$key}) eq 'ARRAY') {
18039:                                             foreach my $item (@{$ordered{$key}}) {
18040:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
18041:                                                     $domdefaults{$type.'selfenroll'.$item} =
18042:                                                         $selfenrollhash{$key}{$type}{$item};
18043:                                                 }
18044:                                             }
18045:                                         }
18046:                                     }
18047:                                 }
18048:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
18049:                                 foreach my $item (@{$ordered{$key}}) {
18050:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
18051:                                         $resulttext .= '<li>';
18052:                                         if ($key eq 'admin') {
18053:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
18054:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
18055:                                         } else {
18056:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
18057:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
18058:                                         }
18059:                                         $resulttext .= '</li>';
18060:                                     }
18061:                                 }
18062:                                 $resulttext .= '</ul></li>';
18063:                             }
18064:                         }
18065:                         $resulttext .= '</ul></li>'; 
18066:                     }
18067:                 }
18068:             }
18069:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
18070:                 my $cachetime = 24*60*60;
18071:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18072:                 if (ref($lastactref) eq 'HASH') {
18073:                     $lastactref->{'domdefaults'} = 1;
18074:                 }
18075:             }
18076:             $resulttext .= '</ul>';
18077:         } else {
18078:             $resulttext = &mt('No changes made to self-enrollment settings');
18079:         }
18080:     } else {
18081:         $resulttext = '<span class="LC_error">'.
18082:             &mt('An error occurred: [_1]',$putresult).'</span>';
18083:     }
18084:     return $resulttext;
18085: }
18086: 
18087: sub modify_usersessions {
18088:     my ($dom,$lastactref,%domconfig) = @_;
18089:     my @hostingtypes = ('version','excludedomain','includedomain');
18090:     my @offloadtypes = ('primary','default');
18091:     my %types = (
18092:                   remote => \@hostingtypes,
18093:                   hosted => \@hostingtypes,
18094:                   spares => \@offloadtypes,
18095:                 );
18096:     my @prefixes = ('remote','hosted','spares');
18097:     my @lcversions = &Apache::lonnet::all_loncaparevs();
18098:     my (%by_ip,%by_location,@intdoms,@instdoms);
18099:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
18100:     my @locations = sort(keys(%by_location));
18101:     my (%defaultshash,%changes);
18102:     foreach my $prefix (@prefixes) {
18103:         $defaultshash{'usersessions'}{$prefix} = {};
18104:     }
18105:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18106:     my $resulttext;
18107:     my %iphost = &Apache::lonnet::get_iphost();
18108:     foreach my $prefix (@prefixes) {
18109:         next if ($prefix eq 'spares');
18110:         foreach my $type (@{$types{$prefix}}) {
18111:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
18112:             if ($type eq 'version') {
18113:                 my $value = $env{'form.'.$prefix.'_'.$type};
18114:                 my $okvalue;
18115:                 if ($value ne '') {
18116:                     if (grep(/^\Q$value\E$/,@lcversions)) {
18117:                         $okvalue = $value;
18118:                     }
18119:                 }
18120:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
18121:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
18122:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
18123:                             if ($inuse == 0) {
18124:                                 $changes{$prefix}{$type} = 1;
18125:                             } else {
18126:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
18127:                                     $changes{$prefix}{$type} = 1;
18128:                                 }
18129:                                 if ($okvalue ne '') {
18130:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
18131:                                 } 
18132:                             }
18133:                         } else {
18134:                             if (($inuse == 1) && ($okvalue ne '')) {
18135:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
18136:                                 $changes{$prefix}{$type} = 1;
18137:                             }
18138:                         }
18139:                     } else {
18140:                         if (($inuse == 1) && ($okvalue ne '')) {
18141:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
18142:                             $changes{$prefix}{$type} = 1;
18143:                         }
18144:                     }
18145:                 } else {
18146:                     if (($inuse == 1) && ($okvalue ne '')) {
18147:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
18148:                         $changes{$prefix}{$type} = 1;
18149:                     }
18150:                 }
18151:             } else {
18152:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
18153:                 my @okvals;
18154:                 foreach my $val (@vals) {
18155:                     if ($val =~ /:/) {
18156:                         my @items = split(/:/,$val);
18157:                         foreach my $item (@items) {
18158:                             if (ref($by_location{$item}) eq 'ARRAY') {
18159:                                 push(@okvals,$item);
18160:                             }
18161:                         }
18162:                     } else {
18163:                         if (ref($by_location{$val}) eq 'ARRAY') {
18164:                             push(@okvals,$val);
18165:                         }
18166:                     }
18167:                 }
18168:                 @okvals = sort(@okvals);
18169:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
18170:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
18171:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
18172:                             if ($inuse == 0) {
18173:                                 $changes{$prefix}{$type} = 1; 
18174:                             } else {
18175:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
18176:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
18177:                                 if (@changed > 0) {
18178:                                     $changes{$prefix}{$type} = 1;
18179:                                 }
18180:                             }
18181:                         } else {
18182:                             if ($inuse == 1) {
18183:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
18184:                                 $changes{$prefix}{$type} = 1;
18185:                             }
18186:                         } 
18187:                     } else {
18188:                         if ($inuse == 1) {
18189:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
18190:                             $changes{$prefix}{$type} = 1;
18191:                         }
18192:                     }
18193:                 } else {
18194:                     if ($inuse == 1) {
18195:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
18196:                         $changes{$prefix}{$type} = 1;
18197:                     }
18198:                 }
18199:             }
18200:         }
18201:     }
18202: 
18203:     my @alldoms = &Apache::lonnet::all_domains();
18204:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
18205:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
18206:     my $savespares;
18207: 
18208:     foreach my $lonhost (sort(keys(%servers))) {
18209:         my $serverhomeID =
18210:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
18211:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
18212:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
18213:         my %spareschg;
18214:         foreach my $type (@{$types{'spares'}}) {
18215:             my @okspares;
18216:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
18217:             foreach my $server (@checked) {
18218:                 if (&Apache::lonnet::hostname($server) ne '') {
18219:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
18220:                         unless (grep(/^\Q$server\E$/,@okspares)) {
18221:                             push(@okspares,$server);
18222:                         }
18223:                     }
18224:                 }
18225:             }
18226:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
18227:             my $newspare;
18228:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
18229:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
18230:                     $newspare = $new;
18231:                 }
18232:             }
18233:             my @spares;
18234:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
18235:                 @spares = sort(@okspares,$newspare);
18236:             } else {
18237:                 @spares = sort(@okspares);
18238:             }
18239:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
18240:             if (ref($spareid{$lonhost}) eq 'HASH') {
18241:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
18242:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
18243:                     if (@diffs > 0) {
18244:                         $spareschg{$type} = 1;
18245:                     }
18246:                 }
18247:             }
18248:         }
18249:         if (keys(%spareschg) > 0) {
18250:             $changes{'spares'}{$lonhost} = \%spareschg;
18251:         }
18252:     }
18253:     $defaultshash{'usersessions'}{'offloadnow'} = {};
18254:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
18255:     my @okoffload;
18256:     if (@offloadnow) {
18257:         foreach my $server (@offloadnow) {
18258:             if (&Apache::lonnet::hostname($server) ne '') {
18259:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
18260:                     push(@okoffload,$server);
18261:                 }
18262:             }
18263:         }
18264:         if (@okoffload) {
18265:             foreach my $lonhost (@okoffload) {
18266:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
18267:             }
18268:         }
18269:     }
18270:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
18271:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
18272:             if (ref($changes{'spares'}) eq 'HASH') {
18273:                 if (keys(%{$changes{'spares'}}) > 0) {
18274:                     $savespares = 1;
18275:                 }
18276:             }
18277:         } else {
18278:             $savespares = 1;
18279:         }
18280:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
18281:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
18282:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
18283:                     $changes{'offloadnow'} = 1;
18284:                     last;
18285:                 }
18286:             }
18287:             unless ($changes{'offloadnow'}) {
18288:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
18289:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
18290:                         $changes{'offloadnow'} = 1;
18291:                         last;
18292:                     }
18293:                 }
18294:             }
18295:         } elsif (@okoffload) {
18296:             $changes{'offloadnow'} = 1;
18297:         }
18298:     } elsif (@okoffload) {
18299:         $changes{'offloadnow'} = 1;
18300:     }
18301:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
18302:     if ((keys(%changes) > 0) || ($savespares)) {
18303:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
18304:                                                  $dom);
18305:         if ($putresult eq 'ok') {
18306:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
18307:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
18308:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
18309:                 }
18310:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
18311:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
18312:                 }
18313:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
18314:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
18315:                 }
18316:             }
18317:             my $cachetime = 24*60*60;
18318:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18319:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
18320:             if (ref($lastactref) eq 'HASH') {
18321:                 $lastactref->{'domdefaults'} = 1;
18322:                 $lastactref->{'usersessions'} = 1;
18323:             }
18324:             if (keys(%changes) > 0) {
18325:                 my %lt = &usersession_titles();
18326:                 $resulttext = &mt('Changes made:').'<ul>';
18327:                 foreach my $prefix (@prefixes) {
18328:                     if (ref($changes{$prefix}) eq 'HASH') {
18329:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
18330:                         if ($prefix eq 'spares') {
18331:                             if (ref($changes{$prefix}) eq 'HASH') {
18332:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
18333:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
18334:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
18335:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
18336:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
18337:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
18338:                                         foreach my $type (@{$types{$prefix}}) {
18339:                                             if ($changes{$prefix}{$lonhost}{$type}) {
18340:                                                 my $offloadto = &mt('None');
18341:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
18342:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
18343:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
18344:                                                     }
18345:                                                 }
18346:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
18347:                                             }
18348:                                         }
18349:                                     }
18350:                                     $resulttext .= '</li>';
18351:                                 }
18352:                             }
18353:                         } else {
18354:                             foreach my $type (@{$types{$prefix}}) {
18355:                                 if (defined($changes{$prefix}{$type})) {
18356:                                     my ($newvalue,$notinuse);
18357:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
18358:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
18359:                                             if ($type eq 'version') {
18360:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
18361:                                             } else {
18362:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
18363:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
18364:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
18365:                                                     }
18366:                                                 } else {
18367:                                                     $notinuse = 1;
18368:                                                 }
18369:                                             }
18370:                                         }
18371:                                     }
18372:                                     if ($newvalue eq '') {
18373:                                         if ($type eq 'version') {
18374:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
18375:                                         } elsif ($notinuse) {
18376:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
18377:                                         } else {
18378:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
18379:                                         }
18380:                                     } else {
18381:                                         if ($type eq 'version') {
18382:                                             $newvalue .= ' '.&mt('(or later)');
18383:                                         }
18384:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
18385:                                     }
18386:                                 }
18387:                             }
18388:                         }
18389:                         $resulttext .= '</ul>';
18390:                     }
18391:                 }
18392:                 if ($changes{'offloadnow'}) {
18393:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
18394:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
18395:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
18396:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
18397:                                 $resulttext .= '<li>'.$lonhost.'</li>';
18398:                             }
18399:                             $resulttext .= '</ul>';
18400:                         } else {
18401:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
18402:                         }
18403:                     } else {
18404:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
18405:                     }
18406:                 }
18407:                 $resulttext .= '</ul>';
18408:             } else {
18409:                 $resulttext = $nochgmsg;
18410:             }
18411:         } else {
18412:             $resulttext = '<span class="LC_error">'.
18413:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18414:         }
18415:     } else {
18416:         $resulttext = $nochgmsg;
18417:     }
18418:     return $resulttext;
18419: }
18420: 
18421: sub modify_ssl {
18422:     my ($dom,$lastactref,%domconfig) = @_;
18423:     my (%by_ip,%by_location,@intdoms,@instdoms);
18424:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
18425:     my @locations = sort(keys(%by_location));
18426:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
18427:     my (%defaultshash,%changes);
18428:     my $action = 'ssl';
18429:     my @prefixes = ('connto','connfrom','replication');
18430:     foreach my $prefix (@prefixes) {
18431:         $defaultshash{$action}{$prefix} = {};
18432:     }
18433:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18434:     my $resulttext;
18435:     my %iphost = &Apache::lonnet::get_iphost();
18436:     my @reptypes = ('certreq','nocertreq');
18437:     my @connecttypes = ('dom','intdom','other');
18438:     my %types = (
18439:                   connto      => \@connecttypes,
18440:                   connfrom    => \@connecttypes,
18441:                   replication => \@reptypes,
18442:                 );
18443:     foreach my $prefix (sort(keys(%types))) {
18444:         foreach my $type (@{$types{$prefix}}) {
18445:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
18446:                 my $value = 'yes';
18447:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
18448:                     $value = $env{'form.'.$prefix.'_'.$type};
18449:                 }
18450:                 if (ref($domconfig{$action}) eq 'HASH') {
18451:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
18452:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
18453:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
18454:                                 $changes{$prefix}{$type} = 1;
18455:                             }
18456:                             $defaultshash{$action}{$prefix}{$type} = $value;
18457:                         } else {
18458:                             $defaultshash{$action}{$prefix}{$type} = $value;
18459:                             $changes{$prefix}{$type} = 1;
18460:                         }
18461:                     } else {
18462:                         $defaultshash{$action}{$prefix}{$type} = $value;
18463:                         $changes{$prefix}{$type} = 1;
18464:                     }
18465:                 } else {
18466:                     $defaultshash{$action}{$prefix}{$type} = $value;
18467:                     $changes{$prefix}{$type} = 1;
18468:                 }
18469:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
18470:                     delete($changes{$prefix}{$type});
18471:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
18472:                     delete($changes{$prefix}{$type});
18473:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
18474:                     delete($changes{$prefix}{$type});
18475:                 }
18476:             } elsif ($prefix eq 'replication') {
18477:                 if (@locations > 0) {
18478:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
18479:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
18480:                     my @okvals;
18481:                     foreach my $val (@vals) {
18482:                         if ($val =~ /:/) {
18483:                             my @items = split(/:/,$val);
18484:                             foreach my $item (@items) {
18485:                                 if (ref($by_location{$item}) eq 'ARRAY') {
18486:                                     push(@okvals,$item);
18487:                                 }
18488:                             }
18489:                         } else {
18490:                             if (ref($by_location{$val}) eq 'ARRAY') {
18491:                                 push(@okvals,$val);
18492:                             }
18493:                         }
18494:                     }
18495:                     @okvals = sort(@okvals);
18496:                     if (ref($domconfig{$action}) eq 'HASH') {
18497:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
18498:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
18499:                                 if ($inuse == 0) {
18500:                                     $changes{$prefix}{$type} = 1;
18501:                                 } else {
18502:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
18503:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
18504:                                     if (@changed > 0) {
18505:                                         $changes{$prefix}{$type} = 1;
18506:                                     }
18507:                                 }
18508:                             } else {
18509:                                 if ($inuse == 1) {
18510:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
18511:                                     $changes{$prefix}{$type} = 1;
18512:                                 }
18513:                             }
18514:                         } else {
18515:                             if ($inuse == 1) {
18516:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
18517:                                 $changes{$prefix}{$type} = 1;
18518:                             }
18519:                         }
18520:                     } else {
18521:                         if ($inuse == 1) {
18522:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
18523:                             $changes{$prefix}{$type} = 1;
18524:                         }
18525:                     }
18526:                 }
18527:             }
18528:         }
18529:     }
18530:     if (keys(%changes)) {
18531:         foreach my $prefix (keys(%changes)) {
18532:             if (ref($changes{$prefix}) eq 'HASH') {
18533:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
18534:                     delete($changes{$prefix});
18535:                 }
18536:             } else {
18537:                 delete($changes{$prefix});
18538:             }
18539:         }
18540:     }
18541:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
18542:     if (keys(%changes) > 0) {
18543:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
18544:                                                  $dom);
18545:         if ($putresult eq 'ok') {
18546:             if (ref($defaultshash{$action}) eq 'HASH') {
18547:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
18548:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
18549:                 }
18550:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
18551:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
18552:                 }
18553:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
18554:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
18555:                 }
18556:             }
18557:             my $cachetime = 24*60*60;
18558:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18559:             if (ref($lastactref) eq 'HASH') {
18560:                 $lastactref->{'domdefaults'} = 1;
18561:             }
18562:             if (keys(%changes) > 0) {
18563:                 my %titles = &ssl_titles();
18564:                 $resulttext = &mt('Changes made:').'<ul>';
18565:                 foreach my $prefix (@prefixes) {
18566:                     if (ref($changes{$prefix}) eq 'HASH') {
18567:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
18568:                         foreach my $type (@{$types{$prefix}}) {
18569:                             if (defined($changes{$prefix}{$type})) {
18570:                                 my ($newvalue,$notinuse);
18571:                                 if (ref($defaultshash{$action}) eq 'HASH') {
18572:                                     if (ref($defaultshash{$action}{$prefix})) {
18573:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
18574:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
18575:                                         } else {
18576:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
18577:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
18578:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
18579:                                                 }
18580:                                             } else {
18581:                                                 $notinuse = 1;
18582:                                             }
18583:                                         }
18584:                                     }
18585:                                     if ($notinuse) {
18586:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
18587:                                     } elsif ($newvalue eq '') {
18588:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
18589:                                     } else {
18590:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
18591:                                     }
18592:                                 }
18593:                             }
18594:                         }
18595:                         $resulttext .= '</ul>';
18596:                     }
18597:                 }
18598:             } else {
18599:                 $resulttext = $nochgmsg;
18600:             }
18601:         } else {
18602:             $resulttext = '<span class="LC_error">'.
18603:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18604:         }
18605:     } else {
18606:         $resulttext = $nochgmsg;
18607:     }
18608:     return $resulttext;
18609: }
18610: 
18611: sub modify_trust {
18612:     my ($dom,$lastactref,%domconfig) = @_;
18613:     my (%by_ip,%by_location,@intdoms,@instdoms);
18614:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
18615:     my @locations = sort(keys(%by_location));
18616:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
18617:     my @types = ('exc','inc');
18618:     my (%defaultshash,%changes);
18619:     foreach my $prefix (@prefixes) {
18620:         $defaultshash{'trust'}{$prefix} = {};
18621:     }
18622:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18623:     my $resulttext;
18624:     foreach my $prefix (@prefixes) {
18625:         foreach my $type (@types) {
18626:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
18627:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
18628:             my @okvals;
18629:             foreach my $val (@vals) {
18630:                 if ($val =~ /:/) {
18631:                     my @items = split(/:/,$val);
18632:                     foreach my $item (@items) {
18633:                         if (ref($by_location{$item}) eq 'ARRAY') {
18634:                             push(@okvals,$item);
18635:                         }
18636:                     }
18637:                 } else {
18638:                     if (ref($by_location{$val}) eq 'ARRAY') {
18639:                         push(@okvals,$val);
18640:                     }
18641:                 }
18642:             }
18643:             @okvals = sort(@okvals);
18644:             if (ref($domconfig{'trust'}) eq 'HASH') {
18645:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
18646:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
18647:                         if ($inuse == 0) {
18648:                             $changes{$prefix}{$type} = 1;
18649:                         } else {
18650:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
18651:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
18652:                             if (@changed > 0) {
18653:                                 $changes{$prefix}{$type} = 1;
18654:                             }
18655:                         }
18656:                     } else {
18657:                         if ($inuse == 1) {
18658:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
18659:                             $changes{$prefix}{$type} = 1;
18660:                         }
18661:                     }
18662:                 } else {
18663:                     if ($inuse == 1) {
18664:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
18665:                         $changes{$prefix}{$type} = 1;
18666:                     }
18667:                 }
18668:             } else {
18669:                 if ($inuse == 1) {
18670:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
18671:                     $changes{$prefix}{$type} = 1;
18672:                 }
18673:             }
18674:         }
18675:     }
18676:     my $nochgmsg = &mt('No changes made to trust settings.');
18677:     if (keys(%changes) > 0) {
18678:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
18679:                                                  $dom);
18680:         if ($putresult eq 'ok') {
18681:             if (ref($defaultshash{'trust'}) eq 'HASH') {
18682:                 foreach my $prefix (@prefixes) {
18683:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
18684:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
18685:                     }
18686:                 }
18687:             }
18688:             my $cachetime = 24*60*60;
18689:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18690:             if (ref($lastactref) eq 'HASH') {
18691:                 $lastactref->{'domdefaults'} = 1;
18692:             }
18693:             if (keys(%changes) > 0) {
18694:                 my %lt = &trust_titles();
18695:                 $resulttext = &mt('Changes made:').'<ul>';
18696:                 foreach my $prefix (@prefixes) {
18697:                     if (ref($changes{$prefix}) eq 'HASH') {
18698:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
18699:                         foreach my $type (@types) {
18700:                             if (defined($changes{$prefix}{$type})) {
18701:                                 my ($newvalue,$notinuse);
18702:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
18703:                                     if (ref($defaultshash{'trust'}{$prefix})) {
18704:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
18705:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
18706:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
18707:                                             }
18708:                                         } else {
18709:                                             $notinuse = 1;
18710:                                         }
18711:                                     }
18712:                                 }
18713:                                 if ($notinuse) {
18714:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
18715:                                 } elsif ($newvalue eq '') {
18716:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
18717:                                 } else {
18718:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
18719:                                 }
18720:                             }
18721:                         }
18722:                         $resulttext .= '</ul>';
18723:                     }
18724:                 }
18725:                 $resulttext .= '</ul>';
18726:             } else {
18727:                 $resulttext = $nochgmsg;
18728:             }
18729:         } else {
18730:             $resulttext = '<span class="LC_error">'.
18731:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18732:         }
18733:     } else {
18734:         $resulttext = $nochgmsg;
18735:     }
18736:     return $resulttext;
18737: }
18738: 
18739: sub modify_loadbalancing {
18740:     my ($dom,%domconfig) = @_;
18741:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
18742:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
18743:     my ($othertitle,$usertypes,$types) =
18744:         &Apache::loncommon::sorted_inst_types($dom);
18745:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
18746:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
18747:     my @sparestypes = ('primary','default');
18748:     my %typetitles = &sparestype_titles();
18749:     my $resulttext;
18750:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
18751:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
18752:         %existing = %{$domconfig{'loadbalancing'}};
18753:     }
18754:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
18755:                               \%currtargets,\%currrules,\%currcookies);
18756:     my ($saveloadbalancing,%defaultshash,%changes);
18757:     my ($alltypes,$othertypes,$titles) =
18758:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
18759:     my %ruletitles = &offloadtype_text();
18760:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
18761:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
18762:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
18763:         if ($balancer eq '') {
18764:             next;
18765:         }
18766:         if (!exists($servers{$balancer})) {
18767:             if (exists($currbalancer{$balancer})) {
18768:                 push(@{$changes{'delete'}},$balancer);
18769:             }
18770:             next;
18771:         }
18772:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
18773:             push(@{$changes{'delete'}},$balancer);
18774:             next;
18775:         }
18776:         if (!exists($currbalancer{$balancer})) {
18777:             push(@{$changes{'add'}},$balancer);
18778:         }
18779:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
18780:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
18781:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
18782:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
18783:             $saveloadbalancing = 1;
18784:         }
18785:         foreach my $sparetype (@sparestypes) {
18786:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
18787:             my @offloadto;
18788:             foreach my $target (@targets) {
18789:                 if (($servers{$target}) && ($target ne $balancer)) {
18790:                     if ($sparetype eq 'default') {
18791:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
18792:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
18793:                         }
18794:                     }
18795:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
18796:                         push(@offloadto,$target);
18797:                     }
18798:                 }
18799:             }
18800:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
18801:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
18802:                     push(@offloadto,$balancer);
18803:                 }
18804:             }
18805:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
18806:         }
18807:         if ($env{'form.loadbalancing_cookie_'.$i}) {
18808:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
18809:             if (exists($currbalancer{$balancer})) { 
18810:                 unless ($currcookies{$balancer}) {
18811:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
18812:                 }
18813:             }
18814:         } elsif (exists($currbalancer{$balancer})) {
18815:             if ($currcookies{$balancer}) {
18816:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
18817:             }
18818:         }
18819:         if (ref($currtargets{$balancer}) eq 'HASH') {
18820:             foreach my $sparetype (@sparestypes) {
18821:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
18822:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
18823:                     if (@targetdiffs > 0) {
18824:                         $changes{'curr'}{$balancer}{'targets'} = 1;
18825:                     }
18826:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
18827:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
18828:                         $changes{'curr'}{$balancer}{'targets'} = 1;
18829:                     }
18830:                 }
18831:             }
18832:         } else {
18833:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
18834:                 foreach my $sparetype (@sparestypes) {
18835:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
18836:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
18837:                             $changes{'curr'}{$balancer}{'targets'} = 1;
18838:                         }
18839:                     }
18840:                 }
18841:             }
18842:         }
18843:         my $ishomedom;
18844:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
18845:             $ishomedom = 1;
18846:         }
18847:         if (ref($alltypes) eq 'ARRAY') {
18848:             foreach my $type (@{$alltypes}) {
18849:                 my $rule;
18850:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
18851:                          (!$ishomedom)) {
18852:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
18853:                 }
18854:                 if ($rule eq 'specific') {
18855:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
18856:                     if (exists($servers{$specifiedhost})) {
18857:                         $rule = $specifiedhost;
18858:                     }
18859:                 }
18860:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
18861:                 if (ref($currrules{$balancer}) eq 'HASH') {
18862:                     if ($rule ne $currrules{$balancer}{$type}) {
18863:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
18864:                     }
18865:                 } elsif ($rule ne '') {
18866:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
18867:                 }
18868:             }
18869:         }
18870:     }
18871:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
18872:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
18873:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
18874:             $defaultshash{'loadbalancing'} = {};
18875:         }
18876:         my $putresult = &Apache::lonnet::put_dom('configuration',
18877:                                                  \%defaultshash,$dom);
18878:         if ($putresult eq 'ok') {
18879:             if (keys(%changes) > 0) {
18880:                 my %toupdate;
18881:                 if (ref($changes{'delete'}) eq 'ARRAY') {
18882:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
18883:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
18884:                         $toupdate{$balancer} = 1;
18885:                     }
18886:                 }
18887:                 if (ref($changes{'add'}) eq 'ARRAY') {
18888:                     foreach my $balancer (sort(@{$changes{'add'}})) {
18889:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
18890:                         $toupdate{$balancer} = 1;
18891:                     }
18892:                 }
18893:                 if (ref($changes{'curr'}) eq 'HASH') {
18894:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
18895:                         $toupdate{$balancer} = 1;
18896:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
18897:                             if ($changes{'curr'}{$balancer}{'targets'}) {
18898:                                 my %offloadstr;
18899:                                 foreach my $sparetype (@sparestypes) {
18900:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
18901:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
18902:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
18903:                                         }
18904:                                     }
18905:                                 }
18906:                                 if (keys(%offloadstr) == 0) {
18907:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
18908:                                 } else {
18909:                                     my $showoffload;
18910:                                     foreach my $sparetype (@sparestypes) {
18911:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
18912:                                         if (defined($offloadstr{$sparetype})) {
18913:                                             $showoffload .= $offloadstr{$sparetype};
18914:                                         } else {
18915:                                             $showoffload .= &mt('None');
18916:                                         }
18917:                                         $showoffload .= ('&nbsp;'x3);
18918:                                     }
18919:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
18920:                                 }
18921:                             }
18922:                         }
18923:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
18924:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
18925:                                 foreach my $type (@{$alltypes}) {
18926:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
18927:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
18928:                                         my $balancetext;
18929:                                         if ($rule eq '') {
18930:                                             $balancetext =  $ruletitles{'default'};
18931:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
18932:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
18933:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
18934:                                                 foreach my $sparetype (@sparestypes) {
18935:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
18936:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
18937:                                                     }
18938:                                                 }
18939:                                                 foreach my $item (@{$alltypes}) {
18940:                                                     next if ($item =~  /^_LC_ipchange/);
18941:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
18942:                                                     if ($hasrule eq 'homeserver') {
18943:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
18944:                                                     } else {
18945:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
18946:                                                             if ($servers{$hasrule}) {
18947:                                                                 $toupdate{$hasrule} = 1;
18948:                                                             }
18949:                                                         }
18950:                                                     }
18951:                                                 }
18952:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
18953:                                                     $balancetext =  $ruletitles{$rule};
18954:                                                 } else {
18955:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
18956:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
18957:                                                     if ($receiver) {
18958:                                                         $toupdate{$receiver};
18959:                                                     }
18960:                                                 }
18961:                                             } else {
18962:                                                 $balancetext =  $ruletitles{$rule};
18963:                                             }
18964:                                         } else {
18965:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
18966:                                         }
18967:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
18968:                                     }
18969:                                 }
18970:                             }
18971:                         }
18972:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
18973:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
18974:                                                       $balancer).'</li>'; 
18975:                         }
18976:                         if (keys(%toupdate)) {
18977:                             my %thismachine;
18978:                             my $updatedhere;
18979:                             my $cachetime = 60*60*24;
18980:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
18981:                             foreach my $lonhost (keys(%toupdate)) {
18982:                                 if ($thismachine{$lonhost}) {
18983:                                     unless ($updatedhere) {
18984:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
18985:                                                                       $defaultshash{'loadbalancing'},
18986:                                                                       $cachetime);
18987:                                         $updatedhere = 1;
18988:                                     }
18989:                                 } else {
18990:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
18991:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
18992:                                 }
18993:                             }
18994:                         }
18995:                     }
18996:                 }
18997:                 if ($resulttext ne '') {
18998:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
18999:                 } else {
19000:                     $resulttext = $nochgmsg;
19001:                 }
19002:             } else {
19003:                 $resulttext = $nochgmsg;
19004:             }
19005:         } else {
19006:             $resulttext = '<span class="LC_error">'.
19007:                           &mt('An error occurred: [_1]',$putresult).'</span>';
19008:         }
19009:     } else {
19010:         $resulttext = $nochgmsg;
19011:     }
19012:     return $resulttext;
19013: }
19014: 
19015: sub recurse_check {
19016:     my ($chkcats,$categories,$depth,$name) = @_;
19017:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
19018:         my $chg = 0;
19019:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
19020:             my $category = $chkcats->[$depth]{$name}[$j];
19021:             my $item;
19022:             if ($category eq '') {
19023:                 $chg ++;
19024:             } else {
19025:                 my $deeper = $depth + 1;
19026:                 $item = &escape($category).':'.&escape($name).':'.$depth;
19027:                 if ($chg) {
19028:                     $categories->{$item} -= $chg;
19029:                 }
19030:                 &recurse_check($chkcats,$categories,$deeper,$category);
19031:                 $deeper --;
19032:             }
19033:         }
19034:     }
19035:     return;
19036: }
19037: 
19038: sub recurse_cat_deletes {
19039:     my ($item,$coursecategories,$deletions) = @_;
19040:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
19041:     my $subdepth = $depth + 1;
19042:     if (ref($coursecategories) eq 'HASH') {
19043:         foreach my $subitem (keys(%{$coursecategories})) {
19044:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
19045:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
19046:                 delete($coursecategories->{$subitem});
19047:                 $deletions->{$subitem} = 1;
19048:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
19049:             }
19050:         }
19051:     }
19052:     return;
19053: }
19054: 
19055: sub active_dc_picker {
19056:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
19057:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
19058:     my @domcoord = keys(%domcoords);
19059:     if (keys(%currhash)) {
19060:         foreach my $dc (keys(%currhash)) {
19061:             unless (exists($domcoords{$dc})) {
19062:                 push(@domcoord,$dc);
19063:             }
19064:         }
19065:     }
19066:     @domcoord = sort(@domcoord);
19067:     my $numdcs = scalar(@domcoord);
19068:     my $rows = 0;
19069:     my $table;
19070:     if ($numdcs > 1) {
19071:         $table = '<table>';
19072:         for (my $i=0; $i<@domcoord; $i++) {
19073:             my $rem = $i%($numinrow);
19074:             if ($rem == 0) {
19075:                 if ($i > 0) {
19076:                     $table .= '</tr>';
19077:                 }
19078:                 $table .= '<tr>';
19079:                 $rows ++;
19080:             }
19081:             my $check = '';
19082:             if ($inputtype eq 'radio') {
19083:                 if (keys(%currhash) == 0) {
19084:                     if (!$i) {
19085:                         $check = ' checked="checked"';
19086:                     }
19087:                 } elsif (exists($currhash{$domcoord[$i]})) {
19088:                     $check = ' checked="checked"';
19089:                 }
19090:             } else {
19091:                 if (exists($currhash{$domcoord[$i]})) {
19092:                     $check = ' checked="checked"';
19093:                 }
19094:             }
19095:             if ($i == @domcoord - 1) {
19096:                 my $colsleft = $numinrow - $rem;
19097:                 if ($colsleft > 1) {
19098:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
19099:                 } else {
19100:                     $table .= '<td class="LC_left_item">';
19101:                 }
19102:             } else {
19103:                 $table .= '<td class="LC_left_item">';
19104:             }
19105:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
19106:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
19107:             $table .= '<span class="LC_nobreak"><label>'.
19108:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
19109:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
19110:             if ($user ne $dcname.':'.$dcdom) {
19111:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
19112:             }
19113:             $table .= '</label></span></td>';
19114:         }
19115:         $table .= '</tr></table>';
19116:     } elsif ($numdcs == 1) {
19117:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
19118:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
19119:         if ($inputtype eq 'radio') {
19120:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
19121:             if ($user ne $dcname.':'.$dcdom) {
19122:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
19123:             }
19124:         } else {
19125:             my $check;
19126:             if (exists($currhash{$domcoord[0]})) {
19127:                 $check = ' checked="checked"';
19128:             }
19129:             $table = '<span class="LC_nobreak"><label>'.
19130:                      '<input type="checkbox" name="'.$name.'" '.
19131:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
19132:             if ($user ne $dcname.':'.$dcdom) {
19133:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
19134:             }
19135:             $table .= '</label></span>';
19136:             $rows ++;
19137:         }
19138:     }
19139:     return ($numdcs,$table,$rows);
19140: }
19141: 
19142: sub usersession_titles {
19143:     return &Apache::lonlocal::texthash(
19144:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
19145:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
19146:                spares => 'Servers offloaded to, when busy',
19147:                version => 'LON-CAPA version requirement',
19148:                excludedomain => 'Allow all, but exclude specific domains',
19149:                includedomain => 'Deny all, but include specific domains',
19150:                primary => 'Primary (checked first)',
19151:                default => 'Default',
19152:            );
19153: }
19154: 
19155: sub id_for_thisdom {
19156:     my (%servers) = @_;
19157:     my %altids;
19158:     foreach my $server (keys(%servers)) {
19159:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
19160:         if ($serverhome ne $server) {
19161:             $altids{$serverhome} = $server;
19162:         }
19163:     }
19164:     return %altids;
19165: }
19166: 
19167: sub count_servers {
19168:     my ($currbalancer,%servers) = @_;
19169:     my (@spares,$numspares);
19170:     foreach my $lonhost (sort(keys(%servers))) {
19171:         next if ($currbalancer eq $lonhost);
19172:         push(@spares,$lonhost);
19173:     }
19174:     if ($currbalancer) {
19175:         $numspares = scalar(@spares);
19176:     } else {
19177:         $numspares = scalar(@spares) - 1;
19178:     }
19179:     return ($numspares,@spares);
19180: }
19181: 
19182: sub lonbalance_targets_js {
19183:     my ($dom,$types,$servers,$settings) = @_;
19184:     my $select = &mt('Select');
19185:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
19186:     if (ref($servers) eq 'HASH') {
19187:         $alltargets = join("','",sort(keys(%{$servers})));
19188:         my @homedoms;
19189:         foreach my $server (sort(keys(%{$servers}))) {
19190:             if (&Apache::lonnet::host_domain($server) eq $dom) {
19191:                 push(@homedoms,'1');
19192:             } else {
19193:                 push(@homedoms,'0');
19194:             }
19195:         }
19196:         $allishome = join("','",@homedoms);
19197:     }
19198:     if (ref($types) eq 'ARRAY') {
19199:         if (@{$types} > 0) {
19200:             @alltypes = @{$types};
19201:         }
19202:     }
19203:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
19204:     $allinsttypes = join("','",@alltypes);
19205:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
19206:     if (ref($settings) eq 'HASH') {
19207:         %existing = %{$settings};
19208:     }
19209:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
19210:                               \%currtargets,\%currrules,\%currcookies);
19211:     my $balancers = join("','",sort(keys(%currbalancer)));
19212:     return <<"END";
19213: 
19214: <script type="text/javascript">
19215: // <![CDATA[
19216: 
19217: currBalancers = new Array('$balancers');
19218: 
19219: function toggleTargets(balnum) {
19220:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
19221:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
19222:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
19223:     var prevbalancer = prevhostitem.value;
19224:     var baltotal = document.getElementById('loadbalancing_total').value;
19225:     prevhostitem.value = balancer;
19226:     if (prevbalancer != '') {
19227:         var prevIdx = currBalancers.indexOf(prevbalancer);
19228:         if (prevIdx != -1) {
19229:             currBalancers.splice(prevIdx,1);
19230:         }
19231:     }
19232:     if (balancer == '') {
19233:         hideSpares(balnum);
19234:     } else {
19235:         var currIdx = currBalancers.indexOf(balancer);
19236:         if (currIdx == -1) {
19237:             currBalancers.push(balancer);
19238:         }
19239:         var homedoms = new Array('$allishome');
19240:         var ishomedom = homedoms[lonhostitem.selectedIndex];
19241:         showSpares(balancer,ishomedom,balnum);
19242:     }
19243:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
19244:     return;
19245: }
19246: 
19247: function showSpares(balancer,ishomedom,balnum) {
19248:     var alltargets = new Array('$alltargets');
19249:     var insttypes = new Array('$allinsttypes');
19250:     var offloadtypes = new Array('primary','default');
19251: 
19252:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
19253:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
19254:  
19255:     for (var i=0; i<offloadtypes.length; i++) {
19256:         var count = 0;
19257:         for (var j=0; j<alltargets.length; j++) {
19258:             if (alltargets[j] != balancer) {
19259:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
19260:                 item.value = alltargets[j];
19261:                 item.style.textAlign='left';
19262:                 item.style.textFace='normal';
19263:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
19264:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
19265:                     item.disabled = '';
19266:                 } else {
19267:                     item.disabled = 'disabled';
19268:                     item.checked = false;
19269:                 }
19270:                 count ++;
19271:             }
19272:         }
19273:     }
19274:     for (var k=0; k<insttypes.length; k++) {
19275:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
19276:             if (ishomedom == 1) {
19277:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
19278:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
19279:             } else {
19280:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
19281:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
19282:             }
19283:         } else {
19284:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
19285:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
19286:         }
19287:         if ((insttypes[k] != '_LC_external') && 
19288:             ((insttypes[k] != '_LC_internetdom') ||
19289:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
19290:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
19291:             item.options.length = 0;
19292:             item.options[0] = new Option("","",true,true);
19293:             var idx = 0;
19294:             for (var m=0; m<alltargets.length; m++) {
19295:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
19296:                     idx ++;
19297:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
19298:                 }
19299:             }
19300:         }
19301:     }
19302:     return;
19303: }
19304: 
19305: function hideSpares(balnum) {
19306:     var alltargets = new Array('$alltargets');
19307:     var insttypes = new Array('$allinsttypes');
19308:     var offloadtypes = new Array('primary','default');
19309: 
19310:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
19311:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
19312: 
19313:     var total = alltargets.length - 1;
19314:     for (var i=0; i<offloadtypes; i++) {
19315:         for (var j=0; j<total; j++) {
19316:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
19317:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
19318:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
19319:         }
19320:     }
19321:     for (var k=0; k<insttypes.length; k++) {
19322:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
19323:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
19324:         if (insttypes[k] != '_LC_external') {
19325:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
19326:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
19327:         }
19328:     }
19329:     return;
19330: }
19331: 
19332: function checkOffloads(item,balnum,type) {
19333:     var alltargets = new Array('$alltargets');
19334:     var offloadtypes = new Array('primary','default');
19335:     if (item.checked) {
19336:         var total = alltargets.length - 1;
19337:         var other;
19338:         if (type == offloadtypes[0]) {
19339:             other = offloadtypes[1];
19340:         } else {
19341:             other = offloadtypes[0];
19342:         }
19343:         for (var i=0; i<total; i++) {
19344:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
19345:             if (server == item.value) {
19346:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
19347:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
19348:                 }
19349:             }
19350:         }
19351:     }
19352:     return;
19353: }
19354: 
19355: function singleServerToggle(balnum,type) {
19356:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
19357:     if (offloadtoSelIdx == 0) {
19358:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
19359:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
19360: 
19361:     } else {
19362:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
19363:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
19364:     }
19365:     return;
19366: }
19367: 
19368: function balanceruleChange(formname,balnum,type) {
19369:     if (type == '_LC_external') {
19370:         return;
19371:     }
19372:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
19373:     for (var i=0; i<typesRules.length; i++) {
19374:         if (formname.elements[typesRules[i]].checked) {
19375:             if (formname.elements[typesRules[i]].value != 'specific') {
19376:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
19377:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
19378:             } else {
19379:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
19380:             }
19381:         }
19382:     }
19383:     return;
19384: }
19385: 
19386: function balancerDeleteChange(balnum) {
19387:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
19388:     var baltotal = document.getElementById('loadbalancing_total').value;
19389:     var addtarget;
19390:     var removetarget;
19391:     var action = 'delete';
19392:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
19393:         var lonhost = hostitem.value;
19394:         var currIdx = currBalancers.indexOf(lonhost);
19395:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
19396:             if (currIdx != -1) {
19397:                 currBalancers.splice(currIdx,1);
19398:             }
19399:             addtarget = lonhost;
19400:         } else {
19401:             if (currIdx == -1) {
19402:                 currBalancers.push(lonhost);
19403:             }
19404:             removetarget = lonhost;
19405:             action = 'undelete';
19406:         }
19407:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
19408:     }
19409:     return;
19410: }
19411: 
19412: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
19413:     if (baltotal > 1) {
19414:         var offloadtypes = new Array('primary','default');
19415:         var alltargets = new Array('$alltargets');
19416:         var insttypes = new Array('$allinsttypes');
19417:         for (var i=0; i<baltotal; i++) {
19418:             if (i != balnum) {
19419:                 for (var j=0; j<offloadtypes.length; j++) {
19420:                     var total = alltargets.length - 1;
19421:                     for (var k=0; k<total; k++) {
19422:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
19423:                         var server = serveritem.value;
19424:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
19425:                             if (server == addtarget) {
19426:                                 serveritem.disabled = '';
19427:                             }
19428:                         }
19429:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
19430:                             if (server == removetarget) {
19431:                                 serveritem.disabled = 'disabled';
19432:                                 serveritem.checked = false;
19433:                             }
19434:                         }
19435:                     }
19436:                 }
19437:                 for (var j=0; j<insttypes.length; j++) {
19438:                     if (insttypes[j] != '_LC_external') {
19439:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
19440:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
19441:                             var currSel = singleserver.selectedIndex;
19442:                             var currVal = singleserver.options[currSel].value;
19443:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
19444:                                 var numoptions = singleserver.options.length;
19445:                                 var needsnew = 1;
19446:                                 for (var k=0; k<numoptions; k++) {
19447:                                     if (singleserver.options[k] == addtarget) {
19448:                                         needsnew = 0;
19449:                                         break;
19450:                                     }
19451:                                 }
19452:                                 if (needsnew == 1) {
19453:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
19454:                                 }
19455:                             }
19456:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
19457:                                 singleserver.options.length = 0;
19458:                                 if ((currVal) && (currVal != removetarget)) {
19459:                                     singleserver.options[0] = new Option("","",false,false);
19460:                                 } else {
19461:                                     singleserver.options[0] = new Option("","",true,true);
19462:                                 }
19463:                                 var idx = 0;
19464:                                 for (var m=0; m<alltargets.length; m++) {
19465:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
19466:                                         idx ++;
19467:                                         if (currVal == alltargets[m]) {
19468:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
19469:                                         } else {
19470:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
19471:                                         }
19472:                                     }
19473:                                 }
19474:                             }
19475:                         }
19476:                     }
19477:                 }
19478:             }
19479:         }
19480:     }
19481:     return;
19482: }
19483: 
19484: // ]]>
19485: </script>
19486: 
19487: END
19488: }
19489: 
19490: sub new_spares_js {
19491:     my @sparestypes = ('primary','default');
19492:     my $types = join("','",@sparestypes);
19493:     my $select = &mt('Select');
19494:     return <<"END";
19495: 
19496: <script type="text/javascript">
19497: // <![CDATA[
19498: 
19499: function updateNewSpares(formname,lonhost) {
19500:     var types = new Array('$types');
19501:     var include = new Array();
19502:     var exclude = new Array();
19503:     for (var i=0; i<types.length; i++) {
19504:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
19505:         for (var j=0; j<spareboxes.length; j++) {
19506:             if (formname.elements[spareboxes[j]].checked) {
19507:                 exclude.push(formname.elements[spareboxes[j]].value);
19508:             } else {
19509:                 include.push(formname.elements[spareboxes[j]].value);
19510:             }
19511:         }
19512:     }
19513:     for (var i=0; i<types.length; i++) {
19514:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
19515:         var selIdx = newSpare.selectedIndex;
19516:         var currnew = newSpare.options[selIdx].value;
19517:         var okSpares = new Array();
19518:         for (var j=0; j<newSpare.options.length; j++) {
19519:             var possible = newSpare.options[j].value;
19520:             if (possible != '') {
19521:                 if (exclude.indexOf(possible) == -1) {
19522:                     okSpares.push(possible);
19523:                 } else {
19524:                     if (currnew == possible) {
19525:                         selIdx = 0;
19526:                     }
19527:                 }
19528:             }
19529:         }
19530:         for (var k=0; k<include.length; k++) {
19531:             if (okSpares.indexOf(include[k]) == -1) {
19532:                 okSpares.push(include[k]);
19533:             }
19534:         }
19535:         okSpares.sort();
19536:         newSpare.options.length = 0;
19537:         if (selIdx == 0) {
19538:             newSpare.options[0] = new Option("$select","",true,true);
19539:         } else {
19540:             newSpare.options[0] = new Option("$select","",false,false);
19541:         }
19542:         for (var m=0; m<okSpares.length; m++) {
19543:             var idx = m+1;
19544:             var selThis = 0;
19545:             if (selIdx != 0) {
19546:                 if (okSpares[m] == currnew) {
19547:                     selThis = 1;
19548:                 }
19549:             }
19550:             if (selThis == 1) {
19551:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
19552:             } else {
19553:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
19554:             }
19555:         }
19556:     }
19557:     return;
19558: }
19559: 
19560: function checkNewSpares(lonhost,type) {
19561:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
19562:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
19563:     if (chosen != '') { 
19564:         var othertype;
19565:         var othernewSpare;
19566:         if (type == 'primary') {
19567:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
19568:         }
19569:         if (type == 'default') {
19570:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
19571:         }
19572:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
19573:             othernewSpare.selectedIndex = 0;
19574:         }
19575:     }
19576:     return;
19577: }
19578: 
19579: // ]]>
19580: </script>
19581: 
19582: END
19583: 
19584: }
19585: 
19586: sub common_domprefs_js {
19587:     return <<"END";
19588: 
19589: <script type="text/javascript">
19590: // <![CDATA[
19591: 
19592: function getIndicesByName(formname,item) {
19593:     var group = new Array();
19594:     for (var i=0;i<formname.elements.length;i++) {
19595:         if (formname.elements[i].name == item) {
19596:             group.push(formname.elements[i].id);
19597:         }
19598:     }
19599:     return group;
19600: }
19601: 
19602: // ]]>
19603: </script>
19604: 
19605: END
19606: 
19607: }
19608: 
19609: sub recaptcha_js {
19610:     my %lt = &captcha_phrases();
19611:     return <<"END";
19612: 
19613: <script type="text/javascript">
19614: // <![CDATA[
19615: 
19616: function updateCaptcha(caller,context) {
19617:     var privitem;
19618:     var pubitem;
19619:     var privtext;
19620:     var pubtext;
19621:     var versionitem;
19622:     var versiontext;
19623:     if (document.getElementById(context+'_recaptchapub')) {
19624:         pubitem = document.getElementById(context+'_recaptchapub');
19625:     } else {
19626:         return;
19627:     }
19628:     if (document.getElementById(context+'_recaptchapriv')) {
19629:         privitem = document.getElementById(context+'_recaptchapriv');
19630:     } else {
19631:         return;
19632:     }
19633:     if (document.getElementById(context+'_recaptchapubtxt')) {
19634:         pubtext = document.getElementById(context+'_recaptchapubtxt');
19635:     } else {
19636:         return;
19637:     }
19638:     if (document.getElementById(context+'_recaptchaprivtxt')) {
19639:         privtext = document.getElementById(context+'_recaptchaprivtxt');
19640:     } else {
19641:         return;
19642:     }
19643:     if (document.getElementById(context+'_recaptchaversion')) {
19644:         versionitem = document.getElementById(context+'_recaptchaversion');
19645:     } else {
19646:         return;
19647:     }
19648:     if (document.getElementById(context+'_recaptchavertxt')) {
19649:         versiontext = document.getElementById(context+'_recaptchavertxt');
19650:     } else {
19651:         return;
19652:     }
19653:     if (caller.checked) {
19654:         if (caller.value == 'recaptcha') {
19655:             pubitem.type = 'text';
19656:             privitem.type = 'text';
19657:             pubitem.size = '40';
19658:             privitem.size = '40';
19659:             pubtext.innerHTML = "$lt{'pub'}";
19660:             privtext.innerHTML = "$lt{'priv'}";
19661:             versionitem.type = 'text';
19662:             versionitem.size = '3';
19663:             versiontext.innerHTML = "$lt{'ver'}";
19664:         } else {
19665:             pubitem.type = 'hidden';
19666:             privitem.type = 'hidden';
19667:             versionitem.type = 'hidden';
19668:             pubtext.innerHTML = '';
19669:             privtext.innerHTML = '';
19670:             versiontext.innerHTML = '';
19671:         }
19672:     }
19673:     return;
19674: }
19675: 
19676: // ]]>
19677: </script>
19678: 
19679: END
19680: 
19681: }
19682: 
19683: sub toggle_display_js {
19684:     return <<"END";
19685: 
19686: <script type="text/javascript">
19687: // <![CDATA[
19688: 
19689: function toggleDisplay(domForm,caller) {
19690:     if (document.getElementById(caller)) {
19691:         var divitem = document.getElementById(caller);
19692:         var optionsElement = domForm.coursecredits;
19693:         var checkval = 1;
19694:         var dispval = 'block';
19695:         var selfcreateRegExp = /^cancreate_emailverified/;
19696:         if (caller == 'emailoptions') {
19697:             optionsElement = domForm.cancreate_email; 
19698:         }
19699:         if (caller == 'studentsubmission') {
19700:             optionsElement = domForm.postsubmit;
19701:         }
19702:         if (caller == 'cloneinstcode') {
19703:             optionsElement = domForm.canclone;
19704:             checkval = 'instcode';
19705:         }
19706:         if (selfcreateRegExp.test(caller)) {
19707:             optionsElement = domForm.elements[caller];
19708:             checkval = 'other';
19709:             dispval = 'inline'
19710:         }
19711:         if (optionsElement.length) {
19712:             var currval;
19713:             for (var i=0; i<optionsElement.length; i++) {
19714:                 if (optionsElement[i].checked) {
19715:                    currval = optionsElement[i].value;
19716:                 }
19717:             }
19718:             if (currval == checkval) {
19719:                 divitem.style.display = dispval;
19720:             } else {
19721:                 divitem.style.display = 'none';
19722:             }
19723:         }
19724:     }
19725:     return;
19726: }
19727: 
19728: // ]]>
19729: </script>
19730: 
19731: END
19732: 
19733: }
19734: 
19735: sub captcha_phrases {
19736:     return &Apache::lonlocal::texthash (
19737:                  priv => 'Private key',
19738:                  pub  => 'Public key',
19739:                  original  => 'original (CAPTCHA)',
19740:                  recaptcha => 'successor (ReCAPTCHA)',
19741:                  notused   => 'unused',
19742:                  ver => 'ReCAPTCHA version (1 or 2)',
19743:     );
19744: }
19745: 
19746: sub devalidate_remote_domconfs {
19747:     my ($dom,$cachekeys) = @_;
19748:     return unless (ref($cachekeys) eq 'HASH');
19749:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19750:     my %thismachine;
19751:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
19752:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
19753:                       'directorysrch','passwdconf','cats');
19754:     if (keys(%servers)) {
19755:         foreach my $server (keys(%servers)) {
19756:             next if ($thismachine{$server});
19757:             my @cached;
19758:             foreach my $name (@posscached) {
19759:                 if ($cachekeys->{$name}) {
19760:                     push(@cached,&escape($name).':'.&escape($dom));
19761:                 }
19762:             }
19763:             if (@cached) {
19764:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
19765:             }
19766:         }
19767:     }
19768:     return;
19769: }
19770: 
19771: 1;

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